Skip to content

Commit

Permalink
[Java] Update version of maven-surefire-plugin (OpenAPITools#5509)
Browse files Browse the repository at this point in the history
* use more recent version of maven-surefire-plugin

* use more recent version of maven-surefire-plugin

* higher debug level for troubleshooting ci issue

* temporarily increase debug log to help troubleshoot

* Use local instance of pet store service for unit test purpose

* Add more logging to troubleshoot unit test failures

* Add more logging to troubleshoot unit test failures

* Add more logging to troubleshoot unit test failures

* Add more logging to troubleshoot unit test failures

* Add more logging to troubleshoot unit test failures

* use random ID for Java unit test instead of fixed id

* add code comments and specify URL for java unit test

* reenable quiet argument

* fix java unit test issues

* fix java unit test issues

* Revert "fix java unit test issues"

This reverts commit e850841.

* fix java unit test issues
  • Loading branch information
sebastien-rosset authored and michaelpro1 committed May 7, 2020
1 parent d021be4 commit dbd0b7e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 17 deletions.
7 changes: 7 additions & 0 deletions CI/circle_parallel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ NODE_INDEX=${CIRCLE_NODE_INDEX:-0}

set -e

function cleanup {
# Show logs of 'petstore.swagger' container to troubleshoot Unit Test failures, if any.
docker logs petstore.swagger # container name specified in circle.yml
}

trap cleanup EXIT

if [ "$NODE_INDEX" = "1" ]; then
echo "Running node $NODE_INDEX to test 'samples.circleci' defined in pom.xml ..."
java -version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.junit.*;

Expand All @@ -42,20 +45,24 @@
public class PetApiTest {

private PetApi api = new PetApi();
private static final Logger LOG = LoggerFactory.getLogger(PetApiTest.class);
// In the circle.yml file, /etc/host is configured with an entry to resolve petstore.swagger.io to 127.0.0.1
private static String basePath = "http://petstore.swagger.io:80/v2";

@Before
public void setup() {
// setup authentication
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key");
api.getApiClient().setBasePath(basePath);
}

@Test
public void testApiClient() {
// the default api client is used
assertEquals(Configuration.getDefaultApiClient(), api.getApiClient());
assertNotNull(api.getApiClient());
assertEquals("http://petstore.swagger.io:80/v2", api.getApiClient().getBasePath());
assertEquals(basePath, api.getApiClient().getBasePath());
assertFalse(api.getApiClient().isDebugging());

ApiClient oldClient = api.getApiClient();
Expand All @@ -74,7 +81,7 @@ public void testApiClient() {
// set api client via setter method
api.setApiClient(oldClient);
assertNotNull(api.getApiClient());
assertEquals("http://petstore.swagger.io:80/v2", api.getApiClient().getBasePath());
assertEquals(basePath, api.getApiClient().getBasePath());
assertFalse(api.getApiClient().isDebugging());
}

Expand All @@ -85,6 +92,7 @@ public void testCreateAndGetPet() throws Exception {

Pet fetched = api.getPetById(pet.getId());
assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
}

@Test
Expand All @@ -98,6 +106,7 @@ public void testCreateAndGetPetWithHttpInfo() throws Exception {
Pet fetched = resp.getData();

assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
}

@Test
Expand Down Expand Up @@ -144,6 +153,7 @@ public void onDownloadProgress(long bytesRead, long contentLength, boolean done)
}
} while (result.isEmpty());
assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
}

@Test
Expand Down Expand Up @@ -197,6 +207,7 @@ public void onDownloadProgress(long bytesRead, long contentLength, boolean done)
assertEquals(404, exception.getCode());
assertEquals("Not Found", exception.getMessage());
assertEquals("application/json", exception.getResponseHeaders().get("Content-Type").get(0));
api.deletePet(pet.getId(), null);
}

@Test
Expand Down Expand Up @@ -255,26 +266,31 @@ public void testCreateAndGetMultiplePetsAsync() throws Exception {
final ApiException exception = getCallback3.getException();
assertNotNull(exception);
assertEquals(404, exception.getCode());
api.deletePet(pet1.getId(), null);
api.deletePet(pet2.getId(), null);
}


