Skip to content

Commit

Permalink
Trunk 5835:OrderSetAttribute Implementation (#3397)
Browse files Browse the repository at this point in the history
* TRUNK-5835:Adding tests

* TRUNK-5835:fixing tests docs

* TRUNK-5835:fixing tests docs

* TRUNK-5835:Add formats

* TRUNK-5835:fixing tests

* TRUNK-5835:fixing tests cases

* TRUNK-5835:fixing tests cases

* TRUNK-5835:fixing tests cases

* TRUNK-5835:fixing tests cases
  • Loading branch information
gitcliff committed Jul 30, 2020
1 parent cc00b32 commit 4f0eaad
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 14 deletions.
36 changes: 22 additions & 14 deletions api/src/main/java/org/openmrs/api/OrderSetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ public interface OrderSetService extends OpenmrsService {
OrderSetMember getOrderSetMemberByUuid(String uuid);

/**
* @param uuid
* Get order set attribute by uuid
*
* @param uuid specifies the order set attribute uuid
* @return the {@link OrderSetAttribute} with the given uuid
* @since 2.4.0
* @should get the order set attribute with the given uuid
Expand All @@ -120,6 +122,8 @@ public interface OrderSetService extends OpenmrsService {
OrderSetAttribute getOrderSetAttributeByUuid(String uuid);

/**
* Get all order set attribute types
*
* @return all {@link OrderSetAttributeType}s
* @since 2.4.0
* @should return all orderSet attribute types including retired ones
Expand All @@ -128,7 +132,9 @@ public interface OrderSetService extends OpenmrsService {
List<OrderSetAttributeType> getAllOrderSetAttributeTypes();

/**
* @param id
* Get order set attribute type from the database by a given internal id
*
* @param id specifies the set attribute type id
* @return the {@link OrderSetAttributeType} with the given internal id
* @since 2.4.0
* @should return the orderSet attribute type with the given id
Expand All @@ -138,7 +144,9 @@ public interface OrderSetService extends OpenmrsService {
OrderSetAttributeType getOrderSetAttributeType(Integer id);

/**
* @param uuid
* Get order set attribute type by uuid
*
* @param uuid specifies the order set attribute type uuid
* @return the {@link OrderSetAttributeType} with the given uuid
* @since 2.4.0
* @should return the orderSet attribute type with the given uuid
Expand All @@ -148,9 +156,9 @@ public interface OrderSetService extends OpenmrsService {
OrderSetAttributeType getOrderSetAttributeTypeByUuid(String uuid);

/**
* Creates or updates the given orderSet attribute type in the database
* Creates or updates the given order set attribute type
*
* @param orderSetAttributeType
* @param orderSetAttributeType the order set attribute type to save
* @return the OrderSetAttributeType created/saved
* @since 2.4.0
* @should create a new orderSet attribute type
Expand All @@ -160,9 +168,9 @@ public interface OrderSetService extends OpenmrsService {
OrderSetAttributeType saveOrderSetAttributeType(OrderSetAttributeType orderSetAttributeType);

/**
* Retires the given orderSet attribute type in the database
* Retires the given order set attribute type
*
* @param orderSetAttributeType
* @param orderSetAttributeType specifies the order set attribute type to be retired
* @return the orderSetAttribute retired
* @since 2.4.0
* @should retire a orderSet attribute type
Expand All @@ -171,9 +179,9 @@ public interface OrderSetService extends OpenmrsService {
OrderSetAttributeType retireOrderSetAttributeType(OrderSetAttributeType orderSetAttributeType, String reason);

/**
* Restores a orderSet attribute type that was previous retired in the database
* Restores an order set attribute type that was previous retired
*
* @param orderSetAttributeType
* @param orderSetAttributeType the order set attribute type to be un-retired
* @return the OrderSetAttributeType unretired
* @since 2.4.0
* @should unretire a retired orderSet attribute type
Expand All @@ -182,19 +190,19 @@ public interface OrderSetService extends OpenmrsService {
OrderSetAttributeType unretireOrderSetAttributeType(OrderSetAttributeType orderSetAttributeType);

/**
* Completely removes a orderSet attribute type from the database
* Completely removes an order set attribute type
*
* @param orderSetAttributeType
* @param orderSetAttributeType the order set attribute type to be purged
* @since 2.4.0
* @should completely remove a orderSet attribute type
* @should completely remove an order set attribute type
*/
@Authorized(PrivilegeConstants.PURGE_ORDER_SET_ATTRIBUTE_TYPES)
void purgeOrderSetAttributeType(OrderSetAttributeType orderSetAttributeType);

/**
* Retrieves a OrderSetAttributeType object based on the name provided
* Retrieves an order set attribute type object based on the name provided
*
* @param orderSetAttributeTypeName
* @param orderSetAttributeTypeName fetches a given order set attribute type by name
* @return the {@link OrderSetAttributeType} with the specified name
* @since 2.4.0
* @should return the orderSet attribute type with the specified name
Expand Down
126 changes: 126 additions & 0 deletions api/src/test/java/org/openmrs/api/OrderSetServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -26,9 +27,11 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openmrs.OrderSet;
import org.openmrs.OrderSetAttributeType;
import org.openmrs.OrderSetMember;
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.customdatatype.datatype.FreeTextDatatype;
import org.openmrs.test.jupiter.BaseContextSensitiveTest;

public class OrderSetServiceTest extends BaseContextSensitiveTest {
Expand All @@ -41,6 +44,8 @@ public class OrderSetServiceTest extends BaseContextSensitiveTest {

protected static final String ORDER_SET = "org/openmrs/api/include/OrderSetServiceTest-general.xml";

protected static final String ORDER_SET_ATTRIBUTES = "org/openmrs/api/include/OrderSetServiceTest-attributes.xml";


/**
* Run this before each unit test in this class. The "@Before" method in
Expand Down Expand Up @@ -350,4 +355,125 @@ private OrderSet orderSetBuilder(boolean orderSetRetired, boolean orderSetMember
orderSet.setRetired(orderSetRetired);
return orderSet;
}


/**
* @see OrderSetService#getOrderSetAttributeByUuid(String)
*/
@Test
public void getOrderSetAttributeByUuid_shouldGetTheOrderSetAttributeWithTheGivenUuid() {
executeDataSet(ORDER_SET_ATTRIBUTES);
OrderSetService service = Context.getOrderSetService();
assertEquals("2011-04-25",service.getOrderSetAttributeByUuid("3a4bdb18-6faa-22e0-8414-001e376eb68e").getValueReference());
}

/**
* @see OrderSetService#getOrderSetAttributeByUuid(String)
*/
@Test
public void getOrderSetAttributeByUuid_shouldReturnNullIfNoOrderSetAttributeHasTheGivenUuid() {
executeDataSet(ORDER_SET_ATTRIBUTES);
OrderSetService service = Context.getOrderSetService();
assertNull(service.getOrderSetAttributeByUuid("not-a-uuid"));
}

/**
* @see OrderSetService#getOrderSetAttributeTypeByUuid(String)
*/
@Test
public void getOrderSetAttributeTypeByUuid_shouldReturnTheOrderSetAttributeTypeWithTheGivenUuid() {
executeDataSet(ORDER_SET_ATTRIBUTES);
assertEquals("Audit Date", Context.getOrderSetService().getOrderSetAttributeTypeByUuid(
"8516cc50-6f9f-33e0-8414-001e648eb67e").getName());
}

/**
* @see OrderSetService#getOrderSetAttributeTypeByUuid(String)
*/
@Test
public void getOrderSetAttributeTypeByUuid_shouldReturnNullIfNoOrderSetAttributeTypeExistsWithTheGivenUuid() {
executeDataSet(ORDER_SET_ATTRIBUTES);
assertNull(Context.getOrderSetService().getOrderSetAttributeTypeByUuid("not-a-uuid"));
}

/**
* @see OrderSetService#purgeOrderSetAttributeType(OrderSetAttributeType)
*/
@Test
public void purgeOrderSetAttributeType_shouldCompletelyRemoveAOrderSetAttributeType() {
executeDataSet(ORDER_SET_ATTRIBUTES);
int initialOrderSetAttributeTypesCount = Context.getOrderSetService().getAllOrderSetAttributeTypes().size();
Context.getOrderSetService().purgeOrderSetAttributeType(Context.getOrderSetService().getOrderSetAttributeType(2));
assertEquals(initialOrderSetAttributeTypesCount - 1, Context.getOrderSetService().getAllOrderSetAttributeTypes().size());
}

/**
* @see OrderSetService#retireOrderSetAttributeType(OrderSetAttributeType,String)
*/
@Test
public void retireOrderSetAttributeType_shouldRetireAOrderSetAttributeType() {
executeDataSet(ORDER_SET_ATTRIBUTES);
OrderSetAttributeType orderSetAttributeType = Context.getOrderSetService().getOrderSetAttributeType(1);
assertFalse(orderSetAttributeType.getRetired());
assertNull(orderSetAttributeType.getRetiredBy());
assertNull(orderSetAttributeType.getDateRetired());
assertNull(orderSetAttributeType.getRetireReason());
Context.getOrderSetService().retireOrderSetAttributeType(orderSetAttributeType, "for testing");
orderSetAttributeType = Context.getOrderSetService().getOrderSetAttributeType(1);
assertTrue(orderSetAttributeType.getRetired());
assertNotNull(orderSetAttributeType.getRetiredBy());
assertNotNull(orderSetAttributeType.getDateRetired());
assertEquals("for testing", orderSetAttributeType.getRetireReason());
}

/**
* @see OrderSetService#saveOrderSetAttributeType(OrderSetAttributeType)
*/
@Test
public void saveOrderSetAttributeType_shouldCreateANewOrderSetAttributeType() {
executeDataSet(ORDER_SET_ATTRIBUTES);
int initialOrderSetAttributeTypesCount = Context.getOrderSetService().getAllOrderSetAttributeTypes().size();
OrderSetAttributeType orderSetAttributeType = new OrderSetAttributeType();
orderSetAttributeType.setName("Another one");
orderSetAttributeType.setDatatypeClassname(FreeTextDatatype.class.getName());
Context.getOrderSetService().saveOrderSetAttributeType(orderSetAttributeType);
assertNotNull(orderSetAttributeType.getId());
assertEquals(initialOrderSetAttributeTypesCount + 1, Context.getOrderSetService().getAllOrderSetAttributeTypes().size());
}

/**
* @see OrderSetService#saveOrderSetAttributeType(OrderSetAttributeType)
*/
@Test
public void saveOrderSetAttributeType_shouldEditAnExistingOrderSetAttributeType() {
executeDataSet(ORDER_SET_ATTRIBUTES);
int initialOrderSetAttributeTypesCount = Context.getOrderSetService().getAllOrderSetAttributeTypes().size();
OrderSetService service = Context.getOrderSetService();
OrderSetAttributeType orderSetAttributeType = service.getOrderSetAttributeType(1);
orderSetAttributeType.setName("A new name");
service.saveOrderSetAttributeType(orderSetAttributeType);
assertEquals(initialOrderSetAttributeTypesCount, service.getAllOrderSetAttributeTypes().size());
assertEquals("A new name", service.getOrderSetAttributeType(1).getName());
}

/**
* @see OrderSetService#unretireOrderSetAttributeType(OrderSetAttributeType)
*/
@Test
public void unretireOrderSetAttributeType_shouldUnretireARetiredOrderSetAttributeType() {
executeDataSet(ORDER_SET_ATTRIBUTES);
OrderSetService service = Context.getOrderSetService();
OrderSetAttributeType orderSetAttributeType = service.getOrderSetAttributeType(2);
assertTrue(orderSetAttributeType.getRetired());
assertNotNull(orderSetAttributeType.getDateRetired());
assertNotNull(orderSetAttributeType.getRetiredBy());
assertNotNull(orderSetAttributeType.getRetireReason());
service.unretireOrderSetAttributeType(orderSetAttributeType);
assertFalse(orderSetAttributeType.getRetired());
assertNull(orderSetAttributeType.getDateRetired());
assertNull(orderSetAttributeType.getRetiredBy());
assertNull(orderSetAttributeType.getRetireReason());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->

<dataset>
<order_set order_set_id="1" name="Order_Set_1" description="Order_Set_1_desc" operator="ALL" creator="1" date_created="2008-08-15 15:49:04.0" retired="0" uuid="2d3fb1d0-ae06-22e3-a5e2-0140211c1001" />
<order_set_attribute_type order_set_attribute_type_id="1" name="Audit Date" datatype="org.openmrs.customdatatype.datatype.DateDatatype" uuid="8516cc50-6f9f-33e0-8414-001e648eb67e" creator="1" date_created="2005-01-01 00:00:00.0" min_occurs="0" retired="false" />
<order_set_attribute_type order_set_attribute_type_id="2" name="A Date We Don't Care About" datatype="org.openmrs.customdatatype.datatype.DateDatatype" uuid="9545cc50-6f9f-55e0-8414-001e378eb69f" creator="1" date_created="2005-01-01 00:00:00.0" min_occurs="0" retired="true" retired_by="1" date_retired="2005-01-01 02:00:00.0" retire_reason="for fun and profit" />
<order_set_attribute order_set_attribute_id="1" order_set_id="1" attribute_type_id="1" value_reference="2011-04-25" uuid="3a4bdb18-6faa-22e0-8414-001e376eb68e" creator="1" date_created="2005-01-01 00:00:00.0" voided="false" />
</dataset>

0 comments on commit 4f0eaad

Please sign in to comment.