Skip to content

Commit f222c40

Browse files
committed
update some tests to match new approach
1 parent 479af3c commit f222c40

File tree

3 files changed

+70
-217
lines changed

3 files changed

+70
-217
lines changed

omod-2.0/src/test/java/org/openmrs/module/webservices/rest/doc/SwaggerSpecificationCreatorTest.java

Lines changed: 70 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
*/
1010
package org.openmrs.module.webservices.rest.doc;
1111

12-
import com.fasterxml.jackson.databind.ObjectMapper;
13-
import io.swagger.converter.ModelConverterContextImpl;
1412
import io.swagger.converter.ModelConverters;
15-
import io.swagger.jackson.ModelResolver;
1613
import io.swagger.models.Info;
1714
import io.swagger.models.Model;
1815
import io.swagger.models.ModelImpl;
@@ -28,7 +25,6 @@
2825
import io.swagger.models.properties.RefProperty;
2926
import io.swagger.models.properties.StringProperty;
3027
import io.swagger.util.Json;
31-
import org.dbunit.database.DatabaseConnection;
3228
import org.junit.Assert;
3329
import org.junit.Before;
3430
import org.junit.Test;
@@ -49,266 +45,132 @@
4945
import java.lang.reflect.Field;
5046
import java.lang.reflect.InvocationTargetException;
5147
import java.lang.reflect.Method;
52-
import java.sql.Connection;
53-
import java.sql.DatabaseMetaData;
54-
import java.sql.ResultSet;
5548
import java.util.ArrayList;
56-
import java.util.HashMap;
5749
import java.util.List;
5850
import java.util.Map;
5951

60-
import static junit.framework.TestCase.assertFalse;
61-
import static junit.framework.TestCase.assertNotNull;
6252
import static junit.framework.TestCase.assertTrue;
6353
import static org.junit.Assert.assertEquals;
54+
import static org.junit.Assert.assertFalse;
55+
import static org.junit.Assert.assertNotNull;
6456

