From f0aa40c9ffa0c78fd9d6450cb0a0896973328a7b Mon Sep 17 00:00:00 2001 From: Aldrin Batac Date: Wed, 15 Jan 2020 09:54:17 +0800 Subject: [PATCH 1/4] Improved class name and uodated the url endpoint name to adhere to REST standards. Simplified the code. IBP-3273 --- .../api/ibpworkbench/rest/BreedingView.java | 121 ------------------ .../rest/BreedingViewResource.java | 98 ++++++++++++++ .../rest/BreedingViewResourceTest.java | 45 +++++++ .../ibpworkbench/rest/BreedingViewTest.java | 80 ------------ 4 files changed, 143 insertions(+), 201 deletions(-) delete mode 100644 src/main/java/org/ibp/api/ibpworkbench/rest/BreedingView.java create mode 100644 src/main/java/org/ibp/api/ibpworkbench/rest/BreedingViewResource.java create mode 100644 src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewResourceTest.java delete mode 100644 src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewTest.java diff --git a/src/main/java/org/ibp/api/ibpworkbench/rest/BreedingView.java b/src/main/java/org/ibp/api/ibpworkbench/rest/BreedingView.java deleted file mode 100644 index 4c4c1525fc..0000000000 --- a/src/main/java/org/ibp/api/ibpworkbench/rest/BreedingView.java +++ /dev/null @@ -1,121 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2012, All Rights Reserved. - * - * Generation Challenge Programme (GCP) - * - * - * This software is licensed for use under the terms of the GNU General Public License (http://bit.ly/8Ztv8M) and the provisions of Part F - * of the Generation Challenge Programme Amended Consortium Agreement (http://bit.ly/KQX1nL) - * - *******************************************************************************/ - -package org.ibp.api.ibpworkbench.rest; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; -import org.ibp.api.ibpworkbench.constants.WebAPIConstants; -import org.ibp.api.ibpworkbench.model.DataResponse; -import org.ibp.api.ibpworkbench.service.BreedingViewService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -@Api(value = "Breeding View Resource") -@RestController -@RequestMapping("/breeding_view") -// TODO rename to BreedingViewResource -public class BreedingView { - - private static final Logger LOG = LoggerFactory.getLogger(BreedingView.class); - - @Autowired - private BreedingViewService breedingViewService; - - @ApiOperation(value = "Save the Single-Site Analysis CSV output file with heritability", notes = "", response = DataResponse.class) - @RequestMapping(value = "{cropName}/ssa/save_result_summary", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE}) - @ResponseBody - public DataResponse saveSsaResultSummary( - @ApiParam(value = "Path and filename of the SSA output file", required = true) @RequestParam("mainOutputFilePath") final String mainOutputFilePath, - - @ApiParam(value = "Path and filename of the Summary output file", required = true) @RequestParam("SummaryOutputFilePath") - final String summaryOutputFilePath, - - @ApiParam(value = "Path and filename of the Outlier output file", required = false) @RequestParam(value = "OutlierFilePath", required = false) - final String outlierOutputFilePath, - - @ApiParam(value = "Current Project ID", required = true) @RequestParam("WorkbenchProjectId") final String workbenchProjectId, - - @ApiParam(value = "Study ID", required = true) @RequestParam("StudyId") final String studyId, - - @ApiParam(value = "Input Dataset ID", required = true) @RequestParam("InputDataSetId") final String inputDataSetId, - - @ApiParam(value = "Output Dataset ID", required = true) @RequestParam("OutputDataSetId") final String outputDataSetId, - - @PathVariable(value = "cropName") final String cropName - ) { - DataResponse response; - - try { - final Map params = new HashMap<>(); - final List errors = new ArrayList<>(); - if (mainOutputFilePath == null || mainOutputFilePath.isEmpty()) { - errors.add("mainOutputFilePath is a required field!"); - } - if (summaryOutputFilePath == null || summaryOutputFilePath.isEmpty()) { - errors.add("summaryOutputFilePath is a required field!"); - } - if (workbenchProjectId == null || workbenchProjectId.isEmpty()) { - errors.add("WorkbenchProjectId is a required field!"); - } - if (studyId == null || studyId.isEmpty()) { - errors.add("StudyId is a required field!"); - } - - if (inputDataSetId == null || inputDataSetId.isEmpty()) { - errors.add("InputDataSetId is a required field!"); - } - if (outputDataSetId == null || outputDataSetId.isEmpty()) { - errors.add("OutputDataSetId is a required field!"); - } - - if (errors.isEmpty()) { - params.put(WebAPIConstants.MAIN_OUTPUT_FILE_PATH.getParamValue(), mainOutputFilePath); - params.put(WebAPIConstants.SUMMARY_OUTPUT_FILE_PATH.getParamValue(), summaryOutputFilePath); - params.put(WebAPIConstants.OUTLIER_OUTPUT_FILE_PATH.getParamValue(), outlierOutputFilePath); - params.put(WebAPIConstants.WORKBENCH_PROJECT_ID.getParamValue(), workbenchProjectId); - params.put(WebAPIConstants.STUDY_ID.getParamValue(), studyId); - params.put(WebAPIConstants.INPUT_DATASET_ID.getParamValue(), inputDataSetId); - params.put(WebAPIConstants.OUTPUT_DATASET_ID.getParamValue(), outputDataSetId); - - this.breedingViewService.execute(params, errors); - response = new DataResponse(true, "Successfully invoked service."); - } else { - response = new DataResponse(false, "Errors invoking web service: " + errors); - } - } catch (final Exception e) { - BreedingView.LOG.debug(e.getMessage(), e); - response = new DataResponse(false, "Failed to invoke service: " + e.toString()); - } - - return response; - } - - public void setBreedingViewService(final BreedingViewService breedingViewService) { - this.breedingViewService = breedingViewService; - } - - -} diff --git a/src/main/java/org/ibp/api/ibpworkbench/rest/BreedingViewResource.java b/src/main/java/org/ibp/api/ibpworkbench/rest/BreedingViewResource.java new file mode 100644 index 0000000000..7f9b235fc4 --- /dev/null +++ b/src/main/java/org/ibp/api/ibpworkbench/rest/BreedingViewResource.java @@ -0,0 +1,98 @@ +/******************************************************************************* + * Copyright (c) 2012, All Rights Reserved. + * + * Generation Challenge Programme (GCP) + * + * + * This software is licensed for use under the terms of the GNU General Public License (http://bit.ly/8Ztv8M) and the provisions of Part F + * of the Generation Challenge Programme Amended Consortium Agreement (http://bit.ly/KQX1nL) + * + *******************************************************************************/ + +package org.ibp.api.ibpworkbench.rest; + +import com.google.common.base.Preconditions; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import liquibase.util.StringUtils; +import org.ibp.api.ibpworkbench.constants.WebAPIConstants; +import org.ibp.api.ibpworkbench.exceptions.IBPWebServiceException; +import org.ibp.api.ibpworkbench.model.DataResponse; +import org.ibp.api.ibpworkbench.service.BreedingViewService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Api(value = "Breeding View Resource") +@RestController +@RequestMapping("/breeding-view") +public class BreedingViewResource { + + private static final Logger LOG = LoggerFactory.getLogger(BreedingViewResource.class); + + @Autowired + private BreedingViewService breedingViewService; + + @ApiOperation(value = "Save the Single-Site Analysis CSV output files", notes = "", response = DataResponse.class) + @RequestMapping(value = "{cropName}/single-site-analysis/summary-statistics", method = RequestMethod.GET, produces = { + MediaType.APPLICATION_XML_VALUE}) + @ResponseBody + public DataResponse saveSingleSiteAnalysisData( + @ApiParam(value = "Path and filename of the SSA output file", required = true) @RequestParam("mainOutputFilePath") + final String mainOutputFilePath, + @ApiParam(value = "Path and filename of the Summary output file", required = true) @RequestParam("SummaryOutputFilePath") + final String summaryOutputFilePath, + @ApiParam(value = "Path and filename of the Outlier output file", required = false) + @RequestParam(value = "OutlierFilePath", required = false) final String outlierOutputFilePath, + @ApiParam(value = "Current Project ID", required = true) @RequestParam("WorkbenchProjectId") final String workbenchProjectId, + @ApiParam(value = "Study ID", required = true) @RequestParam("StudyId") final String studyId, + @ApiParam(value = "Input Dataset ID", required = true) @RequestParam("InputDataSetId") final String inputDataSetId, + @ApiParam(value = "Output Dataset ID", required = true) @RequestParam("OutputDataSetId") final String outputDataSetId, + @PathVariable(value = "cropName") final String cropName + ) { + Preconditions.checkArgument(!StringUtils.isEmpty(mainOutputFilePath), "mainOutputFilePath is required."); + Preconditions.checkArgument(!StringUtils.isEmpty(summaryOutputFilePath), "summaryOutputFilePath is required."); + Preconditions.checkArgument(!StringUtils.isEmpty(workbenchProjectId), "workbenchProjectId is required."); + Preconditions.checkArgument(!StringUtils.isEmpty(studyId), "studyId is required."); + Preconditions.checkArgument(!StringUtils.isEmpty(inputDataSetId), "inputDataSetId is required."); + Preconditions.checkArgument(!StringUtils.isEmpty(outputDataSetId), "outputDataSetId is required."); + + final Map params = new HashMap<>(); + final List errors = new ArrayList<>(); + params.put(WebAPIConstants.MAIN_OUTPUT_FILE_PATH.getParamValue(), mainOutputFilePath); + params.put(WebAPIConstants.SUMMARY_OUTPUT_FILE_PATH.getParamValue(), summaryOutputFilePath); + params.put(WebAPIConstants.OUTLIER_OUTPUT_FILE_PATH.getParamValue(), outlierOutputFilePath); + params.put(WebAPIConstants.WORKBENCH_PROJECT_ID.getParamValue(), workbenchProjectId); + params.put(WebAPIConstants.STUDY_ID.getParamValue(), studyId); + params.put(WebAPIConstants.INPUT_DATASET_ID.getParamValue(), inputDataSetId); + params.put(WebAPIConstants.OUTPUT_DATASET_ID.getParamValue(), outputDataSetId); + + try { + this.breedingViewService.execute(params, errors); + return new DataResponse(true, "Successfully invoked service."); + } catch (IBPWebServiceException e) { + LOG.error(e.getMessage(), e); + return new DataResponse(false, "Errors invoking web service: " + errors); + } + + } + + public void setBreedingViewService(final BreedingViewService breedingViewService) { + this.breedingViewService = breedingViewService; + } + +} + diff --git a/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewResourceTest.java b/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewResourceTest.java new file mode 100644 index 0000000000..7052ed2ee6 --- /dev/null +++ b/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewResourceTest.java @@ -0,0 +1,45 @@ + +package org.ibp.api.ibpworkbench.rest; + +import org.apache.commons.lang3.RandomStringUtils; +import org.ibp.api.ibpworkbench.model.DataResponse; +import org.ibp.api.ibpworkbench.service.BreedingViewService; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class BreedingViewResourceTest { + + private BreedingViewResource breedingView; + + @Mock + private BreedingViewService breedingViewService; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + this.breedingView = new BreedingViewResource(); + this.breedingView.setBreedingViewService(this.breedingViewService); + } + + @Test + public void testSaveSsaResultSummaryWithValidRequiredParams() { + final String mainOutputFilePath = "C:\\BMS4\\workspace\\maize_test\\breeding_view\\output\\BMSOutput_1_6.csv"; + final String summaryOutputFilePath = "C:\\BMS4\\workspace\\maize_test\\breeding_view\\output\\BMSSummary_1_6.csv"; + final String outlierOutputFilePath = null; + final String workbenchProjectId = "1"; + final String studyId = "1"; + final String inputDataSetId = "3"; + final String outputDataSetId = "0"; + final String cropName = "maize"; + + final DataResponse response = + this.breedingView.saveSingleSiteAnalysisData(mainOutputFilePath, summaryOutputFilePath, outlierOutputFilePath, + workbenchProjectId, studyId, inputDataSetId, outputDataSetId, cropName); + Assert.assertTrue("Web service should succeed", response.isSuccessful()); + Assert.assertTrue("Web service should return no errors", response.getMessage().indexOf("Successfully invoked service.") != -1); + } + +} diff --git a/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewTest.java b/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewTest.java deleted file mode 100644 index 8ab8d10446..0000000000 --- a/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewTest.java +++ /dev/null @@ -1,80 +0,0 @@ - -package org.ibp.api.ibpworkbench.rest; - -import org.ibp.api.ibpworkbench.model.DataResponse; -import org.ibp.api.ibpworkbench.service.BreedingViewService; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -public class BreedingViewTest { - - private BreedingView breedingView; - @Mock - private BreedingViewService breedingViewService; - - private String mainOutputFilePath; - private String summaryOutputFilePath; - private String outlierOutputFilePath; - private String workbenchProjectId; - private String studyId; - private String inputDataSetId; - private String outputDataSetId; - private String cropName; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - this.breedingView = new BreedingView(); - this.breedingView.setBreedingViewService(this.breedingViewService); - } - - @Test - public void testSaveSsaResultSummaryWhenRequiredParamsAreNull() { - this.mainOutputFilePath = null; - this.summaryOutputFilePath = null; - this.workbenchProjectId = null; - this.studyId = null; - this.inputDataSetId = null; - this.outputDataSetId = null; - this.cropName = null; - - final DataResponse response = - this.breedingView.saveSsaResultSummary(this.mainOutputFilePath, this.summaryOutputFilePath, this.outlierOutputFilePath, - this.workbenchProjectId, this.studyId, this.inputDataSetId, this.outputDataSetId, this.cropName); - Assert.assertFalse("Web service should fail", response.isSuccessful()); - Assert.assertTrue("Web service should return errors", response.getMessage().indexOf("Errors invoking web service") != -1); - Assert.assertTrue("Web service should flag a null/empty mainOutputFilePath", - response.getMessage().indexOf("mainOutputFilePath is a required field!") != -1); - Assert.assertTrue("Web service should flag a null/empty summaryOutputFilePath", - response.getMessage().indexOf("summaryOutputFilePath is a required field!") != -1); - Assert.assertTrue("Web service should flag a null/empty WorkbenchProjectId", - response.getMessage().indexOf("WorkbenchProjectId is a required field!") != -1); - Assert.assertTrue("Web service should flag a null/empty StudyId", - response.getMessage().indexOf("StudyId is a required field!") != -1); - Assert.assertTrue("Web service should flag a null/empty InputDataSetId", - response.getMessage().indexOf("InputDataSetId is a required field!") != -1); - Assert.assertTrue("Web service should flag a null/empty OutputDataSetId", - response.getMessage().indexOf("OutputDataSetId is a required field!") != -1); - } - - @Test - public void testSaveSsaResultSummaryWithValidRequiredParams() { - this.mainOutputFilePath = "C:\\BMS4\\workspace\\maize_test\\breeding_view\\output\\BMSOutput_1_6.csv"; - this.summaryOutputFilePath = "C:\\BMS4\\workspace\\maize_test\\breeding_view\\output\\BMSSummary_1_6.csv"; - this.outlierOutputFilePath = null; - this.workbenchProjectId = "1"; - this.studyId = "1"; - this.inputDataSetId = "3"; - this.outputDataSetId = "0"; - - final DataResponse response = - this.breedingView.saveSsaResultSummary(this.mainOutputFilePath, this.summaryOutputFilePath, this.outlierOutputFilePath, - this.workbenchProjectId, this.studyId, this.inputDataSetId, this.outputDataSetId, this.cropName); - Assert.assertTrue("Web service should succeed", response.isSuccessful()); - Assert.assertTrue("Web service should return no errors", response.getMessage().indexOf("Successfully invoked service.") != -1); - } - -} From 78bbec31acc0ce4953b5cf915714b9b816ef2cc3 Mon Sep 17 00:00:00 2001 From: Aldrin Batac Date: Wed, 15 Jan 2020 11:35:08 +0800 Subject: [PATCH 2/4] Rename package and class names. IBP-3273 --- .../breedingview/BreedingViewResponse.java} | 12 ++--- .../BreedingViewImportException.java | 6 +-- .../BreedingViewInvalidFormatException.java | 2 +- .../IBPWebServiceException.java | 4 +- .../ibp/api/ibpworkbench/util/ObjectUtil.java | 52 ------------------- .../breedingview}/BreedingViewService.java | 4 +- .../breedingview/BreedingViewParameter.java} | 15 +++--- .../BreedingViewServiceImpl.java | 35 ++++++------- .../breedingview}/BreedingViewResource.java | 48 ++++++++--------- .../api/security/SecurityConfiguration.java | 2 +- .../rest/BreedingViewResourceTest.java | 8 +-- .../service/BreedingViewServiceImplTest.java | 21 ++++---- 12 files changed, 79 insertions(+), 130 deletions(-) rename src/main/java/org/ibp/api/{ibpworkbench/model/DataResponse.java => domain/breedingview/BreedingViewResponse.java} (80%) rename src/main/java/org/ibp/api/{ibpworkbench/exceptions => exception}/BreedingViewImportException.java (62%) rename src/main/java/org/ibp/api/{ibpworkbench/exceptions => exception}/BreedingViewInvalidFormatException.java (90%) rename src/main/java/org/ibp/api/{ibpworkbench/exceptions => exception}/IBPWebServiceException.java (66%) delete mode 100644 src/main/java/org/ibp/api/ibpworkbench/util/ObjectUtil.java rename src/main/java/org/ibp/api/{ibpworkbench/service => java/breedingview}/BreedingViewService.java (86%) rename src/main/java/org/ibp/api/{ibpworkbench/constants/WebAPIConstants.java => java/impl/middleware/breedingview/BreedingViewParameter.java} (61%) rename src/main/java/org/ibp/api/{ibpworkbench/service => java/impl/middleware/breedingview}/BreedingViewServiceImpl.java (68%) rename src/main/java/org/ibp/api/{ibpworkbench/rest => rest/breedingview}/BreedingViewResource.java (66%) diff --git a/src/main/java/org/ibp/api/ibpworkbench/model/DataResponse.java b/src/main/java/org/ibp/api/domain/breedingview/BreedingViewResponse.java similarity index 80% rename from src/main/java/org/ibp/api/ibpworkbench/model/DataResponse.java rename to src/main/java/org/ibp/api/domain/breedingview/BreedingViewResponse.java index 495fddbcfd..e23beded3d 100644 --- a/src/main/java/org/ibp/api/ibpworkbench/model/DataResponse.java +++ b/src/main/java/org/ibp/api/domain/breedingview/BreedingViewResponse.java @@ -9,7 +9,7 @@ * *******************************************************************************/ -package org.ibp.api.ibpworkbench.model; +package org.ibp.api.domain.breedingview; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; @@ -17,15 +17,15 @@ @XmlRootElement(name = "Response") @XmlType(propOrder = {"successful", "message"}) -public class DataResponse { +public class BreedingViewResponse { private boolean successful; private String message; - public DataResponse() { + public BreedingViewResponse() { } - public DataResponse(boolean successful, String message) { + public BreedingViewResponse(final boolean successful, final String message) { this.successful = successful; this.message = message; } @@ -35,7 +35,7 @@ public boolean isSuccessful() { return this.successful; } - public void setSuccessful(boolean successful) { + public void setSuccessful(final boolean successful) { this.successful = successful; } @@ -44,7 +44,7 @@ public String getMessage() { return this.message; } - public void setMessage(String message) { + public void setMessage(final String message) { this.message = message; } diff --git a/src/main/java/org/ibp/api/ibpworkbench/exceptions/BreedingViewImportException.java b/src/main/java/org/ibp/api/exception/BreedingViewImportException.java similarity index 62% rename from src/main/java/org/ibp/api/ibpworkbench/exceptions/BreedingViewImportException.java rename to src/main/java/org/ibp/api/exception/BreedingViewImportException.java index b3f871c37c..e0cabda893 100644 --- a/src/main/java/org/ibp/api/ibpworkbench/exceptions/BreedingViewImportException.java +++ b/src/main/java/org/ibp/api/exception/BreedingViewImportException.java @@ -1,5 +1,5 @@ -package org.ibp.api.ibpworkbench.exceptions; +package org.ibp.api.exception; public class BreedingViewImportException extends Exception { @@ -9,11 +9,11 @@ public BreedingViewImportException() { super("Error with importing breeding view output file."); } - public BreedingViewImportException(String message) { + public BreedingViewImportException(final String message) { super(message); } - public BreedingViewImportException(String message, Throwable cause) { + public BreedingViewImportException(final String message, final Throwable cause) { super(message, cause); } } diff --git a/src/main/java/org/ibp/api/ibpworkbench/exceptions/BreedingViewInvalidFormatException.java b/src/main/java/org/ibp/api/exception/BreedingViewInvalidFormatException.java similarity index 90% rename from src/main/java/org/ibp/api/ibpworkbench/exceptions/BreedingViewInvalidFormatException.java rename to src/main/java/org/ibp/api/exception/BreedingViewInvalidFormatException.java index 09dbbe8fa2..b0af7e8d73 100644 --- a/src/main/java/org/ibp/api/ibpworkbench/exceptions/BreedingViewInvalidFormatException.java +++ b/src/main/java/org/ibp/api/exception/BreedingViewInvalidFormatException.java @@ -1,5 +1,5 @@ -package org.ibp.api.ibpworkbench.exceptions; +package org.ibp.api.exception; public class BreedingViewInvalidFormatException extends Exception { diff --git a/src/main/java/org/ibp/api/ibpworkbench/exceptions/IBPWebServiceException.java b/src/main/java/org/ibp/api/exception/IBPWebServiceException.java similarity index 66% rename from src/main/java/org/ibp/api/ibpworkbench/exceptions/IBPWebServiceException.java rename to src/main/java/org/ibp/api/exception/IBPWebServiceException.java index bd0f540eca..afbe9ca963 100644 --- a/src/main/java/org/ibp/api/ibpworkbench/exceptions/IBPWebServiceException.java +++ b/src/main/java/org/ibp/api/exception/IBPWebServiceException.java @@ -1,5 +1,5 @@ -package org.ibp.api.ibpworkbench.exceptions; +package org.ibp.api.exception; public class IBPWebServiceException extends Exception { @@ -9,7 +9,7 @@ public IBPWebServiceException() { super(); } - public IBPWebServiceException(String message) { + public IBPWebServiceException(final String message) { super(message); } diff --git a/src/main/java/org/ibp/api/ibpworkbench/util/ObjectUtil.java b/src/main/java/org/ibp/api/ibpworkbench/util/ObjectUtil.java deleted file mode 100644 index 156ed6797d..0000000000 --- a/src/main/java/org/ibp/api/ibpworkbench/util/ObjectUtil.java +++ /dev/null @@ -1,52 +0,0 @@ - -package org.ibp.api.ibpworkbench.util; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - -public class ObjectUtil { - - private static final Logger LOG = LoggerFactory.getLogger(ObjectUtil.class); - - public void serializeObject(T o, String filename) { - - // save the object to file - FileOutputStream fos = null; - ObjectOutputStream out = null; - try { - fos = new FileOutputStream(filename); - out = new ObjectOutputStream(fos); - out.writeObject(o); - - out.close(); - } catch (IOException ex) { - ObjectUtil.LOG.error("Error: ", ex); - } - - } - - public T deserializeFromFile(String filename) { - - FileInputStream fis = null; - ObjectInputStream in = null; - T object = null; - - try { - fis = new FileInputStream(filename); - in = new ObjectInputStream(fis); - object = (T) in.readObject(); - in.close(); - } catch (Exception ex) { - ObjectUtil.LOG.error("Error: ", ex); - } - - return object; - } - -} diff --git a/src/main/java/org/ibp/api/ibpworkbench/service/BreedingViewService.java b/src/main/java/org/ibp/api/java/breedingview/BreedingViewService.java similarity index 86% rename from src/main/java/org/ibp/api/ibpworkbench/service/BreedingViewService.java rename to src/main/java/org/ibp/api/java/breedingview/BreedingViewService.java index a238c1589f..a3aa3c9d31 100644 --- a/src/main/java/org/ibp/api/ibpworkbench/service/BreedingViewService.java +++ b/src/main/java/org/ibp/api/java/breedingview/BreedingViewService.java @@ -9,9 +9,9 @@ * *******************************************************************************/ -package org.ibp.api.ibpworkbench.service; +package org.ibp.api.java.breedingview; -import org.ibp.api.ibpworkbench.exceptions.IBPWebServiceException; +import org.ibp.api.exception.IBPWebServiceException; import java.util.List; import java.util.Map; diff --git a/src/main/java/org/ibp/api/ibpworkbench/constants/WebAPIConstants.java b/src/main/java/org/ibp/api/java/impl/middleware/breedingview/BreedingViewParameter.java similarity index 61% rename from src/main/java/org/ibp/api/ibpworkbench/constants/WebAPIConstants.java rename to src/main/java/org/ibp/api/java/impl/middleware/breedingview/BreedingViewParameter.java index 13b1837dcd..58af578059 100644 --- a/src/main/java/org/ibp/api/ibpworkbench/constants/WebAPIConstants.java +++ b/src/main/java/org/ibp/api/java/impl/middleware/breedingview/BreedingViewParameter.java @@ -9,17 +9,18 @@ * *******************************************************************************/ -package org.ibp.api.ibpworkbench.constants; +package org.ibp.api.java.impl.middleware.breedingview; -public enum WebAPIConstants { +public enum BreedingViewParameter { - MAIN_OUTPUT_FILE_PATH("mainOutputFilePath"), SUMMARY_OUTPUT_FILE_PATH("summaryOutputFilePath"), STUDY_ID("StudyId"), WORKBENCH_PROJECT_ID( - "WorkbenchProjectId"), INPUT_DATASET_ID("InputDataSetId"), OUTPUT_DATASET_ID("OutputDataSetId"), OUTLIER_OUTPUT_FILE_PATH( - "OutlierFilePath"); + MAIN_OUTPUT_FILE_PATH("mainOutputFilePath"), SUMMARY_OUTPUT_FILE_PATH("summaryOutputFilePath"), STUDY_ID( + "StudyId"), WORKBENCH_PROJECT_ID( + "WorkbenchProjectId"), INPUT_DATASET_ID("InputDataSetId"), OUTPUT_DATASET_ID("OutputDataSetId"), OUTLIER_OUTPUT_FILE_PATH( + "OutlierFilePath"); - private String paramValue; + private final String paramValue; - private WebAPIConstants(String param) { + private BreedingViewParameter(final String param) { this.paramValue = param; } diff --git a/src/main/java/org/ibp/api/ibpworkbench/service/BreedingViewServiceImpl.java b/src/main/java/org/ibp/api/java/impl/middleware/breedingview/BreedingViewServiceImpl.java similarity index 68% rename from src/main/java/org/ibp/api/ibpworkbench/service/BreedingViewServiceImpl.java rename to src/main/java/org/ibp/api/java/impl/middleware/breedingview/BreedingViewServiceImpl.java index ca0baed09b..4482876d63 100644 --- a/src/main/java/org/ibp/api/ibpworkbench/service/BreedingViewServiceImpl.java +++ b/src/main/java/org/ibp/api/java/impl/middleware/breedingview/BreedingViewServiceImpl.java @@ -9,30 +9,29 @@ * *******************************************************************************/ -package org.ibp.api.ibpworkbench.service; - -import java.io.File; -import java.util.List; -import java.util.Map; +package org.ibp.api.java.impl.middleware.breedingview; +import com.rits.cloning.Cloner; import org.generationcp.commons.service.BreedingViewImportService; import org.generationcp.middleware.domain.dms.ExperimentValues; import org.generationcp.middleware.domain.dms.VariableTypeList; -import org.ibp.api.ibpworkbench.constants.WebAPIConstants; -import org.ibp.api.ibpworkbench.exceptions.IBPWebServiceException; +import org.ibp.api.exception.IBPWebServiceException; +import org.ibp.api.java.breedingview.BreedingViewService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; -import com.rits.cloning.Cloner; +import java.io.File; +import java.util.List; +import java.util.Map; @Component public class BreedingViewServiceImpl implements BreedingViewService { @Autowired - private BreedingViewImportService importService; + private BreedingViewImportService breedingViewImportService; @Autowired private Cloner cloner; @@ -50,19 +49,19 @@ public void execute(final Map params, final List errors) try { - final String mainOutputFilePath = params.get(WebAPIConstants.MAIN_OUTPUT_FILE_PATH.getParamValue()); - final String summaryOutputFilePath = params.get(WebAPIConstants.SUMMARY_OUTPUT_FILE_PATH.getParamValue()); - final String outlierOutputFilePath = params.get(WebAPIConstants.OUTLIER_OUTPUT_FILE_PATH.getParamValue()); - final int studyId = Integer.parseInt(params.get(WebAPIConstants.STUDY_ID.getParamValue())); + final String mainOutputFilePath = params.get(BreedingViewParameter.MAIN_OUTPUT_FILE_PATH.getParamValue()); + final String summaryOutputFilePath = params.get(BreedingViewParameter.SUMMARY_OUTPUT_FILE_PATH.getParamValue()); + final String outlierOutputFilePath = params.get(BreedingViewParameter.OUTLIER_OUTPUT_FILE_PATH.getParamValue()); + final int studyId = Integer.parseInt(params.get(BreedingViewParameter.STUDY_ID.getParamValue())); - this.importService.importMeansData(new File(mainOutputFilePath), studyId); + this.breedingViewImportService.importMeansData(new File(mainOutputFilePath), studyId); if (outlierOutputFilePath != null && !"".equals(outlierOutputFilePath)) { - this.importService.importOutlierData(new File(outlierOutputFilePath), studyId); + this.breedingViewImportService.importOutlierData(new File(outlierOutputFilePath), studyId); } if (summaryOutputFilePath != null && !"".equals(summaryOutputFilePath)) { - this.importService.importSummaryStatsData(new File(summaryOutputFilePath), studyId); + this.breedingViewImportService.importSummaryStatsData(new File(summaryOutputFilePath), studyId); } } catch (final Exception e) { @@ -96,8 +95,8 @@ protected VariableTypeList getVariableTypeListSummaryStats() { return this.variableTypeListSummaryStats; } - public void setImportService(final BreedingViewImportService importService) { - this.importService = importService; + public void setBreedingViewImportService(final BreedingViewImportService breedingViewImportService) { + this.breedingViewImportService = breedingViewImportService; } } diff --git a/src/main/java/org/ibp/api/ibpworkbench/rest/BreedingViewResource.java b/src/main/java/org/ibp/api/rest/breedingview/BreedingViewResource.java similarity index 66% rename from src/main/java/org/ibp/api/ibpworkbench/rest/BreedingViewResource.java rename to src/main/java/org/ibp/api/rest/breedingview/BreedingViewResource.java index 7f9b235fc4..2b9eb42cd8 100644 --- a/src/main/java/org/ibp/api/ibpworkbench/rest/BreedingViewResource.java +++ b/src/main/java/org/ibp/api/rest/breedingview/BreedingViewResource.java @@ -9,27 +9,27 @@ * *******************************************************************************/ -package org.ibp.api.ibpworkbench.rest; +package org.ibp.api.rest.breedingview; import com.google.common.base.Preconditions; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import liquibase.util.StringUtils; -import org.ibp.api.ibpworkbench.constants.WebAPIConstants; -import org.ibp.api.ibpworkbench.exceptions.IBPWebServiceException; -import org.ibp.api.ibpworkbench.model.DataResponse; -import org.ibp.api.ibpworkbench.service.BreedingViewService; +import org.ibp.api.domain.breedingview.BreedingViewResponse; +import org.ibp.api.exception.IBPWebServiceException; +import org.ibp.api.java.breedingview.BreedingViewService; +import org.ibp.api.java.impl.middleware.breedingview.BreedingViewParameter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; import java.util.HashMap; @@ -37,7 +37,7 @@ import java.util.Map; @Api(value = "Breeding View Resource") -@RestController +@Controller @RequestMapping("/breeding-view") public class BreedingViewResource { @@ -46,18 +46,18 @@ public class BreedingViewResource { @Autowired private BreedingViewService breedingViewService; - @ApiOperation(value = "Save the Single-Site Analysis CSV output files", notes = "", response = DataResponse.class) + @ApiOperation(value = "Save the Single-Site Analysis CSV output files", notes = "", response = BreedingViewResponse.class) @RequestMapping(value = "{cropName}/single-site-analysis/summary-statistics", method = RequestMethod.GET, produces = { MediaType.APPLICATION_XML_VALUE}) @ResponseBody - public DataResponse saveSingleSiteAnalysisData( - @ApiParam(value = "Path and filename of the SSA output file", required = true) @RequestParam("mainOutputFilePath") - final String mainOutputFilePath, - @ApiParam(value = "Path and filename of the Summary output file", required = true) @RequestParam("SummaryOutputFilePath") - final String summaryOutputFilePath, - @ApiParam(value = "Path and filename of the Outlier output file", required = false) + public BreedingViewResponse saveSingleSiteAnalysisData( + @ApiParam(value = "Path and filename of the SSA output file that resides on the server", required = true) + @RequestParam("mainOutputFilePath") final String mainOutputFilePath, + @ApiParam(value = "Path and filename of the Summary output file that resides on the server", required = true) + @RequestParam("SummaryOutputFilePath") final String summaryOutputFilePath, + @ApiParam(value = "Path and filename of the Outlier output file that resides on the server", required = false) @RequestParam(value = "OutlierFilePath", required = false) final String outlierOutputFilePath, - @ApiParam(value = "Current Project ID", required = true) @RequestParam("WorkbenchProjectId") final String workbenchProjectId, + @ApiParam(value = "Workbench Project ID", required = true) @RequestParam("WorkbenchProjectId") final String workbenchProjectId, @ApiParam(value = "Study ID", required = true) @RequestParam("StudyId") final String studyId, @ApiParam(value = "Input Dataset ID", required = true) @RequestParam("InputDataSetId") final String inputDataSetId, @ApiParam(value = "Output Dataset ID", required = true) @RequestParam("OutputDataSetId") final String outputDataSetId, @@ -72,20 +72,20 @@ public DataResponse saveSingleSiteAnalysisData( final Map params = new HashMap<>(); final List errors = new ArrayList<>(); - params.put(WebAPIConstants.MAIN_OUTPUT_FILE_PATH.getParamValue(), mainOutputFilePath); - params.put(WebAPIConstants.SUMMARY_OUTPUT_FILE_PATH.getParamValue(), summaryOutputFilePath); - params.put(WebAPIConstants.OUTLIER_OUTPUT_FILE_PATH.getParamValue(), outlierOutputFilePath); - params.put(WebAPIConstants.WORKBENCH_PROJECT_ID.getParamValue(), workbenchProjectId); - params.put(WebAPIConstants.STUDY_ID.getParamValue(), studyId); - params.put(WebAPIConstants.INPUT_DATASET_ID.getParamValue(), inputDataSetId); - params.put(WebAPIConstants.OUTPUT_DATASET_ID.getParamValue(), outputDataSetId); + params.put(BreedingViewParameter.MAIN_OUTPUT_FILE_PATH.getParamValue(), mainOutputFilePath); + params.put(BreedingViewParameter.SUMMARY_OUTPUT_FILE_PATH.getParamValue(), summaryOutputFilePath); + params.put(BreedingViewParameter.OUTLIER_OUTPUT_FILE_PATH.getParamValue(), outlierOutputFilePath); + params.put(BreedingViewParameter.WORKBENCH_PROJECT_ID.getParamValue(), workbenchProjectId); + params.put(BreedingViewParameter.STUDY_ID.getParamValue(), studyId); + params.put(BreedingViewParameter.INPUT_DATASET_ID.getParamValue(), inputDataSetId); + params.put(BreedingViewParameter.OUTPUT_DATASET_ID.getParamValue(), outputDataSetId); try { this.breedingViewService.execute(params, errors); - return new DataResponse(true, "Successfully invoked service."); + return new BreedingViewResponse(true, "Successfully invoked service."); } catch (IBPWebServiceException e) { LOG.error(e.getMessage(), e); - return new DataResponse(false, "Errors invoking web service: " + errors); + return new BreedingViewResponse(false, "Errors invoking web service: " + errors); } } diff --git a/src/main/java/org/ibp/api/security/SecurityConfiguration.java b/src/main/java/org/ibp/api/security/SecurityConfiguration.java index 9335966436..b388410103 100644 --- a/src/main/java/org/ibp/api/security/SecurityConfiguration.java +++ b/src/main/java/org/ibp/api/security/SecurityConfiguration.java @@ -67,7 +67,7 @@ protected void configure(HttpSecurity http) throws Exception { .and() .authorizeRequests() .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() - .antMatchers("/", "/v2/api-docs/**", "/authenticate", "/brapi/**/token", "/breeding_view/**", "/brapi/authorize").permitAll() + .antMatchers("/", "/v2/api-docs/**", "/authenticate", "/brapi/**/token", "/breeding-view/**", "/brapi/authorize").permitAll() .anyRequest() .authenticated() .and() diff --git a/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewResourceTest.java b/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewResourceTest.java index 7052ed2ee6..b391987fc6 100644 --- a/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewResourceTest.java +++ b/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewResourceTest.java @@ -1,9 +1,9 @@ package org.ibp.api.ibpworkbench.rest; -import org.apache.commons.lang3.RandomStringUtils; -import org.ibp.api.ibpworkbench.model.DataResponse; -import org.ibp.api.ibpworkbench.service.BreedingViewService; +import org.ibp.api.domain.breedingview.BreedingViewResponse; +import org.ibp.api.java.breedingview.BreedingViewService; +import org.ibp.api.rest.breedingview.BreedingViewResource; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -35,7 +35,7 @@ public void testSaveSsaResultSummaryWithValidRequiredParams() { final String outputDataSetId = "0"; final String cropName = "maize"; - final DataResponse response = + final BreedingViewResponse response = this.breedingView.saveSingleSiteAnalysisData(mainOutputFilePath, summaryOutputFilePath, outlierOutputFilePath, workbenchProjectId, studyId, inputDataSetId, outputDataSetId, cropName); Assert.assertTrue("Web service should succeed", response.isSuccessful()); diff --git a/src/test/java/org/ibp/api/ibpworkbench/service/BreedingViewServiceImplTest.java b/src/test/java/org/ibp/api/ibpworkbench/service/BreedingViewServiceImplTest.java index 0ef56d943a..3a38705088 100644 --- a/src/test/java/org/ibp/api/ibpworkbench/service/BreedingViewServiceImplTest.java +++ b/src/test/java/org/ibp/api/ibpworkbench/service/BreedingViewServiceImplTest.java @@ -8,8 +8,9 @@ import org.generationcp.commons.exceptions.BreedingViewImportException; import org.generationcp.commons.service.BreedingViewImportService; -import org.ibp.api.ibpworkbench.constants.WebAPIConstants; -import org.ibp.api.ibpworkbench.exceptions.IBPWebServiceException; +import org.ibp.api.java.impl.middleware.breedingview.BreedingViewParameter; +import org.ibp.api.exception.IBPWebServiceException; +import org.ibp.api.java.impl.middleware.breedingview.BreedingViewServiceImpl; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -38,7 +39,7 @@ public class BreedingViewServiceImplTest { public void setup() { MockitoAnnotations.initMocks(this); this.breedingViewService = new BreedingViewServiceImpl(); - this.breedingViewService.setImportService(this.bvImportService); + this.breedingViewService.setBreedingViewImportService(this.bvImportService); } private String getFileName(final String absoluteUrl) { @@ -136,13 +137,13 @@ private Map createBVParams(final String mainOutputFilePath, fina final String outlierOutputFilePath, final String workbenchProjectId, final String studyId, final String inputDataSetId, final String outputDataSetId) { final Map params = new HashMap(); - params.put(WebAPIConstants.MAIN_OUTPUT_FILE_PATH.getParamValue(), mainOutputFilePath); - params.put(WebAPIConstants.SUMMARY_OUTPUT_FILE_PATH.getParamValue(), summaryOutputFilePath); - params.put(WebAPIConstants.OUTLIER_OUTPUT_FILE_PATH.getParamValue(), outlierOutputFilePath); - params.put(WebAPIConstants.WORKBENCH_PROJECT_ID.getParamValue(), workbenchProjectId); - params.put(WebAPIConstants.STUDY_ID.getParamValue(), studyId); - params.put(WebAPIConstants.INPUT_DATASET_ID.getParamValue(), inputDataSetId); - params.put(WebAPIConstants.OUTPUT_DATASET_ID.getParamValue(), outputDataSetId); + params.put(BreedingViewParameter.MAIN_OUTPUT_FILE_PATH.getParamValue(), mainOutputFilePath); + params.put(BreedingViewParameter.SUMMARY_OUTPUT_FILE_PATH.getParamValue(), summaryOutputFilePath); + params.put(BreedingViewParameter.OUTLIER_OUTPUT_FILE_PATH.getParamValue(), outlierOutputFilePath); + params.put(BreedingViewParameter.WORKBENCH_PROJECT_ID.getParamValue(), workbenchProjectId); + params.put(BreedingViewParameter.STUDY_ID.getParamValue(), studyId); + params.put(BreedingViewParameter.INPUT_DATASET_ID.getParamValue(), inputDataSetId); + params.put(BreedingViewParameter.OUTPUT_DATASET_ID.getParamValue(), outputDataSetId); return params; } From 1a0b363a647a5d60a9270264dc3c92234366babf Mon Sep 17 00:00:00 2001 From: Aldrin Batac Date: Wed, 15 Jan 2020 11:36:40 +0800 Subject: [PATCH 3/4] Add missing bean dependency to fix NPE in BreedingViewImportService. IBP-3273 --- .../api/java/impl/middleware/common/MiddlewareFactory.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/org/ibp/api/java/impl/middleware/common/MiddlewareFactory.java b/src/main/java/org/ibp/api/java/impl/middleware/common/MiddlewareFactory.java index 81e94003ba..e43fcf5609 100644 --- a/src/main/java/org/ibp/api/java/impl/middleware/common/MiddlewareFactory.java +++ b/src/main/java/org/ibp/api/java/impl/middleware/common/MiddlewareFactory.java @@ -18,6 +18,7 @@ import org.generationcp.commons.spring.util.ContextUtil; import org.generationcp.middleware.hibernate.DatasourceUtilities; import org.generationcp.middleware.hibernate.HibernateSessionPerRequestProvider; +import org.generationcp.middleware.manager.DaoFactory; import org.generationcp.middleware.manager.GenotypicDataManagerImpl; import org.generationcp.middleware.manager.GermplasmDataManagerImpl; import org.generationcp.middleware.manager.GermplasmListManagerImpl; @@ -504,6 +505,12 @@ public InventoryService getInventoryService() { return new InventoryServiceImpl(this.getCropDatabaseSessionProvider()); } + @Bean + @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) + public DaoFactory getDaoFactory() { + return new DaoFactory(this.getCropDatabaseSessionProvider()); + } + private HibernateSessionPerRequestProvider getWorkbenchSessionProvider() { return new HibernateSessionPerRequestProvider(this.WORKBENCH_SessionFactory); } From 380907ee95f3b7acb4f72f6f55bd8f930e3b23c3 Mon Sep 17 00:00:00 2001 From: Aldrin Batac Date: Wed, 15 Jan 2020 13:19:17 +0800 Subject: [PATCH 4/4] Added unit test, IBP-3273 --- .../breedingview/BreedingViewResource.java | 1 + .../rest/BreedingViewResourceTest.java | 111 +++++++++++++++--- 2 files changed, 98 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/ibp/api/rest/breedingview/BreedingViewResource.java b/src/main/java/org/ibp/api/rest/breedingview/BreedingViewResource.java index 2b9eb42cd8..b813203ae6 100644 --- a/src/main/java/org/ibp/api/rest/breedingview/BreedingViewResource.java +++ b/src/main/java/org/ibp/api/rest/breedingview/BreedingViewResource.java @@ -69,6 +69,7 @@ public BreedingViewResponse saveSingleSiteAnalysisData( Preconditions.checkArgument(!StringUtils.isEmpty(studyId), "studyId is required."); Preconditions.checkArgument(!StringUtils.isEmpty(inputDataSetId), "inputDataSetId is required."); Preconditions.checkArgument(!StringUtils.isEmpty(outputDataSetId), "outputDataSetId is required."); + Preconditions.checkArgument(!StringUtils.isEmpty(cropName), "cropName is required."); final Map params = new HashMap<>(); final List errors = new ArrayList<>(); diff --git a/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewResourceTest.java b/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewResourceTest.java index b391987fc6..f8cd6d0f90 100644 --- a/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewResourceTest.java +++ b/src/test/java/org/ibp/api/ibpworkbench/rest/BreedingViewResourceTest.java @@ -2,42 +2,125 @@ package org.ibp.api.ibpworkbench.rest; import org.ibp.api.domain.breedingview.BreedingViewResponse; +import org.ibp.api.exception.IBPWebServiceException; import org.ibp.api.java.breedingview.BreedingViewService; import org.ibp.api.rest.breedingview.BreedingViewResource; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.ArgumentMatchers.anyMap; + +@RunWith(MockitoJUnitRunner.class) public class BreedingViewResourceTest { - private BreedingViewResource breedingView; + private BreedingViewResource breedingViewResource; @Mock private BreedingViewService breedingViewService; + public static final String MAIN_OUTPUT_FILE_PATH = "C:\\BMS4\\workspace\\maize_test\\breeding_view\\output\\BMSOutput_1_6.csv"; + public static final String SUMMARY_OUTPUT_FILE_PATH = "C:\\BMS4\\workspace\\maize_test\\breeding_view\\output\\BMSSummary_1_6.csv"; + public static final String OUTLIER_OUTPUT_FILE_PATH = null; + public static final String WORKBENCH_PROJECT_ID = "1"; + public static final String STUDY_ID = "1"; + public static final String INPUT_DATASET_ID = "3"; + public static final String OUTPUT_DATASET_ID = "0"; + public static final String CROP_NAME = "maize"; @Before public void setUp() { MockitoAnnotations.initMocks(this); - this.breedingView = new BreedingViewResource(); - this.breedingView.setBreedingViewService(this.breedingViewService); + this.breedingViewResource = new BreedingViewResource(); + this.breedingViewResource.setBreedingViewService(this.breedingViewService); } @Test - public void testSaveSsaResultSummaryWithValidRequiredParams() { - final String mainOutputFilePath = "C:\\BMS4\\workspace\\maize_test\\breeding_view\\output\\BMSOutput_1_6.csv"; - final String summaryOutputFilePath = "C:\\BMS4\\workspace\\maize_test\\breeding_view\\output\\BMSSummary_1_6.csv"; - final String outlierOutputFilePath = null; - final String workbenchProjectId = "1"; - final String studyId = "1"; - final String inputDataSetId = "3"; - final String outputDataSetId = "0"; - final String cropName = "maize"; + public void testSaveSsaResultSummaryWithInvalidParameters() { + + try { + this.breedingViewResource.saveSingleSiteAnalysisData(null, SUMMARY_OUTPUT_FILE_PATH, OUTLIER_OUTPUT_FILE_PATH, + WORKBENCH_PROJECT_ID, STUDY_ID, INPUT_DATASET_ID, OUTPUT_DATASET_ID, CROP_NAME); + Assert.fail("Method should throw an error"); + } catch ( + final IllegalArgumentException e) { + // Do nothing + } + try { + this.breedingViewResource.saveSingleSiteAnalysisData(SUMMARY_OUTPUT_FILE_PATH, null, OUTLIER_OUTPUT_FILE_PATH, + WORKBENCH_PROJECT_ID, STUDY_ID, INPUT_DATASET_ID, OUTPUT_DATASET_ID, CROP_NAME); + Assert.fail("Method should throw an error"); + } catch ( + final IllegalArgumentException e) { + // Do nothing + } + try { + this.breedingViewResource.saveSingleSiteAnalysisData(SUMMARY_OUTPUT_FILE_PATH, SUMMARY_OUTPUT_FILE_PATH, null, + WORKBENCH_PROJECT_ID, STUDY_ID, INPUT_DATASET_ID, OUTPUT_DATASET_ID, CROP_NAME); + } catch ( + final IllegalArgumentException e) { + Assert.fail("Method should throw an error"); + } + try { + this.breedingViewResource + .saveSingleSiteAnalysisData(SUMMARY_OUTPUT_FILE_PATH, SUMMARY_OUTPUT_FILE_PATH, OUTLIER_OUTPUT_FILE_PATH, + null, STUDY_ID, INPUT_DATASET_ID, OUTPUT_DATASET_ID, CROP_NAME); + Assert.fail("Method should throw an error"); + } catch ( + final IllegalArgumentException e) { + // Do nothing + } + try { + this.breedingViewResource + .saveSingleSiteAnalysisData(SUMMARY_OUTPUT_FILE_PATH, SUMMARY_OUTPUT_FILE_PATH, OUTLIER_OUTPUT_FILE_PATH, + WORKBENCH_PROJECT_ID, null, INPUT_DATASET_ID, OUTPUT_DATASET_ID, CROP_NAME); + Assert.fail("Method should throw an error"); + } catch ( + final IllegalArgumentException e) { + // Do nothing + } + try { + this.breedingViewResource + .saveSingleSiteAnalysisData(SUMMARY_OUTPUT_FILE_PATH, SUMMARY_OUTPUT_FILE_PATH, OUTLIER_OUTPUT_FILE_PATH, + WORKBENCH_PROJECT_ID, STUDY_ID, null, OUTPUT_DATASET_ID, CROP_NAME); + Assert.fail("Method should throw an error"); + } catch ( + final IllegalArgumentException e) { + // Do nothing + } + try { + this.breedingViewResource + .saveSingleSiteAnalysisData(SUMMARY_OUTPUT_FILE_PATH, SUMMARY_OUTPUT_FILE_PATH, OUTLIER_OUTPUT_FILE_PATH, + WORKBENCH_PROJECT_ID, STUDY_ID, INPUT_DATASET_ID, null, CROP_NAME); + Assert.fail("Method should throw an error"); + } catch ( + final IllegalArgumentException e) { + // Do nothing + } + try { + this.breedingViewResource + .saveSingleSiteAnalysisData(SUMMARY_OUTPUT_FILE_PATH, SUMMARY_OUTPUT_FILE_PATH, OUTLIER_OUTPUT_FILE_PATH, + WORKBENCH_PROJECT_ID, STUDY_ID, INPUT_DATASET_ID, OUTPUT_DATASET_ID, null); + Assert.fail("Method should throw an error"); + } catch ( + final IllegalArgumentException e) { + // Do nothing + } + + } + @Test + public void testSaveSsaResultSummaryWithValidRequiredParams() throws IBPWebServiceException { final BreedingViewResponse response = - this.breedingView.saveSingleSiteAnalysisData(mainOutputFilePath, summaryOutputFilePath, outlierOutputFilePath, - workbenchProjectId, studyId, inputDataSetId, outputDataSetId, cropName); + this.breedingViewResource.saveSingleSiteAnalysisData(MAIN_OUTPUT_FILE_PATH, SUMMARY_OUTPUT_FILE_PATH, OUTLIER_OUTPUT_FILE_PATH, + WORKBENCH_PROJECT_ID, STUDY_ID, INPUT_DATASET_ID, OUTPUT_DATASET_ID, CROP_NAME); + + Mockito.verify(this.breedingViewService).execute(anyMap(), anyList()); Assert.assertTrue("Web service should succeed", response.isSuccessful()); Assert.assertTrue("Web service should return no errors", response.getMessage().indexOf("Successfully invoked service.") != -1); }