Skip to content

Commit a3de500

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

File tree

6 files changed

+952
-74
lines changed

6 files changed

+952
-74
lines changed

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.openmrs.module.webservices.rest.web.RestConstants;
1616
import org.openmrs.module.webservices.rest.web.annotation.Resource;
1717
import org.openmrs.module.webservices.rest.web.representation.Representation;
18+
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
19+
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
1820
import org.openmrs.module.webservices.rest.web.v1_0.test.GenericChildResource;
1921

2022
/**
@@ -25,33 +27,33 @@
2527
@Resource(name = RestConstants.VERSION_1 + "/unrelated", supportedClass = UnrelatedGenericChild.class, supportedOpenmrsVersions = { "1.9.* - 9.*" })
2628
public class UnrelatedGenericChildResource extends GenericChildResource {
2729

28-
public static boolean getGETCalled = false;
30+
public static boolean getCreatableProperties = false;
2931

30-
public static boolean getCREATECalled = false;
31-
32-
public static boolean getUPDATECalled = false;
33-
34-
/*******************************
35-
* TEST METHOD IMPLEMENTATIONS * These methods are the ones we want to test against. There
36-
* implementaion is unimportant, they just set flags so we can assert the methods were called
37-
* correctly by the reflector.
38-
*/
32+
public static boolean getUpdatableProperties = false;
3933

34+
public static boolean getRepresentationDescription = false;
35+
4036
@Override
41-
public Model getGETModel(Representation rep) {
42-
getGETCalled = true;
43-
return super.getGETModel(rep);
37+
public DelegatingResourceDescription getCreatableProperties() {
38+
getCreatableProperties = true;
39+
return super.getCreatableProperties();
4440
}
45-
41+
4642
@Override
47-
public Model getCREATEModel(Representation rep) {
48-
getCREATECalled = true;
49-
return super.getCREATEModel(rep);
43+
public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoesNotSupportOperationException {
44+
getUpdatableProperties = true;
45+
return super.getUpdatableProperties();
5046
}
51-
47+
5248
@Override
53-
public Model getUPDATEModel(Representation rep) {
54-
getUPDATECalled = true;
55-
return super.getUPDATEModel(rep);
49+
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
50+
getRepresentationDescription = true;
51+
return super.getRepresentationDescription(rep);
5652
}
53+
54+
/*******************************
55+
* TEST METHOD IMPLEMENTATIONS * These methods are the ones we want to test against. There
56+
* implementaion is unimportant, they just set flags so we can assert the methods were called
57+
* correctly by the reflector.
58+
*/
5759
}

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

Lines changed: 57 additions & 6 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,13 @@
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.PersonResource1_8;
3646
import org.openmrs.web.test.BaseModuleWebContextSensitiveTest;
3747

3848
import java.lang.reflect.Field;
@@ -49,6 +59,7 @@
4959
import static junit.framework.TestCase.assertFalse;
5060
import static junit.framework.TestCase.assertNotNull;
5161
import static junit.framework.TestCase.assertTrue;
62+
import static org.junit.Assert.assertEquals;
5263

5364
public class SwaggerSpecificationCreatorTest extends BaseModuleWebContextSensitiveTest {
5465

@@ -270,6 +281,46 @@ public void createOnlySubresourceDefinitions() {
270281
assertFalse(json.contains("SystemsettingSubdetailsUpdate"));
271282
assertTrue(json.contains("SystemsettingSubdetailsCreate"));
272283
}
284+
285+
@Test
286+
public void generateGETModel_shouldCheckForOpenMRSResource() throws NoSuchFieldException {
287+
Model model = SwaggerGenerationUtil.generateGETModel(new ObsResource1_8(), Representation.DEFAULT);
288+
Assert.assertTrue(model instanceof ModelImpl);
289+
290+
Map<String, Property> propertyMap = model.getProperties();
291+
Assert.assertTrue(propertyMap.containsKey("location"));
292+
Assert.assertTrue(propertyMap.containsKey("person"));
293+
Assert.assertTrue(propertyMap.containsKey("obsDatetime"));
294+
Assert.assertTrue(propertyMap.containsKey("accessionNumber"));
295+
296+
Assert.assertTrue(propertyMap.get("location") instanceof RefProperty);
297+
Assert.assertTrue(propertyMap.get("person") instanceof RefProperty);
298+
Assert.assertTrue(propertyMap.get("obsDatetime") instanceof DateProperty);
299+
Assert.assertTrue(propertyMap.get("accessionNumber") instanceof StringProperty);
300+
301+
Property property = propertyMap.get("encounter");
302+
Assert.assertTrue(property instanceof RefProperty);
303+
RefProperty stringProperty = (RefProperty) property;
304+
assertEquals("#/definitions/EncounterGet", stringProperty.get$ref());
305+
}
306+
307+
@Test
308+
public void generateGETModel_shouldReturnAnArrayPropertyWithRefPropertyWhenFieldIsASet() throws NoSuchFieldException {
309+
Model model = SwaggerGenerationUtil.generateGETModel(new PersonResource1_8(), Representation.DEFAULT);
310+
Assert.assertTrue(model instanceof ModelImpl);
311+
312+
Map<String, Property> propertyMap = model.getProperties();
313+
System.out.println(propertyMap);
314+
Assert.assertTrue(propertyMap.containsKey("attributes"));
315+
316+
Property property = propertyMap.get("attributes");
317+
Assert.assertTrue(property instanceof ArrayProperty);
318+
ArrayProperty arrayProperty = (ArrayProperty) property;
319+
Assert.assertTrue(arrayProperty.getItems() instanceof RefProperty);
320+
321+
RefProperty refProperty = (RefProperty) arrayProperty.getItems();
322+
assertEquals("#/definitions/PersonAttributeGet", refProperty.get$ref());
323+
}
273324

274325
/**
275326
* Ensure that resources not directly related to the webservices.rest package are successfully
@@ -278,9 +329,9 @@ public void createOnlySubresourceDefinitions() {
278329
@Test
279330
public void testUnrelatedResourceDefinitions() {
280331
// ensure the statics are false first
281-
UnrelatedGenericChildResource.getGETCalled = false;
282-
UnrelatedGenericChildResource.getCREATECalled = false;
283-
UnrelatedGenericChildResource.getUPDATECalled = false;
332+
UnrelatedGenericChildResource.getCreatableProperties = false;
333+
UnrelatedGenericChildResource.getUpdatableProperties = false;
334+
UnrelatedGenericChildResource.getRepresentationDescription = false;
284335

285336
// make sure to reset the cache for multiple tests in the same run
286337
if (SwaggerSpecificationCreator.isCached()) {
@@ -291,9 +342,9 @@ public void testUnrelatedResourceDefinitions() {
291342
ssc.getJSON();
292343

293344
// check our custom methods were called
294-
assertTrue(UnrelatedGenericChildResource.getGETCalled);
295-
assertTrue(UnrelatedGenericChildResource.getCREATECalled);
296-
assertTrue(UnrelatedGenericChildResource.getUPDATECalled);
345+
assertTrue(UnrelatedGenericChildResource.getCreatableProperties);
346+
assertTrue(UnrelatedGenericChildResource.getUpdatableProperties);
347+
assertTrue(UnrelatedGenericChildResource.getRepresentationDescription);
297348

298349
// assert the definition is now in the swagger object
299350
Swagger swagger = ssc.getSwagger();

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)