Commit df07a9ad authored by Matija Obreza's avatar Matija Obreza

API updates

parent c9483a89
......@@ -30,6 +30,7 @@ import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.genesys2.client.oauth.api.GenesysApi;
import org.genesys2.client.oauth.api.accession.AccessionJson;
import org.scribe.builder.ServiceBuilder;
import org.scribe.exceptions.OAuthConnectionException;
import org.scribe.exceptions.OAuthException;
......@@ -40,7 +41,10 @@ import org.scribe.model.Verb;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
......@@ -59,7 +63,7 @@ public class GenesysClient {
private static final String SCOPE = "read,write";
/** The mapper. */
private static ObjectMapper mapper = new ObjectMapper();
private static ObjectMapper objectMapper = new ObjectMapper();
/** The service. */
private OAuthService service;
......@@ -79,6 +83,13 @@ public class GenesysClient {
/** The api secret. */
private String apiSecret;
static {
objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
objectMapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);
objectMapper.setSerializationInclusion(Include.NON_NULL);
}
/**
* Instantiates a new genesys client.
*/
......@@ -364,7 +375,7 @@ public class GenesysClient {
* @return the object node
*/
public static ObjectNode makeAid3(String instCode, String genus, String acceNumb) {
ObjectNode json = mapper.createObjectNode();
ObjectNode json = objectMapper.createObjectNode();
json.put("instCode", instCode);
json.put("acceNumb", acceNumb);
json.put("genus", genus);
......@@ -372,7 +383,7 @@ public class GenesysClient {
}
/**
* Update mls.
* Update MLS status of accessions
*
* @param instCode
* the inst code
......@@ -381,103 +392,64 @@ public class GenesysClient {
* @return the string
* @throws GenesysApiException
* the genesys api exception
* @throws JsonProcessingException
* @throws PleaseRetryException
*
* @deprecated Please use {@link #updateAccessions(String, Collection)} with
* only the instCode, acceNumb, (genus, ) and mlsStat provided.
*/
public String updateMLS(String instCode, Collection<ObjectNode> accns) throws GenesysApiException {
ArrayNode arr = toArrayNode(accns);
_log.debug("Sending: " + arr);
return query(Verb.PUT, "/acn/" + instCode + "/update", null, arr.toString());
}
/**
* Accession exists.
*
* @param instCode
* the inst code
* @param accns
* the accns
* @return the string
* @throws GenesysApiException
* the genesys api exception
*/
public String accessionExists(String instCode, Collection<ObjectNode> accns) throws GenesysApiException {
ArrayNode arr = toArrayNode(accns);
_log.debug("Sending: " + arr);
return query(Verb.PUT, "/acn/" + instCode + "/check", null, arr.toString());
}
/**
* Update accessions.
*
* @param instCode
* the inst code
* @param accns
* the accns
* @return the string
* @throws GenesysApiException
* the genesys api exception
* @throws InterruptedException
* the interrupted exception
*/
public String updateAccessions(String instCode, Collection<ObjectNode> accns) throws GenesysApiException,
InterruptedException {
@Deprecated
public String updateMLS(String instCode, Collection<ObjectNode> accns) throws GenesysApiException,
PleaseRetryException, JsonProcessingException {
if (accns == null || accns.size() == 0) {
return null;
}
return updateAccessions(instCode, toArrayNode(accns));
}
/**
* To array node.
*
* @param accns
* the accns
* @return the array node
*/
public static ArrayNode toArrayNode(Collection<ObjectNode> accns) {
ArrayNode arr = mapper.createArrayNode();
for (ObjectNode accn : accns) {
arr.add(accn);
}
return arr;
_log.debug("Sending: " + accns);
return query(Verb.PUT, "/acn/" + instCode + "/update", null, objectMapper.writeValueAsString(accns));
}
/**
* Update accessions.
*
* Update accession information with new values provided in the JSON string.
* In case of {@link PleaseRetryException}, this method will attempt to
* re-send the data 5 times before giving up.
*
* @param instCode
* the inst code
* @param arr
* the arr
* @return the string
* @throws OAuthAuthenticationException
* the o auth authentication exception
* the WIEWS institute code
* @param jsonAccessionList
* the JSON array of accessions
* @return "OK"
* @throws GenesysApiException
* the genesys api exception
* when data is not valid
* @throws InterruptedException
* the interrupted exception
* when thread was interrupted during sleep between retries
*
*/
public String updateAccessions(String instCode, ArrayNode arr) throws OAuthAuthenticationException,
GenesysApiException, InterruptedException {
_log.debug("Sending: " + arr);
public String updateAccessions(String instCode, String jsonAccessionList) throws GenesysApiException,
InterruptedException {
for (int retry = 0; retry < 5; retry++) {
try {
return query(Verb.PUT, "/acn/" + instCode + "/upsert", null, arr.toString());
return query(Verb.PUT, "/acn/" + instCode + "/upsert", null, jsonAccessionList);
} catch (PleaseRetryException e) {
long sleepTime = (long) (Math.pow(2, retry) * 100 + Math.pow(2, retry) * 2500 * Math.random());
_log.warn("Retrying PUT after " + sleepTime + " ms.");
Thread.sleep(sleepTime);
}
}
throw new RuntimeException("All retries failed");
}
public String updateAccessions(String instCode, Collection<AccessionJson> accns) throws GenesysApiException,
InterruptedException, JsonProcessingException {
if (accns == null || accns.size() == 0) {
return null;
}
return updateAccessions(instCode, objectMapper.writeValueAsString(accns));
}
/**
* Update organization members.
*
......
......@@ -21,7 +21,7 @@ import org.genesys2.client.oauth.api.accession.Api1Constants.Accession;
import com.fasterxml.jackson.annotation.JsonProperty;
public class AccessionJson {
private long version = 0;
private Long version;
private Long genesysId;
@JsonProperty(value = Accession.INSTCODE)
......@@ -79,11 +79,11 @@ public class AccessionJson {
@JsonProperty(value = Accession.HISTORIC)
private Boolean historic;
public long getVersion() {
public Long getVersion() {
return version;
}
public void setVersion(long l) {
public void setVersion(Long l) {
this.version = l;
}
......
......@@ -43,7 +43,6 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
@RunWith(MockitoJUnitRunner.class)
public class AccessionApiTest {
......@@ -66,11 +65,11 @@ public class AccessionApiTest {
@Before
public void initMocks() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException,
InterruptedException {
InterruptedException, JsonProcessingException {
System.err.println("Initializing mocks");
when(genesysClient.listAccessions(anyString(), anyInt(), Matchers.isNull(String.class))).then(
mockServer.listAccessions());
when(genesysClient.updateAccessions(anyString(), anyCollectionOf(ObjectNode.class))).then(
when(genesysClient.updateAccessions(anyString(), anyCollectionOf(AccessionJson.class))).then(
mockServer.updateAccessions());
}
......@@ -115,19 +114,17 @@ public class AccessionApiTest {
assertThat("Expected empty list", results.size(), is(0));
List<ObjectNode> nodeList = new ArrayList<ObjectNode>();
List<AccessionJson> ajList = new ArrayList<AccessionJson>();
for (int i = 0; i < 4; i++) {
AccessionJson aj = new AccessionJson();
aj.setInstCode(instCode);
aj.setAcceNumb("ACC-" + (i + 1));
aj.setGenus("Genus");
ObjectNode node = (ObjectNode) objectMapper.readTree(objectMapper.writeValueAsString(aj));
nodeList.add(node);
ajList.add(aj);
}
result = genesysClient.updateAccessions(instCode, nodeList);
result = genesysClient.updateAccessions(instCode, ajList);
System.err.println(result);
results = objectMapper.readValue(genesysClient.listAccessions(instCode, 1, null),
......
......@@ -127,32 +127,23 @@ public class ApiTest {
};
}
@SuppressWarnings("deprecation")
@Test(expected = PleaseRetryException.class)
public void testPleaseRetryException() throws OAuthAuthenticationException, PleaseRetryException,
GenesysApiException {
GenesysApiException, JsonProcessingException {
when(genesysClient.updateMLS("INS000", null)).thenThrow(PleaseRetryException.class);
genesysClient.updateMLS("INS000", null);
}
@Test(expected = NullPointerException.class)
public void testNullUpdate() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
when(genesysClient.updateMLS("INS000", null)).thenCallRealMethod();
genesysClient.updateMLS("INS000", null);
}
@Test
public void test2() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException {
when(genesysClient.query(Mockito.isA(Verb.class), anyString(), anyMap(), anyString())).thenCallRealMethod();
when(genesysClient.updateMLS(anyString(), anyCollection())).thenCallRealMethod();
when(genesysClient.query(Mockito.isA(Verb.class), anyString(), anyMapOf(String.class, String.class), anyString())).thenCallRealMethod();
Collection<ObjectNode> list = new ArrayList<ObjectNode>();
list.add(objectMapper.createObjectNode().put(Accession.INSTCODE, "INS000").put(Accession.ACCENUMB, "ACC-1")
.put(Accession.GENUS, "Genus"));
String x = genesysClient.updateMLS("INS000", list);
System.out.println(x);
}
}
......@@ -31,7 +31,6 @@ import org.mockito.stubbing.Answer;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
......@@ -76,7 +75,7 @@ public class MockGenesysServer {
public String answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
String instCode = (String) args[0];
List arrayNode = (List) args[1];
List<?> arrayNode = (List<?>) args[1];
String res = objectMapper.writeValueAsString(upsertAccessions(instCode, arrayNode));
System.err.println("Result: " + res);
......@@ -85,15 +84,14 @@ public class MockGenesysServer {
};
}
protected String upsertAccessions(final String instCode, List nodeList) throws JsonParseException,
protected String upsertAccessions(final String instCode, List<?> nodeList) throws JsonParseException,
JsonMappingException, IOException, GenesysApiException {
List<AccessionJson> accList = instAcc.get(instCode);
if (accList == null) {
instAcc.put(instCode, accList = new ArrayList<AccessionJson>());
}
for (int i = 0; i < nodeList.size(); i++) {
JsonNode node = (JsonNode) nodeList.get(i);
AccessionJson aj = objectMapper.readValue(node.toString(), AccessionJson.class);
AccessionJson aj = (AccessionJson) nodeList.get(i);
AccessionJson existing = findMatch(accList, aj);
merge(accList, existing, aj);
}
......
......@@ -22,9 +22,9 @@ import static org.hamcrest.MatcherAssert.*;
import java.io.IOException;
import org.genesys2.client.oauth.api.accession.AccessionJson;
import org.genesys2.client.oauth.api.accession.GeoJson;
import org.genesys2.client.oauth.api.accession.Api1Constants.Accession;
import org.genesys2.client.oauth.api.accession.Api1Constants.Geo;
import org.genesys2.client.oauth.api.accession.GeoJson;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
......@@ -52,6 +52,7 @@ public class ModelTests {
aj.setGenus("Genus");
JsonNode tree = getJsonTree(aj);
System.err.println(tree.toString());
assertThat("JSON elements count mismatch", tree, JsonNodeMatchers.withSize(3));
assertThat("No INSTCODE", tree.get(Accession.INSTCODE), notNullValue());
assertThat("INSTCODE must be a text node", tree.get(Accession.INSTCODE), JsonNodeMatchers.isTextNode());
......@@ -85,6 +86,7 @@ public class ModelTests {
aj.setStorage(new Integer[] { 1, 2, 3, 4 });
JsonNode tree = getJsonTree(aj);
System.err.println(tree.toString());
assertThat("JSON elements count mismatch", tree, JsonNodeMatchers.withSize(1));
assertThat("STORAGE must be an array", tree.get(Accession.STORAGE), JsonNodeMatchers.isArrayNode());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment