From 90bfbe10c812350dd8daf3400595536e5823322f Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Fri, 8 Sep 2023 15:52:01 +0300 Subject: [PATCH 01/15] RESTWS-920 Create API endpoint for OrderAttribute and OrderAttributeType --- omod-2.5/pom.xml | 219 ++++++++++++++++++ .../webservices/rest/InitPathMatcher2_5.java | 39 ++++ .../openmrs2_5/OrderAttributeResource2_5.java | 137 +++++++++++ .../OrderAttributeTypeResource2_5.java | 98 ++++++++ .../MainResourceControllerTest.java | 90 +++++++ .../controller/RestControllerTestUtils.java | 216 +++++++++++++++++ .../jupiter/MainResourceControllerTest.java | 92 ++++++++ .../jupiter/RestControllerTestUtils.java | 219 ++++++++++++++++++ .../v1_0/resource/RestTestConstants2_5.java | 24 ++ .../OrderAttributeResource2_5Test.java | 65 ++++++ .../OrderAttributeTypeResource2_5Test.java | 64 +++++ .../test/resources/customTestDataset2_5.xml | 18 ++ omod/pom.xml | 12 + omod/src/main/resources/config.xml | 4 + pom.xml | 2 + 15 files changed, 1299 insertions(+) create mode 100644 omod-2.5/pom.xml create mode 100644 omod-2.5/src/main/java/org/openmrs/module/webservices/rest/InitPathMatcher2_5.java create mode 100644 omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java create mode 100644 omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java create mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceControllerTest.java create mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/RestControllerTestUtils.java create mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/MainResourceControllerTest.java create mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/RestControllerTestUtils.java create mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/RestTestConstants2_5.java create mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java create mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5Test.java create mode 100644 omod-2.5/src/test/resources/customTestDataset2_5.xml diff --git a/omod-2.5/pom.xml b/omod-2.5/pom.xml new file mode 100644 index 000000000..10fe9e838 --- /dev/null +++ b/omod-2.5/pom.xml @@ -0,0 +1,219 @@ + + + + webservices.rest + org.openmrs.module + 2.41.0-SNAPSHOT + + 4.0.0 + + webservices.rest-omod-2.5 + Rest Web Services 2.5 OMOD + + + 2.5.0 + 1.8 + 1.8 + + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-common + ${project.parent.version} + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-common + ${project.parent.version} + tests + test + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-1.8 + ${project.parent.version} + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-1.8 + ${project.parent.version} + tests + test + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-1.9 + ${project.parent.version} + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-1.9 + ${project.parent.version} + tests + test + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-1.10 + ${project.parent.version} + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-1.10 + ${project.parent.version} + tests + test + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-1.11 + ${project.parent.version} + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-1.11 + ${project.parent.version} + tests + test + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.0 + ${project.parent.version} + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.2 + ${project.parent.version} + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.2 + ${project.parent.version} + tests + test + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.3 + ${project.parent.version} + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.3 + ${project.parent.version} + tests + test + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.4 + ${project.parent.version} + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.4 + ${project.parent.version} + tests + test + + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.0 + ${project.parent.version} + tests + test + + + + org.openmrs.api + openmrs-api + ${openmrs.version.2.5.0} + + + + org.openmrs.api + openmrs-api + test-jar + test + ${openmrs.version.2.5.0} + + + + org.openmrs.web + openmrs-web + ${openmrs.version.2.5.0} + + + javax.servlet + servlet-api + + + + + + org.openmrs.web + openmrs-web + test-jar + test + ${openmrs.version.2.5.0} + + + + org.openmrs.test + openmrs-test + pom + test + ${openmrs.version.2.5.0} + + + + javax.servlet + javax.servlet-api + ${javaxVersion} + test + + + + org.apache.tomcat + jasper + 6.0.18 + provided + + + + + + + + org.jacoco + jacoco-maven-plugin + + + com.mycila + license-maven-plugin + +
${project.parent.basedir}/license-header.txt
+
+
+
+
+
diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/InitPathMatcher2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/InitPathMatcher2_5.java new file mode 100644 index 000000000..c784c3f55 --- /dev/null +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/InitPathMatcher2_5.java @@ -0,0 +1,39 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest; + +import java.util.List; + +import javax.servlet.ServletContext; + +import org.openmrs.annotation.OpenmrsProfile; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.context.ServletContextAware; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; + +/** + * We should not need to apply this fix in versions where TRUNK-5022 is fixed. + */ +@OpenmrsProfile(openmrsPlatformVersion = "2.5.* - 9.*") +public class InitPathMatcher2_5 implements ServletContextAware { + + @Autowired + private List handlerMappings; + + private OpenmrsPathMatcher pathMatcher = new OpenmrsPathMatcher(); + + @Override + public void setServletContext(ServletContext servletContext) { + + for (RequestMappingHandlerMapping handlerMapping : handlerMappings) { + handlerMapping.setPathMatcher(pathMatcher); + } + } +} diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java new file mode 100644 index 000000000..c4497772b --- /dev/null +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java @@ -0,0 +1,137 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; + +import org.openmrs.Order; +import org.openmrs.OrderType; +import org.openmrs.OrderAttribute; +import org.openmrs.OrderAttributeType; +import org.openmrs.api.context.Context; +import org.openmrs.api.OrderContext; +import org.openmrs.module.webservices.rest.web.RequestContext; +import org.openmrs.module.webservices.rest.web.annotation.PropertySetter; +import org.openmrs.module.webservices.rest.web.annotation.Resource; +import org.openmrs.module.webservices.rest.web.annotation.SubResource; +import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; +import org.openmrs.module.webservices.rest.web.response.ResponseException; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_9.BaseAttributeCrudResource1_9; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_2.OrderResource2_2; + +import java.util.List; + +/** + * {@link Resource} for OrderAttributes, supporting standard CRUD operations + */ +@SubResource(parent = OrderResource2_2.class, path = "attribute", supportedClass = OrderAttribute.class, supportedOpenmrsVersions = { + "2.5.* - 9.*"}) +public class OrderAttributeResource2_5 extends BaseAttributeCrudResource1_9 { + + /** + * Sets attributeType on the given OrderAttribute. + * + * @param instance + * @param attr + */ + @PropertySetter("attributeType") + public static void setAttributeType(OrderAttribute instance, OrderAttributeType attr) { + instance.setAttributeType(attr); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubResource#getParent(java.lang.Object) + */ + @Override + public Order getParent(OrderAttribute instance) { + return instance.getOrder(); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#newDelegate() + */ + @Override + public OrderAttribute newDelegate() { + return new OrderAttribute(); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubResource#setParent(java.lang.Object, + * java.lang.Object) + */ + @Override + public void setParent(OrderAttribute instance, Order order) { + instance.setOrder(order); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getByUniqueId(java.lang.String) + */ + @Override + public OrderAttribute getByUniqueId(String uniqueId) { + return Context.getOrderService().getOrderAttributeByUuid(uniqueId); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubResource#doGetAll(java.lang.Object, + * org.openmrs.module.webservices.rest.web.RequestContext) + */ + @Override + public NeedsPaging doGetAll(Order parent, RequestContext context) throws ResponseException { + return new NeedsPaging((List) parent.getActiveAttributes(), context); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceHandler#save(java.lang.Object) + */ + @Override + public OrderAttribute save(OrderAttribute delegate) { + // make sure it has not already been added to the order + boolean needToAdd = true; + for (OrderAttribute pa : delegate.getOrder().getActiveAttributes()) { + if (pa.equals(delegate)) { + needToAdd = false; + break; + } + } + if (needToAdd) { + delegate.getOrder().addAttribute(delegate); + } + OrderContext orderContext = new OrderContext(); + orderContext.setCareSetting(delegate.getOrder().getCareSetting()); + orderContext.setOrderType(delegate.getOrder().getOrderType()); + + Context.getOrderService().saveOrder(delegate.getOrder(), orderContext); + return delegate; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#delete(java.lang.Object, + * java.lang.String, org.openmrs.module.webservices.rest.web.RequestContext) + */ + @Override + protected void delete(OrderAttribute delegate, String reason, RequestContext context) throws ResponseException { + delegate.setVoided(true); + delegate.setVoidReason(reason); + + OrderContext orderContext = new OrderContext(); + orderContext.setCareSetting(delegate.getOrder().getCareSetting()); + orderContext.setOrderType(delegate.getOrder().getOrderType()); + + Context.getOrderService().saveOrder(delegate.getOrder(),orderContext); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#purge(java.lang.Object, + * org.openmrs.module.webservices.rest.web.RequestContext) + */ + @Override + public void purge(OrderAttribute delegate, RequestContext context) throws ResponseException { + throw new UnsupportedOperationException("Cannot purge OrderAttribute"); + } +} diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java new file mode 100644 index 000000000..2207f7716 --- /dev/null +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java @@ -0,0 +1,98 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; + +import org.openmrs.OrderAttributeType; +import org.openmrs.api.OrderService; +import org.openmrs.api.context.Context; +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.resource.impl.NeedsPaging; +import org.openmrs.module.webservices.rest.web.response.ResponseException; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_9.BaseAttributeTypeCrudResource1_9; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_2.OrderResource2_2; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +/** + * Allows standard CRUD for the {@link OrderAttributeType} domain object + */ +@Resource(name = RestConstants.VERSION_1 + "/orderattributetype", supportedClass = OrderAttributeType.class, supportedOpenmrsVersions = { + "2.5.* - 9.*" }) +public class OrderAttributeTypeResource2_5 extends BaseAttributeTypeCrudResource1_9 { + + public OrderAttributeTypeResource2_5() { + } + + private OrderService service() { + return Context.getOrderService(); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getByUniqueId(java.lang.String) + */ + @Override + public OrderAttributeType getByUniqueId(String uniqueId) { + return service().getOrderAttributeTypeByUuid(uniqueId); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doGetAll(org.openmrs.module.webservices.rest.web.RequestContext) + */ + @Override + protected NeedsPaging doGetAll(RequestContext context) throws ResponseException { + return new NeedsPaging(service().getAllOrderAttributeTypes(), context); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#newDelegate() + */ + @Override + public OrderAttributeType newDelegate() { + return new OrderAttributeType(); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceHandler#save(java.lang.Object) + */ + @Override + public OrderAttributeType save(OrderAttributeType delegate) { + return service().saveOrderAttributeType(delegate); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#purge(java.lang.Object, + * org.openmrs.module.webservices.rest.web.RequestContext) + */ + @Override + public void purge(OrderAttributeType delegate, RequestContext context) throws ResponseException { + service().purgeOrderAttributeType(delegate); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doSearch(org.openmrs.module.webservices.rest.web.RequestContext) + */ + @Override + protected NeedsPaging doSearch(RequestContext context) { + // TODO: Should be a OrderAttributeType search method in OrderService + List allAttrs = service().getAllOrderAttributeTypes(); + List queryResult = new ArrayList(); + for (OrderAttributeType locAttr : allAttrs) { + if (Pattern.compile(Pattern.quote(context.getParameter("q")), Pattern.CASE_INSENSITIVE) + .matcher(locAttr.getName()).find()) { + queryResult.add(locAttr); + } + } + return new NeedsPaging(queryResult, context); + } +} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceControllerTest.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceControllerTest.java new file mode 100644 index 000000000..a344cabad --- /dev/null +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceControllerTest.java @@ -0,0 +1,90 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.controller; + +import org.apache.commons.beanutils.PropertyUtils; +import org.junit.Assert; +import org.junit.Test; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.test.Util; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * Facilitates testing controllers. + */ +public abstract class MainResourceControllerTest extends RestControllerTestUtils { + + public static class Parameter { + + public String name; + + public String value; + + public Parameter(String name, String value) { + this.name = name; + this.value = value; + } + } + + @Test + public void shouldGetDefaultByUuid() throws Exception { + MockHttpServletResponse response = handle(request(RequestMethod.GET, getURI() + "/" + getUuid())); + SimpleObject result = deserialize(response); + + Assert.assertNotNull(result); + Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); + } + + @Test + public void shouldGetRefByUuid() throws Exception { + MockHttpServletRequest request = request(RequestMethod.GET, getURI() + "/" + getUuid()); + request.addParameter("v", "ref"); + SimpleObject result = deserialize(handle(request)); + + Assert.assertNotNull(result); + Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); + } + + @Test + public void shouldGetFullByUuid() throws Exception { + MockHttpServletRequest request = request(RequestMethod.GET, getURI() + "/" + getUuid()); + request.addParameter("v", "full"); + SimpleObject result = deserialize(handle(request)); + + Assert.assertNotNull(result); + Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); + } + + @Test + public void shouldGetAll() throws Exception { + SimpleObject result = deserialize(handle(request(RequestMethod.GET, getURI()))); + + Assert.assertNotNull(result); + Assert.assertEquals(getAllCount(), Util.getResultsSize(result)); + } + + /** + * @return the URI of the resource + */ + public abstract String getURI(); + + /** + * @return the uuid of an existing object + */ + public abstract String getUuid(); + + /** + * @return the count of all not retired/voided objects + */ + public abstract long getAllCount(); + +} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/RestControllerTestUtils.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/RestControllerTestUtils.java new file mode 100644 index 000000000..2cfe3d6ad --- /dev/null +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/RestControllerTestUtils.java @@ -0,0 +1,216 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.controller; + +import java.io.StringReader; +import java.io.StringWriter; +import java.util.List; +import javax.servlet.http.HttpServletRequest; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import org.codehaus.jackson.map.ObjectMapper; +import org.junit.Assert; +import org.openmrs.module.webservices.rest.OpenmrsPathMatcher; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.mock.web.MockMultipartHttpServletRequest; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.HandlerExecutionChain; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; +import org.xml.sax.InputSource; + +public class RestControllerTestUtils extends BaseModuleWebContextSensitiveTest { + + @Autowired + private RequestMappingHandlerAdapter handlerAdapter; + + @Autowired + private List handlerMappings; + + private OpenmrsPathMatcher pathMatcher = new OpenmrsPathMatcher(); + + /** + * Creates a request from the given parameters. + *

+ * The requestURI is automatically preceded with "/rest/" + RestConstants.VERSION_1. + * + * @param method + * @param requestURI + * @return + */ + public MockHttpServletRequest request(RequestMethod method, String requestURI) { + MockHttpServletRequest request = new MockHttpServletRequest(method.toString(), "/rest/" + getNamespace() + "/" + + requestURI); + request.addHeader("content-type", "application/json"); + return request; + } + + /** + * Override this method to test a different namespace than v1. + * + * @return the namespace + */ + public String getNamespace() { + return RestConstants.VERSION_1; + } + + /** + * Passes the given request to a proper controller. + * + * @param request + * @return + * @throws Exception + */ + public MockHttpServletResponse handle(HttpServletRequest request) throws Exception { + MockHttpServletResponse response = new MockHttpServletResponse(); + + HandlerExecutionChain handlerExecutionChain = null; + for (RequestMappingHandlerMapping handlerMapping : handlerMappings) { + handlerMapping.setPathMatcher(pathMatcher); + handlerExecutionChain = handlerMapping.getHandler(request); + if (handlerExecutionChain != null) { + break; + } + } + Assert.assertNotNull("The request URI does not exist", handlerExecutionChain); + + handlerAdapter.handle(request, response, handlerExecutionChain.getHandler()); + + return response; + } + + public MockMultipartHttpServletRequest newUploadRequest(String requestURI) { + MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); + request.addHeader("Content-Type", "multipart/form-data"); + request.setRequestURI(getBaseRestURI() + requestURI); + return request; + } + + public MockHttpServletRequest newRequest(RequestMethod method, String requestURI, + MainResourceControllerTest.Parameter... parameters) { + MockHttpServletRequest request = request(method, requestURI); + for (MainResourceControllerTest.Parameter parameter : parameters) { + request.addParameter(parameter.name, parameter.value); + } + return request; + } + + public MockHttpServletRequest newDeleteRequest(String requestURI, MainResourceControllerTest.Parameter... parameters) { + return newRequest(RequestMethod.DELETE, requestURI, parameters); + } + + public MockHttpServletRequest newGetRequest(String requestURI, MainResourceControllerTest.Parameter... parameters) { + return newRequest(RequestMethod.GET, requestURI, parameters); + } + + public MockHttpServletRequest newPostRequest(String requestURI, Object content) { + MockHttpServletRequest request = request(RequestMethod.POST, requestURI); + try { + String json = new ObjectMapper().writeValueAsString(content); + request.setContent(json.getBytes("UTF-8")); + } + catch (Exception e) { + throw new RuntimeException(e); + } + return request; + } + + public MockHttpServletRequest newPostRequest(String requestURI, String content) { + MockHttpServletRequest request = request(RequestMethod.POST, requestURI); + try { + request.setContent(content.getBytes("UTF-8")); + } + catch (Exception e) { + throw new RuntimeException(e); + } + return request; + } + + public MockHttpServletRequest newPutRequest(String requestURI, Object content) { + try { + String json = new ObjectMapper().writeValueAsString(content); + return newPutRequest(requestURI, json); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + + public MockHttpServletRequest newPutRequest(String requestURI, String content) { + MockHttpServletRequest request = request(RequestMethod.PUT, requestURI); + try { + request.setContent(content.getBytes("UTF-8")); + } + catch (Exception e) { + throw new RuntimeException(e); + } + return request; + } + + /** + * Deserializes the JSON response. + * + * @param response + * @return + * @throws Exception + */ + public SimpleObject deserialize(MockHttpServletResponse response) throws Exception { + return new ObjectMapper().readValue(response.getContentAsString(), SimpleObject.class); + } + + /** + * Evaluates an XPath expression on a XML string + * + * @param xml + * @param xPath + * @return + * @throws XPathExpressionException + */ + protected String evaluateXPath(String xml, String xPath) throws XPathExpressionException { + InputSource source = new InputSource(new StringReader(xml)); + XPath xpath = XPathFactory.newInstance().newXPath(); + return xpath.evaluate(xPath, source); + } + + /** + * Prints an XML string indented + * + * @param xml + * @throws TransformerException + */ + protected void printXML(String xml) throws TransformerException { + + Source xmlInput = new StreamSource(new StringReader(xml)); + StringWriter stringWriter = new StringWriter(); + + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.transform(xmlInput, new StreamResult(stringWriter)); + + System.out.println(stringWriter.toString()); + } + + public String getBaseRestURI() { + return "/rest/" + getNamespace() + "/"; + } +} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/MainResourceControllerTest.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/MainResourceControllerTest.java new file mode 100644 index 000000000..fb41291dd --- /dev/null +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/MainResourceControllerTest.java @@ -0,0 +1,92 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.controller.jupiter; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.apache.commons.beanutils.PropertyUtils; +import org.junit.jupiter.api.Test; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.test.Util; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * Facilitates testing controllers. + */ +public abstract class MainResourceControllerTest extends RestControllerTestUtils { + + public static class Parameter { + + public String name; + + public String value; + + public Parameter(String name, String value) { + this.name = name; + this.value = value; + } + } + + @Test + public void shouldGetDefaultByUuid() throws Exception { + MockHttpServletResponse response = handle(request(RequestMethod.GET, getURI() + "/" + getUuid())); + SimpleObject result = deserialize(response); + + assertNotNull(result); + assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); + } + + @Test + public void shouldGetRefByUuid() throws Exception { + MockHttpServletRequest request = request(RequestMethod.GET, getURI() + "/" + getUuid()); + request.addParameter("v", "ref"); + SimpleObject result = deserialize(handle(request)); + + assertNotNull(result); + assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); + } + + @Test + public void shouldGetFullByUuid() throws Exception { + MockHttpServletRequest request = request(RequestMethod.GET, getURI() + "/" + getUuid()); + request.addParameter("v", "full"); + SimpleObject result = deserialize(handle(request)); + + assertNotNull(result); + assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); + } + + @Test + public void shouldGetAll() throws Exception { + SimpleObject result = deserialize(handle(request(RequestMethod.GET, getURI()))); + + assertNotNull(result); + assertEquals(getAllCount(), Util.getResultsSize(result)); + } + + /** + * @return the URI of the resource + */ + public abstract String getURI(); + + /** + * @return the uuid of an existing object + */ + public abstract String getUuid(); + + /** + * @return the count of all not retired/voided objects + */ + public abstract long getAllCount(); + +} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/RestControllerTestUtils.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/RestControllerTestUtils.java new file mode 100644 index 000000000..530f76dc4 --- /dev/null +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/RestControllerTestUtils.java @@ -0,0 +1,219 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.controller.jupiter; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import javax.servlet.http.HttpServletRequest; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.List; + +import org.codehaus.jackson.map.ObjectMapper; +import org.openmrs.module.webservices.rest.OpenmrsPathMatcher; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; +import org.openmrs.web.test.jupiter.BaseModuleWebContextSensitiveTest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.mock.web.MockMultipartHttpServletRequest; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.HandlerExecutionChain; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; +import org.xml.sax.InputSource; + +public class RestControllerTestUtils extends BaseModuleWebContextSensitiveTest { + + @Autowired + private RequestMappingHandlerAdapter handlerAdapter; + + @Autowired + private List handlerMappings; + + private OpenmrsPathMatcher pathMatcher = new OpenmrsPathMatcher(); + + /** + * Creates a request from the given parameters. + *

+ * The requestURI is automatically preceded with "/rest/" + RestConstants.VERSION_1. + * + * @param method + * @param requestURI + * @return + */ + public MockHttpServletRequest request(RequestMethod method, String requestURI) { + MockHttpServletRequest request = new MockHttpServletRequest(method.toString(), "/rest/" + getNamespace() + "/" + + requestURI); + request.addHeader("content-type", "application/json"); + return request; + } + + /** + * Override this method to test a different namespace than v1. + * + * @return the namespace + */ + public String getNamespace() { + return RestConstants.VERSION_1; + } + + /** + * Passes the given request to a proper controller. + * + * @param request + * @return + * @throws Exception + */ + public MockHttpServletResponse handle(HttpServletRequest request) throws Exception { + MockHttpServletResponse response = new MockHttpServletResponse(); + + HandlerExecutionChain handlerExecutionChain = null; + for (RequestMappingHandlerMapping handlerMapping : handlerMappings) { + handlerMapping.setPathMatcher(pathMatcher); + handlerExecutionChain = handlerMapping.getHandler(request); + if (handlerExecutionChain != null) { + break; + } + } + assertNotNull(handlerExecutionChain, "The request URI does not exist"); + + handlerAdapter.handle(request, response, handlerExecutionChain.getHandler()); + + return response; + } + + public MockMultipartHttpServletRequest newUploadRequest(String requestURI) { + MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); + request.addHeader("Content-Type", "multipart/form-data"); + request.setRequestURI(getBaseRestURI() + requestURI); + return request; + } + + public MockHttpServletRequest newRequest(RequestMethod method, String requestURI, + MainResourceControllerTest.Parameter... parameters) { + MockHttpServletRequest request = request(method, requestURI); + for (MainResourceControllerTest.Parameter parameter : parameters) { + request.addParameter(parameter.name, parameter.value); + } + return request; + } + + public MockHttpServletRequest newDeleteRequest(String requestURI, MainResourceControllerTest.Parameter... parameters) { + return newRequest(RequestMethod.DELETE, requestURI, parameters); + } + + public MockHttpServletRequest newGetRequest(String requestURI, MainResourceControllerTest.Parameter... parameters) { + return newRequest(RequestMethod.GET, requestURI, parameters); + } + + public MockHttpServletRequest newPostRequest(String requestURI, Object content) { + MockHttpServletRequest request = request(RequestMethod.POST, requestURI); + try { + String json = new ObjectMapper().writeValueAsString(content); + request.setContent(json.getBytes("UTF-8")); + } + catch (Exception e) { + throw new RuntimeException(e); + } + return request; + } + + public MockHttpServletRequest newPostRequest(String requestURI, String content) { + MockHttpServletRequest request = request(RequestMethod.POST, requestURI); + try { + request.setContent(content.getBytes("UTF-8")); + } + catch (Exception e) { + throw new RuntimeException(e); + } + return request; + } + + public MockHttpServletRequest newPutRequest(String requestURI, Object content) { + try { + String json = new ObjectMapper().writeValueAsString(content); + return newPutRequest(requestURI, json); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + + public MockHttpServletRequest newPutRequest(String requestURI, String content) { + MockHttpServletRequest request = request(RequestMethod.PUT, requestURI); + try { + request.setContent(content.getBytes("UTF-8")); + } + catch (Exception e) { + throw new RuntimeException(e); + } + return request; + } + + /** + * Deserializes the JSON response. + * + * @param response + * @return + * @throws Exception + */ + public SimpleObject deserialize(MockHttpServletResponse response) throws Exception { + return new ObjectMapper().readValue(response.getContentAsString(), SimpleObject.class); + } + + /** + * Evaluates an XPath expression on a XML string + * + * @param xml + * @param xPath + * @return + * @throws XPathExpressionException + */ + protected String evaluateXPath(String xml, String xPath) throws XPathExpressionException { + InputSource source = new InputSource(new StringReader(xml)); + XPath xpath = XPathFactory.newInstance().newXPath(); + return xpath.evaluate(xPath, source); + } + + /** + * Prints an XML string indented + * + * @param xml + * @throws TransformerException + */ + protected void printXML(String xml) throws TransformerException { + + Source xmlInput = new StreamSource(new StringReader(xml)); + StringWriter stringWriter = new StringWriter(); + + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.transform(xmlInput, new StreamResult(stringWriter)); + + System.out.println(stringWriter.toString()); + } + + public String getBaseRestURI() { + return "/rest/" + getNamespace() + "/"; + } +} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/RestTestConstants2_5.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/RestTestConstants2_5.java new file mode 100644 index 000000000..24398f9ed --- /dev/null +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/RestTestConstants2_5.java @@ -0,0 +1,24 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.resource; + +public class RestTestConstants2_5 { + + public final static String TEST_DATASET = "customTestDataset2_5.xml"; + + public final static String LOCATION_UUID = "f08ba64b-ea57-4a41-nfdr-9dfc59b0c60a"; + + public final static String ORDER_ATTRIBUTE_UUID = "3a2bdb18-6faa-11e0-8414-001e378eb67e"; + + public final static String ORDER_ATTRIBUTE_TYPE_UUID = "9516cc50-6f9f-11e0-8414-001e378eb67e"; + + public final static String LOCATION_ATTRIBUTE_TYPE2_UUID = "9516cc50-6f9f-132r-5433-001e378eb67f"; + +} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java new file mode 100644 index 000000000..b0d59605c --- /dev/null +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java @@ -0,0 +1,65 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; + +import java.text.ParseException; +import java.text.SimpleDateFormat; + +import org.junit.Before; +import org.openmrs.OrderAttribute; +import org.openmrs.api.context.Context; +import org.openmrs.module.webservices.rest.web.v1_0.resource.RestTestConstants2_5; +import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResourceTest; + +public class OrderAttributeResource2_5Test extends BaseDelegatingResourceTest { + + @Before + public void before() throws Exception { + executeDataSet(RestTestConstants2_5.TEST_DATASET); + } + + @Override + public OrderAttribute newObject() { + return Context.getOrderService().getOrderAttributeByUuid(getUuidProperty()); + } + + @Override + public void validateDefaultRepresentation() throws Exception { + super.validateDefaultRepresentation(); + assertPropEquals("value", getObject().getValue()); + assertPropPresent("attributeType"); + assertPropEquals("voided", getObject().getVoided()); + } + + @Override + public void validateFullRepresentation() throws Exception { + super.validateFullRepresentation(); + assertPropEquals("value", getObject().getValue()); + assertPropPresent("attributeType"); + assertPropEquals("voided", getObject().getVoided()); + assertPropPresent("auditInfo"); + } + + @Override + public String getDisplayProperty() { + try { + return "Dispensing Location: " + new SimpleDateFormat("yyyy-MM-dd").parse("2011-04-25"); + } + catch (ParseException ex) { + ex.printStackTrace(); + return null; + } + } + + @Override + public String getUuidProperty() { + return RestTestConstants2_5.ORDER_ATTRIBUTE_UUID; + } +} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5Test.java new file mode 100644 index 000000000..de0c4d3b0 --- /dev/null +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5Test.java @@ -0,0 +1,64 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; + +import org.junit.Before; +import org.openmrs.OrderAttributeType; +import org.openmrs.api.context.Context; +import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResourceTest; +import org.openmrs.module.webservices.rest.web.v1_0.resource.RestTestConstants2_5; + +public class OrderAttributeTypeResource2_5Test extends BaseDelegatingResourceTest { + + @Before + public void before() throws Exception { + executeDataSet(RestTestConstants2_5.TEST_DATASET); + } + + @Override + public OrderAttributeType newObject() { + return Context.getOrderService().getOrderAttributeTypeByUuid(getUuidProperty()); + } + + @Override + public void validateDefaultRepresentation() throws Exception { + super.validateDefaultRepresentation(); + assertPropEquals("name", getObject().getName()); + assertPropEquals("description", getObject().getDescription()); + assertPropEquals("datatypeClassname", getObject().getDatatypeClassname()); + assertPropEquals("preferredHandlerClassname", getObject().getPreferredHandlerClassname()); + assertPropEquals("retired", getObject().getRetired()); + } + + @Override + public void validateFullRepresentation() throws Exception { + super.validateFullRepresentation(); + assertPropEquals("name", getObject().getName()); + assertPropEquals("description", getObject().getDescription()); + assertPropEquals("minOccurs", getObject().getMinOccurs()); + assertPropEquals("maxOccurs", getObject().getMaxOccurs()); + assertPropEquals("datatypeClassname", getObject().getDatatypeClassname()); + assertPropEquals("datatypeConfig", getObject().getDatatypeConfig()); + assertPropEquals("preferredHandlerClassname", getObject().getPreferredHandlerClassname()); + assertPropEquals("handlerConfig", getObject().getHandlerConfig()); + assertPropEquals("retired", getObject().getRetired()); + assertPropPresent("auditInfo"); + } + + @Override + public String getDisplayProperty() { + return "Dispensing Location"; + } + + @Override + public String getUuidProperty() { + return RestTestConstants2_5.ORDER_ATTRIBUTE_TYPE_UUID; + } +} diff --git a/omod-2.5/src/test/resources/customTestDataset2_5.xml b/omod-2.5/src/test/resources/customTestDataset2_5.xml new file mode 100644 index 000000000..413bd7041 --- /dev/null +++ b/omod-2.5/src/test/resources/customTestDataset2_5.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/omod/pom.xml b/omod/pom.xml index 1cc36704d..f722abd90 100644 --- a/omod/pom.xml +++ b/omod/pom.xml @@ -143,6 +143,18 @@ tests test + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.5 + ${project.parent.version} + + + ${project.parent.groupId} + ${project.parent.artifactId}-omod-2.5 + ${project.parent.version} + tests + test + diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index 00e4ef2ef..d7876bec3 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -50,6 +50,10 @@ /lib/webservices.rest-omod-2.4.* 2.4.* - 9.* + + /lib/webservices.rest-omod-2.5.* + 2.5.* - 9.* + diff --git a/pom.xml b/pom.xml index ae596b32d..86871689b 100644 --- a/pom.xml +++ b/pom.xml @@ -428,6 +428,7 @@ omod-2.2 omod-2.3 omod-2.4 + omod-2.5 omod integration-tests @@ -501,6 +502,7 @@ omod-2.2 omod-2.3 omod-2.4 + omod-2.5 omod integration-tests From 51e99274b0668e7728114576eba8120f042e5d14 Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Sun, 10 Sep 2023 20:34:45 +0300 Subject: [PATCH 02/15] removing unnecessary tests and files --- .../webservices/rest/InitPathMatcher2_5.java | 39 ---- .../MainResourceControllerTest.java | 90 ------- .../controller/RestControllerTestUtils.java | 216 ----------------- .../jupiter/MainResourceControllerTest.java | 92 -------- .../jupiter/RestControllerTestUtils.java | 219 ------------------ .../v1_0/resource/RestTestConstants2_5.java | 4 +- 6 files changed, 1 insertion(+), 659 deletions(-) delete mode 100644 omod-2.5/src/main/java/org/openmrs/module/webservices/rest/InitPathMatcher2_5.java delete mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceControllerTest.java delete mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/RestControllerTestUtils.java delete mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/MainResourceControllerTest.java delete mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/RestControllerTestUtils.java diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/InitPathMatcher2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/InitPathMatcher2_5.java deleted file mode 100644 index c784c3f55..000000000 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/InitPathMatcher2_5.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under - * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. - * - * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS - * graphic logo is a trademark of OpenMRS Inc. - */ -package org.openmrs.module.webservices.rest; - -import java.util.List; - -import javax.servlet.ServletContext; - -import org.openmrs.annotation.OpenmrsProfile; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.context.ServletContextAware; -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; - -/** - * We should not need to apply this fix in versions where TRUNK-5022 is fixed. - */ -@OpenmrsProfile(openmrsPlatformVersion = "2.5.* - 9.*") -public class InitPathMatcher2_5 implements ServletContextAware { - - @Autowired - private List handlerMappings; - - private OpenmrsPathMatcher pathMatcher = new OpenmrsPathMatcher(); - - @Override - public void setServletContext(ServletContext servletContext) { - - for (RequestMappingHandlerMapping handlerMapping : handlerMappings) { - handlerMapping.setPathMatcher(pathMatcher); - } - } -} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceControllerTest.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceControllerTest.java deleted file mode 100644 index a344cabad..000000000 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceControllerTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under - * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. - * - * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS - * graphic logo is a trademark of OpenMRS Inc. - */ -package org.openmrs.module.webservices.rest.web.v1_0.controller; - -import org.apache.commons.beanutils.PropertyUtils; -import org.junit.Assert; -import org.junit.Test; -import org.openmrs.module.webservices.rest.SimpleObject; -import org.openmrs.module.webservices.rest.test.Util; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.web.bind.annotation.RequestMethod; - -/** - * Facilitates testing controllers. - */ -public abstract class MainResourceControllerTest extends RestControllerTestUtils { - - public static class Parameter { - - public String name; - - public String value; - - public Parameter(String name, String value) { - this.name = name; - this.value = value; - } - } - - @Test - public void shouldGetDefaultByUuid() throws Exception { - MockHttpServletResponse response = handle(request(RequestMethod.GET, getURI() + "/" + getUuid())); - SimpleObject result = deserialize(response); - - Assert.assertNotNull(result); - Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); - } - - @Test - public void shouldGetRefByUuid() throws Exception { - MockHttpServletRequest request = request(RequestMethod.GET, getURI() + "/" + getUuid()); - request.addParameter("v", "ref"); - SimpleObject result = deserialize(handle(request)); - - Assert.assertNotNull(result); - Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); - } - - @Test - public void shouldGetFullByUuid() throws Exception { - MockHttpServletRequest request = request(RequestMethod.GET, getURI() + "/" + getUuid()); - request.addParameter("v", "full"); - SimpleObject result = deserialize(handle(request)); - - Assert.assertNotNull(result); - Assert.assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); - } - - @Test - public void shouldGetAll() throws Exception { - SimpleObject result = deserialize(handle(request(RequestMethod.GET, getURI()))); - - Assert.assertNotNull(result); - Assert.assertEquals(getAllCount(), Util.getResultsSize(result)); - } - - /** - * @return the URI of the resource - */ - public abstract String getURI(); - - /** - * @return the uuid of an existing object - */ - public abstract String getUuid(); - - /** - * @return the count of all not retired/voided objects - */ - public abstract long getAllCount(); - -} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/RestControllerTestUtils.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/RestControllerTestUtils.java deleted file mode 100644 index 2cfe3d6ad..000000000 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/RestControllerTestUtils.java +++ /dev/null @@ -1,216 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under - * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. - * - * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS - * graphic logo is a trademark of OpenMRS Inc. - */ -package org.openmrs.module.webservices.rest.web.v1_0.controller; - -import java.io.StringReader; -import java.io.StringWriter; -import java.util.List; -import javax.servlet.http.HttpServletRequest; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.stream.StreamSource; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; -import org.codehaus.jackson.map.ObjectMapper; -import org.junit.Assert; -import org.openmrs.module.webservices.rest.OpenmrsPathMatcher; -import org.openmrs.module.webservices.rest.SimpleObject; -import org.openmrs.module.webservices.rest.web.RestConstants; -import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.mock.web.MockMultipartHttpServletRequest; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.servlet.HandlerExecutionChain; -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; -import org.xml.sax.InputSource; - -public class RestControllerTestUtils extends BaseModuleWebContextSensitiveTest { - - @Autowired - private RequestMappingHandlerAdapter handlerAdapter; - - @Autowired - private List handlerMappings; - - private OpenmrsPathMatcher pathMatcher = new OpenmrsPathMatcher(); - - /** - * Creates a request from the given parameters. - *

- * The requestURI is automatically preceded with "/rest/" + RestConstants.VERSION_1. - * - * @param method - * @param requestURI - * @return - */ - public MockHttpServletRequest request(RequestMethod method, String requestURI) { - MockHttpServletRequest request = new MockHttpServletRequest(method.toString(), "/rest/" + getNamespace() + "/" - + requestURI); - request.addHeader("content-type", "application/json"); - return request; - } - - /** - * Override this method to test a different namespace than v1. - * - * @return the namespace - */ - public String getNamespace() { - return RestConstants.VERSION_1; - } - - /** - * Passes the given request to a proper controller. - * - * @param request - * @return - * @throws Exception - */ - public MockHttpServletResponse handle(HttpServletRequest request) throws Exception { - MockHttpServletResponse response = new MockHttpServletResponse(); - - HandlerExecutionChain handlerExecutionChain = null; - for (RequestMappingHandlerMapping handlerMapping : handlerMappings) { - handlerMapping.setPathMatcher(pathMatcher); - handlerExecutionChain = handlerMapping.getHandler(request); - if (handlerExecutionChain != null) { - break; - } - } - Assert.assertNotNull("The request URI does not exist", handlerExecutionChain); - - handlerAdapter.handle(request, response, handlerExecutionChain.getHandler()); - - return response; - } - - public MockMultipartHttpServletRequest newUploadRequest(String requestURI) { - MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); - request.addHeader("Content-Type", "multipart/form-data"); - request.setRequestURI(getBaseRestURI() + requestURI); - return request; - } - - public MockHttpServletRequest newRequest(RequestMethod method, String requestURI, - MainResourceControllerTest.Parameter... parameters) { - MockHttpServletRequest request = request(method, requestURI); - for (MainResourceControllerTest.Parameter parameter : parameters) { - request.addParameter(parameter.name, parameter.value); - } - return request; - } - - public MockHttpServletRequest newDeleteRequest(String requestURI, MainResourceControllerTest.Parameter... parameters) { - return newRequest(RequestMethod.DELETE, requestURI, parameters); - } - - public MockHttpServletRequest newGetRequest(String requestURI, MainResourceControllerTest.Parameter... parameters) { - return newRequest(RequestMethod.GET, requestURI, parameters); - } - - public MockHttpServletRequest newPostRequest(String requestURI, Object content) { - MockHttpServletRequest request = request(RequestMethod.POST, requestURI); - try { - String json = new ObjectMapper().writeValueAsString(content); - request.setContent(json.getBytes("UTF-8")); - } - catch (Exception e) { - throw new RuntimeException(e); - } - return request; - } - - public MockHttpServletRequest newPostRequest(String requestURI, String content) { - MockHttpServletRequest request = request(RequestMethod.POST, requestURI); - try { - request.setContent(content.getBytes("UTF-8")); - } - catch (Exception e) { - throw new RuntimeException(e); - } - return request; - } - - public MockHttpServletRequest newPutRequest(String requestURI, Object content) { - try { - String json = new ObjectMapper().writeValueAsString(content); - return newPutRequest(requestURI, json); - } - catch (Exception e) { - throw new RuntimeException(e); - } - } - - public MockHttpServletRequest newPutRequest(String requestURI, String content) { - MockHttpServletRequest request = request(RequestMethod.PUT, requestURI); - try { - request.setContent(content.getBytes("UTF-8")); - } - catch (Exception e) { - throw new RuntimeException(e); - } - return request; - } - - /** - * Deserializes the JSON response. - * - * @param response - * @return - * @throws Exception - */ - public SimpleObject deserialize(MockHttpServletResponse response) throws Exception { - return new ObjectMapper().readValue(response.getContentAsString(), SimpleObject.class); - } - - /** - * Evaluates an XPath expression on a XML string - * - * @param xml - * @param xPath - * @return - * @throws XPathExpressionException - */ - protected String evaluateXPath(String xml, String xPath) throws XPathExpressionException { - InputSource source = new InputSource(new StringReader(xml)); - XPath xpath = XPathFactory.newInstance().newXPath(); - return xpath.evaluate(xPath, source); - } - - /** - * Prints an XML string indented - * - * @param xml - * @throws TransformerException - */ - protected void printXML(String xml) throws TransformerException { - - Source xmlInput = new StreamSource(new StringReader(xml)); - StringWriter stringWriter = new StringWriter(); - - Transformer transformer = TransformerFactory.newInstance().newTransformer(); - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - transformer.transform(xmlInput, new StreamResult(stringWriter)); - - System.out.println(stringWriter.toString()); - } - - public String getBaseRestURI() { - return "/rest/" + getNamespace() + "/"; - } -} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/MainResourceControllerTest.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/MainResourceControllerTest.java deleted file mode 100644 index fb41291dd..000000000 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/MainResourceControllerTest.java +++ /dev/null @@ -1,92 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under - * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. - * - * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS - * graphic logo is a trademark of OpenMRS Inc. - */ -package org.openmrs.module.webservices.rest.web.v1_0.controller.jupiter; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import org.apache.commons.beanutils.PropertyUtils; -import org.junit.jupiter.api.Test; -import org.openmrs.module.webservices.rest.SimpleObject; -import org.openmrs.module.webservices.rest.test.Util; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.web.bind.annotation.RequestMethod; - -/** - * Facilitates testing controllers. - */ -public abstract class MainResourceControllerTest extends RestControllerTestUtils { - - public static class Parameter { - - public String name; - - public String value; - - public Parameter(String name, String value) { - this.name = name; - this.value = value; - } - } - - @Test - public void shouldGetDefaultByUuid() throws Exception { - MockHttpServletResponse response = handle(request(RequestMethod.GET, getURI() + "/" + getUuid())); - SimpleObject result = deserialize(response); - - assertNotNull(result); - assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); - } - - @Test - public void shouldGetRefByUuid() throws Exception { - MockHttpServletRequest request = request(RequestMethod.GET, getURI() + "/" + getUuid()); - request.addParameter("v", "ref"); - SimpleObject result = deserialize(handle(request)); - - assertNotNull(result); - assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); - } - - @Test - public void shouldGetFullByUuid() throws Exception { - MockHttpServletRequest request = request(RequestMethod.GET, getURI() + "/" + getUuid()); - request.addParameter("v", "full"); - SimpleObject result = deserialize(handle(request)); - - assertNotNull(result); - assertEquals(getUuid(), PropertyUtils.getProperty(result, "uuid")); - } - - @Test - public void shouldGetAll() throws Exception { - SimpleObject result = deserialize(handle(request(RequestMethod.GET, getURI()))); - - assertNotNull(result); - assertEquals(getAllCount(), Util.getResultsSize(result)); - } - - /** - * @return the URI of the resource - */ - public abstract String getURI(); - - /** - * @return the uuid of an existing object - */ - public abstract String getUuid(); - - /** - * @return the count of all not retired/voided objects - */ - public abstract long getAllCount(); - -} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/RestControllerTestUtils.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/RestControllerTestUtils.java deleted file mode 100644 index 530f76dc4..000000000 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/jupiter/RestControllerTestUtils.java +++ /dev/null @@ -1,219 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under - * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. - * - * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS - * graphic logo is a trademark of OpenMRS Inc. - */ -package org.openmrs.module.webservices.rest.web.v1_0.controller.jupiter; - -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import javax.servlet.http.HttpServletRequest; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.stream.StreamSource; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; -import java.io.StringReader; -import java.io.StringWriter; -import java.util.List; - -import org.codehaus.jackson.map.ObjectMapper; -import org.openmrs.module.webservices.rest.OpenmrsPathMatcher; -import org.openmrs.module.webservices.rest.SimpleObject; -import org.openmrs.module.webservices.rest.web.RestConstants; -import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; -import org.openmrs.web.test.jupiter.BaseModuleWebContextSensitiveTest; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.mock.web.MockMultipartHttpServletRequest; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.servlet.HandlerExecutionChain; -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; -import org.xml.sax.InputSource; - -public class RestControllerTestUtils extends BaseModuleWebContextSensitiveTest { - - @Autowired - private RequestMappingHandlerAdapter handlerAdapter; - - @Autowired - private List handlerMappings; - - private OpenmrsPathMatcher pathMatcher = new OpenmrsPathMatcher(); - - /** - * Creates a request from the given parameters. - *

- * The requestURI is automatically preceded with "/rest/" + RestConstants.VERSION_1. - * - * @param method - * @param requestURI - * @return - */ - public MockHttpServletRequest request(RequestMethod method, String requestURI) { - MockHttpServletRequest request = new MockHttpServletRequest(method.toString(), "/rest/" + getNamespace() + "/" - + requestURI); - request.addHeader("content-type", "application/json"); - return request; - } - - /** - * Override this method to test a different namespace than v1. - * - * @return the namespace - */ - public String getNamespace() { - return RestConstants.VERSION_1; - } - - /** - * Passes the given request to a proper controller. - * - * @param request - * @return - * @throws Exception - */ - public MockHttpServletResponse handle(HttpServletRequest request) throws Exception { - MockHttpServletResponse response = new MockHttpServletResponse(); - - HandlerExecutionChain handlerExecutionChain = null; - for (RequestMappingHandlerMapping handlerMapping : handlerMappings) { - handlerMapping.setPathMatcher(pathMatcher); - handlerExecutionChain = handlerMapping.getHandler(request); - if (handlerExecutionChain != null) { - break; - } - } - assertNotNull(handlerExecutionChain, "The request URI does not exist"); - - handlerAdapter.handle(request, response, handlerExecutionChain.getHandler()); - - return response; - } - - public MockMultipartHttpServletRequest newUploadRequest(String requestURI) { - MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); - request.addHeader("Content-Type", "multipart/form-data"); - request.setRequestURI(getBaseRestURI() + requestURI); - return request; - } - - public MockHttpServletRequest newRequest(RequestMethod method, String requestURI, - MainResourceControllerTest.Parameter... parameters) { - MockHttpServletRequest request = request(method, requestURI); - for (MainResourceControllerTest.Parameter parameter : parameters) { - request.addParameter(parameter.name, parameter.value); - } - return request; - } - - public MockHttpServletRequest newDeleteRequest(String requestURI, MainResourceControllerTest.Parameter... parameters) { - return newRequest(RequestMethod.DELETE, requestURI, parameters); - } - - public MockHttpServletRequest newGetRequest(String requestURI, MainResourceControllerTest.Parameter... parameters) { - return newRequest(RequestMethod.GET, requestURI, parameters); - } - - public MockHttpServletRequest newPostRequest(String requestURI, Object content) { - MockHttpServletRequest request = request(RequestMethod.POST, requestURI); - try { - String json = new ObjectMapper().writeValueAsString(content); - request.setContent(json.getBytes("UTF-8")); - } - catch (Exception e) { - throw new RuntimeException(e); - } - return request; - } - - public MockHttpServletRequest newPostRequest(String requestURI, String content) { - MockHttpServletRequest request = request(RequestMethod.POST, requestURI); - try { - request.setContent(content.getBytes("UTF-8")); - } - catch (Exception e) { - throw new RuntimeException(e); - } - return request; - } - - public MockHttpServletRequest newPutRequest(String requestURI, Object content) { - try { - String json = new ObjectMapper().writeValueAsString(content); - return newPutRequest(requestURI, json); - } - catch (Exception e) { - throw new RuntimeException(e); - } - } - - public MockHttpServletRequest newPutRequest(String requestURI, String content) { - MockHttpServletRequest request = request(RequestMethod.PUT, requestURI); - try { - request.setContent(content.getBytes("UTF-8")); - } - catch (Exception e) { - throw new RuntimeException(e); - } - return request; - } - - /** - * Deserializes the JSON response. - * - * @param response - * @return - * @throws Exception - */ - public SimpleObject deserialize(MockHttpServletResponse response) throws Exception { - return new ObjectMapper().readValue(response.getContentAsString(), SimpleObject.class); - } - - /** - * Evaluates an XPath expression on a XML string - * - * @param xml - * @param xPath - * @return - * @throws XPathExpressionException - */ - protected String evaluateXPath(String xml, String xPath) throws XPathExpressionException { - InputSource source = new InputSource(new StringReader(xml)); - XPath xpath = XPathFactory.newInstance().newXPath(); - return xpath.evaluate(xPath, source); - } - - /** - * Prints an XML string indented - * - * @param xml - * @throws TransformerException - */ - protected void printXML(String xml) throws TransformerException { - - Source xmlInput = new StreamSource(new StringReader(xml)); - StringWriter stringWriter = new StringWriter(); - - Transformer transformer = TransformerFactory.newInstance().newTransformer(); - transformer.setOutputProperty(OutputKeys.INDENT, "yes"); - transformer.transform(xmlInput, new StreamResult(stringWriter)); - - System.out.println(stringWriter.toString()); - } - - public String getBaseRestURI() { - return "/rest/" + getNamespace() + "/"; - } -} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/RestTestConstants2_5.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/RestTestConstants2_5.java index 24398f9ed..800679442 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/RestTestConstants2_5.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/RestTestConstants2_5.java @@ -13,12 +13,10 @@ public class RestTestConstants2_5 { public final static String TEST_DATASET = "customTestDataset2_5.xml"; - public final static String LOCATION_UUID = "f08ba64b-ea57-4a41-nfdr-9dfc59b0c60a"; + public final static String ORDER_UUID = "204897de-70c6-4d5f-9051-b269fbd0700c"; public final static String ORDER_ATTRIBUTE_UUID = "3a2bdb18-6faa-11e0-8414-001e378eb67e"; public final static String ORDER_ATTRIBUTE_TYPE_UUID = "9516cc50-6f9f-11e0-8414-001e378eb67e"; - public final static String LOCATION_ATTRIBUTE_TYPE2_UUID = "9516cc50-6f9f-132r-5433-001e378eb67f"; - } From fabd6ac1316d9697a967d4c71861e5a419ebc2be Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Sun, 10 Sep 2023 20:49:08 +0300 Subject: [PATCH 03/15] Changing logging approach in OrderAttributeResource2_5Test --- .../resource/openmrs2_5/OrderAttributeResource2_5Test.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java index b0d59605c..3432c9051 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java @@ -17,8 +17,11 @@ import org.openmrs.api.context.Context; import org.openmrs.module.webservices.rest.web.v1_0.resource.RestTestConstants2_5; import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResourceTest; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; public class OrderAttributeResource2_5Test extends BaseDelegatingResourceTest { + protected final Log log = LogFactory.getLog(this.getClass()); @Before public void before() throws Exception { @@ -53,7 +56,7 @@ public String getDisplayProperty() { return "Dispensing Location: " + new SimpleDateFormat("yyyy-MM-dd").parse("2011-04-25"); } catch (ParseException ex) { - ex.printStackTrace(); + log.error(ex); return null; } } From 9f74240d37cbbae177eb7181464aa0a1dfa2c726 Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Mon, 11 Sep 2023 13:57:26 +0300 Subject: [PATCH 04/15] adding OrderAttributeController2_5Test and OrderAttributeTypeController2_5Test --- .../openmrs2_4/ServerLogResource2_4Test.java | 1 + .../OrderAttributeController2_5Test.java | 129 +++++++++++++++++ .../OrderAttributeTypeController2_5Test.java | 134 ++++++++++++++++++ 3 files changed, 264 insertions(+) create mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java create mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeTypeController2_5Test.java diff --git a/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/ServerLogResource2_4Test.java b/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/ServerLogResource2_4Test.java index 3bf2c1ef3..30be17203 100644 --- a/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/ServerLogResource2_4Test.java +++ b/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_4/ServerLogResource2_4Test.java @@ -35,6 +35,7 @@ public class ServerLogResource2_4Test extends BaseModuleWebContextSensitiveTest private final MockServerLogActionWrapper mockServerLogActionWrapper = new MockServerLogActionWrapper<>( new ServerLogActionWrapper2_4()); + @Autowired RestService restService; diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java new file mode 100644 index 000000000..fff10f05f --- /dev/null +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java @@ -0,0 +1,129 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_5; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openmrs.OrderAttribute; +import org.openmrs.api.OrderService; +import org.openmrs.api.context.Context; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.test.Util; +import org.openmrs.module.webservices.rest.web.v1_0.resource.RestTestConstants2_5; +import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.web.bind.annotation.RequestMethod; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.not; + + +/** + * Tests functionality of {@link OrderAttributeController}. + */ +public class OrderAttributeController2_5Test extends MainResourceControllerTest { + + private OrderService service; + + /** + * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getURI() + */ + @Override + public String getURI() { + return "order/" + RestTestConstants2_5.ORDER_UUID + "/attribute"; + } + + /** + * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getUuid() + */ + @Override + public String getUuid() { + return RestTestConstants2_5.ORDER_ATTRIBUTE_UUID; + } + + /** + * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getAllCount() + */ + @Override + public long getAllCount() { + return service.getOrderByUuid(RestTestConstants2_5.ORDER_UUID).getActiveAttributes().size(); + } + + @Before + public void before() throws Exception { + executeDataSet(RestTestConstants2_5.TEST_DATASET); + this.service = Context.getOrderService(); + } + + @Test + public void shouldAddAttributeToOrder() throws Exception { + int before = service.getOrderByUuid(RestTestConstants2_5.ORDER_UUID).getAttributes().size(); + String json = "{\"attributeType\":\"9516cc50-6f9f-132r-5433-001e378eb67f\", \"value\":\"2012-05-05\"}"; + handle(newPostRequest(getURI(), json)); + int after = service.getOrderByUuid(RestTestConstants2_5.ORDER_UUID).getAttributes().size(); + Assert.assertEquals(before + 1, after); + } + + @Test + public void shouldEditOrderAttribute() throws Exception { + String json = "{ \"attributeType\":\"9516cc50-6f9f-132r-5433-001e378eb67f\" }"; + + OrderAttribute orderAttribute = service.getOrderAttributeByUuid(getUuid()); + Assert.assertEquals("Dispensing Location", orderAttribute.getAttributeType().getName()); + + handle(newPostRequest(getURI() + "/" + getUuid(), json)); + + orderAttribute = service.getOrderAttributeByUuid(getUuid()); + Assert.assertEquals("Dispensing Personnel", orderAttribute.getAttributeType().getName()); + } + + @Test + public void shouldVoidAttribute() throws Exception { + OrderAttribute orderAttribute = service.getOrderAttributeByUuid(getUuid()); + Assert.assertFalse(orderAttribute.isVoided()); + + MockHttpServletRequest request = request(RequestMethod.DELETE, getURI() + "/" + getUuid()); + request.addParameter("reason", "unit test"); + handle(request); + + orderAttribute = service.getOrderAttributeByUuid(getUuid()); + Assert.assertTrue(orderAttribute.isVoided()); + Assert.assertEquals("unit test", orderAttribute.getVoidReason()); + } + + @Test + public void shouldReturnOnlyAttributesOfGivenTypeWhenSearch() throws Exception { + String attributeTypeUuid = "9516cc50-6f9f-11e0-8414-001e378eb67e"; + searchForAttributeOfGivenType_AndCheckIfTypeMatches(attributeTypeUuid); + + String anotherAttributeTypUuid = "9516cc50-6f9f-132r-6556-001e378eb67f"; + searchForAttributeOfGivenType_AndCheckIfTypeMatches(anotherAttributeTypUuid); + } + + /** + * sends search request for attributes with given type, checks if all result attributes are of + * this type + * + * @param attributeTypeUuid + * @throws Exception + */ + private void searchForAttributeOfGivenType_AndCheckIfTypeMatches(String attributeTypeUuid) throws Exception { + SimpleObject response2 = deserialize(handle(newGetRequest(getURI(), + new Parameter("attributeType", attributeTypeUuid)))); + assertThat(Util.getResultsList(response2), is(not(empty()))); + for (Object result : Util.getResultsList(response2)) { + Object resultAttributeTypeUuid = Util.getByPath(Util.getByPath(result, "attributeType"), "uuid"); + Assert.assertThat((String) resultAttributeTypeUuid, is(attributeTypeUuid)); + } + } +} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeTypeController2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeTypeController2_5Test.java new file mode 100644 index 000000000..68f1e5967 --- /dev/null +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeTypeController2_5Test.java @@ -0,0 +1,134 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_5; + +import org.apache.commons.beanutils.PropertyUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openmrs.OrderAttributeType; +import org.openmrs.api.context.Context; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.test.Util; +import org.openmrs.module.webservices.rest.web.v1_0.resource.RestTestConstants2_5; +import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.context.request.WebRequest; + +import javax.servlet.http.HttpServletResponse; +import java.util.List; + +public class OrderAttributeTypeController2_5Test extends MainResourceControllerTest { + + /** + * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getURI() + */ + @Override + public String getURI() { + return "orderattributetype"; + } + + /** + * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getUuid() + */ + @Override + public String getUuid() { + return RestTestConstants2_5.ORDER_ATTRIBUTE_TYPE_UUID; + } + + /** + * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getAllCount() + */ + @Override + public long getAllCount() { + return Context.getOrderService().getAllOrderAttributeTypes().size(); + } + + @Before + public void before() throws Exception { + executeDataSet(RestTestConstants2_5.TEST_DATASET); + } + + /** + * @see OrderAttributeTypeController#createOrderAttributeType(SimpleObject,WebRequest) + * @verifies create a new OrderAttributeType + */ + @Test + public void createOrderAttributeType_shouldCreateANewOrderAttributeType() throws Exception { + int before = Context.getOrderService().getAllOrderAttributeTypes().size(); + String json = "{ \"name\":\"Some attributeType\",\"description\":\"Attribute Type for order\",\"datatypeClassname\":\"org.openmrs.customdatatype.datatype.FreeTextDatatype\"}"; + + handle(newPostRequest(getURI(), json)); + + Assert.assertEquals(before + 1, Context.getOrderService().getAllOrderAttributeTypes().size()); + } + + /** + * @see OrderAttributeTypeController#updateOrderAttributeType(OrderAttributeType,SimpleObject,WebRequest) + * @verifies change a property on a order + */ + @Test + public void updateOrderAttributeType_shouldChangeAPropertyOnAOrderAttributeType() throws Exception { + String json = "{\"description\":\"Updated description\"}"; + + handle(newPostRequest(getURI() + "/" + getUuid(), json)); + + Assert.assertEquals("Updated description", Context.getOrderService().getOrderAttributeTypeById(1).getDescription()); + } + + /** + * @see OrderAttributeTypeController#retireOrderAttributeType(OrderAttributeType,String,WebRequest) + * @verifies void a order attribute type + */ + @Test + public void retireOrderAttributeType_shouldRetireAOrderAttributeType() throws Exception { + OrderAttributeType orderAttributeType = Context.getOrderService().getOrderAttributeTypeById(1); + Assert.assertFalse(orderAttributeType.isRetired()); + + MockHttpServletRequest request = request(RequestMethod.DELETE, getURI() + "/" + getUuid()); + request.addParameter("reason", "test"); + handle(request); + + orderAttributeType = Context.getOrderService().getOrderAttributeTypeById(1); + Assert.assertTrue(orderAttributeType.isRetired()); + Assert.assertEquals("test", orderAttributeType.getRetireReason()); + } + + /** + * @see OrderAttributeTypeController#findOrderAttributeTypes(String,WebRequest,HttpServletResponse) + * @verifies return no results if there are no matching order(s) + */ + @Test + public void findOrderAttributeTypes_shouldReturnNoResultsIfThereAreNoMatchingOrders() throws Exception { + SimpleObject result = deserialize(handle(newGetRequest(getURI(), new Parameter("q", "zzzznotype")))); + + Assert.assertEquals(0, Util.getResultsSize(result)); + } + + /** + * @see OrderAttributeTypeController#findOrderAttributeTypes(String,WebRequest,HttpServletResponse) + * @verifies find matching order attribute types + */ + @Test + public void findOrderAttributeTypes_shouldFindMatchingOrderAttributeTypes() throws Exception { + SimpleObject response = deserialize(handle(newGetRequest(getURI(), new Parameter("q", "Audit")))); + + List results = Util.getResultsList(response); + + Assert.assertEquals(1, results.size()); + Util.log("Found " + results.size() + " OrderAttributeType(s)", results); + Object result = results.get(0); + Assert.assertEquals(RestTestConstants2_5.ORDER_ATTRIBUTE_TYPE_UUID, PropertyUtils.getProperty(result, "uuid")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "links")); + Assert.assertNotNull(PropertyUtils.getProperty(result, "display")); + } + +} From 9d87b93a46c7931ffb4bba56371e95cbb27c263a Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Fri, 22 Sep 2023 11:42:39 +0300 Subject: [PATCH 05/15] Changing order to include order attribute --- omod-2.5/pom.xml | 28 --- .../BaseAttributeCrudResource2_5.java | 162 ++++++++++++++++++ .../BaseAttributeTypeCrudResource2_5.java | 127 ++++++++++++++ .../openmrs2_5/OrderAttributeResource2_5.java | 21 ++- .../OrderAttributeTypeResource2_5.java | 13 +- .../resource/openmrs2_5/OrderResource2_5.java | 77 +++++++++ .../OrderAttributeController2_5Test.java | 61 +------ .../OrderAttributeTypeController2_5Test.java | 2 +- .../test/resources/customTestDataset2_5.xml | 13 +- 9 files changed, 401 insertions(+), 103 deletions(-) create mode 100644 omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeCrudResource2_5.java create mode 100644 omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeTypeCrudResource2_5.java create mode 100644 omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5.java diff --git a/omod-2.5/pom.xml b/omod-2.5/pom.xml index 10fe9e838..70193b412 100644 --- a/omod-2.5/pom.xml +++ b/omod-2.5/pom.xml @@ -31,34 +31,6 @@ test - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.8 - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.8 - ${project.parent.version} - tests - test - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.9 - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.9 - ${project.parent.version} - tests - test - - ${project.parent.groupId} ${project.parent.artifactId}-omod-1.10 diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeCrudResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeCrudResource2_5.java new file mode 100644 index 000000000..2afcc0074 --- /dev/null +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeCrudResource2_5.java @@ -0,0 +1,162 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; + +import io.swagger.models.Model; +import io.swagger.models.ModelImpl; +import io.swagger.models.properties.BooleanProperty; +import io.swagger.models.properties.StringProperty; +import org.apache.commons.lang.StringUtils; +import org.openmrs.attribute.Attribute; +import org.openmrs.customdatatype.CustomDatatype; +import org.openmrs.customdatatype.CustomDatatypeUtil; +import org.openmrs.customdatatype.NotYetPersistedException; +import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter; +import org.openmrs.module.webservices.rest.web.annotation.PropertySetter; +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.Representation; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubResource; + +import java.util.Arrays; +import java.util.List; + +/** + * Subclass of {@link DelegatingSubResource} with helper methods specific to {@link Attribute} + * + * @param The type of the attribute this sub resource is associated to + * @param

The parent/owning type for the type T + * @param The Resource for the parent/owning type P + */ +public abstract class BaseAttributeCrudResource2_5, P, PR> extends DelegatingSubResource> { + + /** + * Sets value on the given attribute. + * + * @param instance + * @param value + */ + @PropertySetter("value") + public static void setValue(Attribute instance, String value) throws Exception { + CustomDatatype datatype = CustomDatatypeUtil.getDatatype(instance.getAttributeType().getDatatypeClassname(), + instance.getAttributeType().getDatatypeConfig()); + if (StringUtils.isNotEmpty(value)) // check empty instead of blank, because " " is meaningful + instance.setValue(datatype.fromReferenceString(value)); + } + + /** + * Gets an attribute value, catching any {@link NotYetPersistedException} and returning null in + * that case + * + * @param instance + * @return + */ + @PropertyGetter("value") + public static Object getValue(Attribute instance) { + try { + return instance.getValue(); + } + catch (NotYetPersistedException ex) { + return null; + } + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getRepresentationDescription(Representation) + */ + @Override + public DelegatingResourceDescription getRepresentationDescription(Representation rep) { + if (rep instanceof DefaultRepresentation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("display"); + description.addProperty("uuid"); + description.addProperty("attributeType", Representation.REF); + description.addProperty("value"); + description.addProperty("voided"); + description.addSelfLink(); + description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL); + return description; + } else if (rep instanceof FullRepresentation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("display"); + description.addProperty("uuid"); + description.addProperty("attributeType", Representation.REF); + description.addProperty("value"); + description.addProperty("voided"); + description.addProperty("auditInfo"); + description.addSelfLink(); + return description; + } + return null; + } + + @Override + public DelegatingResourceDescription getCreatableProperties() { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addRequiredProperty("attributeType"); + description.addRequiredProperty("value"); + return description; + } + + @Override + public Model getCREATEModel(Representation rep) { + return new ModelImpl() + .property("attributeType", new StringProperty().example("uuid")) + .property("value", new StringProperty()) + + .required("attributeType").required("value"); + } + + @Override + public Model getGETModel(Representation rep) { + ModelImpl model = (ModelImpl) super.getGETModel(rep); + if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) { + model + .property("display", new StringProperty()) + .property("uuid", new StringProperty()) + .property("attributeType", new StringProperty()) //FIXME type + .property("value", new StringProperty()) //FIXME type + .property("voided", new BooleanProperty()); + } + return model; + } + + /** + * Gets the display string for an attribute. + * + * @param attr the attribute. + * @return attribute type: value (for concise display purposes) + */ + @PropertyGetter("display") + public String getDisplayString(T attr) { + if (attr.getAttributeType() == null) + return ""; + return attr.getAttributeType().getName() + ": " + attr.getValue(); + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getResourceVersion() + */ + @Override + public String getResourceVersion() { + return "2.5"; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getPropertiesToExposeAsSubResources() + */ + @Override + public List getPropertiesToExposeAsSubResources() { + return Arrays.asList("attributes"); + } +} diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeTypeCrudResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeTypeCrudResource2_5.java new file mode 100644 index 000000000..dea4b4ce3 --- /dev/null +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeTypeCrudResource2_5.java @@ -0,0 +1,127 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; + +import io.swagger.models.Model; +import io.swagger.models.ModelImpl; +import io.swagger.models.properties.IntegerProperty; +import io.swagger.models.properties.StringProperty; +import org.openmrs.attribute.AttributeType; +import org.openmrs.module.webservices.rest.web.RestConstants; +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.Representation; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; +import org.openmrs.module.webservices.rest.web.resource.impl.MetadataDelegatingCrudResource; + +/** + * Subclass of {@link MetadataDelegatingCrudResource} with helper methods specific to + * {@link AttributeType} + * + * @param + */ +public abstract class BaseAttributeTypeCrudResource2_5> extends MetadataDelegatingCrudResource { + + @Override + public Model getGETModel(Representation rep) { + ModelImpl model = (ModelImpl) super.getGETModel(rep); + if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) { + model + .property("minOccurs", new IntegerProperty()) + .property("maxOccurs", new IntegerProperty()) + .property("datatypeClassname", new StringProperty()) + .property("preferredHandlerClassname", new StringProperty()); + } + if (rep instanceof FullRepresentation) { + model + .property("datatypeConfig", new StringProperty()) + .property("handlerConfig", new StringProperty()); + } + return model; + } + + @Override + public Model getCREATEModel(Representation rep) { + return ((ModelImpl) super.getCREATEModel(rep)) + .property("datatypeClassname", new StringProperty()) + .property("minOccurs", new IntegerProperty()) + .property("maxOccurs", new IntegerProperty()) + .property("datatypeConfig", new StringProperty()) + .property("preferredHandlerClassname", new StringProperty()) + .property("handlerConfig", new StringProperty()) + + .required("datatypeClassname"); + + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getRepresentationDescription(Representation) + */ + @Override + public DelegatingResourceDescription getRepresentationDescription(Representation rep) { + if (rep instanceof DefaultRepresentation) { + DelegatingResourceDescription description = new DelegatingResourceDescription(); + description.addProperty("uuid"); + description.addProperty("display"); + description.addProperty("name"); + description.addProperty("description"); + description.addProperty("minOccurs"); + description.addProperty("maxOccurs"); + description.addProperty("datatypeClassname"); + description.addProperty("preferredHandlerClassname"); + description.addProperty("retired"); + 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("display"); + description.addProperty("name"); + description.addProperty("description"); + description.addProperty("minOccurs"); + description.addProperty("maxOccurs"); + description.addProperty("datatypeClassname"); + description.addProperty("datatypeConfig"); + description.addProperty("preferredHandlerClassname"); + description.addProperty("handlerConfig"); + description.addProperty("retired"); + description.addProperty("auditInfo"); + 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("name"); + description.addRequiredProperty("datatypeClassname"); + description.addProperty("description"); + description.addProperty("minOccurs"); + description.addProperty("maxOccurs"); + description.addProperty("datatypeConfig"); + description.addProperty("preferredHandlerClassname"); + description.addProperty("handlerConfig"); + return description; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getResourceVersion() + */ + @Override + public String getResourceVersion() { + return "2.5"; + } +} diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java index c4497772b..f4e99ae58 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java @@ -10,7 +10,6 @@ package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; import org.openmrs.Order; -import org.openmrs.OrderType; import org.openmrs.OrderAttribute; import org.openmrs.OrderAttributeType; import org.openmrs.api.context.Context; @@ -21,7 +20,6 @@ import org.openmrs.module.webservices.rest.web.annotation.SubResource; import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; import org.openmrs.module.webservices.rest.web.response.ResponseException; -import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_9.BaseAttributeCrudResource1_9; import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_2.OrderResource2_2; import java.util.List; @@ -31,7 +29,7 @@ */ @SubResource(parent = OrderResource2_2.class, path = "attribute", supportedClass = OrderAttribute.class, supportedOpenmrsVersions = { "2.5.* - 9.*"}) -public class OrderAttributeResource2_5 extends BaseAttributeCrudResource1_9 { +public class OrderAttributeResource2_5 extends BaseAttributeCrudResource2_5 { /** * Sets attributeType on the given OrderAttribute. @@ -116,14 +114,7 @@ public OrderAttribute save(OrderAttribute delegate) { */ @Override protected void delete(OrderAttribute delegate, String reason, RequestContext context) throws ResponseException { - delegate.setVoided(true); - delegate.setVoidReason(reason); - - OrderContext orderContext = new OrderContext(); - orderContext.setCareSetting(delegate.getOrder().getCareSetting()); - orderContext.setOrderType(delegate.getOrder().getOrderType()); - - Context.getOrderService().saveOrder(delegate.getOrder(),orderContext); + throw new UnsupportedOperationException("Cannot purge OrderAttribute"); } /** @@ -134,4 +125,12 @@ protected void delete(OrderAttribute delegate, String reason, RequestContext con public void purge(OrderAttribute delegate, RequestContext context) throws ResponseException { throw new UnsupportedOperationException("Cannot purge OrderAttribute"); } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getResourceVersion() + */ + @Override + public String getResourceVersion() { + return "2.5"; + } } diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java index 2207f7716..5afc059d5 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java @@ -17,8 +17,6 @@ import org.openmrs.module.webservices.rest.web.annotation.Resource; import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; import org.openmrs.module.webservices.rest.web.response.ResponseException; -import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_9.BaseAttributeTypeCrudResource1_9; -import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_2.OrderResource2_2; import java.util.ArrayList; import java.util.List; @@ -29,7 +27,7 @@ */ @Resource(name = RestConstants.VERSION_1 + "/orderattributetype", supportedClass = OrderAttributeType.class, supportedOpenmrsVersions = { "2.5.* - 9.*" }) -public class OrderAttributeTypeResource2_5 extends BaseAttributeTypeCrudResource1_9 { +public class OrderAttributeTypeResource2_5 extends BaseAttributeTypeCrudResource2_5 { public OrderAttributeTypeResource2_5() { } @@ -84,7 +82,6 @@ public void purge(OrderAttributeType delegate, RequestContext context) throws Re */ @Override protected NeedsPaging doSearch(RequestContext context) { - // TODO: Should be a OrderAttributeType search method in OrderService List allAttrs = service().getAllOrderAttributeTypes(); List queryResult = new ArrayList(); for (OrderAttributeType locAttr : allAttrs) { @@ -95,4 +92,12 @@ protected NeedsPaging doSearch(RequestContext context) { } return new NeedsPaging(queryResult, context); } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getResourceVersion() + */ + @Override + public String getResourceVersion() { + return "2.5"; + } } diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5.java new file mode 100644 index 000000000..74f65dc1e --- /dev/null +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5.java @@ -0,0 +1,77 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; + +import org.openmrs.Order; +import org.openmrs.OrderAttribute; +import org.openmrs.module.webservices.rest.web.RestConstants; +import org.openmrs.module.webservices.rest.web.annotation.PropertySetter; +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.Representation; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource; +import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_2.OrderResource2_2; + +import java.util.Arrays; +import java.util.List; + +@Resource(name = RestConstants.VERSION_1 + "/order", supportedClass = Order.class, supportedOpenmrsVersions = { "2.5.* - 9.*" }) +public class OrderResource2_5 extends OrderResource2_2 { + + /** + * Sets attributes on the given Order. + * + * @param instance + * @param attrs + */ + @PropertySetter("attributes") + public static void setAttributes(Order instance, List attrs) { + for (OrderAttribute attr : attrs) { + instance.addAttribute(attr); + } + } + + /** + * @see DelegatingCrudResource#getRepresentationDescription(Representation) + */ + @Override + public DelegatingResourceDescription getRepresentationDescription(Representation rep) { + if (rep instanceof DefaultRepresentation) { + DelegatingResourceDescription description = super.getRepresentationDescription(rep); + description.addProperty("attributes", Representation.REF); + return description; + } else if (rep instanceof FullRepresentation) { + DelegatingResourceDescription description = super.getRepresentationDescription(rep); + description.addProperty("attributes", Representation.DEFAULT); + return description; + } + return null; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getCreatableProperties() + */ + @Override + public DelegatingResourceDescription getCreatableProperties() { + DelegatingResourceDescription description = super.getCreatableProperties(); + description.addProperty("attributes"); + return description; + } + + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getPropertiesToExposeAsSubResources() + */ + @Override + public List getPropertiesToExposeAsSubResources() { + return Arrays.asList("attributes"); + } +} \ No newline at end of file diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java index fff10f05f..08e83afe0 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java @@ -12,6 +12,8 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.openmrs.DrugOrder; +import org.openmrs.Order; import org.openmrs.OrderAttribute; import org.openmrs.api.OrderService; import org.openmrs.api.context.Context; @@ -40,7 +42,7 @@ public class OrderAttributeController2_5Test extends MainResourceControllerTest */ @Override public String getURI() { - return "order/" + RestTestConstants2_5.ORDER_UUID + "/attribute"; + return "order"; } /** @@ -68,62 +70,11 @@ public void before() throws Exception { @Test public void shouldAddAttributeToOrder() throws Exception { int before = service.getOrderByUuid(RestTestConstants2_5.ORDER_UUID).getAttributes().size(); - String json = "{\"attributeType\":\"9516cc50-6f9f-132r-5433-001e378eb67f\", \"value\":\"2012-05-05\"}"; + + String json = "{\"type\":\"order\",\"concept\":\"d144d24f-6913-4b63-9660-a9108c2bebef\",\"orderer\":\"c2299800-cca9-11e0-9572-0800200c9a66\",\"patient\":\"5946f880-b197-400b-9caa-a3c661d23041\",\"careSetting\":\"6f0c9a92-6f24-11e3-af88-005056821db0\",\"urgency\":\"STAT\",\"dose\":30,\"doseUnits\":\"5a2aa3db-68a3-11e3-bd76-0800271c1b75\",\"frequency\":\"28090760-7c38-11e3-baa7-0800200c9a66\",\"asNeeded\":true,\"quantityUnits\":\"5a2aa3db-68a3-11e3-bd76-0800271c1b75\",\"numRefills\":1,\"duration\":30,\"durationUnits\":\"7bfdcbf0-d9e7-11e3-9c1a-0800200c9a66\",\"route\":\"e10ffe54-5184-4efe-8960-cd565ec1cdf8\",\"dispenseAsWritten\":true,\"attributes\":[{\"attributeType\":\"6f09f118-bb2e-4459-8815-bd786d5e0e59\",\"value\":\"167ce20c-4785-4285-9119-d197268f7f4a\"}]}"; handle(newPostRequest(getURI(), json)); int after = service.getOrderByUuid(RestTestConstants2_5.ORDER_UUID).getAttributes().size(); Assert.assertEquals(before + 1, after); } - - @Test - public void shouldEditOrderAttribute() throws Exception { - String json = "{ \"attributeType\":\"9516cc50-6f9f-132r-5433-001e378eb67f\" }"; - - OrderAttribute orderAttribute = service.getOrderAttributeByUuid(getUuid()); - Assert.assertEquals("Dispensing Location", orderAttribute.getAttributeType().getName()); - - handle(newPostRequest(getURI() + "/" + getUuid(), json)); - - orderAttribute = service.getOrderAttributeByUuid(getUuid()); - Assert.assertEquals("Dispensing Personnel", orderAttribute.getAttributeType().getName()); - } - - @Test - public void shouldVoidAttribute() throws Exception { - OrderAttribute orderAttribute = service.getOrderAttributeByUuid(getUuid()); - Assert.assertFalse(orderAttribute.isVoided()); - - MockHttpServletRequest request = request(RequestMethod.DELETE, getURI() + "/" + getUuid()); - request.addParameter("reason", "unit test"); - handle(request); - - orderAttribute = service.getOrderAttributeByUuid(getUuid()); - Assert.assertTrue(orderAttribute.isVoided()); - Assert.assertEquals("unit test", orderAttribute.getVoidReason()); - } - - @Test - public void shouldReturnOnlyAttributesOfGivenTypeWhenSearch() throws Exception { - String attributeTypeUuid = "9516cc50-6f9f-11e0-8414-001e378eb67e"; - searchForAttributeOfGivenType_AndCheckIfTypeMatches(attributeTypeUuid); - - String anotherAttributeTypUuid = "9516cc50-6f9f-132r-6556-001e378eb67f"; - searchForAttributeOfGivenType_AndCheckIfTypeMatches(anotherAttributeTypUuid); - } - - /** - * sends search request for attributes with given type, checks if all result attributes are of - * this type - * - * @param attributeTypeUuid - * @throws Exception - */ - private void searchForAttributeOfGivenType_AndCheckIfTypeMatches(String attributeTypeUuid) throws Exception { - SimpleObject response2 = deserialize(handle(newGetRequest(getURI(), - new Parameter("attributeType", attributeTypeUuid)))); - assertThat(Util.getResultsList(response2), is(not(empty()))); - for (Object result : Util.getResultsList(response2)) { - Object resultAttributeTypeUuid = Util.getByPath(Util.getByPath(result, "attributeType"), "uuid"); - Assert.assertThat((String) resultAttributeTypeUuid, is(attributeTypeUuid)); - } - } + } diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeTypeController2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeTypeController2_5Test.java index 68f1e5967..3b6960bb5 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeTypeController2_5Test.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeTypeController2_5Test.java @@ -119,7 +119,7 @@ public void findOrderAttributeTypes_shouldReturnNoResultsIfThereAreNoMatchingOrd */ @Test public void findOrderAttributeTypes_shouldFindMatchingOrderAttributeTypes() throws Exception { - SimpleObject response = deserialize(handle(newGetRequest(getURI(), new Parameter("q", "Audit")))); + SimpleObject response = deserialize(handle(newGetRequest(getURI(), new Parameter("q", "Dispensing Location")))); List results = Util.getResultsList(response); diff --git a/omod-2.5/src/test/resources/customTestDataset2_5.xml b/omod-2.5/src/test/resources/customTestDataset2_5.xml index 413bd7041..b9d0e19b8 100644 --- a/omod-2.5/src/test/resources/customTestDataset2_5.xml +++ b/omod-2.5/src/test/resources/customTestDataset2_5.xml @@ -1,17 +1,22 @@ - - - + - + + + + + + + + From 5f1756ae43fb5d5aea2d9ca078db56955cde597f Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Sun, 3 Mar 2024 16:20:31 +0300 Subject: [PATCH 06/15] changing test to focus on creation of order with attribute --- .../resource/openmrs2_2/OrderResource2_2.java | 2 +- .../BaseAttributeCrudResource2_5.java | 2 +- .../BaseAttributeTypeCrudResource2_5.java | 2 +- .../openmrs2_5/OrderAttributeResource2_5.java | 6 +-- .../OrderAttributeTypeResource2_5.java | 2 +- .../resource/openmrs2_5/OrderResource2_5.java | 18 ++++++++- .../resource/openmrs2_5/RestConstants2_5.java | 21 ++++++++++ .../OrderAttributeController2_5Test.java | 16 +------- .../openmrs2_5/OrderResource2_5Test.java | 40 +++++++++++++++++++ .../test/resources/customTestDataset2_5.xml | 17 ++++++-- 10 files changed, 99 insertions(+), 27 deletions(-) create mode 100644 omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/RestConstants2_5.java create mode 100644 omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5Test.java diff --git a/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/OrderResource2_2.java b/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/OrderResource2_2.java index 07751664a..6d3c3f161 100644 --- a/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/OrderResource2_2.java +++ b/omod-2.2/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_2/OrderResource2_2.java @@ -17,7 +17,7 @@ import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_10.OrderResource1_10; -@Resource(name = RestConstants.VERSION_1 + "/order", supportedClass = Order.class, supportedOpenmrsVersions = { "2.2.* - 9.*" }) +@Resource(name = RestConstants.VERSION_1 + "/order", supportedClass = Order.class, supportedOpenmrsVersions = { "2.2.* - 2.4.*" }) public class OrderResource2_2 extends OrderResource1_10 { /** diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeCrudResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeCrudResource2_5.java index 2afcc0074..4b24a915a 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeCrudResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeCrudResource2_5.java @@ -149,7 +149,7 @@ public String getDisplayString(T attr) { */ @Override public String getResourceVersion() { - return "2.5"; + return RestConstants2_5.RESOURCE_VERSION; } /** diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeTypeCrudResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeTypeCrudResource2_5.java index dea4b4ce3..768d6208e 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeTypeCrudResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeTypeCrudResource2_5.java @@ -122,6 +122,6 @@ public DelegatingResourceDescription getCreatableProperties() { */ @Override public String getResourceVersion() { - return "2.5"; + return RestConstants2_5.RESOURCE_VERSION; } } diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java index f4e99ae58..b2edc0a00 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java @@ -91,8 +91,8 @@ public NeedsPaging doGetAll(Order parent, RequestContext context public OrderAttribute save(OrderAttribute delegate) { // make sure it has not already been added to the order boolean needToAdd = true; - for (OrderAttribute pa : delegate.getOrder().getActiveAttributes()) { - if (pa.equals(delegate)) { + for (OrderAttribute orderAttribute : delegate.getOrder().getActiveAttributes()) { + if (orderAttribute.equals(delegate)) { needToAdd = false; break; } @@ -131,6 +131,6 @@ public void purge(OrderAttribute delegate, RequestContext context) throws Respon */ @Override public String getResourceVersion() { - return "2.5"; + return RestConstants2_5.RESOURCE_VERSION; } } diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java index 5afc059d5..bbf7aad45 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java @@ -98,6 +98,6 @@ protected NeedsPaging doSearch(RequestContext context) { */ @Override public String getResourceVersion() { - return "2.5"; + return RestConstants2_5.RESOURCE_VERSION; } } diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5.java index 74f65dc1e..a433d9061 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5.java @@ -20,7 +20,6 @@ import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource; import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_2.OrderResource2_2; - import java.util.Arrays; import java.util.List; @@ -51,7 +50,7 @@ public DelegatingResourceDescription getRepresentationDescription(Representation return description; } else if (rep instanceof FullRepresentation) { DelegatingResourceDescription description = super.getRepresentationDescription(rep); - description.addProperty("attributes", Representation.DEFAULT); + description.addProperty("attributes", Representation.FULL); return description; } return null; @@ -67,6 +66,16 @@ public DelegatingResourceDescription getCreatableProperties() { return description; } + /** + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getUpdatableProperties() + */ + @Override + public DelegatingResourceDescription getUpdatableProperties() { + DelegatingResourceDescription description = super.getUpdatableProperties(); + description.addProperty("attributes"); + return description; + } + /** * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getPropertiesToExposeAsSubResources() */ @@ -74,4 +83,9 @@ public DelegatingResourceDescription getCreatableProperties() { public List getPropertiesToExposeAsSubResources() { return Arrays.asList("attributes"); } + + @Override + public String getResourceVersion() { + return RestConstants2_5.RESOURCE_VERSION; + } } \ No newline at end of file diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/RestConstants2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/RestConstants2_5.java new file mode 100644 index 000000000..17e0cc768 --- /dev/null +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/RestConstants2_5.java @@ -0,0 +1,21 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; + +/** + * {@link org.openmrs.module.webservices.rest.web.RestConstants} for 2.0 resources. + */ +public class RestConstants2_5 { + + /** + * A default value for the resource version parameter. + */ + public static final String RESOURCE_VERSION = "2.5"; +} diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java index 08e83afe0..3b2cb4e10 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java @@ -12,22 +12,10 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.openmrs.DrugOrder; -import org.openmrs.Order; -import org.openmrs.OrderAttribute; import org.openmrs.api.OrderService; import org.openmrs.api.context.Context; -import org.openmrs.module.webservices.rest.SimpleObject; -import org.openmrs.module.webservices.rest.test.Util; import org.openmrs.module.webservices.rest.web.v1_0.resource.RestTestConstants2_5; import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; -import org.springframework.mock.web.MockHttpServletRequest; -import org.springframework.web.bind.annotation.RequestMethod; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.not; /** @@ -68,10 +56,10 @@ public void before() throws Exception { } @Test - public void shouldAddAttributeToOrder() throws Exception { + public void shouldCreateOrderWithAttribute() throws Exception { int before = service.getOrderByUuid(RestTestConstants2_5.ORDER_UUID).getAttributes().size(); - String json = "{\"type\":\"order\",\"concept\":\"d144d24f-6913-4b63-9660-a9108c2bebef\",\"orderer\":\"c2299800-cca9-11e0-9572-0800200c9a66\",\"patient\":\"5946f880-b197-400b-9caa-a3c661d23041\",\"careSetting\":\"6f0c9a92-6f24-11e3-af88-005056821db0\",\"urgency\":\"STAT\",\"dose\":30,\"doseUnits\":\"5a2aa3db-68a3-11e3-bd76-0800271c1b75\",\"frequency\":\"28090760-7c38-11e3-baa7-0800200c9a66\",\"asNeeded\":true,\"quantityUnits\":\"5a2aa3db-68a3-11e3-bd76-0800271c1b75\",\"numRefills\":1,\"duration\":30,\"durationUnits\":\"7bfdcbf0-d9e7-11e3-9c1a-0800200c9a66\",\"route\":\"e10ffe54-5184-4efe-8960-cd565ec1cdf8\",\"dispenseAsWritten\":true,\"attributes\":[{\"attributeType\":\"6f09f118-bb2e-4459-8815-bd786d5e0e59\",\"value\":\"167ce20c-4785-4285-9119-d197268f7f4a\"}]}"; + String json = "{\"encounter\":\"d2d69553-2247-414c-b0c5-46be893605af\",\"orderType\":\"2d3fb1d0-ae06-22e3-a5e2-0140211c9a66\",\"type\":\"order\",\"action\":\"NEW\",\"accessionNumber\":\"string\",\"patient\":\"5946f880-b197-400b-9caa-a3c661d23041\",\"concept\":\"d144d24f-6913-4b63-9660-a9108c2bebef\",\"careSetting\":\"6f0c9a92-6f24-11e3-af88-005056821db0\",\"orderer\":\"c2299800-cca9-11e0-9572-0800200c9a66\",\"previousOrder\":\"\",\"urgency\":\"ROUTINE\",\"orderReason\":\"\",\"orderReasonNonCoded\":\"for Test\",\"instructions\":\"string\",\"commentToFulfiller\":\"string\",\"attributes\":[{\"attributeType\":\"9516cc50-6f9f-132r-6556-001e378eb67f\",\"value\":\"EMR Pharmacy 1\",\"valueReference\":\"EMR Pharmacy 1\"}]}"; handle(newPostRequest(getURI(), json)); int after = service.getOrderByUuid(RestTestConstants2_5.ORDER_UUID).getAttributes().size(); Assert.assertEquals(before + 1, after); diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5Test.java new file mode 100644 index 000000000..8fce5135c --- /dev/null +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5Test.java @@ -0,0 +1,40 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; + +import org.openmrs.Order; +import org.openmrs.api.context.Context; + +import org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResourceTest; +import org.openmrs.module.webservices.rest.web.v1_0.resource.RestTestConstants2_5; + +public class OrderResource2_5Test extends BaseDelegatingResourceTest { + + @Override + public Order newObject() { + return Context.getOrderService().getOrderByUuid(getUuidProperty()); + } + + @Override + public void validateFullRepresentation() throws Exception { + super.validateFullRepresentation(); + assertPropPresent("attributes"); + } + + @Override + public String getDisplayProperty() { + return "CD4 COUNT"; + } + + @Override + public String getUuidProperty() { + return RestTestConstants2_5.ORDER_UUID; + } +} diff --git a/omod-2.5/src/test/resources/customTestDataset2_5.xml b/omod-2.5/src/test/resources/customTestDataset2_5.xml index b9d0e19b8..5a4b7ee4a 100644 --- a/omod-2.5/src/test/resources/customTestDataset2_5.xml +++ b/omod-2.5/src/test/resources/customTestDataset2_5.xml @@ -1,18 +1,27 @@ + + + + - - - - + + + + + + From e77fb1af71054362e58d81b17a02012095fa5bb9 Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Mon, 4 Mar 2024 13:20:27 +0300 Subject: [PATCH 07/15] change version in omod-2.5 to match other module poms --- omod-2.5/pom.xml | 2 +- .../openmrs2_5/OrderAttributeTypeController2_5Test.java | 2 +- .../v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java | 2 +- .../resource/openmrs2_5/OrderAttributeTypeResource2_5Test.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/omod-2.5/pom.xml b/omod-2.5/pom.xml index 70193b412..118d7b773 100644 --- a/omod-2.5/pom.xml +++ b/omod-2.5/pom.xml @@ -3,7 +3,7 @@ webservices.rest org.openmrs.module - 2.41.0-SNAPSHOT + 2.40.0-SNAPSHOT 4.0.0 diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeTypeController2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeTypeController2_5Test.java index 3b6960bb5..6f703c350 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeTypeController2_5Test.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeTypeController2_5Test.java @@ -119,7 +119,7 @@ public void findOrderAttributeTypes_shouldReturnNoResultsIfThereAreNoMatchingOrd */ @Test public void findOrderAttributeTypes_shouldFindMatchingOrderAttributeTypes() throws Exception { - SimpleObject response = deserialize(handle(newGetRequest(getURI(), new Parameter("q", "Dispensing Location")))); + SimpleObject response = deserialize(handle(newGetRequest(getURI(), new Parameter("q", "Dispensing Date")))); List results = Util.getResultsList(response); diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java index 3432c9051..a0d03229c 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5Test.java @@ -53,7 +53,7 @@ public void validateFullRepresentation() throws Exception { @Override public String getDisplayProperty() { try { - return "Dispensing Location: " + new SimpleDateFormat("yyyy-MM-dd").parse("2011-04-25"); + return "Dispensing Date: " + new SimpleDateFormat("yyyy-MM-dd").parse("2011-04-25"); } catch (ParseException ex) { log.error(ex); diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5Test.java index de0c4d3b0..0d47b3245 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5Test.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5Test.java @@ -54,7 +54,7 @@ public void validateFullRepresentation() throws Exception { @Override public String getDisplayProperty() { - return "Dispensing Location"; + return "Dispensing Date"; } @Override From 6cd7ba05c530bb15ffd8ce6224d16fadbec21788 Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Wed, 12 Jun 2024 12:46:46 +0300 Subject: [PATCH 08/15] change version to 2.44.0-SNAPSHOT --- omod-2.5/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/omod-2.5/pom.xml b/omod-2.5/pom.xml index 118d7b773..46a750846 100644 --- a/omod-2.5/pom.xml +++ b/omod-2.5/pom.xml @@ -3,7 +3,7 @@ webservices.rest org.openmrs.module - 2.40.0-SNAPSHOT + 2.44.0-SNAPSHOT 4.0.0 From 8bb76d2d5719f37e7ea2bf9b96840cdc5b82ef12 Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Wed, 12 Jun 2024 15:29:28 +0300 Subject: [PATCH 09/15] change Dispensing Date order_attribute_type uuid to c0de4f5c-6626-418e-9f4f-5396a31e68fb --- .../rest/web/v1_0/resource/RestTestConstants2_5.java | 2 +- omod-2.5/src/test/resources/customTestDataset2_5.xml | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/RestTestConstants2_5.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/RestTestConstants2_5.java index 800679442..bcab19d4a 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/RestTestConstants2_5.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/RestTestConstants2_5.java @@ -17,6 +17,6 @@ public class RestTestConstants2_5 { public final static String ORDER_ATTRIBUTE_UUID = "3a2bdb18-6faa-11e0-8414-001e378eb67e"; - public final static String ORDER_ATTRIBUTE_TYPE_UUID = "9516cc50-6f9f-11e0-8414-001e378eb67e"; + public final static String ORDER_ATTRIBUTE_TYPE_UUID = "c0de4f5c-6626-418e-9f4f-5396a31e68fb"; } diff --git a/omod-2.5/src/test/resources/customTestDataset2_5.xml b/omod-2.5/src/test/resources/customTestDataset2_5.xml index 5a4b7ee4a..a71316e0a 100644 --- a/omod-2.5/src/test/resources/customTestDataset2_5.xml +++ b/omod-2.5/src/test/resources/customTestDataset2_5.xml @@ -13,8 +13,7 @@ - - + From 67b90a24a98a0d2487918dd5a50d3a0c8afb60ec Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Thu, 13 Jun 2024 12:22:22 +0300 Subject: [PATCH 10/15] make changes to test data --- .../v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java | 4 ++-- .../openmrs2_5/OrderAttributeController2_5Test.java | 2 +- omod-2.5/src/test/resources/customTestDataset2_5.xml | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java index b2edc0a00..16dd55c76 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java @@ -27,9 +27,9 @@ /** * {@link Resource} for OrderAttributes, supporting standard CRUD operations */ -@SubResource(parent = OrderResource2_2.class, path = "attribute", supportedClass = OrderAttribute.class, supportedOpenmrsVersions = { +@SubResource(parent = OrderResource2_5.class, path = "attribute", supportedClass = OrderAttribute.class, supportedOpenmrsVersions = { "2.5.* - 9.*"}) -public class OrderAttributeResource2_5 extends BaseAttributeCrudResource2_5 { +public class OrderAttributeResource2_5 extends BaseAttributeCrudResource2_5 { /** * Sets attributeType on the given OrderAttribute. diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java index 3b2cb4e10..b1854312d 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java @@ -59,7 +59,7 @@ public void before() throws Exception { public void shouldCreateOrderWithAttribute() throws Exception { int before = service.getOrderByUuid(RestTestConstants2_5.ORDER_UUID).getAttributes().size(); - String json = "{\"encounter\":\"d2d69553-2247-414c-b0c5-46be893605af\",\"orderType\":\"2d3fb1d0-ae06-22e3-a5e2-0140211c9a66\",\"type\":\"order\",\"action\":\"NEW\",\"accessionNumber\":\"string\",\"patient\":\"5946f880-b197-400b-9caa-a3c661d23041\",\"concept\":\"d144d24f-6913-4b63-9660-a9108c2bebef\",\"careSetting\":\"6f0c9a92-6f24-11e3-af88-005056821db0\",\"orderer\":\"c2299800-cca9-11e0-9572-0800200c9a66\",\"previousOrder\":\"\",\"urgency\":\"ROUTINE\",\"orderReason\":\"\",\"orderReasonNonCoded\":\"for Test\",\"instructions\":\"string\",\"commentToFulfiller\":\"string\",\"attributes\":[{\"attributeType\":\"9516cc50-6f9f-132r-6556-001e378eb67f\",\"value\":\"EMR Pharmacy 1\",\"valueReference\":\"EMR Pharmacy 1\"}]}"; + String json = "{\"encounter\":\"d2d69553-2247-414c-b0c5-46be893605af\",\"orderType\":\"2d3fb1d0-ae06-22e3-a5e2-0140211c9a66\",\"type\":\"order\",\"action\":\"NEW\",\"accessionNumber\":\"string\",\"patient\":\"5946f880-b197-400b-9caa-a3c661d23041\",\"concept\":\"d144d24f-6913-4b63-9660-a9108c2bebef\",\"careSetting\":\"6f0c9a92-6f24-11e3-af88-005056821db0\",\"orderer\":\"c2299800-cca9-11e0-9572-0800200c9a66\",\"previousOrder\":\"\",\"urgency\":\"ROUTINE\",\"orderReason\":\"\",\"orderReasonNonCoded\":\"for Test\",\"instructions\":\"string\",\"commentToFulfiller\":\"string\",\"attributes\":[{\"attributeType\":\"c0de4f5c-6626-418e-9f4f-5396a31e68fb\",\"value\":\"2023-08-14 17:11:39\"}]}"; handle(newPostRequest(getURI(), json)); int after = service.getOrderByUuid(RestTestConstants2_5.ORDER_UUID).getAttributes().size(); Assert.assertEquals(before + 1, after); diff --git a/omod-2.5/src/test/resources/customTestDataset2_5.xml b/omod-2.5/src/test/resources/customTestDataset2_5.xml index a71316e0a..4e346e0c5 100644 --- a/omod-2.5/src/test/resources/customTestDataset2_5.xml +++ b/omod-2.5/src/test/resources/customTestDataset2_5.xml @@ -13,7 +13,8 @@ - + + From c0e54475c772350ac293c240d2ce91c9ed46e512 Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Thu, 13 Jun 2024 20:25:35 +0300 Subject: [PATCH 11/15] response to comments --- .../BaseAttributeCrudResource2_5.java | 162 ------------------ .../BaseAttributeTypeCrudResource2_5.java | 127 -------------- .../openmrs2_5/OrderAttributeResource2_5.java | 11 +- .../OrderAttributeTypeResource2_5.java | 3 +- ...5Test.java => OrderController2_5Test.java} | 4 +- .../openmrs2_5/OrderResource2_5Test.java | 8 +- 6 files changed, 16 insertions(+), 299 deletions(-) delete mode 100644 omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeCrudResource2_5.java delete mode 100644 omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeTypeCrudResource2_5.java rename omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/{OrderAttributeController2_5Test.java => OrderController2_5Test.java} (95%) diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeCrudResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeCrudResource2_5.java deleted file mode 100644 index 4b24a915a..000000000 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeCrudResource2_5.java +++ /dev/null @@ -1,162 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under - * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. - * - * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS - * graphic logo is a trademark of OpenMRS Inc. - */ -package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; - -import io.swagger.models.Model; -import io.swagger.models.ModelImpl; -import io.swagger.models.properties.BooleanProperty; -import io.swagger.models.properties.StringProperty; -import org.apache.commons.lang.StringUtils; -import org.openmrs.attribute.Attribute; -import org.openmrs.customdatatype.CustomDatatype; -import org.openmrs.customdatatype.CustomDatatypeUtil; -import org.openmrs.customdatatype.NotYetPersistedException; -import org.openmrs.module.webservices.rest.web.RestConstants; -import org.openmrs.module.webservices.rest.web.annotation.PropertyGetter; -import org.openmrs.module.webservices.rest.web.annotation.PropertySetter; -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.Representation; -import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource; -import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; -import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubResource; - -import java.util.Arrays; -import java.util.List; - -/** - * Subclass of {@link DelegatingSubResource} with helper methods specific to {@link Attribute} - * - * @param The type of the attribute this sub resource is associated to - * @param

The parent/owning type for the type T - * @param The Resource for the parent/owning type P - */ -public abstract class BaseAttributeCrudResource2_5, P, PR> extends DelegatingSubResource> { - - /** - * Sets value on the given attribute. - * - * @param instance - * @param value - */ - @PropertySetter("value") - public static void setValue(Attribute instance, String value) throws Exception { - CustomDatatype datatype = CustomDatatypeUtil.getDatatype(instance.getAttributeType().getDatatypeClassname(), - instance.getAttributeType().getDatatypeConfig()); - if (StringUtils.isNotEmpty(value)) // check empty instead of blank, because " " is meaningful - instance.setValue(datatype.fromReferenceString(value)); - } - - /** - * Gets an attribute value, catching any {@link NotYetPersistedException} and returning null in - * that case - * - * @param instance - * @return - */ - @PropertyGetter("value") - public static Object getValue(Attribute instance) { - try { - return instance.getValue(); - } - catch (NotYetPersistedException ex) { - return null; - } - } - - /** - * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getRepresentationDescription(Representation) - */ - @Override - public DelegatingResourceDescription getRepresentationDescription(Representation rep) { - if (rep instanceof DefaultRepresentation) { - DelegatingResourceDescription description = new DelegatingResourceDescription(); - description.addProperty("display"); - description.addProperty("uuid"); - description.addProperty("attributeType", Representation.REF); - description.addProperty("value"); - description.addProperty("voided"); - description.addSelfLink(); - description.addLink("full", ".?v=" + RestConstants.REPRESENTATION_FULL); - return description; - } else if (rep instanceof FullRepresentation) { - DelegatingResourceDescription description = new DelegatingResourceDescription(); - description.addProperty("display"); - description.addProperty("uuid"); - description.addProperty("attributeType", Representation.REF); - description.addProperty("value"); - description.addProperty("voided"); - description.addProperty("auditInfo"); - description.addSelfLink(); - return description; - } - return null; - } - - @Override - public DelegatingResourceDescription getCreatableProperties() { - DelegatingResourceDescription description = new DelegatingResourceDescription(); - description.addRequiredProperty("attributeType"); - description.addRequiredProperty("value"); - return description; - } - - @Override - public Model getCREATEModel(Representation rep) { - return new ModelImpl() - .property("attributeType", new StringProperty().example("uuid")) - .property("value", new StringProperty()) - - .required("attributeType").required("value"); - } - - @Override - public Model getGETModel(Representation rep) { - ModelImpl model = (ModelImpl) super.getGETModel(rep); - if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) { - model - .property("display", new StringProperty()) - .property("uuid", new StringProperty()) - .property("attributeType", new StringProperty()) //FIXME type - .property("value", new StringProperty()) //FIXME type - .property("voided", new BooleanProperty()); - } - return model; - } - - /** - * Gets the display string for an attribute. - * - * @param attr the attribute. - * @return attribute type: value (for concise display purposes) - */ - @PropertyGetter("display") - public String getDisplayString(T attr) { - if (attr.getAttributeType() == null) - return ""; - return attr.getAttributeType().getName() + ": " + attr.getValue(); - } - - /** - * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getResourceVersion() - */ - @Override - public String getResourceVersion() { - return RestConstants2_5.RESOURCE_VERSION; - } - - /** - * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getPropertiesToExposeAsSubResources() - */ - @Override - public List getPropertiesToExposeAsSubResources() { - return Arrays.asList("attributes"); - } -} diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeTypeCrudResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeTypeCrudResource2_5.java deleted file mode 100644 index 768d6208e..000000000 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/BaseAttributeTypeCrudResource2_5.java +++ /dev/null @@ -1,127 +0,0 @@ -/** - * This Source Code Form is subject to the terms of the Mozilla Public License, - * v. 2.0. If a copy of the MPL was not distributed with this file, You can - * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under - * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. - * - * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS - * graphic logo is a trademark of OpenMRS Inc. - */ -package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; - -import io.swagger.models.Model; -import io.swagger.models.ModelImpl; -import io.swagger.models.properties.IntegerProperty; -import io.swagger.models.properties.StringProperty; -import org.openmrs.attribute.AttributeType; -import org.openmrs.module.webservices.rest.web.RestConstants; -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.Representation; -import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription; -import org.openmrs.module.webservices.rest.web.resource.impl.MetadataDelegatingCrudResource; - -/** - * Subclass of {@link MetadataDelegatingCrudResource} with helper methods specific to - * {@link AttributeType} - * - * @param - */ -public abstract class BaseAttributeTypeCrudResource2_5> extends MetadataDelegatingCrudResource { - - @Override - public Model getGETModel(Representation rep) { - ModelImpl model = (ModelImpl) super.getGETModel(rep); - if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) { - model - .property("minOccurs", new IntegerProperty()) - .property("maxOccurs", new IntegerProperty()) - .property("datatypeClassname", new StringProperty()) - .property("preferredHandlerClassname", new StringProperty()); - } - if (rep instanceof FullRepresentation) { - model - .property("datatypeConfig", new StringProperty()) - .property("handlerConfig", new StringProperty()); - } - return model; - } - - @Override - public Model getCREATEModel(Representation rep) { - return ((ModelImpl) super.getCREATEModel(rep)) - .property("datatypeClassname", new StringProperty()) - .property("minOccurs", new IntegerProperty()) - .property("maxOccurs", new IntegerProperty()) - .property("datatypeConfig", new StringProperty()) - .property("preferredHandlerClassname", new StringProperty()) - .property("handlerConfig", new StringProperty()) - - .required("datatypeClassname"); - - } - - /** - * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getRepresentationDescription(Representation) - */ - @Override - public DelegatingResourceDescription getRepresentationDescription(Representation rep) { - if (rep instanceof DefaultRepresentation) { - DelegatingResourceDescription description = new DelegatingResourceDescription(); - description.addProperty("uuid"); - description.addProperty("display"); - description.addProperty("name"); - description.addProperty("description"); - description.addProperty("minOccurs"); - description.addProperty("maxOccurs"); - description.addProperty("datatypeClassname"); - description.addProperty("preferredHandlerClassname"); - description.addProperty("retired"); - 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("display"); - description.addProperty("name"); - description.addProperty("description"); - description.addProperty("minOccurs"); - description.addProperty("maxOccurs"); - description.addProperty("datatypeClassname"); - description.addProperty("datatypeConfig"); - description.addProperty("preferredHandlerClassname"); - description.addProperty("handlerConfig"); - description.addProperty("retired"); - description.addProperty("auditInfo"); - 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("name"); - description.addRequiredProperty("datatypeClassname"); - description.addProperty("description"); - description.addProperty("minOccurs"); - description.addProperty("maxOccurs"); - description.addProperty("datatypeConfig"); - description.addProperty("preferredHandlerClassname"); - description.addProperty("handlerConfig"); - return description; - } - - /** - * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getResourceVersion() - */ - @Override - public String getResourceVersion() { - return RestConstants2_5.RESOURCE_VERSION; - } -} diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java index 16dd55c76..810f16d2c 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java @@ -20,6 +20,7 @@ import org.openmrs.module.webservices.rest.web.annotation.SubResource; import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; import org.openmrs.module.webservices.rest.web.response.ResponseException; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_9.BaseAttributeCrudResource1_9; import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_2.OrderResource2_2; import java.util.List; @@ -29,7 +30,7 @@ */ @SubResource(parent = OrderResource2_5.class, path = "attribute", supportedClass = OrderAttribute.class, supportedOpenmrsVersions = { "2.5.* - 9.*"}) -public class OrderAttributeResource2_5 extends BaseAttributeCrudResource2_5 { +public class OrderAttributeResource2_5 extends BaseAttributeCrudResource1_9 { /** * Sets attributeType on the given OrderAttribute. @@ -91,11 +92,8 @@ public NeedsPaging doGetAll(Order parent, RequestContext context public OrderAttribute save(OrderAttribute delegate) { // make sure it has not already been added to the order boolean needToAdd = true; - for (OrderAttribute orderAttribute : delegate.getOrder().getActiveAttributes()) { - if (orderAttribute.equals(delegate)) { - needToAdd = false; - break; - } + if (delegate.getOrder().getActiveAttributes().contains(delegate)) { + delegate.getOrder().addAttribute(delegate); } if (needToAdd) { delegate.getOrder().addAttribute(delegate); @@ -103,6 +101,7 @@ public OrderAttribute save(OrderAttribute delegate) { OrderContext orderContext = new OrderContext(); orderContext.setCareSetting(delegate.getOrder().getCareSetting()); orderContext.setOrderType(delegate.getOrder().getOrderType()); + delegate.getOrder().setAction(Order.Action.REVISE); Context.getOrderService().saveOrder(delegate.getOrder(), orderContext); return delegate; diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java index bbf7aad45..9dcabfadd 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java @@ -17,6 +17,7 @@ import org.openmrs.module.webservices.rest.web.annotation.Resource; import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging; import org.openmrs.module.webservices.rest.web.response.ResponseException; +import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_9.BaseAttributeTypeCrudResource1_9; import java.util.ArrayList; import java.util.List; @@ -27,7 +28,7 @@ */ @Resource(name = RestConstants.VERSION_1 + "/orderattributetype", supportedClass = OrderAttributeType.class, supportedOpenmrsVersions = { "2.5.* - 9.*" }) -public class OrderAttributeTypeResource2_5 extends BaseAttributeTypeCrudResource2_5 { +public class OrderAttributeTypeResource2_5 extends BaseAttributeTypeCrudResource1_9 { public OrderAttributeTypeResource2_5() { } diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderController2_5Test.java similarity index 95% rename from omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java rename to omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderController2_5Test.java index b1854312d..05b47106f 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderAttributeController2_5Test.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderController2_5Test.java @@ -19,9 +19,9 @@ /** - * Tests functionality of {@link OrderAttributeController}. + * Tests functionality of {@link OrderController}. */ -public class OrderAttributeController2_5Test extends MainResourceControllerTest { +public class OrderController2_5Test extends MainResourceControllerTest { private OrderService service; diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5Test.java index 8fce5135c..c93cca207 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5Test.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5Test.java @@ -9,6 +9,7 @@ */ package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_5; +import org.junit.Before; import org.openmrs.Order; import org.openmrs.api.context.Context; @@ -16,6 +17,11 @@ import org.openmrs.module.webservices.rest.web.v1_0.resource.RestTestConstants2_5; public class OrderResource2_5Test extends BaseDelegatingResourceTest { + + @Before + public void before() throws Exception { + executeDataSet(RestTestConstants2_5.TEST_DATASET); + } @Override public Order newObject() { @@ -30,7 +36,7 @@ public void validateFullRepresentation() throws Exception { @Override public String getDisplayProperty() { - return "CD4 COUNT"; + return "ASPIRIN"; } @Override From 949c59f6e061aed82798c8b53b3e3f46a2d3a3a5 Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Sat, 20 Jul 2024 13:41:21 +0300 Subject: [PATCH 12/15] response to comments --- .../controller/MainResourceControllerTest.java | 1 - .../openmrs2_5/OrderAttributeResource2_5.java | 16 ++++++++-------- .../OrderAttributeTypeResource2_5.java | 3 +-- .../resource/openmrs2_5/OrderResource2_5.java | 1 + .../openmrs2_5/OrderController2_5Test.java | 15 +++++++++------ 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceControllerTest.java b/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceControllerTest.java index a344cabad..b0846cb2a 100644 --- a/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceControllerTest.java +++ b/omod-2.4/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceControllerTest.java @@ -67,7 +67,6 @@ public void shouldGetFullByUuid() throws Exception { @Test public void shouldGetAll() throws Exception { SimpleObject result = deserialize(handle(request(RequestMethod.GET, getURI()))); - Assert.assertNotNull(result); Assert.assertEquals(getAllCount(), Util.getResultsSize(result)); } diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java index 810f16d2c..57594c736 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeResource2_5.java @@ -91,13 +91,9 @@ public NeedsPaging doGetAll(Order parent, RequestContext context @Override public OrderAttribute save(OrderAttribute delegate) { // make sure it has not already been added to the order - boolean needToAdd = true; - if (delegate.getOrder().getActiveAttributes().contains(delegate)) { - delegate.getOrder().addAttribute(delegate); - } - if (needToAdd) { - delegate.getOrder().addAttribute(delegate); - } + + delegate.getOrder().addAttribute(delegate); + OrderContext orderContext = new OrderContext(); orderContext.setCareSetting(delegate.getOrder().getCareSetting()); orderContext.setOrderType(delegate.getOrder().getOrderType()); @@ -113,7 +109,11 @@ public OrderAttribute save(OrderAttribute delegate) { */ @Override protected void delete(OrderAttribute delegate, String reason, RequestContext context) throws ResponseException { - throw new UnsupportedOperationException("Cannot purge OrderAttribute"); + OrderContext orderContext = new OrderContext(); + orderContext.setCareSetting(delegate.getOrder().getCareSetting()); + orderContext.setOrderType(delegate.getOrder().getOrderType()); + delegate.getOrder().setAction(Order.Action.REVISE); + Context.getOrderService().saveOrder(delegate.getOrder(), orderContext); } /** diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java index 9dcabfadd..b3e000925 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderAttributeTypeResource2_5.java @@ -86,8 +86,7 @@ protected NeedsPaging doSearch(RequestContext context) { List allAttrs = service().getAllOrderAttributeTypes(); List queryResult = new ArrayList(); for (OrderAttributeType locAttr : allAttrs) { - if (Pattern.compile(Pattern.quote(context.getParameter("q")), Pattern.CASE_INSENSITIVE) - .matcher(locAttr.getName()).find()) { + if (locAttr.getName().toLowerCase().contains(context.getParameter("q").toLowerCase())) { queryResult.add(locAttr); } } diff --git a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5.java b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5.java index a433d9061..3fc716217 100644 --- a/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5.java +++ b/omod-2.5/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_5/OrderResource2_5.java @@ -35,6 +35,7 @@ public class OrderResource2_5 extends OrderResource2_2 { @PropertySetter("attributes") public static void setAttributes(Order instance, List attrs) { for (OrderAttribute attr : attrs) { + attr.setValueReferenceInternal(attr.getValue().toString()); instance.addAttribute(attr); } } diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderController2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderController2_5Test.java index 05b47106f..535f19a01 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderController2_5Test.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderController2_5Test.java @@ -12,11 +12,14 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.openmrs.Order; import org.openmrs.api.OrderService; import org.openmrs.api.context.Context; import org.openmrs.module.webservices.rest.web.v1_0.resource.RestTestConstants2_5; import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; +import java.util.List; + /** * Tests functionality of {@link OrderController}. @@ -32,7 +35,7 @@ public class OrderController2_5Test extends MainResourceControllerTest { public String getURI() { return "order"; } - + /** * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getUuid() */ @@ -40,7 +43,7 @@ public String getURI() { public String getUuid() { return RestTestConstants2_5.ORDER_ATTRIBUTE_UUID; } - + /** * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#getAllCount() */ @@ -57,12 +60,12 @@ public void before() throws Exception { @Test public void shouldCreateOrderWithAttribute() throws Exception { - int before = service.getOrderByUuid(RestTestConstants2_5.ORDER_UUID).getAttributes().size(); - String json = "{\"encounter\":\"d2d69553-2247-414c-b0c5-46be893605af\",\"orderType\":\"2d3fb1d0-ae06-22e3-a5e2-0140211c9a66\",\"type\":\"order\",\"action\":\"NEW\",\"accessionNumber\":\"string\",\"patient\":\"5946f880-b197-400b-9caa-a3c661d23041\",\"concept\":\"d144d24f-6913-4b63-9660-a9108c2bebef\",\"careSetting\":\"6f0c9a92-6f24-11e3-af88-005056821db0\",\"orderer\":\"c2299800-cca9-11e0-9572-0800200c9a66\",\"previousOrder\":\"\",\"urgency\":\"ROUTINE\",\"orderReason\":\"\",\"orderReasonNonCoded\":\"for Test\",\"instructions\":\"string\",\"commentToFulfiller\":\"string\",\"attributes\":[{\"attributeType\":\"c0de4f5c-6626-418e-9f4f-5396a31e68fb\",\"value\":\"2023-08-14 17:11:39\"}]}"; handle(newPostRequest(getURI(), json)); - int after = service.getOrderByUuid(RestTestConstants2_5.ORDER_UUID).getAttributes().size(); - Assert.assertEquals(before + 1, after); + List orderList= Context.getOrderService().getActiveOrders(Context.getPatientService().getPatientByUuid("5946f880-b197-400b-9caa-a3c661d23041"),Context.getOrderService().getOrderTypeByUuid("2d3fb1d0-ae06-22e3-a5e2-0140211c9a66"),null,null); + Assert.assertEquals(orderList.size(),1); + Assert.assertEquals(orderList.get(0).getAttributes().size(),1); + Assert.assertEquals(orderList.get(0).getAttributes().iterator().next().getAttributeType().getUuid(),"c0de4f5c-6626-418e-9f4f-5396a31e68fb"); } } From 14a16a4db383c8c4c9552a47a422750e974432eb Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Sat, 20 Jul 2024 14:49:43 +0300 Subject: [PATCH 13/15] change to snapshot 2.45.0-SNAPSHOT --- omod-2.5/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/omod-2.5/pom.xml b/omod-2.5/pom.xml index 46a750846..8ac004901 100644 --- a/omod-2.5/pom.xml +++ b/omod-2.5/pom.xml @@ -3,7 +3,7 @@ webservices.rest org.openmrs.module - 2.44.0-SNAPSHOT + 2.45.0-SNAPSHOT 4.0.0 From ceff5c5f7053b4589d1f5d819aba2ba4e43764e8 Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Fri, 20 Sep 2024 22:40:33 +0300 Subject: [PATCH 14/15] add override for shouldGetAll() to one that throws an exception. --- .../openmrs2_5/OrderController2_5Test.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderController2_5Test.java b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderController2_5Test.java index 535f19a01..e8d71aacb 100644 --- a/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderController2_5Test.java +++ b/omod-2.5/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_5/OrderController2_5Test.java @@ -12,11 +12,16 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.function.ThrowingRunnable; import org.openmrs.Order; import org.openmrs.api.OrderService; import org.openmrs.api.context.Context; +import org.openmrs.module.webservices.rest.SimpleObject; +import org.openmrs.module.webservices.rest.test.Util; +import org.openmrs.module.webservices.rest.web.response.ResponseException; import org.openmrs.module.webservices.rest.web.v1_0.resource.RestTestConstants2_5; import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; +import org.springframework.web.bind.annotation.RequestMethod; import java.util.List; @@ -41,7 +46,7 @@ public String getURI() { */ @Override public String getUuid() { - return RestTestConstants2_5.ORDER_ATTRIBUTE_UUID; + return RestTestConstants2_5.ORDER_UUID; } /** @@ -68,4 +73,14 @@ public void shouldCreateOrderWithAttribute() throws Exception { Assert.assertEquals(orderList.get(0).getAttributes().iterator().next().getAttributeType().getUuid(),"c0de4f5c-6626-418e-9f4f-5396a31e68fb"); } + @Test + @Override + public void shouldGetAll() { + ThrowingRunnable throwingRunnable= () -> { + handle(request(RequestMethod.GET, getURI())); + }; + Assert.assertThrows(ResponseException.class,throwingRunnable); + } + + } From 8ef2d272c6928c20000b4390268a46c2581f0c00 Mon Sep 17 00:00:00 2001 From: slubwama1 Date: Sat, 21 Sep 2024 00:38:50 +0300 Subject: [PATCH 15/15] clean pom --- omod-2.5/pom.xml | 44 ++++---------------------------------------- 1 file changed, 4 insertions(+), 40 deletions(-) diff --git a/omod-2.5/pom.xml b/omod-2.5/pom.xml index d85aa9f14..9c2d75886 100644 --- a/omod-2.5/pom.xml +++ b/omod-2.5/pom.xml @@ -1,16 +1,14 @@ - 4.0.0 - org.openmrs.module webservices.rest + org.openmrs.module 2.45.0-SNAPSHOT + 4.0.0 webservices.rest-omod-2.5 - jar Rest Web Services 2.5 OMOD - OpenMRS module project for Rest Web Services 2.5.0 @@ -33,34 +31,6 @@ test - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.8 - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.8 - ${project.parent.version} - tests - test - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.9 - ${project.parent.version} - - - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-1.9 - ${project.parent.version} - tests - test - - ${project.parent.groupId} ${project.parent.artifactId}-omod-1.10 @@ -95,14 +65,6 @@ ${project.parent.version} - - ${project.parent.groupId} - ${project.parent.artifactId}-omod-2.0 - ${project.parent.version} - tests - test - - ${project.parent.groupId} ${project.parent.artifactId}-omod-2.2 @@ -144,6 +106,7 @@ tests test + ${project.parent.groupId} ${project.parent.artifactId}-omod-2.0 @@ -151,6 +114,7 @@ tests test + org.openmrs.api openmrs-api