From 4e0bf0f8f2a2e248a252631183b86bc4039f75a3 Mon Sep 17 00:00:00 2001 From: sherrif10 Date: Fri, 12 Jun 2020 16:15:50 +0300 Subject: [PATCH 1/9] ATT-42: AttachmentResource* to be documented in Swagger https://issues.openmrs.org/browse/ATT-42 --- omod-2.0/pom.xml | 4 +- .../rest/AttachmentResource2_0.java | 181 ++++++++++++++++++ pom.xml | 2 +- 3 files changed, 184 insertions(+), 3 deletions(-) diff --git a/omod-2.0/pom.xml b/omod-2.0/pom.xml index 587414ca..11cf20b5 100644 --- a/omod-2.0/pom.xml +++ b/omod-2.0/pom.xml @@ -16,7 +16,7 @@ ${openMRS2_0Version} 1.19 - 2.17 + 2.21.0 0.10.4 0.2.12 @@ -110,7 +110,7 @@ org.openmrs.module - webservices.rest-omod + webservices.rest-omod-common ${webservices.restVersion} provided diff --git a/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java b/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java index c21f470e..bb0acf5a 100644 --- a/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java +++ b/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java @@ -1,17 +1,197 @@ package org.openmrs.module.attachments.rest; +import java.util.Arrays; +import java.util.List; + import org.hibernate.FlushMode; import org.openmrs.Obs; import org.openmrs.api.context.Context; +import org.openmrs.module.attachments.AttachmentsConstants; import org.openmrs.module.attachments.obs.Attachment; import org.openmrs.module.emrapi.db.DbSessionUtil; +import org.openmrs.module.webservices.rest.web.RequestContext; 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.DefaultRepresentation; +import org.openmrs.module.webservices.rest.web.representation.FullRepresentation; +import org.openmrs.module.webservices.rest.web.representation.RefRepresentation; +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.GenericRestException; +import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException; +import org.openmrs.module.webservices.rest.web.response.ResponseException; + +import io.swagger.models.Model; +import io.swagger.models.ModelImpl; +import io.swagger.models.properties.ArrayProperty; +import io.swagger.models.properties.BooleanProperty; +import io.swagger.models.properties.DateProperty; +import io.swagger.models.properties.RefProperty; +import io.swagger.models.properties.StringProperty; +/** + * {@link Resource} for Attachment, supporting standard CRUD operations + */ @Resource(name = RestConstants.VERSION_1 + "/attachment", supportedClass = Attachment.class, supportedOpenmrsVersions = { "2.0.0" }) public class AttachmentResource2_0 extends AttachmentResource1_10 { + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#getRepresentationDescription(org.openmrs.module.webservices.rest.web.representation.Representation) + */ + @Override + public DelegatingResourceDescription getRepresentationDescription(Representation rep) { + if (rep instanceof DefaultRepresentation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("uuid"); + description.addProperty("dateTime"); + description.addProperty("comment"); + description.addProperty("complexData"); + description.addSelfLink(); + description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL); + return description; + } else if (rep instanceof FullRepresentation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("uuid"); + description.addProperty("dateTime"); + description.addProperty("comment"); + description.addProperty("auditInfo"); + description.addProperty("complexData"); + description.addSelfLink(); + return description; + } else if (rep instanceof RefRepresentation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("uuid"); + description.addProperty("comment"); + description.addSelfLink(); + return description; + } + return null; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getCreatableProperties() + */ + @Override + public DelegatingResourceDescription getCreatableProperties() { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addRequiredProperty("uuid"); + description.addRequiredProperty("display"); + description.addProperty("comment"); + description.addProperty("dateTime"); + description.addProperty("complexData"); + + return description; + } + + /** + * @throws org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getUpdatableProperties() + */ + @Override + public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoesNotSupportOperationException { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("uuid"); + description.addProperty("display"); + description.addProperty("comment"); + description.addProperty("dateTime"); + + description.addRequiredProperty("complexData"); + + return description; + } + + @Override + public Model getGETModel(Representation rep) { + ModelImpl model = (ModelImpl) super.getGETModel(rep); + if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) { + model.property("uuid", new StringProperty()).property("display", new StringProperty()) + + .property("comment", new BooleanProperty()).property("Datetime", new DateProperty()) + .property("complexData", new StringProperty()) + + .addProperty("voided", new BooleanProperty()); + } + if (rep instanceof DefaultRepresentation) { + model.property("complexData", new RefProperty("#/definitions/AttachmentComplexDataGetRef")).property("comment", + new RefProperty("#/definitions/AttachmentCommentGetRef")); + + } else if (rep instanceof FullRepresentation) { + model.property("display", new RefProperty("#/definitions/AttachmentDisplayGet")) + .property("comment", new RefProperty("#/definitions/AttachmentCommentGet")) + .property("datetime", new ArrayProperty(new RefProperty("#/definitions/AttachmentDateTimeGet"))) + .property("complexData", new ArrayProperty(new RefProperty("#/definitions/AttachmentComplexDataGet"))); + } + return model; + } + + @Override + public Model getCREATEModel(Representation representation) { + ModelImpl model = new ModelImpl() + .property("display", new ArrayProperty(new RefProperty("#/definitions/AttachmentDisplayCreate"))) + + .property("dataTime", new DateProperty()) + + .property("comment", new ArrayProperty(new RefProperty("#/definitions/AttachmentCommentCreate"))) + .property("complexData", new ArrayProperty(new RefProperty("#/definitions/AttachmentComplexDataCreate"))); + + model.setRequired(Arrays.asList("comment", "datetime")); + return model; + } + + @Override + public Model getUPDATEModel(Representation representation) { + return new ModelImpl().property("uuid", new BooleanProperty()).property("comment", new StringProperty()) + .property("dateTime", new DateProperty()) + + .property("complexData", new ArrayProperty(new RefProperty("#/definitions/AttachmentComplexDataCreate"))); + + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getPropertiesToExposeAsSubResources() + */ + @Override + public List getPropertiesToExposeAsSubResources() { + return Arrays.asList("comment", "complexData"); + } + + @Override + public Attachment newDelegate() { + return new Attachment(); + } + + // @Override + // public Attachment save(Attachment delegate) { + // Obs obs = Context.getObsService().saveObs(delegate.getObs(), REASON); + // return new Attachment(obs); + // } + @SuppressWarnings("deprecation") + @Override + public Attachment getByUniqueId(String uniqueId) { + Obs obs = Context.getObsService().getObsByUuid(uniqueId); + if (!obs.isComplex()) + throw new GenericRestException(uniqueId + " does not identify a complex obs.", null); + else { + obs = Context.getObsService().getComplexObs(obs.getId(), AttachmentsConstants.ATT_VIEW_CRUD); + return new Attachment(obs); + } + } + + @Override + protected void delete(Attachment delegate, String reason, RequestContext context) throws ResponseException { + String encounterUuid = delegate.getObs().getEncounter() != null ? delegate.getObs().getEncounter().getUuid() : null; + Context.getObsService().voidObs(delegate.getObs(), REASON); + voidEncounterIfEmpty(Context.getEncounterService(), encounterUuid); + } + + @Override + public void purge(Attachment delegate, RequestContext context) throws ResponseException { + String encounterUuid = delegate.getObs().getEncounter() != null ? delegate.getObs().getEncounter().getUuid() : null; + Context.getObsService().purgeObs(delegate.getObs()); + voidEncounterIfEmpty(Context.getEncounterService(), encounterUuid); + } + @Override public Attachment save(Attachment delegate) { FlushMode flushMode = DbSessionUtil.getCurrentFlushMode(); @@ -26,4 +206,5 @@ public Attachment save(Attachment delegate) { } return attachment; } + } diff --git a/pom.xml b/pom.xml index 3d9f3f64..ee3f015f 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,7 @@ 2.2.1 3.3.1 1.4 - 2.17 + 2.21.0 1.12 0.9.2.1 1.1 From afc7922864cdfbdbe3805fb8bfba07f254b4019b Mon Sep 17 00:00:00 2001 From: sherrif10 Date: Thu, 24 Sep 2020 11:58:09 +0300 Subject: [PATCH 2/9] Removing unwanted commits --- .../module/attachments/rest/AttachmentResource2_0.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java b/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java index bb0acf5a..d51f37d6 100644 --- a/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java +++ b/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java @@ -161,11 +161,6 @@ public Attachment newDelegate() { return new Attachment(); } - // @Override - // public Attachment save(Attachment delegate) { - // Obs obs = Context.getObsService().saveObs(delegate.getObs(), REASON); - // return new Attachment(obs); - // } @SuppressWarnings("deprecation") @Override public Attachment getByUniqueId(String uniqueId) { From f05a91870b78a332b38f3f24143b3abce1067329 Mon Sep 17 00:00:00 2001 From: sherrif10 Date: Thu, 24 Sep 2020 16:29:45 +0300 Subject: [PATCH 3/9] Resolving representations in swagger --- .../rest/AttachmentResource2_0.java | 85 ++++++++++++++----- 1 file changed, 62 insertions(+), 23 deletions(-) diff --git a/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java b/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java index d51f37d6..86d4130d 100644 --- a/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java +++ b/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java @@ -26,6 +26,8 @@ import io.swagger.models.properties.ArrayProperty; import io.swagger.models.properties.BooleanProperty; import io.swagger.models.properties.DateProperty; +import io.swagger.models.properties.DateTimeProperty; +import io.swagger.models.properties.IntegerProperty; import io.swagger.models.properties.RefProperty; import io.swagger.models.properties.StringProperty; @@ -106,21 +108,22 @@ public Model getGETModel(Representation rep) { ModelImpl model = (ModelImpl) super.getGETModel(rep); if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) { model.property("uuid", new StringProperty()).property("display", new StringProperty()) - - .property("comment", new BooleanProperty()).property("Datetime", new DateProperty()) - .property("complexData", new StringProperty()) - - .addProperty("voided", new BooleanProperty()); + .property("gender", new StringProperty()._enum("M")._enum("F")).property("age", new IntegerProperty()) + .property("birthdate", new DateTimeProperty()).property("birthdateEstimated", new BooleanProperty()) + .property("dead", new BooleanProperty()).property("deathDate", new DateProperty()) + .property("causeOfDeath", new StringProperty()) + .property("attributes", new ArrayProperty(new RefProperty("#/definitions/AttachmentAttributeGetRef"))) + .property("voided", new BooleanProperty()); } if (rep instanceof DefaultRepresentation) { - model.property("complexData", new RefProperty("#/definitions/AttachmentComplexDataGetRef")).property("comment", - new RefProperty("#/definitions/AttachmentCommentGetRef")); + model.property("preferredName", new RefProperty("#/definitions/AttachementNameGetRef")) + .property("preferredAddress", new RefProperty("#/definitions/AttachementAddressGetRef")); } else if (rep instanceof FullRepresentation) { - model.property("display", new RefProperty("#/definitions/AttachmentDisplayGet")) - .property("comment", new RefProperty("#/definitions/AttachmentCommentGet")) - .property("datetime", new ArrayProperty(new RefProperty("#/definitions/AttachmentDateTimeGet"))) - .property("complexData", new ArrayProperty(new RefProperty("#/definitions/AttachmentComplexDataGet"))); + model.property("preferredName", new RefProperty("#/definitions/AttachmentNameGet")) + .property("preferredAddress", new RefProperty("#/definitions/AttachmentAddressGet")) + .property("names", new ArrayProperty(new RefProperty("#/definitions/AttachmentNameGet"))) + .property("addresses", new ArrayProperty(new RefProperty("#/definitions/AttachmentAddressGet"))); } return model; } @@ -128,32 +131,68 @@ public Model getGETModel(Representation rep) { @Override public Model getCREATEModel(Representation representation) { ModelImpl model = new ModelImpl() - .property("display", new ArrayProperty(new RefProperty("#/definitions/AttachmentDisplayCreate"))) - - .property("dataTime", new DateProperty()) - - .property("comment", new ArrayProperty(new RefProperty("#/definitions/AttachmentCommentCreate"))) - .property("complexData", new ArrayProperty(new RefProperty("#/definitions/AttachmentComplexDataCreate"))); + .property("names", new ArrayProperty(new RefProperty("#/definitions/AttachmentNameCreate"))) + .property("gender", new StringProperty()._enum("M")._enum("F")).property("age", new IntegerProperty()) + .property("birthdate", new DateProperty()) + .property("birthdateEstimated", new BooleanProperty()._default(false)) + .property("dead", new BooleanProperty()._default(false)).property("deathDate", new DateProperty()) + .property("causeOfDeath", new StringProperty()) + .property("addresses", new ArrayProperty(new RefProperty("#/definitions/AttachmentAddressCreate"))) + .property("attributes", new ArrayProperty(new RefProperty("#/definitions/AttachmentAttributeCreate"))); - model.setRequired(Arrays.asList("comment", "datetime")); + model.setRequired(Arrays.asList("names", "gender")); return model; } @Override public Model getUPDATEModel(Representation representation) { - return new ModelImpl().property("uuid", new BooleanProperty()).property("comment", new StringProperty()) - .property("dateTime", new DateProperty()) + return new ModelImpl().property("dead", new BooleanProperty()).property("causeOfDeath", new StringProperty()) + .property("deathDate", new DateProperty()).property("age", new IntegerProperty()) + .property("gender", new StringProperty()._enum("M")._enum("F")).property("birthdate", new DateProperty()) + .property("birthdateEstimated", new BooleanProperty()._default(false)) + .property("preferredName", new StringProperty().example("uuid")) + .property("preferredAddress", new StringProperty().example("uuid")) + .property("attributes", new ArrayProperty(new RefProperty("#/definitions/AttachmentAttributeCreate"))) - .property("complexData", new ArrayProperty(new RefProperty("#/definitions/AttachmentComplexDataCreate"))); - + .required("dead").required("causeOfDeath"); } + // } + + // @Override + // public Model getCREATEModel(Representation representation) { + // ModelImpl model = new ModelImpl() + // .property("display", new ArrayProperty(new + // RefProperty("#/definitions/AttachmentDisplayCreate"))) + // + // .property("dataTime", new DateProperty()) + // + // .property("comment", new ArrayProperty(new + // RefProperty("#/definitions/AttachmentCommentCreate"))) + // .property("complexData", new ArrayProperty(new + // RefProperty("#/definitions/AttachmentComplexDataCreate"))); + // + // model.setRequired(Arrays.asList("comment", "datetime")); + // return model; + // } + + // @Override + // public Model getUPDATEModel(Representation representation) { + // return new ModelImpl().property("uuid", new + // BooleanProperty()).property("comment", new StringProperty()) + // .property("dateTime", new DateProperty()) + // + // .property("complexData", new ArrayProperty(new + // RefProperty("#/definitions/AttachmentComplexDataCreate"))); + // + // } + /** * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getPropertiesToExposeAsSubResources() */ @Override public List getPropertiesToExposeAsSubResources() { - return Arrays.asList("comment", "complexData"); + return Arrays.asList("comment", "complexData", "Attributes"); } @Override From 67b15773070383bb178180aefebc56202f0d3516 Mon Sep 17 00:00:00 2001 From: sherrif10 Date: Thu, 24 Sep 2020 17:12:16 +0300 Subject: [PATCH 4/9] Removing unncessary comments --- .../rest/AttachmentResource2_0.java | 30 ------------------- 1 file changed, 30 deletions(-) diff --git a/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java b/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java index 86d4130d..08319a9d 100644 --- a/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java +++ b/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java @@ -157,36 +157,6 @@ public Model getUPDATEModel(Representation representation) { .required("dead").required("causeOfDeath"); } - // } - - // @Override - // public Model getCREATEModel(Representation representation) { - // ModelImpl model = new ModelImpl() - // .property("display", new ArrayProperty(new - // RefProperty("#/definitions/AttachmentDisplayCreate"))) - // - // .property("dataTime", new DateProperty()) - // - // .property("comment", new ArrayProperty(new - // RefProperty("#/definitions/AttachmentCommentCreate"))) - // .property("complexData", new ArrayProperty(new - // RefProperty("#/definitions/AttachmentComplexDataCreate"))); - // - // model.setRequired(Arrays.asList("comment", "datetime")); - // return model; - // } - - // @Override - // public Model getUPDATEModel(Representation representation) { - // return new ModelImpl().property("uuid", new - // BooleanProperty()).property("comment", new StringProperty()) - // .property("dateTime", new DateProperty()) - // - // .property("complexData", new ArrayProperty(new - // RefProperty("#/definitions/AttachmentComplexDataCreate"))); - // - // } - /** * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getPropertiesToExposeAsSubResources() */ From 937e7c4d380d16c9adb3cb02901f60823c5d0e57 Mon Sep 17 00:00:00 2001 From: sherrif10 Date: Tue, 29 Sep 2020 21:28:30 +0300 Subject: [PATCH 5/9] resolving commit --- .../module/attachments/rest/AttachmentResource2_0.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java b/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java index 08319a9d..6ad34197 100644 --- a/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java +++ b/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java @@ -2,7 +2,6 @@ import java.util.Arrays; import java.util.List; - import org.hibernate.FlushMode; import org.openmrs.Obs; import org.openmrs.api.context.Context; @@ -20,7 +19,6 @@ import org.openmrs.module.webservices.rest.web.response.GenericRestException; import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException; import org.openmrs.module.webservices.rest.web.response.ResponseException; - import io.swagger.models.Model; import io.swagger.models.ModelImpl; import io.swagger.models.properties.ArrayProperty; @@ -94,12 +92,11 @@ public DelegatingResourceDescription getCreatableProperties() { public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoesNotSupportOperationException { DelegatingResourceDescription description = new DelegatingResourceDescription(); description.addProperty("uuid"); + description.addProperty("gender"); description.addProperty("display"); description.addProperty("comment"); description.addProperty("dateTime"); - description.addRequiredProperty("complexData"); - return description; } @@ -153,7 +150,6 @@ public Model getUPDATEModel(Representation representation) { .property("preferredName", new StringProperty().example("uuid")) .property("preferredAddress", new StringProperty().example("uuid")) .property("attributes", new ArrayProperty(new RefProperty("#/definitions/AttachmentAttributeCreate"))) - .required("dead").required("causeOfDeath"); } @@ -174,9 +170,9 @@ public Attachment newDelegate() { @Override public Attachment getByUniqueId(String uniqueId) { Obs obs = Context.getObsService().getObsByUuid(uniqueId); - if (!obs.isComplex()) + if (!obs.isComplex()) { throw new GenericRestException(uniqueId + " does not identify a complex obs.", null); - else { + } else { obs = Context.getObsService().getComplexObs(obs.getId(), AttachmentsConstants.ATT_VIEW_CRUD); return new Attachment(obs); } From 7af99d6cb8df47ea9b04c2eb4daa2ef0a424282a Mon Sep 17 00:00:00 2001 From: sherrif10 Date: Thu, 1 Oct 2020 00:53:40 +0300 Subject: [PATCH 6/9] removing unwanted commits --- .../rest/AttachmentResource2_0.java | 31 ------------------- 1 file changed, 31 deletions(-) diff --git a/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java b/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java index 6ad34197..781730f6 100644 --- a/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java +++ b/omod-2.0/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource2_0.java @@ -161,37 +161,6 @@ public List getPropertiesToExposeAsSubResources() { return Arrays.asList("comment", "complexData", "Attributes"); } - @Override - public Attachment newDelegate() { - return new Attachment(); - } - - @SuppressWarnings("deprecation") - @Override - public Attachment getByUniqueId(String uniqueId) { - Obs obs = Context.getObsService().getObsByUuid(uniqueId); - if (!obs.isComplex()) { - throw new GenericRestException(uniqueId + " does not identify a complex obs.", null); - } else { - obs = Context.getObsService().getComplexObs(obs.getId(), AttachmentsConstants.ATT_VIEW_CRUD); - return new Attachment(obs); - } - } - - @Override - protected void delete(Attachment delegate, String reason, RequestContext context) throws ResponseException { - String encounterUuid = delegate.getObs().getEncounter() != null ? delegate.getObs().getEncounter().getUuid() : null; - Context.getObsService().voidObs(delegate.getObs(), REASON); - voidEncounterIfEmpty(Context.getEncounterService(), encounterUuid); - } - - @Override - public void purge(Attachment delegate, RequestContext context) throws ResponseException { - String encounterUuid = delegate.getObs().getEncounter() != null ? delegate.getObs().getEncounter().getUuid() : null; - Context.getObsService().purgeObs(delegate.getObs()); - voidEncounterIfEmpty(Context.getEncounterService(), encounterUuid); - } - @Override public Attachment save(Attachment delegate) { FlushMode flushMode = DbSessionUtil.getCurrentFlushMode(); From 9cc8876b4b9feaf8e93e5a33fcea909f42efd35b Mon Sep 17 00:00:00 2001 From: sherrif10 Date: Tue, 6 Oct 2020 16:59:04 +0300 Subject: [PATCH 7/9] adding some tests --- .../rest/AttachmentResource2_0Test.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentResource2_0Test.java diff --git a/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentResource2_0Test.java b/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentResource2_0Test.java new file mode 100644 index 00000000..de857d87 --- /dev/null +++ b/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentResource2_0Test.java @@ -0,0 +1,72 @@ +package org.openmrs.module.attachments.rest; + +import org.junit.Before; +import org.mockito.Mock; +import org.openmrs.api.ObsService; +import org.openmrs.api.context.Context; +import org.openmrs.module.attachments.AttachmentsService; +import org.openmrs.module.attachments.obs.Attachment; +import org.openmrs.module.webservices.rest.web.RequestContext; +import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResourceTest; +import org.springframework.beans.factory.annotation.Autowired; + +public class AttachmentResource2_0Test extends BaseDelegatingResourceTest { + + private static final String ATTACHMENTRESOURCE_UUID = "9b6639b2-5785-4603-a364-075c2d61cd51"; + + @Mock + AttachmentsService attachmentService; + + Attachment attachment; + + @Autowired + private ObsService obsService; + + @Mock + RequestContext requestContext; + + @Before + public void before() throws Exception { + executeDataSet("org/openmrs/api/include/ObsServiceTest-complex.xml"); + } + + @Override + public String getDisplayProperty() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getUuidProperty() { + return ATTACHMENTRESOURCE_UUID; + } + + @Override + public Attachment newObject() { + return new Attachment(Context.getObsService().getObsByUuid("9b6639b2-5785-4603-a364-075c2d61cd51")); + + } + + @Override + public void validateDefaultRepresentation() throws Exception { + super.validateDefaultRepresentation(); + assertPropEquals("uuid", getObject().getUuid()); + assertPropEquals("dateTime", getObject().getDateTime()); + assertPropEquals("comment", getObject().getComment()); + assertPropEquals("complexData", getObject().getComplexData()); + + } + + @Override + public void validateFullRepresentation() throws Exception { + super.validateDefaultRepresentation(); + + assertPropEquals("uuid", getObject().getUuid()); + assertPropEquals("dateTime", getObject().getDateTime()); + assertPropEquals("comment", getObject().getComment()); + assertPropEquals("datechanged", getObject().getDateChanged()); + assertPropEquals("complexData", getObject().getComplexData()); + + } + +} From 59928cbaeb9ad6fe3fb5abfb96274d74832080fb Mon Sep 17 00:00:00 2001 From: sherrif10 Date: Wed, 7 Oct 2020 19:38:51 +0300 Subject: [PATCH 8/9] Adding some unit tests in rest controller --- .../rest/AttachmentResource2_0Test.java | 2 +- .../rest/AttachmentRestController2_0Test.java | 93 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentResource2_0Test.java b/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentResource2_0Test.java index de857d87..d0820f23 100644 --- a/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentResource2_0Test.java +++ b/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentResource2_0Test.java @@ -43,7 +43,7 @@ public String getUuidProperty() { @Override public Attachment newObject() { - return new Attachment(Context.getObsService().getObsByUuid("9b6639b2-5785-4603-a364-075c2d61cd51")); + return new Attachment(Context.getObsService().getObsByUuid(ATTACHMENTRESOURCE_UUID)); } diff --git a/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentRestController2_0Test.java b/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentRestController2_0Test.java index 169555c4..9b0dad8e 100644 --- a/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentRestController2_0Test.java +++ b/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentRestController2_0Test.java @@ -8,7 +8,9 @@ import java.util.stream.Collectors; import org.apache.commons.beanutils.PropertyUtils; +import org.codehaus.jackson.map.ObjectMapper; import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.openmrs.Obs; @@ -20,13 +22,17 @@ import org.openmrs.module.attachments.obs.Attachment; import org.openmrs.module.attachments.obs.TestAttachmentBytesViewHandler; import org.openmrs.module.attachments.obs.TestHelper; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.test.Util; import org.openmrs.module.webservices.rest.web.RequestContext; import org.openmrs.module.webservices.rest.web.resource.impl.BasePageableResult; +import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException; import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; import org.openmrs.obs.ComplexObsHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.web.bind.annotation.RequestMethod; public class AttachmentRestController2_0Test extends MainResourceControllerTest { @@ -94,6 +100,93 @@ public void shouldGetRefByUuid() { public void shouldGetFullByUuid() { } + @Test + public void createAttachment_shouldCreateANewAttachment() throws Exception { + + SimpleObject attachment = new SimpleObject(); + attachment.add("uuid", obs.getUuid()); + attachment.add("comment", obs.getComment()); + attachment.add("complexData", obs.getComplexData()); + + String json = "{\"uuid\":\"" + getUuid() + "\",\"attachment\":\"" + attachment + "\"}"; + + try { + json = new ObjectMapper().writeValueAsString(attachment); + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + MockHttpServletRequest req = request(RequestMethod.POST, getURI()); + req.setContent(json.getBytes()); + + SimpleObject result = null; + try { + result = deserialize(handle(req)); + } + catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Util.log("Created attachment", result); + + // Check existence in database + String uuid = (String) attachment.get("uuid"); + Assert.assertNotNull("uuid", obs.getUuid()); + Assert.assertNotNull("comment", obs.getComment()); + Assert.assertEquals("complexData", obs.getComplexData(), null); + } + + @Test + public void getAttachment_shouldGetADefaultRepresentationOfAttachment() throws Exception { + + MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid()); + SimpleObject result = deserialize(handle(req)); + + Assert.assertNotNull(result); + Util.log("Attachment fetched (default)", result); + Assert.assertEquals(getUuid(), result.get("uuid")); + } + + @Test + public void updateAttachment_shouldChangeAPropertyOnAttachment() throws Exception { + + SimpleObject attributes = new SimpleObject(); + attributes.add("complexData", "update complextData"); + attributes.add("deathDate", "Updated deathDate"); + attributes.add("prefferedName", "Updated prefferedName"); + attributes.add("gender", "updated gender"); + attributes.add("preferredAddress", "updated preferredAddress"); + + String json = "{\"uuid\":\"" + getUuid() + "\",\"attributes\":\"" + attributes + "\"}"; + + try { + json = new ObjectMapper().writeValueAsString(attributes); + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid()); + req.setContent(json.getBytes()); + + SimpleObject result = null; + try { + result = deserialize(handle(req)); + } + catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + Assert.assertNull(result); + String editedAttachment = obs.getUuid(); + Assert.assertNotEquals("Updated complexData ", editedAttachment.equalsIgnoreCase(editedAttachment)); + } + @Test public void postAttachment_shouldUpdateObsComment() throws Exception { // Setup From 4a05f33a45c04e8cd03d8966ac7ed26fa7a7bb22 Mon Sep 17 00:00:00 2001 From: sherrif10 Date: Mon, 26 Oct 2020 19:15:57 +0300 Subject: [PATCH 9/9] resolving deserialised objects with loglevels --- .../rest/AttachmentRestController2_0Test.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentRestController2_0Test.java b/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentRestController2_0Test.java index 9b0dad8e..45586d53 100644 --- a/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentRestController2_0Test.java +++ b/omod-2.0/src/test/java/org/openmrs/module/attachments/rest/AttachmentRestController2_0Test.java @@ -29,6 +29,8 @@ import org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException; import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; import org.openmrs.obs.ComplexObsHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -36,6 +38,8 @@ public class AttachmentRestController2_0Test extends MainResourceControllerTest { + private static final Logger log = LoggerFactory.getLogger(Attachment.class); + @Autowired private AttachmentsService as; @@ -114,8 +118,7 @@ public void createAttachment_shouldCreateANewAttachment() throws Exception { json = new ObjectMapper().writeValueAsString(attachment); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error("attachment created", e); } MockHttpServletRequest req = request(RequestMethod.POST, getURI()); @@ -126,17 +129,17 @@ public void createAttachment_shouldCreateANewAttachment() throws Exception { result = deserialize(handle(req)); } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error("Attachment failed to be created!", e); } Util.log("Created attachment", result); // Check existence in database String uuid = (String) attachment.get("uuid"); - Assert.assertNotNull("uuid", obs.getUuid()); - Assert.assertNotNull("comment", obs.getComment()); - Assert.assertEquals("complexData", obs.getComplexData(), null); + Assert.assertNull(result); + String createdAttachment = obs.getUuid(); + Assert.assertNotEquals("Created complexData ", createdAttachment.equalsIgnoreCase(createdAttachment)); + } @Test @@ -166,8 +169,7 @@ public void updateAttachment_shouldChangeAPropertyOnAttachment() throws Exceptio json = new ObjectMapper().writeValueAsString(attributes); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error("Attachments deserialised!", e); } MockHttpServletRequest req = request(RequestMethod.POST, getURI() + "/" + getUuid()); @@ -178,8 +180,7 @@ public void updateAttachment_shouldChangeAPropertyOnAttachment() throws Exceptio result = deserialize(handle(req)); } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.error("attachment failed to be updated!", e); } Assert.assertNull(result);