diff --git a/src/main/java/org/genesys2/client/oauth/CLI.java b/src/main/java/org/genesys2/client/oauth/CLI.java deleted file mode 100644 index 1c10757adacecbdc79bf4d61fdad922104e59869..0000000000000000000000000000000000000000 --- a/src/main/java/org/genesys2/client/oauth/CLI.java +++ /dev/null @@ -1,631 +0,0 @@ -/* - * Copyright 2016 Global Crop Diversity Trust - * - * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.genesys2.client.oauth; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map.Entry; -import java.util.Properties; -import java.util.Scanner; -import java.util.Set; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.DoubleNode; -import com.fasterxml.jackson.databind.node.IntNode; -import com.fasterxml.jackson.databind.node.NullNode; -import com.fasterxml.jackson.databind.node.ObjectNode; -import com.fasterxml.jackson.databind.node.TextNode; -import com.github.scribejava.core.model.Verb; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Simple command line interface to Genesys. - * - * @author matijaobreza - */ -public class CLI { - - /** The Constant LOG. */ - private static final Logger LOG = LoggerFactory.getLogger(CLI.class); - - /** The properties file. */ - private File propertiesFile; - - /** The properties. */ - private Properties properties; - - /** The in. */ - private final Scanner in = new Scanner(System.in); - - /** The mapper. */ - private static ObjectMapper mapper = new ObjectMapper(); - - /** The genesys client. */ - private GenesysClient genesysClient; - - /** The ignored fields. */ - private static Set ignoredFields; - - static { - ignoredFields = new HashSet(); - ignoredFields.add("id"); - ignoredFields.add("createdBy"); - ignoredFields.add("createdDate"); - ignoredFields.add("lastModifiedBy"); - ignoredFields.add("lastModifiedDate"); - ignoredFields.add("version"); - } - - /** - * The main method. - * - * @param args the arguments - */ - public static void main(final String[] args) { - LOG.info("Hello World!"); - - final CLI cli = new CLI(); - cli.genesysClient = GenesysClient.build(cli.loadProperties("client.properties")); - cli.run(); - } - - /** - * Run. - */ - private void run() { - checkPreconditions(); - - try { - System.out.println("/me: " + genesysClient.query("/me")); - - properties.put("access.token", genesysClient.getTokens().getAccessToken()); - saveProperties(); - - } catch (final OAuthAuthenticationException e) { - LOG.error("Failed to fetch /me", e); - try { - authenticate(); - } catch (OAuthAuthenticationException e1) { - LOG.error("Failed to authenticate", e1); - return; - } - } catch (final Throwable e) { - LOG.error(e.getMessage(), e); - return; - } - - try { - doWork(); - } catch (final Throwable e) { - LOG.error(e.getMessage(), e); - } - } - - /** - * Do work. - * - * @throws GenesysApiException the genesys api exception - * @throws PleaseRetryException the please retry exception - * @throws IOException Signals that an I/O exception has occurred. - */ - private void doWork() throws GenesysApiException, PleaseRetryException, IOException { - String line = null; - do { - System.out.println("1 Datasets"); - System.out.println("2 Traits"); - System.out.println("3 Crops"); - System.out.println("0 Custom"); - System.out.println("Q QUIT"); - - line = in.nextLine(); - if ("1".equals(line)) { - doDatasets(); - } else if ("2".equals(line)) { - doTraits(); - } else if ("3".equals(line)) { - doCrops(); - } else if ("0".equals(line)) { - doCustom(); - } - } while (!"Q".equalsIgnoreCase(line)); - } - - /** - * Update json data. - * - * @param label the label - * @param n the n - */ - private void updateJsonData(final String label, final JsonNode n) { - if (n.isArray()) { - updateJsonArray(label, (ArrayNode) n); - } else if (n.isObject()) { - updateJsonObject(label, n); - } - } - - /** - * Update json object. - * - * @param label the label - * @param n the n - */ - private void updateJsonObject(final String label, final JsonNode n) { - final Iterator> f = n.fields(); - - while (f.hasNext()) { - final Entry field = f.next(); - System.out.print(label + "." + StringUtils.capitalize(field.getKey()) + ":"); - System.out.println(field.getValue()); - - if (ignoredFields.contains(field.getKey())) { - continue; - } - - if (field.getValue().isObject() || field.getValue().isArray()) { - System.out.println("Edit subobject ?"); - if (StringUtils.equalsIgnoreCase("y", in.nextLine())) { - updateJsonData(label + "." + field.getKey(), field.getValue()); - } - continue; - } - - final String val = in.nextLine(); - if (!field.getValue().isNull() && val.length() == 0) { - continue; - } - if (val.startsWith("i ")) { - field.setValue(new IntNode(Integer.parseInt(val.substring(2)))); - } else if (val.startsWith("d ")) { - field.setValue(new DoubleNode(Double.parseDouble(val.substring(2)))); - } else if (StringUtils.isBlank(val)) { - field.setValue(NullNode.getInstance()); - } else { - field.setValue(new TextNode(val.trim())); - } - } - - System.out.println("Done editing " + label); - } - - /** - * Update json array. - * - * @param label the label - * @param n the n - */ - private void updateJsonArray(final String label, final ArrayNode n) { - System.out.println("Array: " + n); - final ArrayNode na = n.arrayNode(); - String val; - do { - val = in.nextLine().trim(); - if (val.startsWith("i ")) { - na.add(Integer.parseInt(val.substring(2))); - } else if (val.startsWith("d ")) { - na.add(Double.parseDouble(val.substring(2))); - } else if (val.startsWith("o ")) { - try { - final Object o = Class.forName("org.genesys2.client.rest.model." + val.substring(2)).newInstance(); - final JsonNode newNode = mapper.readTree(mapper.writeValueAsString(o)); - System.out.println(newNode); - updateJsonObject(label + "." + val.substring(2), newNode); - na.add(newNode); - } catch (final ClassNotFoundException e) { - System.err.println(e.getMessage()); - } catch (final JsonProcessingException e) { - System.err.println(e.getMessage()); - } catch (final IOException e) { - System.err.println(e.getMessage()); - } catch (final InstantiationException e) { - System.err.println(e.getMessage()); - } catch (final IllegalAccessException e) { - System.err.println(e.getMessage()); - } - } else if (StringUtils.isBlank(val)) { - break; - } else { - na.add(val); - } - } while (StringUtils.isNotBlank(val)); - n.removeAll(); - n.addAll(na); - System.out.println("Done editing array " + label); - } - - /** - * Do custom. - * - * @throws GenesysApiException the genesys api exception - */ - private void doCustom() throws GenesysApiException { - System.out.print("URL: "); - final String url = in.nextLine(); - System.out.print("Method [GET]: "); - final String method = StringUtils.defaultIfBlank(in.nextLine(), "GET"); - System.out.println("JSON: "); - final String data = StringUtils.defaultIfBlank(in.nextLine(), null); - - // Exec - System.out.println(genesysClient.query(Verb.valueOf(method), url, null, data)); - } - - /** - * Do crops. - * - * @throws GenesysApiException the genesys api exception - */ - private void doCrops() throws GenesysApiException { - String line = null; - do { - System.out.println("1 List crops"); - System.out.println("2 Update crop"); - System.out.println("3 Update crop rules"); - System.out.println("0 Back"); - - line = in.nextLine(); - if ("1".equals(line)) { - System.out.println("/crops: " + genesysClient.query("/crops")); - } else if ("2".equals(line)) { - updateCrop(); - } else if ("3".equals(line)) { - updateCropRules(); - } else if ("9".equals(line)) { - System.out.println("/methods: " + genesysClient.query("/methods")); - } else if ("0".equalsIgnoreCase(line)) { - return; - } - } while (!"0".equalsIgnoreCase(line)); - } - - /** - * Update crop rules. - * - * @throws GenesysApiException the genesys api exception - */ - private void updateCropRules() throws GenesysApiException { - System.out.println("Crop shortName: "); - final String shortName = in.nextLine().trim(); - - final String rules = genesysClient.query("/crops/" + shortName + "/rules"); - System.out.println("Crop rules: " + rules); - - final String newRules = StringUtils.defaultIfBlank(in.nextLine().trim(), rules); - - if (StringUtils.equals(newRules, rules)) { - System.out.println("No change."); - return; - } - - ArrayNode arr; - try { - arr = (ArrayNode) mapper.readTree(newRules); - } catch (final Throwable e) { - System.err.println(e.getMessage()); - return; - } - - System.out.println(arr.toString()); - // make a crop - System.out.println("New rules: " + genesysClient.query(Verb.POST, "/crops/" + shortName + "/rules", null, arr.toString())); - } - - /** - * Update crop. - * - * @throws GenesysApiException the genesys api exception - */ - private void updateCrop() throws GenesysApiException { - System.out.println("Crop shortName: "); - final String shortName = in.nextLine().trim(); - - ObjectNode cropJson = null; - try { - cropJson = (ObjectNode) mapper.readTree(genesysClient.getCrop(shortName)); - cropJson.remove("rules"); - } catch (final IOException e) { - System.err.println(e.getMessage()); - } - if (cropJson == null) { - cropJson = mapper.createObjectNode(); - cropJson.put("shortName", shortName); - cropJson.put("name", ""); - cropJson.put("i18n", "{}"); - } - - System.out.println(">> " + cropJson.toString()); - - System.out.println("Name [" + cropJson.get("name").textValue() + "]: "); - cropJson.put("name", StringUtils.defaultIfBlank(in.nextLine().trim(), cropJson.get("name").textValue())); - System.out.println("i18n [" + cropJson.get("i18n").textValue() + "]: "); - cropJson.put("i18n", StringUtils.defaultIfBlank(in.nextLine().trim(), cropJson.get("i18n").textValue())); - - System.out.println(cropJson.toString()); - // make a crop - System.out.println("Put method: " + genesysClient.query(Verb.PUT, "/crops", null, cropJson.toString())); - } - - /** - * Do traits. - * - * @throws GenesysApiException the genesys api exception - */ - private void doTraits() throws GenesysApiException { - String line = null; - do { - System.out.println("1 List parameters"); - System.out.println("2 List my methods"); - System.out.println("9 List all methods"); - System.out.println("3 Add method"); - System.out.println("0 Back"); - - line = in.nextLine(); - if ("1".equals(line)) { - System.out.println("/parameters: " + genesysClient.query("/descriptors")); - } else if ("2".equals(line)) { - System.out.println("/mymethods: " + genesysClient.query("/mymethods")); - } else if ("3".equals(line)) { - addMethod(); - } else if ("9".equals(line)) { - System.out.println("/methods: " + genesysClient.query("/methods")); - } else if ("0".equalsIgnoreCase(line)) { - return; - } - } while (!"0".equalsIgnoreCase(line)); - } - - /** - * Do datasets. - * - * @throws GenesysApiException the genesys api exception - */ - private void doDatasets() throws GenesysApiException { - String line = null; - do { - System.out.println("1 List"); - System.out.println("2 Add"); - System.out.println("3 Add data"); - System.out.println("4 Add RAW"); - System.out.println("0 Back"); - - line = in.nextLine(); - if ("1".equals(line)) { - System.out.println("/datasets: " + genesysClient.query("/datasets")); - } else if ("2".equals(line)) { - addDataset(); - } else if ("3".equals(line)) { - addDatasetData(); - } else if ("4".equals(line)) { - addDatasetRaw(); - } else if ("0".equalsIgnoreCase(line)) { - return; - } - } while (!"0".equalsIgnoreCase(line)); - } - - /** - * Adds the method. - * - * @throws GenesysApiException the genesys api exception - */ - private void addMethod() throws GenesysApiException { - final ObjectNode datasetJson = mapper.createObjectNode(); - System.out.println("Method description: "); - datasetJson.put("description", in.nextLine()); - - System.out.println("UOM: "); - datasetJson.put("unit", StringUtils.defaultIfBlank(in.nextLine(), null)); - - System.out.println("Field name: "); - datasetJson.put("fieldName", StringUtils.defaultIfBlank(in.nextLine(), null)); - - System.out.println("Field type: (0=String, 1=Double, 2=Long)"); - final int fieldType = Integer.parseInt(in.nextLine()); - datasetJson.put("fieldType", fieldType); - - if (fieldType == 0) { - System.out.println("Field size: "); - datasetJson.put("fieldSize", Integer.parseInt(in.nextLine())); - } - - System.out.println("Options: "); - datasetJson.put("options", StringUtils.defaultIfBlank(in.nextLine(), null)); - - System.out.println("Range: "); - datasetJson.put("range", StringUtils.defaultIfBlank(in.nextLine(), null)); - - System.err.println(datasetJson.toString()); - - // make a dataset - System.out.println("Put method: " + genesysClient.query(Verb.PUT, "/methods", null, datasetJson.toString())); - - } - - /** - * Adds the dataset. - * - * @throws GenesysApiException the genesys api exception - */ - private void addDataset() throws GenesysApiException { - final ObjectNode datasetJson = mapper.createObjectNode(); - System.out.println("WIEWS Code: "); - datasetJson.put("institute", in.nextLine()); - - System.out.println("Dataset title: "); - datasetJson.put("title", in.nextLine()); - - System.out.println("Dataset description: "); - datasetJson.put("description", in.nextLine()); - - System.err.println(datasetJson.toString()); - - // make a dataset - System.out.println("Put dataset: " + genesysClient.query(Verb.PUT, "/datasets", null, datasetJson.toString())); - - } - - /** - * Adds the dataset data. - * - * @throws GenesysApiException the genesys api exception - */ - private void addDatasetData() throws GenesysApiException { - final ObjectNode datasetJson = mapper.createObjectNode(); - - System.out.println("Dataset ID: "); - final long datasetId = Long.parseLong(in.nextLine()); - - System.out.println("WIEWS Code: "); - datasetJson.put("instCode", in.nextLine()); - System.out.println("Data data: "); - datasetJson.put("data", in.nextLine()); - - System.err.println(datasetJson.toString()); - - // make a dataset - System.out.println("Put dataset: " + genesysClient.query(Verb.PUT, "/datasets/" + datasetId + "/data", null, datasetJson.toString())); - } - - /** - * Adds the dataset raw. - * - * @throws GenesysApiException the genesys api exception - */ - private void addDatasetRaw() throws GenesysApiException { - System.out.println("Dataset ID: "); - final long datasetId = Long.parseLong(in.nextLine()); - - System.out.println("JSON: "); - final String json = in.nextLine(); - - System.err.println(json); - - // make a dataset - System.out.println("Put dataset: " + genesysClient.query(Verb.PUT, "/datasets/" + datasetId + "/data", null, json)); - } - - /** - * Check preconditions. - */ - private void checkPreconditions() { - boolean restart = false; - if (StringUtils.isBlank(properties.getProperty("client.key"))) { - System.out.print("Provide client key: "); - properties.put("client.key", in.nextLine()); - restart = true; - } - - if (StringUtils.isBlank(properties.getProperty("client.secret"))) { - System.out.print("Provide client secret: "); - properties.put("client.secret", in.nextLine()); - restart = true; - } - - if (StringUtils.isBlank(properties.getProperty("api.url"))) { - System.out.print("Provide API url: "); - properties.put("api.url", in.nextLine()); - restart = true; - } - - if (restart) { - saveProperties(); - LOG.warn("Properties udpated, please restart CLI application"); - System.exit(-1); - } - } - - /** - * Authenticate. - * - * @throws OAuthAuthenticationException - */ - private void authenticate() throws OAuthAuthenticationException { - final String authorizationUrl = genesysClient.getAuthorizationUrl(); - - System.out.println("Authorization URL: \n" + authorizationUrl); - System.out.print("\nVerifier: "); - final String verifierCode = in.nextLine(); - System.out.println(); - - // Trade the Request Token and Verifier for the Access Token - genesysClient.authenticate(verifierCode); - properties.put("access.token", genesysClient.getTokens().getAccessToken()); - - // Get refresh token () - properties.put("refresh.token", genesysClient.getTokens().getRefreshToken()); - - saveProperties(); - } - - /** - * Save properties. - */ - private void saveProperties() { - FileOutputStream fis = null; - try { - fis = new FileOutputStream(propertiesFile); - properties.store(fis, "OAuth client properties"); - } catch (final IOException e) { - LOG.error(e.getMessage(), e); - } finally { - IOUtils.closeQuietly(fis); - } - } - - /** - * Load properties. - * - * @param propertiesFileName the properties file name - * @return - */ - private Properties loadProperties(final String propertiesFileName) { - // .properties file location - propertiesFile = new File(propertiesFileName); - - final Properties properties = new Properties(); - - FileInputStream fis = null; - try { - LOG.info("Loading {}", propertiesFile.getAbsolutePath()); - fis = new FileInputStream(propertiesFile); - properties.load(fis); - // Keep properties - this.properties = properties; - } catch (final FileNotFoundException e) { - LOG.warn(e.getMessage(), e); - } catch (final IOException e) { - LOG.error(e.getMessage(), e); - } finally { - IOUtils.closeQuietly(fis); - } - - return properties; - } - -} diff --git a/src/main/java/org/genesys2/client/oauth/GenesysApiException.java b/src/main/java/org/genesys2/client/oauth/GenesysApiException.java index 4c23f35aafb137b313b9b1c56f9d1c415fa660ac..ff3123f0323945344e5dc88fe7c030c17ff2a5d5 100644 --- a/src/main/java/org/genesys2/client/oauth/GenesysApiException.java +++ b/src/main/java/org/genesys2/client/oauth/GenesysApiException.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/org/genesys2/client/oauth/GenesysClient.java b/src/main/java/org/genesys2/client/oauth/GenesysClient.java index abf26afeaff12ab1282b5b4291132302e382156b..24d29f7a892c24b67d70974bad5b7adfd7f477e2 100644 --- a/src/main/java/org/genesys2/client/oauth/GenesysClient.java +++ b/src/main/java/org/genesys2/client/oauth/GenesysClient.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -305,7 +305,7 @@ public class GenesysClient { * @return the api url */ private String getApiUrl(final String url) { - return baseUrl.concat("/api/v0").concat(url); + return baseUrl.concat("/api/v1").concat(url); } /** @@ -333,19 +333,19 @@ public class GenesysClient { /** * Accession exists. * - * @param instCode the inst code - * @param acceNumb the acce numb + * @param instituteCode the inst code + * @param accessionNumber the acce numb * @param genus the genus * @return the string * @throws GenesysApiException the Genesys API exception */ - public String accessionExists(final String instCode, final String acceNumb, final String genus) throws GenesysApiException { + public String accessionExists(final String instituteCode, final String accessionNumber, final String genus) throws GenesysApiException { try { final HashMap queryString = new HashMap(); - queryString.put("acceNumb", acceNumb); + queryString.put("accessionNumber", accessionNumber); - return query(Verb.GET, new URI(null, null, "/acn/exists/" + instCode + "/" + genus, null).toString(), queryString, null); + return query(Verb.GET, new URI(null, null, "/acn/exists/" + instituteCode + "/" + genus, null).toString(), queryString, null); } catch (final URISyntaxException e) { e.printStackTrace(); return null; @@ -355,90 +355,53 @@ public class GenesysClient { /** * Make aid3. * - * @param instCode the inst code + * @param instituteCode the inst code * @param genus the genus - * @param acceNumb the acce numb + * @param accessionNumber the acce numb * @return the object node */ - public static ObjectNode makeAid3(final String instCode, final String genus, final String acceNumb) { + public static ObjectNode makeAid3(final String instituteCode, final String genus, final String accessionNumber) { final ObjectNode json = objectMapper.createObjectNode(); - json.put("instCode", instCode); - json.put("acceNumb", acceNumb); + json.put("instituteCode", instituteCode); + json.put("accessionNumber", accessionNumber); json.put("genus", genus); return json; } /** - * Update MLS status of accessions. + * Update accessions. * - * @param instCode the inst code + * @param instituteCode the inst code * @param accns the accns * @return the string * @throws GenesysApiException the Genesys API exception - * @throws PleaseRetryException exception indicating the call shold be re-attempted + * @throws InterruptedException the interrupted exception * @throws JsonProcessingException the json processing exception - * @deprecated Please use {@link #updateAccessions(String, Collection)} with only the instCode, - * acceNumb, (genus, ) and mlsStat provided. */ - @Deprecated - public String updateMLS(final String instCode, final Collection accns) throws GenesysApiException, PleaseRetryException, JsonProcessingException { + public String updateAccessions(final String instituteCode, final Collection accns) throws GenesysApiException, InterruptedException, JsonProcessingException { if (accns == null || accns.size() == 0) { return null; } - LOG.debug("Sending: {}", accns); - return query(Verb.PUT, "/acn/" + instCode + "/update", null, objectMapper.writeValueAsString(accns)); - } + final String data = objectMapper.writeValueAsString(accns); - /** - * 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 WIEWS institute code - * @param jsonAccessionList the JSON array of accessions - * @return "OK" - * @throws GenesysApiException when data is not valid - * @throws InterruptedException when thread was interrupted during sleep between retries - * - * @deprecated Will be removed by 1.0.0 release - */ - @Deprecated - public String updateAccessions(final String instCode, final String jsonAccessionList) throws GenesysApiException, InterruptedException { - for (int retry = 0; retry < 5; retry++) { - try { - return query(Verb.PUT, "/acn/" + instCode + "/upsert", null, jsonAccessionList); - } catch (final PleaseRetryException e) { - final long sleepTime = (long) (Math.pow(2, retry) * 100 + Math.pow(2, retry) * 2500 * Math.random()); - LOG.warn("Retrying PUT after {} ms.", sleepTime); - Thread.sleep(sleepTime); - } - } - throw new RuntimeException("All retries failed"); + return updateAccession(instituteCode, data); } /** - * Update accessions. + * Update accession. * - * @param instCode the inst code - * @param accns the accns + * @param instituteCode the institute code + * @param data the data * @return the string * @throws GenesysApiException the Genesys API exception * @throws InterruptedException the interrupted exception - * @throws JsonProcessingException the json processing exception */ - public String updateAccessions(final String instCode, final Collection accns) throws GenesysApiException, InterruptedException, JsonProcessingException { - - if (accns == null || accns.size() == 0) { - return null; - } - - final String data = objectMapper.writeValueAsString(accns); - + public String updateAccession(String instituteCode, String data) throws InterruptedException, GenesysApiException { for (int retry = 0; retry < 5; retry++) { try { - return query(Verb.PUT, "/acn/" + instCode + "/upsert", null, data); + return query(Verb.POST, "/acn/" + instituteCode + "/upsert", null, data); } catch (final PleaseRetryException e) { final long sleepTime = (long) (Math.pow(2, retry) * 100 + Math.pow(2, retry) * 2500 * Math.random()); LOG.warn("Retrying PUT after {} ms.", sleepTime); @@ -448,113 +411,23 @@ public class GenesysClient { throw e; } } + throw new RuntimeException("All retries failed"); } - /** - * Update organization members. - * - * @param organizationSlug the organization slug - * @param institutes the institutes - * @return the string - * @throws GenesysApiException the Genesys API exception - */ - public String updateOrganizationMembers(final String organizationSlug, final ArrayNode institutes) throws GenesysApiException { - LOG.debug("Sending: {}", institutes); - try { - return query(Verb.PUT, "/org/" + organizationSlug + "/set-institutes", null, institutes.toString()); - } catch (final PleaseRetryException e) { - LOG.warn("Retrying PUT after some time..."); - try { - Thread.sleep((long) (1000 * Math.random())); - } catch (final InterruptedException e1) { - e1.printStackTrace(); - } - return query(Verb.PUT, "/org/" + organizationSlug + "/set-institutes", null, institutes.toString()); - } - } - - /** - * Update accession names. - * - * @param instCode the inst code - * @param batch the batch - * @return the string - * @throws GenesysApiException the Genesys API exception - */ - public String updateAccessionNames(final String instCode, final Collection batch) throws GenesysApiException { - LOG.debug("Sending: {}", batch); - try { - return query(Verb.PUT, "/acn/" + instCode + "/names", null, batch.toString()); - } catch (final PleaseRetryException e) { - LOG.warn("Retrying PUT after some time..."); - try { - Thread.sleep((long) (1000 * Math.random())); - } catch (final InterruptedException e1) { - e1.printStackTrace(); - } - LOG.warn("Retrying PUT"); - return query(Verb.PUT, "/acn/" + instCode + "/names", null, batch.toString()); - } - } - - /** - * Delete accessions. - * - * @param instCode the inst code - * @param array the array - * @return the string - * @throws GenesysApiException the Genesys API exception - * - * @deprecated Use {@link #deleteAccessionsByName(String, String)} - */ - @Deprecated - public String deleteAccessions(final String instCode, final ArrayNode array) throws GenesysApiException { - return deleteAccessionsByName(instCode, array.toString()); - } - - /** - * Delete accessions from Genesys by ID-triplet (INSTCODE, ACCENUMB, GENUS). - * - * @param instCode the inst code - * @param jsonAccessionId3List the json accession id3 list - * @return the string - * @throws GenesysApiException the Genesys API exception - */ - public String deleteAccessionsByName(final String instCode, final String jsonAccessionId3List) throws GenesysApiException { - return query(Verb.POST, "/acn/" + instCode + "/delete-named", null, jsonAccessionId3List); - } - - /** - * Delete accession. - * - * @param instCode the inst code - * @param ids the ids - * @return the string - * @throws OAuthAuthenticationException authentication exception - * @throws PleaseRetryException exception indicating the call shold be re-attempted - * @throws GenesysApiException the Genesys API exception - * - * @deprecated Use - */ - @Deprecated - public String deleteAccession(final String instCode, final ArrayNode ids) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException { - return deleteAccessionsByGenesysId(instCode, ids.toString()); - } - /** * Delete accessions from Genesys. * - * @param instCode the inst code + * @param instituteCode the inst code * @param jsonAccessionIdList the json accession id list * @return the string * @throws OAuthAuthenticationException authentication exception * @throws PleaseRetryException exception indicating the call shold be re-attempted * @throws GenesysApiException the Genesys API exception */ - public String deleteAccessionsByGenesysId(final String instCode, final String jsonAccessionIdList) + public String deleteAccessions(final String instituteCode, final String jsonAccessionIdList) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException { - return query(Verb.POST, "/acn/" + instCode + "/delete", null, jsonAccessionIdList); + return query(Verb.POST, "/acn/" + instituteCode + "/delete", null, jsonAccessionIdList); } /** @@ -638,7 +511,7 @@ public class GenesysClient { * @throws GenesysApiException the Genesys API exception */ public String me() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException { - return query("/me"); + return query("/me/profile"); } /** @@ -1043,7 +916,7 @@ public class GenesysClient { /** * List accessions. * - * @param instCode the inst code + * @param instituteCode the inst code * @param page the page * @param query the query * @return the string @@ -1051,18 +924,18 @@ public class GenesysClient { * @throws PleaseRetryException exception indicating the call shold be re-attempted * @throws GenesysApiException the Genesys API exception */ - public String listAccessions(final String instCode, final int page, final String query) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException { + public String listAccessions(final String instituteCode, final int page, final String query) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException { final Map params = new HashMap(); params.put("page", String.valueOf(page)); params.put("query", query); - return query(Verb.GET, "/acn/" + instCode + "/list", params, null); + return query(Verb.GET, "/acn/" + instituteCode + "/list", params, null); } /** * List existing image galleries for INSTCODE. The response is paginated, provide page * argument to request a specific page. * - * @param instCode institute code (MCPD INSTCODE) + * @param instituteCode institute code (MCPD INSTCODE) * @param page 1 for first page * @return the string * @throws OAuthAuthenticationException authentication exception @@ -1070,15 +943,15 @@ public class GenesysClient { * @throws HttpRedirectException the http redirect exception * @throws GenesysApiException the Genesys API exception */ - public String listGalleries(final String instCode, final int page) throws OAuthAuthenticationException, PleaseRetryException, HttpRedirectException, GenesysApiException { - return query(Verb.GET, String.format("/img/%1$s/_galleries", instCode), Collections.singletonMap("page", Integer.toString(page)), null); + public String listGalleries(final String instituteCode, final int page) throws OAuthAuthenticationException, PleaseRetryException, HttpRedirectException, GenesysApiException { + return query(Verb.GET, String.format("/img/%1$s/_galleries", instituteCode), Collections.singletonMap("page", Integer.toString(page)), null); } /** * List UUIDs of images in an existing accession gallery. * - * @param instCode institute code (MCPD INSTCODE) - * @param acceNumb the acce numb + * @param instituteCode institute code (MCPD INSTCODE) + * @param accessionNumber the acce numb * @return the list * @throws OAuthAuthenticationException authentication exception * @throws PleaseRetryException exception indicating the call shold be re-attempted @@ -1087,10 +960,10 @@ public class GenesysClient { * @throws JsonMappingException the json mapping exception * @throws IOException Signals that an I/O exception has occurred. */ - public List listGalleryImages(final String instCode, final String acceNumb) + public List listGalleryImages(final String instituteCode, final String accessionNumber) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException, JsonParseException, JsonMappingException, IOException { - final String json = query(Verb.GET, String.format("/img/%1$s/acn/%2$s", instCode)); + final String json = query(Verb.GET, String.format("/img/%1$s/acn/%2$s", instituteCode)); return objectMapper.readValue(json, new TypeReference>() { }); } @@ -1098,15 +971,15 @@ public class GenesysClient { /** * Add image to accession. * - * @param instCode institute code (MCPD INSTCODE) - * @param acceNumb accession number (MCPD ACCENUMB) + * @param instituteCode institute code (MCPD INSTCODE) + * @param accessionNumber accession number (MCPD ACCENUMB) * @param file image to upload * @param contentType the content type * @return the repository image * @throws GenesysApiException the Genesys API exception * @throws IOException if file cannot be read */ - public RepositoryImage uploadImage(final String instCode, final String acceNumb, final File file, final String contentType) throws GenesysApiException, IOException { + public RepositoryImage uploadImage(final String instituteCode, final String accessionNumber, final File file, final String contentType) throws GenesysApiException, IOException { if (StringUtils.isBlank(contentType)) { throw new GenesysApiException("Content-Type must be provided for the file " + file.getAbsolutePath()); } @@ -1114,7 +987,7 @@ public class GenesysClient { LOG.debug("Image content type: {}", contentType); // PUT file on server - final String json = query(Verb.PUT, String.format("/img/%1$s/acn/%2$s/", instCode, acceNumb), Collections.singletonMap("originalFilename", file.getName()), contentType, + final String json = query(Verb.PUT, String.format("/img/%1$s/acn/%2$s/", instituteCode, accessionNumber), Collections.singletonMap("originalFilename", file.getName()), contentType, FileUtils.readFileToByteArray(file)); System.err.println(json); @@ -1124,32 +997,32 @@ public class GenesysClient { /** * Remove image from accession gallery by UUID. URL template: - * /img/{instCode}/acn/{acceNumb:.+}/{uuid}/_metadata + * /img/{instituteCode}/acn/{accessionNumber:.+}/{uuid}/_metadata * - * @param instCode institute code (MCPD INSTCODE) - * @param acceNumb accession number (MCPD ACCENUMB) + * @param instituteCode institute code (MCPD INSTCODE) + * @param accessionNumber accession number (MCPD ACCENUMB) * @param uuid Repository image UUID as assigned by Genesys * @throws OAuthAuthenticationException authentication exception * @throws PleaseRetryException exception indicating the call shold be re-attempted * @throws GenesysApiException the Genesys API exception */ - public void deleteImage(final String instCode, final String acceNumb, final UUID uuid) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException { - query(Verb.DELETE, String.format("/img/%1$s/acn/%2$s/%3$s", instCode, acceNumb, uuid)); + public void deleteImage(final String instituteCode, final String accessionNumber, final UUID uuid) throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException { + query(Verb.DELETE, String.format("/img/%1$s/acn/%2$s/%3$s", instituteCode, accessionNumber, uuid)); } /** * Get image metadata from Genesys. * - * @param instCode institute code (MCPD INSTCODE) - * @param acceNumb accession number (MCPD ACCENUMB) + * @param instituteCode institute code (MCPD INSTCODE) + * @param accessionNumber accession number (MCPD ACCENUMB) * @param uuid Repository image UUID as assigned by Genesys * @return the metadata * @throws GenesysApiException the Genesys API exception * @throws IOException Signals that an I/O exception has occurred. */ - public RepositoryImage getImageMetadata(final String instCode, final String acceNumb, final UUID uuid) throws GenesysApiException, IOException { + public RepositoryImage getImageMetadata(final String instituteCode, final String accessionNumber, final UUID uuid) throws GenesysApiException, IOException { // PUT file on server - final String json = query(Verb.GET, String.format("/img/%1$s/acn/%2$s/%3$s/_metadata", instCode, acceNumb, uuid)); + final String json = query(Verb.GET, String.format("/img/%1$s/acn/%2$s/%3$s/_metadata", instituteCode, accessionNumber, uuid)); // Deserialize JSON return objectMapper.readValue(json, RepositoryImage.class); @@ -1157,19 +1030,19 @@ public class GenesysClient { /** * Set image metadata. Endpoint PUT - * /img/{instCode}/acn/{acceNumb:.+}/{uuid}/_metadata + * /img/{instituteCode}/acn/{accessionNumber:.+}/{uuid}/_metadata * - * @param instCode institute code (MCPD INSTCODE) - * @param acceNumb accession number (MCPD ACCENUMB) + * @param instituteCode institute code (MCPD INSTCODE) + * @param accessionNumber accession number (MCPD ACCENUMB) * @param uuid Repository image UUID as assigned by Genesys * @param imageData the image data * @return the metadata * @throws GenesysApiException the Genesys API exception * @throws IOException Signals that an I/O exception has occurred. */ - public RepositoryImage putImageMetadata(final String instCode, final String acceNumb, final UUID uuid, final RepositoryImage imageData) + public RepositoryImage putImageMetadata(final String instituteCode, final String accessionNumber, final UUID uuid, final RepositoryImage imageData) throws GenesysApiException, IOException { - final String json = query(Verb.PUT, String.format("/img/%1$s/acn/%2$s/%3$s/_metadata", instCode, acceNumb, uuid), null, imageData); + final String json = query(Verb.PUT, String.format("/img/%1$s/acn/%2$s/%3$s/_metadata", instituteCode, accessionNumber, uuid), null, imageData); // Deserialize JSON return objectMapper.readValue(json, RepositoryImage.class); diff --git a/src/main/java/org/genesys2/client/oauth/GenesysTokens.java b/src/main/java/org/genesys2/client/oauth/GenesysTokens.java index fd57aa421d65e68c8ed29ea86a96f747033744d1..6bb1546907560f23369e73b178f30051edf25a41 100644 --- a/src/main/java/org/genesys2/client/oauth/GenesysTokens.java +++ b/src/main/java/org/genesys2/client/oauth/GenesysTokens.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/org/genesys2/client/oauth/HttpConstants.java b/src/main/java/org/genesys2/client/oauth/HttpConstants.java index f00fc269f3c165a3df6aba2964461b503fcb68fa..eda646d08035246f6db1d046ba58a80b6ba09f96 100644 --- a/src/main/java/org/genesys2/client/oauth/HttpConstants.java +++ b/src/main/java/org/genesys2/client/oauth/HttpConstants.java @@ -1,3 +1,18 @@ +/* + * Copyright 2019 Global Crop Diversity Trust + * + * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.genesys2.client.oauth; public class HttpConstants { diff --git a/src/main/java/org/genesys2/client/oauth/HttpRedirectException.java b/src/main/java/org/genesys2/client/oauth/HttpRedirectException.java index a3da794ad91486aeeb9e76f9442ca4e1d2f052f7..8a2218fc45555e5a6b74e3410288e8ac32f28eb3 100644 --- a/src/main/java/org/genesys2/client/oauth/HttpRedirectException.java +++ b/src/main/java/org/genesys2/client/oauth/HttpRedirectException.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/org/genesys2/client/oauth/OAuthAuthenticationException.java b/src/main/java/org/genesys2/client/oauth/OAuthAuthenticationException.java index 05e3c4b7dce548f2d6800804d28b55942355cd58..ebc4e68a9c5a88ebbd82b6d611cdaca8dab75255 100644 --- a/src/main/java/org/genesys2/client/oauth/OAuthAuthenticationException.java +++ b/src/main/java/org/genesys2/client/oauth/OAuthAuthenticationException.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/org/genesys2/client/oauth/PleaseRetryException.java b/src/main/java/org/genesys2/client/oauth/PleaseRetryException.java index dcd619da3d2349dd3ffa904b8d5e149bb274cdbd..eaf2f0f9a199ed00748c333ad654fa38ed5b1928 100644 --- a/src/main/java/org/genesys2/client/oauth/PleaseRetryException.java +++ b/src/main/java/org/genesys2/client/oauth/PleaseRetryException.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/org/genesys2/client/oauth/api/GenesysApi.java b/src/main/java/org/genesys2/client/oauth/api/GenesysApi.java index 050a136858d39e6bbb7d519c7461e7cab5df388c..82639f8e5fd4f6d12a10b79f96ac10d3a7b3c8ca 100644 --- a/src/main/java/org/genesys2/client/oauth/api/GenesysApi.java +++ b/src/main/java/org/genesys2/client/oauth/api/GenesysApi.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/org/genesys2/client/oauth/api/accession/AccessionJson.java b/src/main/java/org/genesys2/client/oauth/api/accession/AccessionJson.java index 065973ec402ecf627bf910c9e24d46beea8d7d2e..53af24a0fcc8e835fc3c89c8066f703f3659679e 100644 --- a/src/main/java/org/genesys2/client/oauth/api/accession/AccessionJson.java +++ b/src/main/java/org/genesys2/client/oauth/api/accession/AccessionJson.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,45 +32,21 @@ public class AccessionJson { private Long genesysId; /** The puid. */ - @JsonProperty(value = Accession.PUID) - private String puid; + @JsonProperty(value = Accession.DOI) + private String doi; /** The inst code. */ @JsonProperty(value = Accession.INSTCODE) - private String instCode; + private String instituteCode; /** The acce numb. */ @JsonProperty(value = Accession.ACCENUMB) - private String acceNumb; + private String accessionNumber; /** The new acce numb. */ @JsonProperty(value = Accession.ACCENUMB_NEW) private String newAcceNumb; - /** The genus. */ - @JsonProperty(value = Accession.GENUS) - private String genus; - - /** The new genus. */ - @JsonProperty(value = Accession.GENUS_NEW) - private String newGenus; - - /** The species. */ - @JsonProperty(value = Accession.SPECIES) - private String species; - - /** The spauthor. */ - @JsonProperty(value = Accession.SPAUTHOR) - private String spauthor; - - /** The subtaxa. */ - @JsonProperty(value = Accession.SUBTAXA) - private String subtaxa; - - /** The subtauthor. */ - @JsonProperty(value = Accession.SUBTAUTHOR) - private String subtauthor; - /** The uuid. */ @JsonProperty(value = Accession.UUID) private String uuid; @@ -81,7 +57,7 @@ public class AccessionJson { /** The acq date. */ @JsonProperty(value = Accession.ACQDATE) - private String acqDate; + private String acquisitionDate; /** The mls stat. */ @JsonProperty(value = Accession.MLSSTAT) @@ -109,7 +85,11 @@ public class AccessionJson { /** The bred code. */ @JsonProperty(value = Accession.BREDCODE) - private String[] bredCode; + private String[] breederCode; + + /** The bred code. */ + @JsonProperty(value = Accession.BREDNAME) + private String[] breederName; /** The ancest. */ @JsonProperty(value = Accession.ANCEST) @@ -135,6 +115,10 @@ public class AccessionJson { @JsonProperty(value = Accession.GEO) private GeoJson geo; + /** The taxonomy. */ + @JsonProperty(value = Accession.TAXONOMY) + private TaxonomyJson taxonomy; + /** The remarks. */ @JsonProperty(value = Accession.REMARKS) private Remark[] remarks; @@ -162,6 +146,7 @@ public class AccessionJson { public AccessionJson() { this.geo = new GeoJson(); this.coll = new CollectingJson(); + this.taxonomy = new TaxonomyJson(); } /** @@ -205,56 +190,49 @@ public class AccessionJson { * @return the puid */ public String getPuid() { - return puid; + return doi; } /** - * @param puid the puid to set - */ - public void setPuid(String puid) { - this.puid = puid; - } - - /** - * Gets the inst code. + * Gets the FAO WIEWS institute code. * * @return the inst code */ - public String getInstCode() { - return instCode; + public String getInstituteCode() { + return instituteCode; } /** * Sets the inst code. * - * @param instCode the new inst code + * @param instituteCode the new inst code */ - public void setInstCode(final String instCode) { - this.instCode = instCode; + public void setInstituteCode(final String instituteCode) { + this.instituteCode = instituteCode; } /** - * Gets the acce numb. + * Gets the accession number. * - * @return the acce numb + * @return the accession number */ - public String getAcceNumb() { - return acceNumb; + public String getAccessionNumber() { + return accessionNumber; } - + /** - * Sets the acce numb. + * Sets the accession number. * - * @param acceNumb the new acce numb + * @param accessionNumber the new accession number */ - public void setAcceNumb(final String acceNumb) { - this.acceNumb = acceNumb; + public void setAccessionNumber(String accessionNumber) { + this.accessionNumber = accessionNumber; } - + /** - * Gets the new acce numb. + * Gets the new accession number. * - * @return the new acce numb + * @return the new accession number */ public String getNewAcceNumb() { return newAcceNumb; @@ -269,114 +247,6 @@ public class AccessionJson { this.newAcceNumb = newAcceNumb; } - /** - * Gets the genus. - * - * @return the genus - */ - public String getGenus() { - return genus; - } - - /** - * Sets the genus. - * - * @param genus the new genus - */ - public void setGenus(final String genus) { - this.genus = genus; - } - - /** - * Gets the new genus. - * - * @return the new genus - */ - public String getNewGenus() { - return newGenus; - } - - /** - * Sets the new genus. - * - * @param newGenus the new new genus - */ - public void setNewGenus(final String newGenus) { - this.newGenus = newGenus; - } - - /** - * Gets the species. - * - * @return the species - */ - public String getSpecies() { - return species; - } - - /** - * Sets the species. - * - * @param species the new species - */ - public void setSpecies(final String species) { - this.species = species; - } - - /** - * Gets the spauthor. - * - * @return the spauthor - */ - public String getSpauthor() { - return spauthor; - } - - /** - * Sets the spauthor. - * - * @param spauthor the new spauthor - */ - public void setSpauthor(final String spauthor) { - this.spauthor = spauthor; - } - - /** - * Gets the subtaxa. - * - * @return the subtaxa - */ - public String getSubtaxa() { - return subtaxa; - } - - /** - * Sets the subtaxa. - * - * @param subtaxa the new subtaxa - */ - public void setSubtaxa(final String subtaxa) { - this.subtaxa = subtaxa; - } - - /** - * Gets the subtauthor. - * - * @return the subtauthor - */ - public String getSubtauthor() { - return subtauthor; - } - - /** - * Sets the subtauthor. - * - * @param subtauthor the new subtauthor - */ - public void setSubtauthor(final String subtauthor) { - this.subtauthor = subtauthor; - } - /** * Gets the uuid. * @@ -414,21 +284,21 @@ public class AccessionJson { } /** - * Gets the acq date. + * Gets the acquisition date. * - * @return the acq date + * @return the acquisition date */ - public String getAcqDate() { - return acqDate; + public String getAcquisitionDate() { + return acquisitionDate; } - + /** - * Sets the acq date. + * Sets the acquisition date. * - * @param acqDate the new acq date + * @param acquisitionDate the new acquisition date */ - public void setAcqDate(final String acqDate) { - this.acqDate = acqDate; + public void setAcquisitionDate(String acquisitionDate) { + this.acquisitionDate = acquisitionDate; } /** @@ -545,7 +415,7 @@ public class AccessionJson { * @return the bred code */ public String[] getBredCode() { - return bredCode; + return breederCode; } /** @@ -554,7 +424,7 @@ public class AccessionJson { * @param bredCode the new bred code */ public void setBredCode(final String[] bredCode) { - this.bredCode = bredCode; + this.breederCode = bredCode; } /** @@ -665,6 +535,24 @@ public class AccessionJson { this.geo = geo; } + /** + * Gets the taxonomy. + * + * @return the taxonomy + */ + public TaxonomyJson getTaxonomy() { + return taxonomy; + } + + /** + * Sets the taxonomy. + * + * @param taxonomy the new taxonomy + */ + public void setTaxonomy(final TaxonomyJson taxonomy) { + this.taxonomy = taxonomy; + } + /** * Gets the remarks. * diff --git a/src/main/java/org/genesys2/client/oauth/api/accession/Api1Constants.java b/src/main/java/org/genesys2/client/oauth/api/accession/Api1Constants.java index 769a9049e70d5fe90625b1f78996c9bb8dfee1d7..f1952e7ee1107d023ab185e20aa522446c6a8032 100644 --- a/src/main/java/org/genesys2/client/oauth/api/accession/Api1Constants.java +++ b/src/main/java/org/genesys2/client/oauth/api/accession/Api1Constants.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,69 +40,52 @@ public interface Api1Constants { /** * WIEWS code of accession holding institute. */ - public static final String INSTCODE = "instCode"; + public static final String INSTCODE = "instituteCode"; /** * Accession number. */ - public static final String ACCENUMB = "acceNumb"; - - /** Used to assign new ACCENUMB. */ - public static final String ACCENUMB_NEW = "newAcceNumb"; + public static final String ACCENUMB = "accessionNumber"; /** - * Reported genus of accession. + * Used to assign new ACCENUMB */ - public static final String GENUS = "genus"; - - /** - * May be used to assign a different genus. - */ - public static final String GENUS_NEW = "newGenus"; - - /** - * Specific epithet portion of the scientific name, in latin, in lowercase letters. - * Following abbreviation is allowed: ‘sp.’ - */ - public static final String SPECIES = "species"; - - /** Corresponds to MCPD SPAUTHOR. */ - public static final String SPAUTHOR = "spauthor"; - - /** Corresponds to MCPD SUBTAXA. */ - public static final String SUBTAXA = "subtaxa"; - - /** Corresponds to MCPD SUBTAUTHOR. */ - public static final String SUBTAUTHOR = "subtauthor"; + public static final String ACCENUMB_NEW = "newAcceNumb"; /** - * Universally Unique IDentifier for the accession, assigned by the first holding institute - * and immutable when accession is duplicated in another institute. + * Universally Unique IDentifier for the accession, assigned by the + * first holding institute and immutable when accession is duplicated in + * another institute. */ public static final String UUID = "uuid"; /** * ISO3 country code of country of origin. */ - public static final String ORIGCTY = "orgCty"; + public static final String ORIGCTY = "origCty"; /** - * Date on which the accession entered the collection as YYYYMMDD. Missing data (MM or DD) - * should be indicated with hyphens. Leading zeros are required. + * Date on which the accession entered the collection as YYYYMMDD. + * Missing data (MM or DD) should be indicated with hyphens. Leading + * zeros are required. */ - public static final String ACQDATE = "acqDate"; + public static final String ACQDATE = "acquisitionDate"; /** - * The status of an accession with regards to the Multilateral System (MLS) of the - * International Treaty on PlantGenetic Resources for Food and Agriculture. + * The status of an accession with regards to the Multilateral System + * (MLS) of the International Treaty on PlantGenetic Resources for Food + * and Agriculture. */ - public static final String MLSSTAT = "mlsStat"; + public static final String MLSSTAT = "mlsStatus"; - /** Is the accession “FAO In trust”. */ + /** + * Is the accession “FAO In trust” + */ public static final String INTRUST = "inTrust"; /** - * The general availability of the accession for distribution, but subject to current stock. + * The general availability of the accession for distribution, but + * subject to current stock. */ public static final String AVAILABLE = "available"; @@ -111,26 +94,30 @@ public interface Api1Constants { */ public static final String STORAGE = "storage"; - /** MCPD Sample status. */ + /** + * MCPD Sample status + */ public static final String SAMPSTAT = "sampStat"; /** - * FAO Institute Code (WIEWS code) of the institute where a safety duplicate of the - * accession is maintained. + * FAO Institute Code (WIEWS code) of the institute where a safety + * duplicate of the accession is maintained. */ public static final String DUPLSITE = "duplSite"; /** * Corresponds to BREDCODE. */ - public static final String BREDCODE = "bredCode"; + public static final String BREDCODE = "breederCode"; /** * Corresponds to ANCEST. */ public static final String ANCEST = "ancest"; - /** FAO WIEWS code of the donor institute. */ + /** + * FAO WIEWS code of the donor institute + */ public static final String DONORCODE = "donorCode"; /** @@ -139,58 +126,125 @@ public interface Api1Constants { public static final String DONORNUMB = "donorNumb"; /** - * Name of the donor institute (or person). Provide only when donorCode is not available. + * Name of the donor institute (or person). Provide only when donorCode + * is not available. */ public static final String DONORNAME = "donorName"; - /** Object containing the collecting data. */ + /** + * Object containing the collecting data + */ public static final String COLL = "coll"; - /** Object containing georeference data. */ + /** + * Object containing georeference data + */ public static final String GEO = "geo"; - /** MCPD Remarks. */ + /** + * Object containing taxonomy data. + * */ + public static final String TAXONOMY = "taxonomy"; + + /** + * MCPD Remarks + */ public static final String REMARKS = "remarks"; /** - * Marks if accession record is about a historic holding and the accession no longer exists. + * Marks if accession record is about a historic holding and the + * accession no longer exists. */ public static final String HISTORIC = "historic"; - /** Accession name. */ + /** + * Accession name + */ public static final String ACCENAME = "acceName"; - /** Other numbers. */ + /** + * Other numbers + */ public static final String OTHERNUMB = "otherNumb"; /** - * URL linking to additional data about the accession either in the holding genebank or from - * another source. + * URL linking to additional data about the accession either in the + * holding genebank or from another source. */ public static final String ACCEURL = "acceUrl"; - /** Gene bank provided MCPD#CROPNAME. */ + /** + * Gene bank provided MCPD#CROPNAME + */ public static final String CROPNAME = "cropName"; + + /** + * Corresponds to BREDNAME. + */ + public static final String BREDNAME = "breederName"; + + } + + public static interface Taxonomy { + + /** + * Reported genus of accession. + */ + public static final String GENUS = "genus"; + + /** + * May be used to assign a different genus. + */ + public static final String GENUS_NEW = "newGenus"; + + /** + * Specific epithet portion of the scientific name, in latin, in + * lowercase letters. Following abbreviation is allowed: ‘sp.’ + */ + public static final String SPECIES = "species"; + + /** + * Corresponds to MCPD SPAUTHOR + */ + public static final String SPAUTHOR = "spAuthor"; + + /** + * Corresponds to MCPD SUBTAXA + */ + public static final String SUBTAXA = "subtaxa"; + + /** + * Corresponds to MCPD SUBTAUTHOR + */ + public static final String SUBTAUTHOR = "subtAuthor"; + } - /** - * The Interface Collecting. - */ public static interface Collecting { - /** Corresponds to COLLDATE. */ + /** + * Corresponds to COLLDATE + */ public static final String COLLDATE = "collDate"; - /** Corresponds to COLLSITE. */ + /** + * Corresponds to COLLSITE + */ public static final String COLLSITE = "collSite"; - /** Corresponds to MCPD COLLNUMB. */ + /** + * Corresponds to MCPD COLLNUMB + */ public static final String COLLNUMB = "collNumb"; - /** Corresponds to MCPD COLLSRC. */ + /** + * Corresponds to MCPD COLLSRC + */ public static final String COLLSRC = "collSrc"; - /** FAO WIEWS code of collecting institute. */ + /** + * FAO WIEWS code of collecting institute + */ public static final String COLLCODE = "collCode"; /** @@ -203,31 +257,43 @@ public interface Api1Constants { */ public static final String COLLINSTADDRESS = "collInstAddress"; - /** Corresponds to COLLMISSID. */ + /** + * Corresponds to COLLMISSID + */ public static final String COLLMISSID = "collMissId"; } - /** - * The Interface Geo. - */ public static interface Geo { - /** Latitude. */ + /** + * Latitude + */ public static final String LATITUDE = "latitude"; - /** Longitude. */ + /** + * Longitude + */ public static final String LONGITUDE = "longitude"; - /** Elevation. */ + /** + * Elevation + */ public static final String ELEVATION = "elevation"; - /** COORDUNCERT (in meters). */ - public static final String COORDUNCERT = "coordUncert"; + /** + * COORDUNCERT (in meters) + */ + public static final String COORDUNCERT = "uncertainty"; - /** Corresponds to COORDATUM. */ - public static final String COORDDATUM = "coordDatum"; + /** + * Corresponds to COORDATUM + */ + public static final String COORDDATUM = "datum"; - /** Corresponds to GEOREFMETH. */ - public static final String GEOREFMETH = "georefMeth"; + /** + * Corresponds to GEOREFMETH + */ + public static final String GEOREFMETH = "method"; } + } diff --git a/src/main/java/org/genesys2/client/oauth/api/accession/CollectingJson.java b/src/main/java/org/genesys2/client/oauth/api/accession/CollectingJson.java index 85cc1d01d7c1ef92680440434819f776d9b27946..44e5bea52fe961651d2d3faf6f373809e955898e 100644 --- a/src/main/java/org/genesys2/client/oauth/api/accession/CollectingJson.java +++ b/src/main/java/org/genesys2/client/oauth/api/accession/CollectingJson.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/org/genesys2/client/oauth/api/accession/GeoJson.java b/src/main/java/org/genesys2/client/oauth/api/accession/GeoJson.java index 23414232afb97aed0f4de3d9ad2d5e9deab907f5..49be922b430658fa91eda49ea6e2bc767945dd72 100644 --- a/src/main/java/org/genesys2/client/oauth/api/accession/GeoJson.java +++ b/src/main/java/org/genesys2/client/oauth/api/accession/GeoJson.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/org/genesys2/client/oauth/api/accession/Remark.java b/src/main/java/org/genesys2/client/oauth/api/accession/Remark.java index 4a223d839b4984b449bd5cb461ab56ff79af8c78..a3b0c5b59856f78d438142ae6e666ff1378f0b9f 100644 --- a/src/main/java/org/genesys2/client/oauth/api/accession/Remark.java +++ b/src/main/java/org/genesys2/client/oauth/api/accession/Remark.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/org/genesys2/client/oauth/api/accession/TaxonomyJson.java b/src/main/java/org/genesys2/client/oauth/api/accession/TaxonomyJson.java new file mode 100644 index 0000000000000000000000000000000000000000..dce111c1c5488d7c6785af7c9ab356b1eb2b3287 --- /dev/null +++ b/src/main/java/org/genesys2/client/oauth/api/accession/TaxonomyJson.java @@ -0,0 +1,157 @@ +/* + * Copyright 2019 Global Crop Diversity Trust + * + * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.genesys2.client.oauth.api.accession; + +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * The Class TaxonomyJson. + */ +public class TaxonomyJson { + + /** The genus. */ + @JsonProperty(value = Api1Constants.Taxonomy.GENUS) + private String genus; + + /** The new genus. */ + @JsonProperty(value = Api1Constants.Taxonomy.GENUS_NEW) + private String newGenus; + + /** The spAuthor. */ + @JsonProperty(value = Api1Constants.Taxonomy.SPAUTHOR) + private String spAuthor; + + /** The species. */ + @JsonProperty(value = Api1Constants.Taxonomy.SPECIES) + private String species; + + /** The subtAuthor. */ + @JsonProperty(value = Api1Constants.Taxonomy.SUBTAUTHOR) + private String subtAuthor; + + /** The subtaxa. */ + @JsonProperty(value = Api1Constants.Taxonomy.SUBTAXA) + private String subtaxa; + + /** + * Gets the genus. + * + * @return the genus + */ + public String getGenus() { + return genus; + } + + /** + * Sets the genus. + * + * @param genus the new genus + */ + public void setGenus(final String genus) { + this.genus = genus; + } + + /** + * Gets the new genus. + * + * @return the new genus + */ + public String getNewGenus() { + return newGenus; + } + + /** + * Sets the new genus. + * + * @param newGenus the new new genus + */ + public void setNewGenus(final String newGenus) { + this.newGenus = newGenus; + } + + /** + * Gets the spAuthor. + * + * @return the spAuthor + */ + public String getSpAuthor() { + return spAuthor; + } + + /** + * Sets the spAuthor. + * + * @param spAuthor the new spAuthor + */ + public void setSpAuthor(final String spAuthor) { + this.spAuthor = spAuthor; + } + + /** + * Gets the species. + * + * @return the species + */ + public String getSpecies() { + return species; + } + + /** + * Sets the species. + * + * @param species the new species + */ + public void setSpecies(final String species) { + this.species = species; + } + + /** + * Gets the subtAuthor. + * + * @return the subtAuthor + */ + public String getSubtAuthor() { + return subtAuthor; + } + + /** + * Sets the subtAuthor. + * + * @param subtAuthor the new subtAuthor + */ + public void setSubtAuthor(final String subtAuthor) { + this.subtAuthor = subtAuthor; + } + + /** + * Gets the subtaxa. + * + * @return the subtaxa + */ + public String getSubtaxa() { + return subtaxa; + } + + /** + * Sets the subtaxa. + * + * @param subtaxa the new subtaxa + */ + public void setSubtaxa(final String subtaxa) { + this.subtaxa = subtaxa; + } +} diff --git a/src/main/java/org/genesys2/client/oauth/api/images/RepositoryFile.java b/src/main/java/org/genesys2/client/oauth/api/images/RepositoryFile.java index 770903004f2dde98e0866abb95477f356d52ffa8..1a0e01cc5b842543b8cc11e8b1f4f9632464a701 100644 --- a/src/main/java/org/genesys2/client/oauth/api/images/RepositoryFile.java +++ b/src/main/java/org/genesys2/client/oauth/api/images/RepositoryFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/main/java/org/genesys2/client/oauth/api/images/RepositoryImage.java b/src/main/java/org/genesys2/client/oauth/api/images/RepositoryImage.java index 443aa15f6f225a82536f2875fac630d09278ff84..bafe76c8eefb269a3f1aba54adc12cd5fe17009f 100644 --- a/src/main/java/org/genesys2/client/oauth/api/images/RepositoryImage.java +++ b/src/main/java/org/genesys2/client/oauth/api/images/RepositoryImage.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/org/geneys2/client/oauth/AccessionApiTest.java b/src/test/java/org/geneys2/client/oauth/AccessionApiTest.java index 9e0744aa07935c977200b24f14c5db82e600cbb4..e9c12c8ac95a39e24f4b842ff41fa4d9a5648fd0 100644 --- a/src/test/java/org/geneys2/client/oauth/AccessionApiTest.java +++ b/src/test/java/org/geneys2/client/oauth/AccessionApiTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,20 +17,14 @@ package org.geneys2.client.oauth; import static org.hamcrest.CoreMatchers.*; -import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Matchers.*; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.when; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import com.fasterxml.jackson.annotation.JsonInclude.Include; -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 org.genesys2.client.oauth.GenesysApiException; import org.genesys2.client.oauth.GenesysClient; import org.genesys2.client.oauth.OAuthAuthenticationException; @@ -44,9 +38,14 @@ import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -// TODO: Auto-generated Javadoc +import com.fasterxml.jackson.annotation.JsonInclude.Include; +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; + /** - * The Class AccessionApiTest. + * Accession API v1 test */ @RunWith(MockitoJUnitRunner.class) public class AccessionApiTest { @@ -59,7 +58,7 @@ public class AccessionApiTest { protected GenesysClient genesysClient; /** The inst code. */ - private final String instCode = "INS000"; + private final String instituteCode = "INS000"; /** The mock server. */ private final MockGenesysServer mockServer = new MockGenesysServer(); @@ -99,7 +98,7 @@ public class AccessionApiTest { @Test public void testAccessions0() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException, JsonProcessingException, IOException { - final List results = objectMapper.readValue(genesysClient.listAccessions(instCode, 1, null), new TypeReference>() { + final List results = objectMapper.readValue(genesysClient.listAccessions(instituteCode, 1, null), new TypeReference>() { }); assertThat("Expected empty list", results.size(), is(0)); @@ -117,14 +116,14 @@ public class AccessionApiTest { @Test public void testAccessions1() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException, JsonProcessingException, IOException { - when(genesysClient.listAccessions(instCode, 1, null)).thenReturn("[{\"instCode\":\"INS000\",\"acceNumb\":\"ACC-1\"}]"); + when(genesysClient.listAccessions(instituteCode, 1, null)).thenReturn("[{\"instituteCode\":\"INS000\",\"accessionNumber\":\"ACC-1\"}]"); - final List results = objectMapper.readValue(genesysClient.listAccessions(instCode, 1, null), new TypeReference>() { + final List results = objectMapper.readValue(genesysClient.listAccessions(instituteCode, 1, null), new TypeReference>() { }); assertThat("Expected empty list", results.size(), is(1)); final AccessionJson acc1 = results.get(0); - assertThat("INSTCODE doesn't match", acc1.getInstCode(), is(instCode)); + assertThat("INSTCODE doesn't match", acc1.getInstituteCode(), is(instituteCode)); acc1.setHistoric(true); @@ -143,7 +142,7 @@ public class AccessionApiTest { @Test public void testUpsertAccessions1() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException, JsonProcessingException, IOException, InterruptedException { - String result = genesysClient.listAccessions(instCode, 1, null); + String result = genesysClient.listAccessions(instituteCode, 1, null); assertThat("Non-null result expected from genesysClient", result, notNullValue()); List results = objectMapper.readValue(result, new TypeReference>() { }); @@ -154,25 +153,25 @@ public class AccessionApiTest { for (int i = 0; i < 4; i++) { final AccessionJson aj = new AccessionJson(); - aj.setInstCode(instCode); - aj.setAcceNumb("ACC-" + (i + 1)); - aj.setGenus("Genus"); + aj.setInstituteCode(instituteCode); + aj.setAccessionNumber("ACC-" + (i + 1)); + aj.getTaxonomy().setGenus("Genus"); ajList.add(aj); } - result = genesysClient.updateAccessions(instCode, ajList); + result = genesysClient.updateAccessions(instituteCode, ajList); System.err.println(result); - results = objectMapper.readValue(genesysClient.listAccessions(instCode, 1, null), new TypeReference>() { + results = objectMapper.readValue(genesysClient.listAccessions(instituteCode, 1, null), new TypeReference>() { }); assertThat("Expected list with 4 entries", results.size(), is(4)); for (final AccessionJson aj1 : results) { - assertThat("Expected matching INSTCODE", aj1.getInstCode(), is(instCode)); - assertThat("Expected ACCENUMB starting with", aj1.getAcceNumb(), CoreMatchers.startsWith("ACC-")); - assertThat("Expected GENUS=Genus", aj1.getGenus(), is("Genus")); - assertThat("Expected SPECIES=null", aj1.getSpecies(), nullValue()); + assertThat("Expected matching INSTCODE", aj1.getInstituteCode(), is(instituteCode)); + assertThat("Expected ACCENUMB starting with", aj1.getAccessionNumber(), CoreMatchers.startsWith("ACC-")); + assertThat("Expected GENUS=Genus", aj1.getTaxonomy().getGenus(), is("Genus")); + assertThat("Expected SPECIES=null", aj1.getTaxonomy().getSpecies(), nullValue()); } } diff --git a/src/test/java/org/geneys2/client/oauth/AccessionImagesTest.java b/src/test/java/org/geneys2/client/oauth/AccessionImagesTest.java index 6c3af8b6280d638893c9cbad2fdeefea20a411e4..3191a41862069e63c867235991b330f1e31ac32f 100644 --- a/src/test/java/org/geneys2/client/oauth/AccessionImagesTest.java +++ b/src/test/java/org/geneys2/client/oauth/AccessionImagesTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/org/geneys2/client/oauth/ApiTest.java b/src/test/java/org/geneys2/client/oauth/ApiTest.java index 3b0cdc0ba79f85afbc2e866a7db8d71f07f76672..00daf186d5b5ea8b09a37dee883bc67c20fa5612 100644 --- a/src/test/java/org/geneys2/client/oauth/ApiTest.java +++ b/src/test/java/org/geneys2/client/oauth/ApiTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,23 +16,12 @@ package org.geneys2.client.oauth; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Matchers.anyMapOf; -import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.*; import static org.mockito.Mockito.when; -import java.io.IOException; import java.util.ArrayList; import java.util.Collection; -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.ObjectNode; -import com.github.scribejava.core.model.Verb; - import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.genesys2.client.oauth.GenesysApiException; @@ -40,6 +29,7 @@ import org.genesys2.client.oauth.GenesysClient; import org.genesys2.client.oauth.OAuthAuthenticationException; import org.genesys2.client.oauth.PleaseRetryException; import org.genesys2.client.oauth.api.accession.Api1Constants.Accession; +import org.genesys2.client.oauth.api.accession.Api1Constants.Taxonomy; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; @@ -47,11 +37,14 @@ import org.junit.runner.RunWith; import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.mockito.invocation.InvocationOnMock; import org.mockito.runners.MockitoJUnitRunner; -import org.mockito.stubbing.Answer; -// TODO: Auto-generated Javadoc +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.github.scribejava.core.model.Verb; + /** * The Class ApiTest. */ @@ -61,9 +54,6 @@ public class ApiTest { /** The object mapper. */ private static ObjectMapper objectMapper; - /** The Constant bananaJson. */ - private static final String bananaJson; - /** The http get mock. */ @Mock protected HttpGet httpGetMock; @@ -81,8 +71,6 @@ public class ApiTest { objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); objectMapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false); objectMapper.setSerializationInclusion(Include.NON_NULL); - - bananaJson = objectMapper.createObjectNode().put("shortName", "banana").toString(); } /** @@ -105,95 +93,6 @@ public class ApiTest { MockitoAnnotations.initMocks(this); when(httpGetMock.getMethod()).thenReturn("GET"); when(httpPostMock.getMethod()).thenReturn("POST"); - - when(genesysClient.getCrop("banana")).thenReturn(bananaJson); - } - - /** - * Test mock get crop. - * - * @throws OAuthAuthenticationException the o auth authentication exception - * @throws PleaseRetryException the please retry exception - * @throws GenesysApiException the genesys api exception - * @throws JsonProcessingException the json processing exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testMockGetCrop() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException, JsonProcessingException, IOException { - - final String json = genesysClient.getCrop("banana"); - assertThat("Returned banana JSON doesn't match", json, equalTo(bananaJson)); - - // Must be able to read the json - objectMapper.readTree(json); - } - - /** - * Test mock update crop. - * - * @throws OAuthAuthenticationException the o auth authentication exception - * @throws PleaseRetryException the please retry exception - * @throws GenesysApiException the genesys api exception - * @throws JsonProcessingException the json processing exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testMockUpdateCrop() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException, JsonProcessingException, IOException { - - final String json = genesysClient.getCrop("banana"); - assertThat("Returned banana JSON doesn't match", json, equalTo(bananaJson)); - - // Must be able to read the json - objectMapper.readTree(json); - } - - /** - * Test organization members. - * - * @throws OAuthAuthenticationException the o auth authentication exception - * @throws PleaseRetryException the please retry exception - * @throws GenesysApiException the genesys api exception - * @throws JsonProcessingException the json processing exception - * @throws IOException Signals that an I/O exception has occurred. - */ - @Test - public void testOrganizationMembers() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException, JsonProcessingException, IOException { - final String orgSlug = "org"; - when(genesysClient.getOrganizationMembers(orgSlug)).then(returnOrganizationMembers(orgSlug)); - final String members = genesysClient.getOrganizationMembers(orgSlug); - objectMapper.readTree(members); - System.err.println(members); - } - - /** - * Return organization members. - * - * @param orgSlug the org slug - * @return the answer - */ - private Answer returnOrganizationMembers(final String orgSlug) { - return new Answer() { - @Override - public String answer(final InvocationOnMock invocation) throws Throwable { - return "[\"ORG1\",\"ORG2\"]"; - } - }; - } - - /** - * Test please retry exception. - * - * @throws OAuthAuthenticationException the o auth authentication exception - * @throws PleaseRetryException the please retry exception - * @throws GenesysApiException the genesys api exception - * @throws JsonProcessingException the json processing exception - */ - @SuppressWarnings("deprecation") - @Test(expected = PleaseRetryException.class) - public void testPleaseRetryException() throws OAuthAuthenticationException, PleaseRetryException, GenesysApiException, JsonProcessingException { - - when(genesysClient.updateMLS("INS000", null)).thenThrow(PleaseRetryException.class); - genesysClient.updateMLS("INS000", null); } /** @@ -210,6 +109,6 @@ public class ApiTest { final Collection list = new ArrayList(); - list.add(objectMapper.createObjectNode().put(Accession.INSTCODE, "INS000").put(Accession.ACCENUMB, "ACC-1").put(Accession.GENUS, "Genus")); + list.add(objectMapper.createObjectNode().put(Accession.INSTCODE, "INS000").put(Accession.ACCENUMB, "ACC-1").putObject(Accession.TAXONOMY).put(Taxonomy.GENUS, "Genus")); } } diff --git a/src/test/java/org/geneys2/client/oauth/JsonNodeMatchers.java b/src/test/java/org/geneys2/client/oauth/JsonNodeMatchers.java index 0ee0b66cdd116c2d7311d3cf9ddec7bb83e73b6a..df9e8ae9e585953a310343c466e2279a0ec54e03 100644 --- a/src/test/java/org/geneys2/client/oauth/JsonNodeMatchers.java +++ b/src/test/java/org/geneys2/client/oauth/JsonNodeMatchers.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/org/geneys2/client/oauth/MockGenesysException.java b/src/test/java/org/geneys2/client/oauth/MockGenesysException.java index 70847ba036319e6d93dad8386316bea9760c2252..379c65ccae35f3f73d4c994b500d8b0c1c643f17 100644 --- a/src/test/java/org/geneys2/client/oauth/MockGenesysException.java +++ b/src/test/java/org/geneys2/client/oauth/MockGenesysException.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/test/java/org/geneys2/client/oauth/MockGenesysServer.java b/src/test/java/org/geneys2/client/oauth/MockGenesysServer.java index 229a5e6640e2fa3e2805b6bb4a4910643da13947..84c175afed6c5bda6e4eace8001d631be430c8da 100644 --- a/src/test/java/org/geneys2/client/oauth/MockGenesysServer.java +++ b/src/test/java/org/geneys2/client/oauth/MockGenesysServer.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -70,12 +70,12 @@ public class MockGenesysServer { if (args[2] != null) { throw new MockGenesysException("Querying is not supported."); } - final String instCode = (String) args[0]; + final String instituteCode = (String) args[0]; final int page = (Integer) args[1]; - System.err.println("Listing accessions instCode=" + instCode + " page=" + page); + System.err.println("Listing accessions instituteCode=" + instituteCode + " page=" + page); - return objectMapper.writeValueAsString(getAccessionsPage(instCode, page)); + return objectMapper.writeValueAsString(getAccessionsPage(instituteCode, page)); } }; } @@ -90,10 +90,10 @@ public class MockGenesysServer { @Override public String answer(final InvocationOnMock invocation) throws Throwable { final Object[] args = invocation.getArguments(); - final String instCode = (String) args[0]; + final String instituteCode = (String) args[0]; final List arrayNode = (List) args[1]; - final String res = objectMapper.writeValueAsString(upsertAccessions(instCode, arrayNode)); + final String res = objectMapper.writeValueAsString(upsertAccessions(instituteCode, arrayNode)); System.err.println("Result: " + res); return res; } @@ -103,7 +103,7 @@ public class MockGenesysServer { /** * Upsert accessions. * - * @param instCode the inst code + * @param instituteCode the inst code * @param nodeList the node list * @return the string * @throws JsonParseException the json parse exception @@ -111,10 +111,10 @@ public class MockGenesysServer { * @throws IOException Signals that an I/O exception has occurred. * @throws GenesysApiException the genesys api exception */ - protected String upsertAccessions(final String instCode, final List nodeList) throws JsonParseException, JsonMappingException, IOException, GenesysApiException { - List accList = instAcc.get(instCode); + protected String upsertAccessions(final String instituteCode, final List nodeList) throws JsonParseException, JsonMappingException, IOException, GenesysApiException { + List accList = instAcc.get(instituteCode); if (accList == null) { - instAcc.put(instCode, accList = new ArrayList()); + instAcc.put(instituteCode, accList = new ArrayList()); } for (int i = 0; i < nodeList.size(); i++) { final AccessionJson aj = (AccessionJson) nodeList.get(i); @@ -149,11 +149,11 @@ public class MockGenesysServer { if (aj.getMlsStat() != null) { existing.setMlsStat(aj.getMlsStat()); } - if (aj.getAcceNumb() != null) { - existing.setAcceNumb(aj.getAcceNumb()); + if (aj.getAccessionNumber() != null) { + existing.setAccessionNumber(aj.getAccessionNumber()); } - if (aj.getAcqDate() != null) { - existing.setAcqDate(aj.getAcqDate()); + if (aj.getAcquisitionDate() != null) { + existing.setAcquisitionDate(aj.getAcquisitionDate()); } if (aj.getAncest() != null) { existing.setAncest(aj.getAncest()); @@ -188,17 +188,8 @@ public class MockGenesysServer { if (aj.getSampStat() != null) { existing.setSampStat(aj.getSampStat()); } - if (aj.getSpecies() != null) { - existing.setSpecies(aj.getSpecies()); - } - if (aj.getSpauthor() != null) { - existing.setSpauthor(aj.getSpauthor()); - } - if (aj.getSubtaxa() != null) { - existing.setSubtaxa(aj.getSubtaxa()); - } - if (aj.getSubtauthor() != null) { - existing.setSubtauthor(aj.getSubtauthor()); + if (aj.getTaxonomy() != null) { + } if (aj.getUuid() != null) { if (existing.getUuid() == null) { @@ -226,7 +217,7 @@ public class MockGenesysServer { return findMatch(accList, aj.getUuid()); } else { for (final AccessionJson m : accList) { - if (m.getInstCode().equals(aj.getInstCode()) && m.getAcceNumb().equals(aj.getAcceNumb()) && m.getGenus().equals(aj.getGenus())) { + if (m.getInstituteCode().equals(aj.getInstituteCode()) && m.getAccessionNumber().equals(aj.getAccessionNumber()) && m.getTaxonomy().getGenus().equals(aj.getTaxonomy().getGenus())) { return m; } } @@ -253,13 +244,13 @@ public class MockGenesysServer { /** * Gets the accessions page. * - * @param instCode the inst code + * @param instituteCode the inst code * @param page the page * @return the accessions page * @throws MockGenesysException the mock genesys exception */ - protected List getAccessionsPage(final String instCode, final int page) throws MockGenesysException { - final List accList = instAcc.get(instCode); + protected List getAccessionsPage(final String instituteCode, final int page) throws MockGenesysException { + final List accList = instAcc.get(instituteCode); if (accList == null || accList.isEmpty()) { System.err.println("Returning blank list"); return BLANK_LIST; diff --git a/src/test/java/org/geneys2/client/oauth/ModelTests.java b/src/test/java/org/geneys2/client/oauth/ModelTests.java index ab66263fc2c9da6f47a3da088033eb0db6299ede..6c71e147d15619f2e7da80cb048d7e969694b9fb 100644 --- a/src/test/java/org/geneys2/client/oauth/ModelTests.java +++ b/src/test/java/org/geneys2/client/oauth/ModelTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,7 @@ import com.fasterxml.jackson.databind.SerializationFeature; import org.genesys2.client.oauth.api.accession.AccessionJson; 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.Api1Constants.Taxonomy; import org.genesys2.client.oauth.api.accession.GeoJson; import org.junit.Test; @@ -57,9 +58,9 @@ public class ModelTests { @Test public void serializeCore() throws IOException { final AccessionJson aj = new AccessionJson(); - aj.setInstCode("INS000"); - aj.setAcceNumb("ACC-1"); - aj.setGenus("Genus"); + aj.setInstituteCode("INS000"); + aj.setAccessionNumber("ACC-1"); + aj.getTaxonomy().setGenus("Genus"); final JsonNode tree = getJsonTree(aj); System.err.println(tree.toString()); @@ -69,8 +70,8 @@ public class ModelTests { assertThat("No ACCENUMB", tree.get(Accession.ACCENUMB), notNullValue()); assertThat("ACCENUMB must be a text node", tree.get(Accession.ACCENUMB), JsonNodeMatchers.isTextNode()); assertThat("Wrong ACCENUMB", tree.get(Accession.ACCENUMB).asText(), is("ACC-1")); - assertThat("No GENUS", tree.get(Accession.GENUS), notNullValue()); - assertThat("Wrong GENUS", tree.get(Accession.GENUS).asText(), is("Genus")); + assertThat("No GENUS", tree.get(Accession.TAXONOMY).get(Taxonomy.GENUS), notNullValue()); + assertThat("Wrong GENUS", tree.get(Accession.TAXONOMY).get(Taxonomy.GENUS).asText(), is("Genus")); } /** diff --git a/src/test/java/org/geneys2/client/oauth/RemarkTest.java b/src/test/java/org/geneys2/client/oauth/RemarkTest.java index 167c320aabd974d23615dd62232d7c42a30f9167..d215cce045e861cc436fed368f6cd96c7e3f03dd 100644 --- a/src/test/java/org/geneys2/client/oauth/RemarkTest.java +++ b/src/test/java/org/geneys2/client/oauth/RemarkTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Global Crop Diversity Trust + * Copyright 2019 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.