-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RESTWS-629 - Add new resource PersonNameResource2_1 (#298)
* Add new resource PersonNameResource2_1 * Add test PersonNameController2_1Test
- Loading branch information
Showing
3 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...g/openmrs/module/webservices/rest/web/v1_0/resource/openmrs2_1/PersonNameResource2_1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 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_1; | ||
|
||
import io.swagger.models.Model; | ||
import org.openmrs.PersonName; | ||
import org.openmrs.module.webservices.rest.web.annotation.SubResource; | ||
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.v1_0.resource.openmrs1_11.PersonResource1_11; | ||
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs2_0.PersonNameResource2_0; | ||
|
||
@SubResource(parent = PersonResource1_11.class, path = "name", supportedClass = PersonName.class, supportedOpenmrsVersions = { "2.1.*" }) | ||
public class PersonNameResource2_1 extends PersonNameResource2_0 { | ||
|
||
@Override | ||
public DelegatingResourceDescription getCreatableProperties() { | ||
DelegatingResourceDescription resourceDescription = super.getCreatableProperties(); | ||
resourceDescription.getProperties().get("familyName").setRequired(false); | ||
return resourceDescription; | ||
} | ||
|
||
@Override | ||
public Model getCREATEModel(Representation rep) { | ||
Model model = super.getCREATEModel(rep); | ||
model.getProperties().get("familyName").setRequired(false); | ||
return model; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...s/module/webservices/rest/web/v1_0/controller/openmrs2_1/PersonNameController2_1Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* 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_1; | ||
|
||
import org.apache.commons.beanutils.PropertyUtils; | ||
import org.codehaus.jackson.map.ObjectMapper; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openmrs.api.PersonService; | ||
import org.openmrs.api.context.Context; | ||
import org.openmrs.module.webservices.rest.SimpleObject; | ||
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.openmrs.module.webservices.rest.web.RestTestConstants1_8.PERSON_NAME_UUID; | ||
import static org.openmrs.module.webservices.rest.web.RestTestConstants1_8.PERSON_UUID; | ||
|
||
/** | ||
* Tests functionality of {@link PersonNameController}. | ||
*/ | ||
public class PersonNameController2_1Test extends MainResourceControllerTest { | ||
|
||
private PersonService service; | ||
|
||
@Before | ||
public void before() { | ||
this.service = Context.getPersonService(); | ||
} | ||
|
||
/** | ||
* @see MainResourceControllerTest#getURI() | ||
*/ | ||
@Override | ||
public String getURI() { | ||
return "person/" + PERSON_UUID + "/name"; | ||
} | ||
|
||
/** | ||
* @see MainResourceControllerTest#getAllCount() | ||
*/ | ||
@Override | ||
public long getAllCount() { | ||
return service.getPersonByUuid(PERSON_UUID).getNames().size(); | ||
} | ||
|
||
/** | ||
* @see MainResourceControllerTest#getUuid() | ||
*/ | ||
@Override | ||
public String getUuid() { | ||
return PERSON_NAME_UUID; | ||
} | ||
|
||
@Test | ||
public void shouldAddNameToPerson() throws Exception { | ||
long originalCount = getAllCount(); | ||
|
||
SimpleObject personName = new SimpleObject(); | ||
personName.add("givenName", "name1"); | ||
|
||
String json = new ObjectMapper().writeValueAsString(personName); | ||
|
||
MockHttpServletRequest req = request(RequestMethod.POST, getURI()); | ||
req.setContent(json.getBytes()); | ||
|
||
SimpleObject newPersonName = deserialize(handle(req)); | ||
|
||
Assert.assertNotNull(PropertyUtils.getProperty(newPersonName, "uuid")); | ||
Assert.assertEquals(originalCount + 1, getAllCount()); | ||
} | ||
} |