Skip to content

Commit

Permalink
RESTWS-963: Add a swagger autogeneration util to scan resource handle…
Browse files Browse the repository at this point in the history
…rs and generate the swagger spec
  • Loading branch information
mherman22 committed Dec 3, 2024
1 parent 9786f8d commit a3de500
Show file tree
Hide file tree
Showing 6 changed files with 952 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException;
import org.openmrs.module.webservices.rest.web.v1_0.test.GenericChildResource;

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

public static boolean getGETCalled = false;
public static boolean getCreatableProperties = false;

public static boolean getCREATECalled = false;

public static boolean getUPDATECalled = false;

/*******************************
* TEST METHOD IMPLEMENTATIONS * These methods are the ones we want to test against. There
* implementaion is unimportant, they just set flags so we can assert the methods were called
* correctly by the reflector.
*/
public static boolean getUpdatableProperties = false;

public static boolean getRepresentationDescription = false;

@Override
public Model getGETModel(Representation rep) {
getGETCalled = true;
return super.getGETModel(rep);
public DelegatingResourceDescription getCreatableProperties() {
getCreatableProperties = true;
return super.getCreatableProperties();
}

@Override
public Model getCREATEModel(Representation rep) {
getCREATECalled = true;
return super.getCREATEModel(rep);
public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoesNotSupportOperationException {
getUpdatableProperties = true;
return super.getUpdatableProperties();
}

@Override
public Model getUPDATEModel(Representation rep) {
getUPDATECalled = true;
return super.getUPDATEModel(rep);
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
getRepresentationDescription = true;
return super.getRepresentationDescription(rep);
}

/*******************************
* TEST METHOD IMPLEMENTATIONS * These methods are the ones we want to test against. There
* implementaion is unimportant, they just set flags so we can assert the methods were called
* correctly by the reflector.
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
import io.swagger.jackson.ModelResolver;
import io.swagger.models.Info;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Scheme;
import io.swagger.models.Swagger;
import io.swagger.models.auth.BasicAuthDefinition;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.DateProperty;
import io.swagger.models.properties.Property;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import io.swagger.util.Json;
import org.dbunit.database.DatabaseConnection;
import org.junit.Assert;
Expand All @@ -30,9 +36,13 @@
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.module.unrelatedtest.rest.resource.UnrelatedGenericChildResource;
import org.openmrs.module.webservices.docs.swagger.SwaggerGenerationUtil;
import org.openmrs.module.webservices.docs.swagger.SwaggerSpecificationCreator;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.api.RestService;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.ObsResource1_8;
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PersonResource1_8;
import org.openmrs.web.test.BaseModuleWebContextSensitiveTest;

import java.lang.reflect.Field;
Expand All @@ -49,6 +59,7 @@
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;

public class SwaggerSpecificationCreatorTest extends BaseModuleWebContextSensitiveTest {

Expand Down Expand Up @@ -270,6 +281,46 @@ public void createOnlySubresourceDefinitions() {
assertFalse(json.contains("SystemsettingSubdetailsUpdate"));
assertTrue(json.contains("SystemsettingSubdetailsCreate"));
}

@Test
public void generateGETModel_shouldCheckForOpenMRSResource() throws NoSuchFieldException {
Model model = SwaggerGenerationUtil.generateGETModel(new ObsResource1_8(), Representation.DEFAULT);
Assert.assertTrue(model instanceof ModelImpl);

Map<String, Property> propertyMap = model.getProperties();
Assert.assertTrue(propertyMap.containsKey("location"));
Assert.assertTrue(propertyMap.containsKey("person"));
Assert.assertTrue(propertyMap.containsKey("obsDatetime"));
Assert.assertTrue(propertyMap.containsKey("accessionNumber"));

Assert.assertTrue(propertyMap.get("location") instanceof RefProperty);
Assert.assertTrue(propertyMap.get("person") instanceof RefProperty);
Assert.assertTrue(propertyMap.get("obsDatetime") instanceof DateProperty);
Assert.assertTrue(propertyMap.get("accessionNumber") instanceof StringProperty);

Property property = propertyMap.get("encounter");
Assert.assertTrue(property instanceof RefProperty);
RefProperty stringProperty = (RefProperty) property;
assertEquals("#/definitions/EncounterGet", stringProperty.get$ref());
}

@Test
public void generateGETModel_shouldReturnAnArrayPropertyWithRefPropertyWhenFieldIsASet() throws NoSuchFieldException {
Model model = SwaggerGenerationUtil.generateGETModel(new PersonResource1_8(), Representation.DEFAULT);
Assert.assertTrue(model instanceof ModelImpl);

Map<String, Property> propertyMap = model.getProperties();
System.out.println(propertyMap);
Assert.assertTrue(propertyMap.containsKey("attributes"));

Property property = propertyMap.get("attributes");
Assert.assertTrue(property instanceof ArrayProperty);
ArrayProperty arrayProperty = (ArrayProperty) property;
Assert.assertTrue(arrayProperty.getItems() instanceof RefProperty);

RefProperty refProperty = (RefProperty) arrayProperty.getItems();
assertEquals("#/definitions/PersonAttributeGet", refProperty.get$ref());
}

/**
* Ensure that resources not directly related to the webservices.rest package are successfully
Expand All @@ -278,9 +329,9 @@ public void createOnlySubresourceDefinitions() {
@Test
public void testUnrelatedResourceDefinitions() {
// ensure the statics are false first
UnrelatedGenericChildResource.getGETCalled = false;
UnrelatedGenericChildResource.getCREATECalled = false;
UnrelatedGenericChildResource.getUPDATECalled = false;
UnrelatedGenericChildResource.getCreatableProperties = false;
UnrelatedGenericChildResource.getUpdatableProperties = false;
UnrelatedGenericChildResource.getRepresentationDescription = false;

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

// check our custom methods were called
assertTrue(UnrelatedGenericChildResource.getGETCalled);
assertTrue(UnrelatedGenericChildResource.getCREATECalled);
assertTrue(UnrelatedGenericChildResource.getUPDATECalled);
assertTrue(UnrelatedGenericChildResource.getCreatableProperties);
assertTrue(UnrelatedGenericChildResource.getUpdatableProperties);
assertTrue(UnrelatedGenericChildResource.getRepresentationDescription);

// assert the definition is now in the swagger object
Swagger swagger = ssc.getSwagger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ public void purge(SubDetails delegate, RequestContext context) throws ResponseEx
public DelegatingResourceDescription getRepresentationDescription(Representation rep) {
return new DelegatingResourceDescription();
}


@Override
public DelegatingResourceDescription getCreatableProperties() throws ResourceDoesNotSupportOperationException {
return new DelegatingResourceDescription();
}

@Override
public SubDetails newDelegate() {
return new SubDetails();
Expand Down
Loading

0 comments on commit a3de500

Please sign in to comment.