@Test
public void testUpdatePet() throws Exception {
Pet pet = createPet();
api.addPet(pet);
pet.setName("programmer");

api.updatePet(pet);

Pet fetched = api.getPetById(pet.getId());
assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
}

@Test
public void testFindPetsByStatus() throws Exception {
assertEquals(basePath, api.getApiClient().getBasePath());
Pet pet = createPet();
api.addPet(pet);
pet.setName("programmer");
pet.setStatus(Pet.StatusEnum.PENDING);

api.updatePet(pet);

List<Pet> pets = api.findPetsByStatus(Arrays.asList("pending"));
Expand Down Expand Up @@ -335,6 +351,7 @@ public void testUpdatePetWithForm() throws Exception {
Pet updated = api.getPetById(fetched.getId());

assertEquals(updated.getName(), "furt");
api.deletePet(pet.getId(), null);
}

@Test
Expand All @@ -343,12 +360,13 @@ public void testDeletePet() throws Exception {
api.addPet(pet);

Pet fetched = api.getPetById(pet.getId());
api.deletePet(fetched.getId(), null);
api.deletePet(pet.getId(), null);

try {
fetched = api.getPetById(fetched.getId());
fail("expected an error");
} catch (ApiException e) {
LOG.info("Code: {}. Message: {}", e.getCode(), e.getMessage());
assertEquals(404, e.getCode());
}
}
Expand All @@ -364,6 +382,7 @@ public void testUploadFile() throws Exception {
writer.close();

api.uploadFile(pet.getId(), "a test file", new File(file.getAbsolutePath()));
api.deletePet(pet.getId(), null);
}

@Test
Expand Down Expand Up @@ -396,7 +415,7 @@ public void testEqualsAndHashCode() {

private Pet createPet() {
Pet pet = new Pet();
pet.setId(1234567L);
pet.setId(ThreadLocalRandom.current().nextLong(Long.MAX_VALUE));
pet.setName("gorilla");

Category category = new Category();
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
# - run: docker pull openapitools/openapi-petstore
# - run: docker run -d -e OPENAPI_BASE_PATH=/v3 -e DISABLE_API_KEY=1 -e DISABLE_OAUTH=1 -p 80:8080 openapitools/openapi-petstore
- run: docker pull swaggerapi/petstore
- run: docker run -d -e SWAGGER_HOST=http://petstore.swagger.io -e SWAGGER_BASE_PATH=/v2 -p 80:8080 swaggerapi/petstore
- run: docker run --name petstore.swagger -d -e SWAGGER_HOST=http://petstore.swagger.io -e SWAGGER_BASE_PATH=/v2 -p 80:8080 swaggerapi/petstore
- run: docker ps -a
- run: sleep 30
- run: cat /etc/hosts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<version>3.0.0-M4</version>
<configuration>
<systemProperties>
<property>
Expand All @@ -73,7 +73,7 @@
</systemProperties>
<argLine>-Xms512m -Xmx1500m</argLine>
<parallel>methods</parallel>
<forkMode>pertest</forkMode>
<threadCount>10</threadCount>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<version>3.0.0-M4</version>
<configuration>
<systemProperties>
<property>
Expand All @@ -66,7 +66,7 @@
</systemProperties>
<argLine>-Xms512m -Xmx1500m</argLine>
<parallel>methods</parallel>
<forkMode>pertest</forkMode>
<threadCount>10</threadCount>
</configuration>
</plugin>
<plugin>
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/java/okhttp-gson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<version>3.0.0-M4</version>
<configuration>
<systemProperties>
<property>
Expand All @@ -66,7 +66,7 @@
</systemProperties>
<argLine>-Xms512m -Xmx1500m</argLine>
<parallel>methods</parallel>
<forkMode>pertest</forkMode>
<threadCount>10</threadCount>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.junit.*;

Expand All @@ -42,20 +45,24 @@
public class PetApiTest {

private PetApi api = new PetApi();
private static final Logger LOG = LoggerFactory.getLogger(PetApiTest.class);
// In the circle.yml file, /etc/host is configured with an entry to resolve petstore.swagger.io to 127.0.0.1
private static String basePath = "http://petstore.swagger.io:80/v2";

@Before
public void setup() {
// setup authentication
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key");
api.getApiClient().setBasePath(basePath);
}

@Test
public void testApiClient() {
// the default api client is used
assertEquals(Configuration.getDefaultApiClient(), api.getApiClient());
assertNotNull(api.getApiClient());
assertEquals("http://petstore.swagger.io:80/v2", api.getApiClient().getBasePath());
assertEquals(basePath, api.getApiClient().getBasePath());
assertFalse(api.getApiClient().isDebugging());

ApiClient oldClient = api.getApiClient();
Expand All @@ -74,7 +81,7 @@ public void testApiClient() {
// set api client via setter method
api.setApiClient(oldClient);
assertNotNull(api.getApiClient());
assertEquals("http://petstore.swagger.io:80/v2", api.getApiClient().getBasePath());
assertEquals(basePath, api.getApiClient().getBasePath());
assertFalse(api.getApiClient().isDebugging());
}

Expand All @@ -85,6 +92,7 @@ public void testCreateAndGetPet() throws Exception {

Pet fetched = api.getPetById(pet.getId());
assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
}

@Test
Expand All @@ -98,6 +106,7 @@ public void testCreateAndGetPetWithHttpInfo() throws Exception {
Pet fetched = resp.getData();

assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
}

@Test
Expand Down Expand Up @@ -144,6 +153,7 @@ public void onDownloadProgress(long bytesRead, long contentLength, boolean done)
}
} while (result.isEmpty());
assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
}

@Test
Expand Down Expand Up @@ -197,6 +207,7 @@ public void onDownloadProgress(long bytesRead, long contentLength, boolean done)
assertEquals(404, exception.getCode());
assertEquals("Not Found", exception.getMessage());
assertEquals("application/json", exception.getResponseHeaders().get("Content-Type").get(0));
api.deletePet(pet.getId(), null);
}

@Test
Expand Down Expand Up @@ -255,26 +266,31 @@ public void testCreateAndGetMultiplePetsAsync() throws Exception {
final ApiException exception = getCallback3.getException();
assertNotNull(exception);
assertEquals(404, exception.getCode());
api.deletePet(pet1.getId(), null);
api.deletePet(pet2.getId(), null);
}


@Test
public void testUpdatePet() throws Exception {
Pet pet = createPet();
api.addPet(pet);
pet.setName("programmer");

api.updatePet(pet);

Pet fetched = api.getPetById(pet.getId());
assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
}

@Test
public void testFindPetsByStatus() throws Exception {
assertEquals(basePath, api.getApiClient().getBasePath());
Pet pet = createPet();
api.addPet(pet);
pet.setName("programmer");
pet.setStatus(Pet.StatusEnum.PENDING);

api.updatePet(pet);

List<Pet> pets = api.findPetsByStatus(Arrays.asList("pending"));
Expand Down Expand Up @@ -335,6 +351,7 @@ public void testUpdatePetWithForm() throws Exception {
Pet updated = api.getPetById(fetched.getId());

assertEquals(updated.getName(), "furt");
api.deletePet(pet.getId(), null);
}

@Test
Expand All @@ -343,12 +360,13 @@ public void testDeletePet() throws Exception {
api.addPet(pet);

Pet fetched = api.getPetById(pet.getId());
api.deletePet(fetched.getId(), null);
api.deletePet(pet.getId(), null);

try {
fetched = api.getPetById(fetched.getId());
fail("expected an error");
} catch (ApiException e) {
LOG.info("Code: {}. Message: {}", e.getCode(), e.getMessage());
assertEquals(404, e.getCode());
}
}
Expand All @@ -364,6 +382,7 @@ public void testUploadFile() throws Exception {
writer.close();

api.uploadFile(pet.getId(), "a test file", new File(file.getAbsolutePath()));
api.deletePet(pet.getId(), null);
}

@Test
Expand Down Expand Up @@ -396,7 +415,7 @@ public void testEqualsAndHashCode() {

private Pet createPet() {
Pet pet = new Pet();
pet.setId(1234567L);
pet.setId(ThreadLocalRandom.current().nextLong(Long.MAX_VALUE));
pet.setName("gorilla");

Category category = new Category();
Expand Down

0 comments on commit dbd0b7e

Please sign in to comment.