Skip to content

Commit 527d5e4

Browse files
[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 e850841. * fix java unit test issues
1 parent 2c1ca02 commit 527d5e4

7 files changed

Lines changed: 62 additions & 17 deletions

File tree

CI/circle_parallel.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ NODE_INDEX=${CIRCLE_NODE_INDEX:-0}
77

88
set -e
99

10+
function cleanup {
11+
# Show logs of 'petstore.swagger' container to troubleshoot Unit Test failures, if any.
12+
docker logs petstore.swagger # container name specified in circle.yml
13+
}
14+
15+
trap cleanup EXIT
16+
1017
if [ "$NODE_INDEX" = "1" ]; then
1118
echo "Running node $NODE_INDEX to test 'samples.circleci' defined in pom.xml ..."
1219
java -version

CI/samples.ci/client/petstore/java/test-manual/okhttp-gson/api/PetApiTest.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
import java.util.HashMap;
2929
import java.util.List;
3030
import java.util.Map;
31+
import java.util.concurrent.ThreadLocalRandom;
3132
import java.io.BufferedWriter;
3233
import java.io.File;
3334
import java.io.FileWriter;
35+
import org.slf4j.Logger;
36+
import org.slf4j.LoggerFactory;
3437

3538
import org.junit.*;
3639

@@ -42,20 +45,24 @@
4245
public class PetApiTest {
4346

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

4652
@Before
4753
public void setup() {
4854
// setup authentication
4955
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
5056
apiKeyAuth.setApiKey("special-key");
57+
api.getApiClient().setBasePath(basePath);
5158
}
5259

5360
@Test
5461
public void testApiClient() {
5562
// the default api client is used
5663
assertEquals(Configuration.getDefaultApiClient(), api.getApiClient());
5764
assertNotNull(api.getApiClient());
58-
assertEquals("http://petstore.swagger.io:80/v2", api.getApiClient().getBasePath());
65+
assertEquals(basePath, api.getApiClient().getBasePath());
5966
assertFalse(api.getApiClient().isDebugging());
6067

6168
ApiClient oldClient = api.getApiClient();
@@ -74,7 +81,7 @@ public void testApiClient() {
7481
// set api client via setter method
7582
api.setApiClient(oldClient);
7683
assertNotNull(api.getApiClient());
77-
assertEquals("http://petstore.swagger.io:80/v2", api.getApiClient().getBasePath());
84+
assertEquals(basePath, api.getApiClient().getBasePath());
7885
assertFalse(api.getApiClient().isDebugging());
7986
}
8087

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

8693
Pet fetched = api.getPetById(pet.getId());
8794
assertPetMatches(pet, fetched);
95+
api.deletePet(pet.getId(), null);
8896
}
8997

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

100108
assertPetMatches(pet, fetched);
109+
api.deletePet(pet.getId(), null);
101110
}
102111

103112
@Test
@@ -144,6 +153,7 @@ public void onDownloadProgress(long bytesRead, long contentLength, boolean done)
144153
}
145154
} while (result.isEmpty());
146155
assertPetMatches(pet, fetched);
156+
api.deletePet(pet.getId(), null);
147157
}
148158

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

202213
@Test
@@ -255,26 +266,31 @@ public void testCreateAndGetMultiplePetsAsync() throws Exception {
255266
final ApiException exception = getCallback3.getException();
256267
assertNotNull(exception);
257268
assertEquals(404, exception.getCode());
269+
api.deletePet(pet1.getId(), null);
270+
api.deletePet(pet2.getId(), null);
258271
}
259272

260273

261274
@Test
262275
public void testUpdatePet() throws Exception {
263276
Pet pet = createPet();
277+
api.addPet(pet);
264278
pet.setName("programmer");
265279

266280
api.updatePet(pet);
267281

268282
Pet fetched = api.getPetById(pet.getId());
269283
assertPetMatches(pet, fetched);
284+
api.deletePet(pet.getId(), null);
270285
}
271286

272287
@Test
273288
public void testFindPetsByStatus() throws Exception {
289+
assertEquals(basePath, api.getApiClient().getBasePath());
274290
Pet pet = createPet();
291+
api.addPet(pet);
275292
pet.setName("programmer");
276293
pet.setStatus(Pet.StatusEnum.PENDING);
277-
278294
api.updatePet(pet);
279295

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

337353
assertEquals(updated.getName(), "furt");
354+
api.deletePet(pet.getId(), null);
338355
}
339356

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

