Skip to content

Commit fb399b3

Browse files
committed
RESTWS-963: Add a swagger autogeneration util to scan resource handlers and generate the swagger spec
1 parent ef50052 commit fb399b3

File tree

8 files changed

+926
-30
lines changed

8 files changed

+926
-30
lines changed

omod-2.0/src/test/java/org/openmrs/module/unrelatedtest/rest/resource/UnrelatedGenericChildResource.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,50 @@
99
*/
1010
package org.openmrs.module.unrelatedtest.rest.resource;
1111

12-
import io.swagger.models.Model;
1312
import org.openmrs.module.unrelatedtest.UnrelatedGenericChild;
1413
import org.openmrs.module.webservices.rest.doc.SwaggerSpecificationCreatorTest;
1514
import org.openmrs.module.webservices.rest.web.RestConstants;
1615
import org.openmrs.module.webservices.rest.web.annotation.Resource;
1716
import org.openmrs.module.webservices.rest.web.representation.Representation;
17+
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
18+
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
1819
import org.openmrs.module.webservices.rest.web.v1_0.test.GenericChildResource;
1920

2021
/**
2122
* A test resource that is unrelated to the main webservices package.
22-
*
23+
*
2324
* @see SwaggerSpecificationCreatorTest#testUnrelatedResourceDefinitions()
2425
*/
2526
@Resource(name = RestConstants.VERSION_1 + "/unrelated", supportedClass = UnrelatedGenericChild.class, supportedOpenmrsVersions = { "1.9.* - 9.*" })
2627
public class UnrelatedGenericChildResource extends GenericChildResource {
27-
28+
2829
public static boolean getGETCalled = false;
29-
30+
3031
public static boolean getCREATECalled = false;
31-
32+
3233
public static boolean getUPDATECalled = false;
33-
34+
3435
/*******************************
3536
* TEST METHOD IMPLEMENTATIONS * These methods are the ones we want to test against. There
3637
* implementaion is unimportant, they just set flags so we can assert the methods were called
3738
* correctly by the reflector.
3839
*/
39-
40+
4041
@Override
41-
public Model getGETModel(Representation rep) {
42+
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
4243
getGETCalled = true;
43-
return super.getGETModel(rep);
44+
return super.getRepresentationDescription(rep);
4445
}
45-
46+
4647
@Override
47-
public Model getCREATEModel(Representation rep) {
48+
public DelegatingResourceDescription getCreatableProperties() {
4849
getCREATECalled = true;
49-
return super.getCREATEModel(rep);
50+
return super.getCreatableProperties();
5051
}
51-
52+
5253
@Override
53-
public Model getUPDATEModel(Representation rep) {
54+
public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoesNotSupportOperationException {
5455
getUPDATECalled = true;
55-
return super.getUPDATEModel(rep);
56+
return super.getUpdatableProperties();
5657
}
5758
}

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

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@
1515
import io.swagger.jackson.ModelResolver;
1616
import io.swagger.models.Info;
1717
import io.swagger.models.Model;
18+
import io.swagger.models.ModelImpl;
1819
import io.swagger.models.Operation;
1920
import io.swagger.models.Path;
2021
import io.swagger.models.Scheme;
2122
import io.swagger.models.Swagger;
2223
import io.swagger.models.auth.BasicAuthDefinition;
2324
import io.swagger.models.parameters.Parameter;
25+
import io.swagger.models.properties.ArrayProperty;
26+
import io.swagger.models.properties.DateProperty;
27+
import io.swagger.models.properties.Property;
28+
import io.swagger.models.properties.RefProperty;
29+
import io.swagger.models.properties.StringProperty;
2430
import io.swagger.util.Json;
2531
import org.dbunit.database.DatabaseConnection;
2632
import org.junit.Assert;
@@ -30,9 +36,14 @@
3036
import org.openmrs.Patient;
3137
import org.openmrs.api.context.Context;
3238
import org.openmrs.module.unrelatedtest.rest.resource.UnrelatedGenericChildResource;
39+
import org.openmrs.module.webservices.docs.swagger.SwaggerGenerationUtil;
3340
import org.openmrs.module.webservices.docs.swagger.SwaggerSpecificationCreator;
3441
import org.openmrs.module.webservices.rest.web.RestConstants;
3542
import org.openmrs.module.webservices.rest.web.api.RestService;
43+
import org.openmrs.module.webservices.rest.web.representation.Representation;
44+
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.ObsResource1_8;
45+
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PatientResource1_8;
46+
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PersonResource1_8;
3647
import org.openmrs.web.test.BaseModuleWebContextSensitiveTest;
3748

3849
import java.lang.reflect.Field;
@@ -49,6 +60,7 @@
4960
import static junit.framework.TestCase.assertFalse;
5061
import static junit.framework.TestCase.assertNotNull;
5162
import static junit.framework.TestCase.assertTrue;
63+
import static org.junit.Assert.assertEquals;
5264

5365
public class SwaggerSpecificationCreatorTest extends BaseModuleWebContextSensitiveTest {
5466

@@ -270,7 +282,7 @@ public void createOnlySubresourceDefinitions() {
270282
assertFalse(json.contains("SystemsettingSubdetailsUpdate"));
271283
assertTrue(json.contains("SystemsettingSubdetailsCreate"));
272284
}
273-
285+
274286
/**
275287
* Ensure that resources not directly related to the webservices.rest package are successfully
276288
* defined in the swagger documentation.
@@ -281,24 +293,82 @@ public void testUnrelatedResourceDefinitions() {
281293
UnrelatedGenericChildResource.getGETCalled = false;
282294
UnrelatedGenericChildResource.getCREATECalled = false;
283295
UnrelatedGenericChildResource.getUPDATECalled = false;
284-
296+
285297
// make sure to reset the cache for multiple tests in the same run
286298
if (SwaggerSpecificationCreator.isCached()) {
287299
SwaggerSpecificationCreator.clearCache();
288300
}
289-
301+
290302
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
291303
ssc.getJSON();
292-
304+
293305
// check our custom methods were called
294306
assertTrue(UnrelatedGenericChildResource.getGETCalled);
295307
assertTrue(UnrelatedGenericChildResource.getCREATECalled);
296308
assertTrue(UnrelatedGenericChildResource.getUPDATECalled);
297-
309+
298310
// assert the definition is now in the swagger object
299311
Swagger swagger = ssc.getSwagger();
300312
assertTrue(swagger.getDefinitions().containsKey("UnrelatedGet"));
301313
assertTrue(swagger.getDefinitions().containsKey("UnrelatedUpdate"));
302314
assertTrue(swagger.getDefinitions().containsKey("UnrelatedCreate"));
303315
}
316+
317+
@Test
318+
public void generateGETModel_shouldCheckForOpenMRSResource() {
319+
Model model = SwaggerGenerationUtil.generateGETModel(new ObsResource1_8(), Representation.DEFAULT);
320+
Assert.assertTrue(model instanceof ModelImpl);
321+
322+
Map<String, Property> propertyMap = model.getProperties();
323+
Assert.assertTrue(propertyMap.containsKey("location"));
324+
Assert.assertTrue(propertyMap.containsKey("person"));
325+
Assert.assertTrue(propertyMap.containsKey("obsDatetime"));
326+
Assert.assertTrue(propertyMap.containsKey("accessionNumber"));
327+
328+
Assert.assertTrue(propertyMap.get("location") instanceof RefProperty);
329+
Assert.assertTrue(propertyMap.get("person") instanceof RefProperty);
330+
Assert.assertTrue(propertyMap.get("obsDatetime") instanceof DateProperty);
331+
Assert.assertTrue(propertyMap.get("accessionNumber") instanceof StringProperty);
332+
333+
Property property = propertyMap.get("encounter");
334+
Assert.assertTrue(property instanceof RefProperty);
335+
RefProperty stringProperty = (RefProperty) property;
336+
assertEquals("#/definitions/EncounterGet", stringProperty.get$ref());
337+
}
338+
339+
@Test
340+
public void generateGETModel_shouldReturnAnArrayPropertyWithRefPropertyWhenFieldIsASet() {
341+
Model model = SwaggerGenerationUtil.generateGETModel(new PersonResource1_8(), Representation.DEFAULT);
342+
Assert.assertTrue(model instanceof ModelImpl);
343+
344+
Map<String, Property> propertyMap = model.getProperties();
345+
System.out.println(propertyMap);
346+
Assert.assertTrue(propertyMap.containsKey("attributes"));
347+
348+
Property property = propertyMap.get("attributes");
349+
Assert.assertTrue(property instanceof ArrayProperty);
350+
ArrayProperty arrayProperty = (ArrayProperty) property;
351+
Assert.assertTrue(arrayProperty.getItems() instanceof RefProperty);
352+
353+
RefProperty refProperty = (RefProperty) arrayProperty.getItems();
354+
assertEquals("#/definitions/PersonAttributeGet", refProperty.get$ref());
355+
}
356+
357+
@Test
358+
public void generateGETModelPatient_shouldReturnAnArrayPropertyWithRefPropertyWhenFieldIsASet() {
359+
Model model = SwaggerGenerationUtil.generateGETModel(new PatientResource1_8(), Representation.DEFAULT);
360+
Assert.assertTrue(model instanceof ModelImpl);
361+
362+
Map<String, Property> propertyMap = model.getProperties();
363+
System.out.println(propertyMap);
364+
Assert.assertTrue(propertyMap.containsKey("identifiers"));
365+
366+
Property property = propertyMap.get("identifiers");
367+
Assert.assertTrue(property instanceof ArrayProperty);
368+
ArrayProperty arrayProperty = (ArrayProperty) property;
369+
Assert.assertTrue(arrayProperty.getItems() instanceof RefProperty);
370+
371+
RefProperty refProperty = (RefProperty) arrayProperty.getItems();
372+
assertEquals("#/definitions/PatientIdentifierGet", refProperty.get$ref());
373+
}
304374
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ public void purge(SubDetails delegate, RequestContext context) throws ResponseEx
6363
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
6464
return new DelegatingResourceDescription();
6565
}
66-
66+
67+
@Override
68+
public DelegatingResourceDescription getCreatableProperties() throws ResourceDoesNotSupportOperationException {
69+
return new DelegatingResourceDescription();
70+
}
71+
6772
@Override
6873
public SubDetails newDelegate() {
6974
return new SubDetails();

0 commit comments

Comments
 (0)