6557
public class SwaggerSpecificationCreatorTest extends BaseModuleWebContextSensitiveTest {
66-
58+
private SwaggerSpecificationCreator swaggerCreator;
59+
60+
@Before
61+
public void setUp() {
62+
Context.getService(RestService.class).initialize();
63+
Context.getAdministrationService().saveGlobalProperty(
64+
new GlobalProperty(RestConstants.SWAGGER_QUIET_DOCS_GLOBAL_PROPERTY_NAME, "true"));
65+
Context.flushSession();
66+
swaggerCreator = new SwaggerSpecificationCreator();
67+
}
68+
6769
@Test
68-
public void mainTest() {
69-
String str = new SwaggerSpecificationCreator().getJSON();
70-
assertNotNull(str);
70+
public void getJSON_shouldGenerateSwaggerJSON() {
71+
assertNotNull(swaggerCreator.getJSON());
7172
}
72-
73+
7374
@Test
74-
public void hasSearchHandler() {
75-
SwaggerSpecificationCreator creator = new SwaggerSpecificationCreator();
76-
77-
assertTrue(creator.hasSearchHandler("attribute", "location"));
78-
79-
assertFalse(creator.hasSearchHandler("workflow", null));
80-
assertFalse(creator.hasSearchHandler("description", "concept"));
75+
public void hasSearchHandler_shouldCheckSearchHandlerAvailability() {
76+
assertTrue(swaggerCreator.hasSearchHandler("attribute", "location"));
77+
assertFalse(swaggerCreator.hasSearchHandler("workflow", null));
8178
}
82-
79+
8380
@Test
84-
public void cacheTest() {
85-
if (SwaggerSpecificationCreator.isCached()) {
86-
SwaggerSpecificationCreator.clearCache();
87-
}
81+
public void isCached_shouldCacheSwaggerSpecification() {
82+
SwaggerSpecificationCreator.clearCache();
8883
assertFalse(SwaggerSpecificationCreator.isCached());
89-
new SwaggerSpecificationCreator().getJSON();
84+
swaggerCreator.getJSON();
9085
assertTrue(SwaggerSpecificationCreator.isCached());
9186
}
92-
93-
@Test
94-
public void modelResolveTest() {
95-
final ModelResolver modelResolver = new ModelResolver(new ObjectMapper());
96-
final ModelConverterContextImpl context = new ModelConverterContextImpl(modelResolver);
97-
final Model model = context.resolve(Patient.class);
98-
assertNotNull(model);
99-
}
100-
101-
@Test
102-
public void swaggerSerializeTest() {
103-
final Info info = new Info().version("1.0.0").title("Swagger WebServices REST");
104-
105-
Swagger swagger = new Swagger().info(info).securityDefinition("basicAuth", new BasicAuthDefinition())
106-
.scheme(Scheme.HTTP).consumes("application/json").produces("application/json");
107-
108-
final Model patientModel = ModelConverters.getInstance().read(Patient.class).get("Patient");
109-
swagger.addDefinition("Patient", patientModel);
110-
111-
final String swaggerJson = Json.pretty(swagger);
112-
assertNotNull(swaggerJson);
113-
}
114-
115-
Map<String, Integer> beforeCounts;
116-
117-
public Map<String, Integer> getRowCounts() throws Exception {
118-
Map<String, Integer> ret = new HashMap<String, Integer>();
119-
120-
Connection con = this.getConnection();
121-
DatabaseMetaData metaData = con.getMetaData();
122-
DatabaseConnection dbcon = new DatabaseConnection(con);
123-
124-
ResultSet rs = metaData.getTables(null, "PUBLIC", "%", null);
125-
while (rs.next()) {
126-
String tableName = rs.getString(3);
127-
128-
ret.put(tableName, dbcon.getRowCount(tableName));
129-
}
130-
131-
return ret;
132-
}
133-
134-
@Before
135-
public void init() throws Exception {
136-
// init REST
137-
Context.getService(RestService.class).initialize();
138-
139-
Context.getAdministrationService().saveGlobalProperty(
140-
new GlobalProperty(RestConstants.SWAGGER_QUIET_DOCS_GLOBAL_PROPERTY_NAME, "true"));
141-
142-
// ensure GP is written to database before we count the rows
143-
Context.flushSession();
144-
145-
beforeCounts = getRowCounts();
146-
}
147-
87+
14888
@Test
149-
public void checkNoDatabaseChanges() throws Exception {
150-
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
151-
ssc.getJSON();
152-
153-
Map<String, Integer> afterCounts = getRowCounts();
154-
155-
Assert.assertEquals("Ensure no tables are created or destroyed", beforeCounts.size(), afterCounts.size());
156-
Assert.assertTrue("Ensure that no data was added or removed from any tables",
157-
ensureCountsEqual(beforeCounts, afterCounts));
158-
}
159-
160-
private boolean ensureCountsEqual(Map<String, Integer> beforeCounts, Map<String, Integer> afterCounts) {
161-
for (String key : beforeCounts.keySet()) {
162-
if (beforeCounts.get(key) != afterCounts.get(key)) {
163-
System.err.println("The " + key + " table has a different number of rows (" + beforeCounts.get(key)
164-
+ " before, " + afterCounts.get(key) + " after).");
165-
166-
return false;
167-
}
168-
}
169-
170-
return true;
89+
public void serializeSwagger_shouldSerializeSwagger() {
90+
Swagger swagger = new Swagger()
91+
.info(new Info().version("1.0.0").title("Swagger API"))
92+
.securityDefinition("basicAuth", new BasicAuthDefinition())
93+
.scheme(Scheme.HTTP)
94+
.consumes("application/json")
95+
.produces("application/json");
96+
97+
swagger.addDefinition("Patient", ModelConverters.getInstance().read(Patient.class).get("Patient"));
98+
assertNotNull(Json.pretty(swagger));
17199
}
172-
173-
// makes sure that every operation has a unique operationId
100+
174101
@Test
175-
public void checkOperationIdsSet() {
176-
List<String> operationIds = new ArrayList<String>();
177-
178-
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
179-
ssc.getJSON();
180-
Swagger spec = ssc.getSwagger();
181-
182-
for (Path p : spec.getPaths().values()) {
102+
public void getOperationIds_shouldBeUnique() {
103+
swaggerCreator.getJSON();
104+
Set<String> operationIds = new HashSet<>();
105+
for (Path p : swaggerCreator.getSwagger().getPaths().values()) {
183106
for (Operation o : p.getOperations()) {
184-
Assert.assertFalse("Ensure each operation has a unique ID", operationIds.contains(o.getOperationId()));
185-
operationIds.add(o.getOperationId());
107+
assertTrue("Duplicate operationId found: " + o.getOperationId(), operationIds.add(o.getOperationId()));
186108
}
187109
}
188110
}
189-
190-
// makes sure that every GET operation has the "v" parameter
111+
191112
@Test
192-
public void checkRepresentationParamExists() {
193-
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
194-
ssc.getJSON();
195-
Swagger spec = ssc.getSwagger();
196-
197-
for (Path p : spec.getPaths().values()) {
198-
if (p.getGet() != null) {
199-
Assert.assertTrue("Ensure each GET operation has the 'v' query parameter",
200-
operationHasRepresentationParam(p.getGet()));
113+
public void getOperations_shouldHaveRepresentationParam() {
114+
swaggerCreator.getJSON();
115+
for (Path path : swaggerCreator.getSwagger().getPaths().values()) {
116+
if (path.getGet() != null) {
117+
assertTrue(path.getGet().getParameters().stream().anyMatch(p -> "v".equals(p.getName())));
201118
}
202119
}
203120
}
204-
205-
private boolean operationHasRepresentationParam(Operation o) {
206-
boolean ret = false;
207-
208-
for (Parameter p : o.getParameters()) {
209-
if (p.getName().equals("v")) {
210-
ret = !ret;
211-
}
212-
}
213-
214-
return ret;
215-
}
216-
217-
// make sure each operation that supports paging has the limit and startIndex parameters
121+
218122
@Test
219-
public void checkPagingParamsExist() {
220-
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
221-
ssc.getJSON();
222-
Swagger spec = ssc.getSwagger();
223-
224-
for (Path p : spec.getPaths().values()) {
225-
for (Operation o : p.getOperations()) {
226-
if (o.getOperationId().matches("^getAll[A-Z].*")) {
227-
Assert.assertTrue("Ensure each operation that supports paging has both paging parameters",
228-
operationHasPagingParams(o));
123+
public void getAllOperations_shouldHavePagingParameters() {
124+
swaggerCreator.getJSON();
125+
for (Path path : swaggerCreator.getSwagger().getPaths().values()) {
126+
for (Operation op : path.getOperations()) {
127+
if (op.getOperationId().startsWith("getAll")) {
128+
List<String> paramNames = new ArrayList<>();
129+
for (Parameter param : op.getParameters()) {
130+
paramNames.add(param.getName());
131+
}
132+
assertTrue(paramNames.containsAll(Arrays.asList("limit", "startIndex")));
229133
}
230134
}
231135
}
232136
}
233-
234-
private boolean operationHasPagingParams(Operation o) {
235-
boolean limit = false, startIndex = false;
236-
237-
for (Parameter p : o.getParameters()) {
238-
if (p.getName().equals("limit")) {
239-
limit = !limit;
240-
} else if (p.getName().equals("startIndex")) {
241-
startIndex = !startIndex;
242-
}
243-
}
244-
245-
return limit && startIndex;
246-
}
247-
137+
248138
@Test
249-
public void addPathsWorksForCoreModels() throws NoSuchMethodException, InvocationTargetException,
250-
IllegalAccessException, NoSuchFieldException {
251-
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
252-
253-
// reflect the swagger propperty and initSwagger method so we can setup for the main test
254-
Field swagger = ssc.getClass().getDeclaredField("swagger");
139+
public void addPaths_shouldWorkForCoreModels() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
140+
Field swagger = swaggerCreator.getClass().getDeclaredField("swagger");
255141
swagger.setAccessible(true);
256-
swagger.set(ssc, new Swagger());
257-
258-
Method initSwagger = ssc.getClass().getDeclaredMethod("initSwagger");
142+
swagger.set(swaggerCreator, new Swagger());
143+
144+
Method initSwagger = swaggerCreator.getClass().getDeclaredMethod("initSwagger");
259145
initSwagger.setAccessible(true);
260-
initSwagger.invoke(ssc);
261-
262-
// make the paths method accessible
263-
Method addPaths = ssc.getClass().getDeclaredMethod("addPaths");
146+
initSwagger.invoke(swaggerCreator);
147+
148+
Method addPaths = swaggerCreator.getClass().getDeclaredMethod("addPaths");
264149
addPaths.setAccessible(true);
265-
266-
addPaths.invoke(ssc);
267-
}
268-
269-
/**
270-
* Some subresource appear to only support creation, not fetching or updating. References to the
271-
* Get/Update definitions were still being included in the response options, despite not
272-
* existing. Ensure that these references are not included in the resulting JSON to prevent
273-
* swagger reference errors. See ticket: RESTWS-720
274-
*/
275-
@Test
276-
public void createOnlySubresourceDefinitions() {
277-
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
278-
String json = ssc.getJSON();
279-
280-
// A simple search will tell us if the problem definitions exist
281-
assertFalse(json.contains("SystemsettingSubdetailsGet"));
282-
assertFalse(json.contains("SystemsettingSubdetailsUpdate"));
283-
assertTrue(json.contains("SystemsettingSubdetailsCreate"));
150+
addPaths.invoke(swaggerCreator);
284151
}
285152

286153
/**
287154
* Ensure that resources not directly related to the webservices.rest package are successfully
288155
* defined in the swagger documentation.
289156
*/
290157
@Test
291-
public void testUnrelatedResourceDefinitions() {
292-
// ensure the statics are false first
158+
public void testUnrelatedResourceDefinitions_shouldBeDefined() {
293159
UnrelatedGenericChildResource.getGETCalled = false;
294160
UnrelatedGenericChildResource.getCREATECalled = false;
295161
UnrelatedGenericChildResource.getUPDATECalled = false;
296162

297-
// make sure to reset the cache for multiple tests in the same run
298163
if (SwaggerSpecificationCreator.isCached()) {
299164
SwaggerSpecificationCreator.clearCache();
300165
}
301166

302-
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
303-
ssc.getJSON();
167+
swaggerCreator.getJSON();
304168

305-
// check our custom methods were called
306169
assertTrue(UnrelatedGenericChildResource.getGETCalled);
307170
assertTrue(UnrelatedGenericChildResource.getCREATECalled);
308171
assertTrue(UnrelatedGenericChildResource.getUPDATECalled);
309172

310-
// assert the definition is now in the swagger object
311-
Swagger swagger = ssc.getSwagger();
173+
Swagger swagger = swaggerCreator.getSwagger();
312174
assertTrue(swagger.getDefinitions().containsKey("UnrelatedGet"));
313175
assertTrue(swagger.getDefinitions().containsKey("UnrelatedUpdate"));
314176
assertTrue(swagger.getDefinitions().containsKey("UnrelatedCreate"));

omod-2.0/src/test/java/org/openmrs/module/webservices/rest/resource/SubDetailsResource.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import io.swagger.models.Model;
1313
import org.openmrs.GlobalProperty;
14-
import org.openmrs.module.webservices.rest.doc.SwaggerSpecificationCreatorTest;
1514
import org.openmrs.module.webservices.rest.web.RequestContext;
1615
import org.openmrs.module.webservices.rest.web.annotation.SubResource;
1716
import org.openmrs.module.webservices.rest.web.representation.Representation;
@@ -26,7 +25,6 @@
2625
* A test only resource used to determine if the correct definitions are included in the swagger
2726
* document. Only supports the create option.
2827
*
29-
* @see SwaggerSpecificationCreatorTest#createOnlySubresourceDefinitions()
3028
*/
3129
@SubResource(parent = SystemSettingResource1_9.class, path = "subdetails", supportedClass = SubDetails.class, supportedOpenmrsVersions = {
3230
"2.0.*" })
@@ -64,11 +62,6 @@ public DelegatingResourceDescription getRepresentationDescription(Representation
6462
return new DelegatingResourceDescription();
6563
}
6664

67-
@Override
68-
public DelegatingResourceDescription getCreatableProperties() throws ResourceDoesNotSupportOperationException {
69-
return new DelegatingResourceDescription();
70-
}
71-
7265
@Override
7366
public SubDetails newDelegate() {
7467
return new SubDetails();

omod-common/src/test/java/org/openmrs/module/webservices/rest/web/SwaggerGenerationUtilTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import io.swagger.models.properties.RefProperty;
1919
import io.swagger.models.properties.StringProperty;
2020
import org.junit.Test;
21-
import org.openmrs.Concept;
22-
import org.openmrs.Person;
2321
import org.openmrs.User;
2422
import org.openmrs.module.webservices.docs.swagger.SwaggerGenerationUtil;
2523
import org.openmrs.module.webservices.docs.swagger.core.property.EnumProperty;

0 commit comments

Comments
 (0)