345362
Pet fetched = api.getPetById(pet.getId());
346-
api.deletePet(fetched.getId(), null);
363+
api.deletePet(pet.getId(), null);
347364

348365
try {
349366
fetched = api.getPetById(fetched.getId());
350367
fail("expected an error");
351368
} catch (ApiException e) {
369+
LOG.info("Code: {}. Message: {}", e.getCode(), e.getMessage());
352370
assertEquals(404, e.getCode());
353371
}
354372
}
@@ -364,6 +382,7 @@ public void testUploadFile() throws Exception {
364382
writer.close();
365383

366384
api.uploadFile(pet.getId(), "a test file", new File(file.getAbsolutePath()));
385+
api.deletePet(pet.getId(), null);
367386
}
368387

369388
@Test
@@ -396,7 +415,7 @@ public void testEqualsAndHashCode() {
396415

397416
private Pet createPet() {
398417
Pet pet = new Pet();
399-
pet.setId(1234567L);
418+
pet.setId(ThreadLocalRandom.current().nextLong(Long.MAX_VALUE));
400419
pet.setName("gorilla");
401420

402421
Category category = new Category();

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jobs:
6161
# - run: docker pull openapitools/openapi-petstore
6262
# - run: docker run -d -e OPENAPI_BASE_PATH=/v3 -e DISABLE_API_KEY=1 -e DISABLE_OAUTH=1 -p 80:8080 openapitools/openapi-petstore
6363
- run: docker pull swaggerapi/petstore
64-
- run: docker run -d -e SWAGGER_HOST=http://petstore.swagger.io -e SWAGGER_BASE_PATH=/v2 -p 80:8080 swaggerapi/petstore
64+
- run: docker run --name petstore.swagger -d -e SWAGGER_HOST=http://petstore.swagger.io -e SWAGGER_BASE_PATH=/v2 -p 80:8080 swaggerapi/petstore
6565
- run: docker ps -a
6666
- run: sleep 30
6767
- run: cat /etc/hosts

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/pom.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<plugin>
6464
<groupId>org.apache.maven.plugins</groupId>
6565
<artifactId>maven-surefire-plugin</artifactId>
66-
<version>2.12</version>
66+
<version>3.0.0-M4</version>
6767
<configuration>
6868
<systemProperties>
6969
<property>
@@ -73,7 +73,7 @@
7373
</systemProperties>
7474
<argLine>-Xms512m -Xmx1500m</argLine>
7575
<parallel>methods</parallel>
76-
<forkMode>pertest</forkMode>
76+
<threadCount>10</threadCount>
7777
</configuration>
7878
</plugin>
7979
<plugin>

samples/client/petstore/java/okhttp-gson-parcelableModel/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<plugin>
5757
<groupId>org.apache.maven.plugins</groupId>
5858
<artifactId>maven-surefire-plugin</artifactId>
59-
<version>2.12</version>
59+
<version>3.0.0-M4</version>
6060
<configuration>
6161
<systemProperties>
6262
<property>
@@ -66,7 +66,7 @@
6666
</systemProperties>
6767
<argLine>-Xms512m -Xmx1500m</argLine>
6868
<parallel>methods</parallel>
69-
<forkMode>pertest</forkMode>
69+
<threadCount>10</threadCount>
7070
</configuration>
7171
</plugin>
7272
<plugin>

samples/client/petstore/java/okhttp-gson/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<plugin>
5757
<groupId>org.apache.maven.plugins</groupId>
5858
<artifactId>maven-surefire-plugin</artifactId>
59-
<version>2.12</version>
59+
<version>3.0.0-M4</version>
6060
<configuration>
6161
<systemProperties>
6262
<property>
@@ -66,7 +66,7 @@
6666
</systemProperties>
6767
<argLine>-Xms512m -Xmx1500m</argLine>
6868
<parallel>methods</parallel>
69-
<forkMode>pertest</forkMode>
69+
<threadCount>10</threadCount>
7070
</configuration>
7171
</plugin>
7272
<plugin>

samples/client/petstore/java/okhttp-gson/src/test/java/org/openapitools/client/api/PetApiTest.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@
2828
import java.util.HashMap;
2929
import java.util.List;
3030
import java.util.Map;
31+
import java.util.concurrent.ThreadLocalRandom;
3132
import java.io.BufferedWriter;
3233
import java.io.File;
3334
import java.io.FileWriter;
35+
import org.slf4j.Logger;
36+
import org.slf4j.LoggerFactory;
3437

3538
import org.junit.*;
3639

@@ -42,20 +45,24 @@
4245
public class PetApiTest {
4346

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

4652
@Before
4753
public void setup() {
4854
// setup authentication
4955
ApiKeyAuth apiKeyAuth = (ApiKeyAuth) api.getApiClient().getAuthentication("api_key");
5056
apiKeyAuth.setApiKey("special-key");
57+
api.getApiClient().setBasePath(basePath);
5158
}
5259

5360
@Test
5461
public void testApiClient() {
5562
// the default api client is used
5663
assertEquals(Configuration.getDefaultApiClient(), api.getApiClient());
5764
assertNotNull(api.getApiClient());
58-
assertEquals("http://petstore.swagger.io:80/v2", api.getApiClient().getBasePath());
65+
assertEquals(basePath, api.getApiClient().getBasePath());
5966
assertFalse(api.getApiClient().isDebugging());
6067

6168
ApiClient oldClient = api.getApiClient();
@@ -74,7 +81,7 @@ public void testApiClient() {
7481
// set api client via setter method
7582
api.setApiClient(oldClient);
7683
assertNotNull(api.getApiClient());
77-
assertEquals("http://petstore.swagger.io:80/v2", api.getApiClient().getBasePath());
84+
assertEquals(basePath, api.getApiClient().getBasePath());
7885
assertFalse(api.getApiClient().isDebugging());
7986
}
8087

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

8693
Pet fetched = api.getPetById(pet.getId());
8794
assertPetMatches(pet, fetched);
95+
api.deletePet(pet.getId(), null);
8896
}
8997

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

100108
assertPetMatches(pet, fetched);
109+
api.deletePet(pet.getId(), null);
101110
}
102111

103112
@Test
@@ -144,6 +153,7 @@ public void onDownloadProgress(long bytesRead, long contentLength, boolean done)
144153
}
145154
} while (result.isEmpty());
146155
assertPetMatches(pet, fetched);
156+
api.deletePet(pet.getId(), null);
147157
}
148158

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

