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 .PatientResource1_8 ;
46+ import org .openmrs .module .webservices .rest .web .v1_0 .resource .openmrs1_8 .PersonResource1_8 ;
3647import org .openmrs .web .test .BaseModuleWebContextSensitiveTest ;
3748
3849import java .lang .reflect .Field ;
4960import static junit .framework .TestCase .assertFalse ;
5061import static junit .framework .TestCase .assertNotNull ;
5162import static junit .framework .TestCase .assertTrue ;
63+ import static org .junit .Assert .assertEquals ;
5264
5365public 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}
0 commit comments