[Java] Update version of maven-surefire-plugin (#5509)

* 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 e8508416ff0f2aeb568e3f916b013dc967390f74.

* fix java unit test issues
This commit is contained in:
Sebastien Rosset 2020-03-26 01:47:51 -07:00 committed by GitHub
parent 2c1ca02b61
commit 527d5e4248
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 17 deletions

View File

@ -7,6 +7,13 @@ NODE_INDEX=${CIRCLE_NODE_INDEX:-0}
set -e 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 if [ "$NODE_INDEX" = "1" ]; then
echo "Running node $NODE_INDEX to test 'samples.circleci' defined in pom.xml ..." echo "Running node $NODE_INDEX to test 'samples.circleci' defined in pom.xml ..."
java -version java -version

View File

@ -28,9 +28,12 @@ import java.util.concurrent.TimeUnit;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.*; import org.junit.*;
@ -42,12 +45,16 @@ import static org.junit.Assert.*;
public class PetApiTest { public class PetApiTest {
private PetApi api = new PetApi(); 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 @Before
public void setup() { public void setup() {
// setup authentication // setup authentication
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key"); ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key"); apiKeyAuth.setApiKey("special-key");
api.getApiClient().setBasePath(basePath);
} }
@Test @Test
@ -55,7 +62,7 @@ public class PetApiTest {
// the default api client is used // the default api client is used
assertEquals(Configuration.getDefaultApiClient(), api.getApiClient()); assertEquals(Configuration.getDefaultApiClient(), api.getApiClient());
assertNotNull(api.getApiClient()); assertNotNull(api.getApiClient());
assertEquals("http://petstore.swagger.io:80/v2", api.getApiClient().getBasePath()); assertEquals(basePath, api.getApiClient().getBasePath());
assertFalse(api.getApiClient().isDebugging()); assertFalse(api.getApiClient().isDebugging());
ApiClient oldClient = api.getApiClient(); ApiClient oldClient = api.getApiClient();
@ -74,7 +81,7 @@ public class PetApiTest {
// set api client via setter method // set api client via setter method
api.setApiClient(oldClient); api.setApiClient(oldClient);
assertNotNull(api.getApiClient()); assertNotNull(api.getApiClient());
assertEquals("http://petstore.swagger.io:80/v2", api.getApiClient().getBasePath()); assertEquals(basePath, api.getApiClient().getBasePath());
assertFalse(api.getApiClient().isDebugging()); assertFalse(api.getApiClient().isDebugging());
} }
@ -85,6 +92,7 @@ public class PetApiTest {
Pet fetched = api.getPetById(pet.getId()); Pet fetched = api.getPetById(pet.getId());
assertPetMatches(pet, fetched); assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
} }
@Test @Test
@ -98,6 +106,7 @@ public class PetApiTest {
Pet fetched = resp.getData(); Pet fetched = resp.getData();
assertPetMatches(pet, fetched); assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
} }
@Test @Test
@ -144,6 +153,7 @@ public class PetApiTest {
} }
} while (result.isEmpty()); } while (result.isEmpty());
assertPetMatches(pet, fetched); assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
} }
@Test @Test
@ -197,6 +207,7 @@ public class PetApiTest {
assertEquals(404, exception.getCode()); assertEquals(404, exception.getCode());
assertEquals("Not Found", exception.getMessage()); assertEquals("Not Found", exception.getMessage());
assertEquals("application/json", exception.getResponseHeaders().get("Content-Type").get(0)); assertEquals("application/json", exception.getResponseHeaders().get("Content-Type").get(0));
api.deletePet(pet.getId(), null);
} }
@Test @Test
@ -255,26 +266,31 @@ public class PetApiTest {
final ApiException exception = getCallback3.getException(); final ApiException exception = getCallback3.getException();
assertNotNull(exception); assertNotNull(exception);
assertEquals(404, exception.getCode()); assertEquals(404, exception.getCode());
api.deletePet(pet1.getId(), null);
api.deletePet(pet2.getId(), null);
} }
@Test @Test
public void testUpdatePet() throws Exception { public void testUpdatePet() throws Exception {
Pet pet = createPet(); Pet pet = createPet();
api.addPet(pet);
pet.setName("programmer"); pet.setName("programmer");
api.updatePet(pet); api.updatePet(pet);
Pet fetched = api.getPetById(pet.getId()); Pet fetched = api.getPetById(pet.getId());
assertPetMatches(pet, fetched); assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
} }
@Test @Test
public void testFindPetsByStatus() throws Exception { public void testFindPetsByStatus() throws Exception {
assertEquals(basePath, api.getApiClient().getBasePath());
Pet pet = createPet(); Pet pet = createPet();
api.addPet(pet);
pet.setName("programmer"); pet.setName("programmer");
pet.setStatus(Pet.StatusEnum.PENDING); pet.setStatus(Pet.StatusEnum.PENDING);
api.updatePet(pet); api.updatePet(pet);
List<Pet> pets = api.findPetsByStatus(Arrays.asList("pending")); List<Pet> pets = api.findPetsByStatus(Arrays.asList("pending"));
@ -335,6 +351,7 @@ public class PetApiTest {
Pet updated = api.getPetById(fetched.getId()); Pet updated = api.getPetById(fetched.getId());
assertEquals(updated.getName(), "furt"); assertEquals(updated.getName(), "furt");
api.deletePet(pet.getId(), null);
} }
@Test @Test
@ -343,12 +360,13 @@ public class PetApiTest {
api.addPet(pet); api.addPet(pet);
Pet fetched = api.getPetById(pet.getId()); Pet fetched = api.getPetById(pet.getId());
api.deletePet(fetched.getId(), null); api.deletePet(pet.getId(), null);
try { try {
fetched = api.getPetById(fetched.getId()); fetched = api.getPetById(fetched.getId());
fail("expected an error"); fail("expected an error");
} catch (ApiException e) { } catch (ApiException e) {
LOG.info("Code: {}. Message: {}", e.getCode(), e.getMessage());
assertEquals(404, e.getCode()); assertEquals(404, e.getCode());
} }
} }
@ -364,6 +382,7 @@ public class PetApiTest {
writer.close(); writer.close();
api.uploadFile(pet.getId(), "a test file", new File(file.getAbsolutePath())); api.uploadFile(pet.getId(), "a test file", new File(file.getAbsolutePath()));
api.deletePet(pet.getId(), null);
} }
@Test @Test
@ -396,7 +415,7 @@ public class PetApiTest {
private Pet createPet() { private Pet createPet() {
Pet pet = new Pet(); Pet pet = new Pet();
pet.setId(1234567L); pet.setId(ThreadLocalRandom.current().nextLong(Long.MAX_VALUE));
pet.setName("gorilla"); pet.setName("gorilla");
Category category = new Category(); Category category = new Category();

View File

@ -61,7 +61,7 @@ jobs:
# - run: docker pull openapitools/openapi-petstore # - 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 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 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: docker ps -a
- run: sleep 30 - run: sleep 30
- run: cat /etc/hosts - run: cat /etc/hosts

View File

@ -63,7 +63,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version> <version>3.0.0-M4</version>
<configuration> <configuration>
<systemProperties> <systemProperties>
<property> <property>
@ -73,7 +73,7 @@
</systemProperties> </systemProperties>
<argLine>-Xms512m -Xmx1500m</argLine> <argLine>-Xms512m -Xmx1500m</argLine>
<parallel>methods</parallel> <parallel>methods</parallel>
<forkMode>pertest</forkMode> <threadCount>10</threadCount>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

View File

@ -56,7 +56,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version> <version>3.0.0-M4</version>
<configuration> <configuration>
<systemProperties> <systemProperties>
<property> <property>
@ -66,7 +66,7 @@
</systemProperties> </systemProperties>
<argLine>-Xms512m -Xmx1500m</argLine> <argLine>-Xms512m -Xmx1500m</argLine>
<parallel>methods</parallel> <parallel>methods</parallel>
<forkMode>pertest</forkMode> <threadCount>10</threadCount>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

View File

@ -56,7 +56,7 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version> <version>3.0.0-M4</version>
<configuration> <configuration>
<systemProperties> <systemProperties>
<property> <property>
@ -66,7 +66,7 @@
</systemProperties> </systemProperties>
<argLine>-Xms512m -Xmx1500m</argLine> <argLine>-Xms512m -Xmx1500m</argLine>
<parallel>methods</parallel> <parallel>methods</parallel>
<forkMode>pertest</forkMode> <threadCount>10</threadCount>
</configuration> </configuration>
</plugin> </plugin>
<plugin> <plugin>

View File

@ -28,9 +28,12 @@ import java.util.concurrent.TimeUnit;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.*; import org.junit.*;
@ -42,12 +45,16 @@ import static org.junit.Assert.*;
public class PetApiTest { public class PetApiTest {
private PetApi api = new PetApi(); 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 @Before
public void setup() { public void setup() {
// setup authentication // setup authentication
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key"); ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
apiKeyAuth.setApiKey("special-key"); apiKeyAuth.setApiKey("special-key");
api.getApiClient().setBasePath(basePath);
} }
@Test @Test
@ -55,7 +62,7 @@ public class PetApiTest {
// the default api client is used // the default api client is used
assertEquals(Configuration.getDefaultApiClient(), api.getApiClient()); assertEquals(Configuration.getDefaultApiClient(), api.getApiClient());
assertNotNull(api.getApiClient()); assertNotNull(api.getApiClient());
assertEquals("http://petstore.swagger.io:80/v2", api.getApiClient().getBasePath()); assertEquals(basePath, api.getApiClient().getBasePath());
assertFalse(api.getApiClient().isDebugging()); assertFalse(api.getApiClient().isDebugging());
ApiClient oldClient = api.getApiClient(); ApiClient oldClient = api.getApiClient();
@ -74,7 +81,7 @@ public class PetApiTest {
// set api client via setter method // set api client via setter method
api.setApiClient(oldClient); api.setApiClient(oldClient);
assertNotNull(api.getApiClient()); assertNotNull(api.getApiClient());
assertEquals("http://petstore.swagger.io:80/v2", api.getApiClient().getBasePath()); assertEquals(basePath, api.getApiClient().getBasePath());
assertFalse(api.getApiClient().isDebugging()); assertFalse(api.getApiClient().isDebugging());
} }
@ -85,6 +92,7 @@ public class PetApiTest {
Pet fetched = api.getPetById(pet.getId()); Pet fetched = api.getPetById(pet.getId());
assertPetMatches(pet, fetched); assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
} }
@Test @Test
@ -98,6 +106,7 @@ public class PetApiTest {
Pet fetched = resp.getData(); Pet fetched = resp.getData();
assertPetMatches(pet, fetched); assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
} }
@Test @Test
@ -144,6 +153,7 @@ public class PetApiTest {
} }
} while (result.isEmpty()); } while (result.isEmpty());
assertPetMatches(pet, fetched); assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
} }
@Test @Test
@ -197,6 +207,7 @@ public class PetApiTest {
assertEquals(404, exception.getCode()); assertEquals(404, exception.getCode());
assertEquals("Not Found", exception.getMessage()); assertEquals("Not Found", exception.getMessage());
assertEquals("application/json", exception.getResponseHeaders().get("Content-Type").get(0)); assertEquals("application/json", exception.getResponseHeaders().get("Content-Type").get(0));
api.deletePet(pet.getId(), null);
} }
@Test @Test
@ -255,26 +266,31 @@ public class PetApiTest {
final ApiException exception = getCallback3.getException(); final ApiException exception = getCallback3.getException();
assertNotNull(exception); assertNotNull(exception);
assertEquals(404, exception.getCode()); assertEquals(404, exception.getCode());
api.deletePet(pet1.getId(), null);
api.deletePet(pet2.getId(), null);
} }
@Test @Test
public void testUpdatePet() throws Exception { public void testUpdatePet() throws Exception {
Pet pet = createPet(); Pet pet = createPet();
api.addPet(pet);
pet.setName("programmer"); pet.setName("programmer");
api.updatePet(pet); api.updatePet(pet);
Pet fetched = api.getPetById(pet.getId()); Pet fetched = api.getPetById(pet.getId());
assertPetMatches(pet, fetched); assertPetMatches(pet, fetched);
api.deletePet(pet.getId(), null);
} }
@Test @Test
public void testFindPetsByStatus() throws Exception { public void testFindPetsByStatus() throws Exception {
assertEquals(basePath, api.getApiClient().getBasePath());
Pet pet = createPet(); Pet pet = createPet();
api.addPet(pet);
pet.setName("programmer"); pet.setName("programmer");
pet.setStatus(Pet.StatusEnum.PENDING); pet.setStatus(Pet.StatusEnum.PENDING);
api.updatePet(pet); api.updatePet(pet);
List<Pet> pets = api.findPetsByStatus(Arrays.asList("pending")); List<Pet> pets = api.findPetsByStatus(Arrays.asList("pending"));
@ -335,6 +351,7 @@ public class PetApiTest {
Pet updated = api.getPetById(fetched.getId()); Pet updated = api.getPetById(fetched.getId());
assertEquals(updated.getName(), "furt"); assertEquals(updated.getName(), "furt");
api.deletePet(pet.getId(), null);
} }
@Test @Test
@ -343,12 +360,13 @@ public class PetApiTest {
api.addPet(pet); api.addPet(pet);
Pet fetched = api.getPetById(pet.getId()); Pet fetched = api.getPetById(pet.getId());
api.deletePet(fetched.getId(), null); api.deletePet(pet.getId(), null);
try { try {
fetched = api.getPetById(fetched.getId()); fetched = api.getPetById(fetched.getId());
fail("expected an error"); fail("expected an error");
} catch (ApiException e) { } catch (ApiException e) {
LOG.info("Code: {}. Message: {}", e.getCode(), e.getMessage());
assertEquals(404, e.getCode()); assertEquals(404, e.getCode());
} }
} }
@ -364,6 +382,7 @@ public class PetApiTest {
writer.close(); writer.close();
api.uploadFile(pet.getId(), "a test file", new File(file.getAbsolutePath())); api.uploadFile(pet.getId(), "a test file", new File(file.getAbsolutePath()));
api.deletePet(pet.getId(), null);
} }
@Test @Test
@ -396,7 +415,7 @@ public class PetApiTest {
private Pet createPet() { private Pet createPet() {
Pet pet = new Pet(); Pet pet = new Pet();
pet.setId(1234567L); pet.setId(ThreadLocalRandom.current().nextLong(Long.MAX_VALUE));
pet.setName("gorilla"); pet.setName("gorilla");
Category category = new Category(); Category category = new Category();