1515import io .swagger .jackson .ModelResolver ;
1616import io .swagger .models .Info ;
1717import io .swagger .models .Model ;
18+ import io .swagger .models .ModelImpl ;
1819import io .swagger .models .Operation ;
1920import io .swagger .models .Path ;
2021import io .swagger .models .Scheme ;
2122import io .swagger .models .Swagger ;
2223import io .swagger .models .auth .BasicAuthDefinition ;
2324import 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 ;
2430import io .swagger .util .Json ;
2531import org .dbunit .database .DatabaseConnection ;
2632import org .junit .Assert ;
3036import org .openmrs .Patient ;
3137import org .openmrs .api .context .Context ;
3238import org .openmrs .module .unrelatedtest .rest .resource .UnrelatedGenericChildResource ;
39+ import org .openmrs .module .webservices .docs .swagger .SwaggerGenerationUtil ;
3340import org .openmrs .module .webservices .docs .swagger .SwaggerSpecificationCreator ;
3441import org .openmrs .module .webservices .rest .web .RestConstants ;
3542import 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 ;
3646import org .openmrs .web .test .BaseModuleWebContextSensitiveTest ;
3747
3848import java .lang .reflect .Field ;
4959import static junit .framework .TestCase .assertFalse ;
5060import static junit .framework .TestCase .assertNotNull ;
5161import static junit .framework .TestCase .assertTrue ;
62+ import static org .junit .Assert .assertEquals ;
5263
5364public 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 ();
0 commit comments