Java tutorial
/** * The contents of this file are subject to the OpenMRS Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://license.openmrs.org * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * Copyright (C) OpenMRS, LLC. All Rights Reserved. */ package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_10.controller.openmrs1_10; import static org.hamcrest.Matchers.hasItems; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import java.util.Arrays; import java.util.List; import org.apache.commons.beanutils.PropertyUtils; import org.junit.Before; import org.junit.Test; import org.openmrs.CareSetting; 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.RestConstants; import org.openmrs.module.webservices.rest.web.RestTestConstants1_10; import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.web.bind.annotation.RequestMethod; /** * Tests CRUD operations for {@link org.openmrs.CareSetting}s via web service calls */ public class CareSettingController1_10Test extends MainResourceControllerTest { private OrderService service; @Override public String getURI() { return "caresetting"; } @Override public String getUuid() { return RestTestConstants1_10.CARE_SETTING_UUID; } @Override public long getAllCount() { return service.getCareSettings(false).size(); } @Before public void before() { this.service = Context.getOrderService(); } @Test public void shouldGetAnCareSettingByUuid() throws Exception { SimpleObject result = deserialize(handle(newGetRequest(getURI() + "/" + getUuid()))); CareSetting expectedCareSetting = service.getCareSettingByUuid(getUuid()); assertEquals(expectedCareSetting.getUuid(), PropertyUtils.getProperty(result, "uuid")); assertEquals(expectedCareSetting.getName(), PropertyUtils.getProperty(result, "name")); assertEquals(expectedCareSetting.getCareSettingType().name(), PropertyUtils.getProperty(result, "careSettingType")); assertEquals(expectedCareSetting.getDescription(), PropertyUtils.getProperty(result, "description")); assertEquals(expectedCareSetting.isRetired(), PropertyUtils.getProperty(result, "retired")); assertNull(PropertyUtils.getProperty(result, "auditInfo")); } @Test public void shouldGetACareSettingByName() throws Exception { final String name = "outpatient"; SimpleObject result = deserialize(handle(newGetRequest(getURI() + "/" + name))); CareSetting expectedCareSetting = service.getCareSettingByName(name); assertEquals(expectedCareSetting.getUuid(), PropertyUtils.getProperty(result, "uuid")); assertEquals(expectedCareSetting.getName(), PropertyUtils.getProperty(result, "name")); } @Test public void shouldListAllCareSettings() throws Exception { SimpleObject result = deserialize(handle(newGetRequest(getURI()))); assertNotNull(result); assertEquals(getAllCount(), Util.getResultsSize(result)); } @Test public void shouldListAllCareSettingsIncludingRetiredOnesIfIncludeAllIsSetToTrue() throws Exception { SimpleObject result = deserialize(handle(newGetRequest(getURI(), new Parameter("includeAll", "true")))); assertNotNull(result); assertEquals(service.getCareSettings(true).size(), Util.getResultsSize(result)); } @Test public void shouldReturnTheAuditInfoForTheFullRepresentation() throws Exception { MockHttpServletRequest req = request(RequestMethod.GET, getURI() + "/" + getUuid()); req.addParameter(RestConstants.REQUEST_PROPERTY_FOR_REPRESENTATION, RestConstants.REPRESENTATION_FULL); SimpleObject result = deserialize(handle(req)); assertNotNull(PropertyUtils.getProperty(result, "auditInfo")); } @Test public void shouldSearchAndReturnAListOfCareSettingsMatchingTheQueryString() throws Exception { MockHttpServletRequest req = request(RequestMethod.GET, getURI()); req.addParameter("q", "out"); SimpleObject result = deserialize(handle(req)); assertEquals(1, Util.getResultsSize(result)); assertEquals(getUuid(), PropertyUtils.getProperty(Util.getResultsList(result).get(0), "uuid")); req.removeAllParameters(); req.addParameter("q", "pati"); result = deserialize(handle(req)); assertEquals(2, Util.getResultsSize(result)); List<String> uuids = Arrays.asList( new String[] { PropertyUtils.getProperty(Util.getResultsList(result).get(0), "uuid").toString(), PropertyUtils.getProperty(Util.getResultsList(result).get(1), "uuid").toString() }); assertThat(uuids, hasItems(getUuid(), "2ed1e57d-9f18-41d3-b067-2eeaf4b30fb2")); } }