@@ -13,23 +13,17 @@
*/
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.openmrs.*;
import org.openmrs.Concept;
import org.openmrs.ConceptAnswer;
import org.openmrs.ConceptClass;
import org.openmrs.ConceptDatatype;
import org.openmrs.ConceptDescription;
import org.openmrs.ConceptMap;
import org.openmrs.ConceptName;
import org.openmrs.ConceptNumeric;
import org.openmrs.ConceptSearchResult;
import org.openmrs.Drug;
import org.openmrs.api.ConceptService;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.helper.HibernateCollectionHelper;
@@ -56,6 +50,19 @@
import org.openmrs.module.webservices.rest.web.response.ResponseException;
import org.openmrs.util.LocaleUtility;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;

/**
* {@link Resource} for {@link Concept}, supporting standard CRUD operations
*/
@@ -340,27 +347,30 @@ public void remove(ConceptDescription item) {
}
}.set(descriptions);
}

/**
* It's needed, because of ConversionException: Don't know how to handle collection class:
* interface java.util.Collection
*
* @param instance
* @param mappings
*/
* It's needed, because of ConversionException: Don't know how to handle collection class:
* interface java.util.Collection
*
* @param instance
* @param mappings
*/
@PropertySetter("mappings")
public static void setMappings(Concept instance, List<ConceptMap> mappings) {
instance.setConceptMappings(mappings);
instance.getConceptMappings().clear();
for (ConceptMap map : mappings) {
instance.addConceptMapping(map);
}
}

@PropertyGetter("mappings")
public static List<ConceptMap> getMappings(Concept instance) {
return new ArrayList<ConceptMap>(instance.getConceptMappings());
}

/**
* Gets the display name of the Concept delegate
*
*
* @param instance the delegate instance to get the display name off
*/
@PropertyGetter("display")
@@ -176,7 +176,8 @@ public void purge(Obs delegate, RequestContext context) throws ResponseException
*/
@Override
public Obs save(Obs delegate) {
return Context.getObsService().saveObs(delegate, "REST web service");
Obs savedObs = Context.getObsService().saveObs(delegate, "REST web service");
return Context.getObsService().getObs(savedObs.getId());
}

/**
@@ -13,25 +13,18 @@
*/
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs1_9;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.openmrs.Concept;
import org.openmrs.ConceptAnswer;
import org.openmrs.ConceptMap;
import org.openmrs.ConceptName;
import org.openmrs.ConceptReferenceTerm;
import org.openmrs.ConceptSet;
import org.openmrs.Drug;
import org.openmrs.api.APIException;
@@ -48,14 +41,27 @@
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Locale;
import java.util.Map;


import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

/**
* Tests functionality of {@link ConceptController}. This does not use @should annotations because
* the controller inherits those methods from a subclass
*/
public class ConceptController1_8Test extends MainResourceControllerTest {

private ConceptService service;

@Before
public void before() {
this.service = Context.getConceptService();
@@ -392,6 +398,44 @@ public void shouldRemoveAnswersFromConcept() throws Exception {
Assert.assertEquals(2, concept.getAnswers().size());
}

@Test
public void shouldSetMappingsOnConcept() throws Exception {
//before adding
Concept concept = service.getConceptByUuid(getUuid());
assertThat(concept.getConceptMappings().size(), is(0));

//add one mapping
MockHttpServletRequest request = request(RequestMethod.POST, getURI() + "/" + getUuid());
ConceptReferenceTerm referenceTerm = service.getAllConceptReferenceTerms().get(0);
String mapTypeUuid = service.getDefaultConceptMapType().getUuid();
String json = "{ \"mappings\": [{\"conceptReferenceTerm\":\""+referenceTerm.getUuid()+"\",\"conceptMapType\":\""+mapTypeUuid+"\"}]}";
request.setContent(json.getBytes());

handle(request);

concept = service.getConceptByUuid(getUuid());
assertThat(concept.getConceptMappings().size(), is(1));
assertThat(concept.getConceptMappings(), hasItem(hasTerm(referenceTerm)));

//set mappings to empty
MockHttpServletRequest requestEmpty = request(RequestMethod.POST, getURI() + "/" + getUuid());
String jsonEmpty = "{ \"mappings\": []}";
requestEmpty.setContent(jsonEmpty.getBytes());

handle(requestEmpty);

assertThat(concept.getConceptMappings().size(), is(0));
}

private Matcher<ConceptMap> hasTerm(final ConceptReferenceTerm term) {
return new FeatureMatcher<ConceptMap, ConceptReferenceTerm >(equalTo(term), "conceptReferenceTerm", "conceptReferenceTerm") {
@Override
protected ConceptReferenceTerm featureValueOf(final ConceptMap actual) {
return actual.getConceptReferenceTerm();
}
};
}

@Test
public void shouldReturnDefaultAndSelfLinkForCustomUuid() throws Exception {
String conceptUuid = "95312123-e0c2-466d-b6b1-cb6e990d0d65";