202213
@Test
@@ -255,26 +266,31 @@ public void testCreateAndGetMultiplePetsAsync() throws Exception {
255266
final ApiException exception = getCallback3.getException();
256267
assertNotNull(exception);
257268
assertEquals(404, exception.getCode());
269+
api.deletePet(pet1.getId(), null);
270+
api.deletePet(pet2.getId(), null);
258271
}
259272

260273

261274
@Test
262275
public void testUpdatePet() throws Exception {
263276
Pet pet = createPet();
277+
api.addPet(pet);
264278
pet.setName("programmer");
265279

266280
api.updatePet(pet);
267281

268282
Pet fetched = api.getPetById(pet.getId());
269283
assertPetMatches(pet, fetched);
284+
api.deletePet(pet.getId(), null);
270285
}
271286

272287
@Test
273288
public void testFindPetsByStatus() throws Exception {
289+
assertEquals(basePath, api.getApiClient().getBasePath());
274290
Pet pet = createPet();
291+
api.addPet(pet);
275292
pet.setName("programmer");
276293
pet.setStatus(Pet.StatusEnum.PENDING);
277-
278294
api.updatePet(pet);
279295

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

337353
assertEquals(updated.getName(), "furt");
354+
api.deletePet(pet.getId(), null);
338355
}
339356

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

345362
Pet fetched = api.getPetById(pet.getId());
346-
api.deletePet(fetched.getId(), null);
363+
api.deletePet(pet.getId(), null);
347364

348365
try {
349366
fetched = api.getPetById(fetched.getId());
350367
fail("expected an error");
351368
} catch (ApiException e) {
369+
LOG.info("Code: {}. Message: {}", e.getCode(), e.getMessage());
352370
assertEquals(404, e.getCode());
353371
}
354372
}
@@ -364,6 +382,7 @@ public void testUploadFile() throws Exception {
364382
writer.close();
365383

366384
api.uploadFile(pet.getId(), "a test file", new File(file.getAbsolutePath()));
385+
api.deletePet(pet.getId(), null);
367386
}
368387

369388
@Test
@@ -396,7 +415,7 @@ public void testEqualsAndHashCode() {
396415

397416
private Pet createPet() {
398417
Pet pet = new Pet();
399-
pet.setId(1234567L);
418+
pet.setId(ThreadLocalRandom.current().nextLong(Long.MAX_VALUE));
400419
pet.setName("gorilla");
401420

402421
Category category = new Category();

0 commit comments

Comments
 (0)