Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTWS-629 - Add new resource PersonNameResource2_1 #298

Merged
merged 1 commit into from
Sep 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_11.PersonResource1_11;
import org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PersonNameResource1_8;

@SubResource(parent = PersonResource1_11.class, path = "name", supportedClass = PersonName.class, supportedOpenmrsVersions = {
"2.0.*", "2.1.*" })
@SubResource(parent = PersonResource1_11.class, path = "name", supportedClass = PersonName.class, supportedOpenmrsVersions = { "2.0.*" })
public class PersonNameResource2_0 extends PersonNameResource1_8 {

@Override
Expand Down
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;
}
}
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());
}
}