Cleaning up minor warnings and backfilling test coverage for edge cases.
diff --git a/component-test/src/main/java/io/mifos/portfolio/TestCases.java b/component-test/src/main/java/io/mifos/portfolio/TestCases.java
index 6bfc9f1..7493e81 100644
--- a/component-test/src/main/java/io/mifos/portfolio/TestCases.java
+++ b/component-test/src/main/java/io/mifos/portfolio/TestCases.java
@@ -16,6 +16,7 @@
package io.mifos.portfolio;
import com.google.gson.Gson;
+import io.mifos.core.api.util.NotFoundException;
import io.mifos.core.test.domain.TimeStampChecker;
import io.mifos.individuallending.api.v1.domain.caseinstance.CaseParameters;
import io.mifos.individuallending.api.v1.domain.caseinstance.CreditWorthinessFactor;
@@ -46,6 +47,22 @@
public TestCases() { }
@Test
+ public void shouldFailToCreateCaseForNonexistentProduct() throws InterruptedException {
+ try {
+ final String productIdentifier = "nonexistantProduct";
+ final Case caseInstance = Fixture.getTestCase(productIdentifier);
+
+ portfolioManager.createCase(productIdentifier, caseInstance);
+ Assert.fail("Should fail because product doesn't exist.");
+
+ portfolioManager.getCase(productIdentifier, caseInstance.getIdentifier());
+ Assert.fail("Should fail because product doesn't exist.");
+ }
+ catch (final NotFoundException ignored) {
+ }
+ }
+
+ @Test
public void shouldCreateCase() throws InterruptedException {
final Product product = createAndEnableProduct();
diff --git a/service/src/main/java/io/mifos/portfolio/service/internal/service/CaseService.java b/service/src/main/java/io/mifos/portfolio/service/internal/service/CaseService.java
index 9250ee0..fd00e9e 100644
--- a/service/src/main/java/io/mifos/portfolio/service/internal/service/CaseService.java
+++ b/service/src/main/java/io/mifos/portfolio/service/internal/service/CaseService.java
@@ -126,6 +126,11 @@
return caseRepository.existsByProductIdentifier(productIdentifier);
}
+ public boolean existsByIdentifier(final String productIdentifier,
+ final String caseIdentifier) {
+ return this.findByIdentifier(productIdentifier, caseIdentifier).isPresent();
+ }
+
public List<CostComponent> getActionCostComponentsForCase(final String productIdentifier,
final String caseIdentifier,
final String actionIdentifier) {
diff --git a/service/src/main/java/io/mifos/portfolio/service/internal/service/ProductService.java b/service/src/main/java/io/mifos/portfolio/service/internal/service/ProductService.java
index 5d0d92b..1bc10d4 100644
--- a/service/src/main/java/io/mifos/portfolio/service/internal/service/ProductService.java
+++ b/service/src/main/java/io/mifos/portfolio/service/internal/service/ProductService.java
@@ -99,6 +99,12 @@
return sortColumn;
}
+ public boolean existsByIdentifier(final String identifier)
+ {
+ //TODO: replace with existsBy once we've upgraded to spring data 1.11 or later.
+ return productRepository.findByIdentifier(identifier).isPresent();
+ }
+
public Optional<Product> findByIdentifier(final String identifier)
{
return productRepository.findByIdentifier(identifier).map(ProductMapper::map);
diff --git a/service/src/main/java/io/mifos/portfolio/service/rest/CaseRestController.java b/service/src/main/java/io/mifos/portfolio/service/rest/CaseRestController.java
index fb4750f..19ffdb1 100644
--- a/service/src/main/java/io/mifos/portfolio/service/rest/CaseRestController.java
+++ b/service/src/main/java/io/mifos/portfolio/service/rest/CaseRestController.java
@@ -226,16 +226,16 @@
return new ResponseEntity<>(HttpStatus.ACCEPTED);
}
- private Case checkThatCaseExists(final String productIdentifier, final String caseIdentifier) {
+ private void checkThatCaseExists(final String productIdentifier, final String caseIdentifier) {
checkThatProductExists(productIdentifier);
- return caseService.findByIdentifier(productIdentifier, caseIdentifier)
- .orElseThrow(() -> ServiceException.notFound("Case with identifier " + productIdentifier + "." + caseIdentifier + " doesn't exist."));
+ if (!caseService.existsByIdentifier(productIdentifier, caseIdentifier))
+ throw ServiceException.notFound("Case with identifier ''{0}.{1}'' doesn''t exist.", productIdentifier, caseIdentifier);
}
private void checkThatProductExists(final String productIdentifier) {
- productService.findByIdentifier(productIdentifier)
- .orElseThrow(() -> ServiceException.notFound("Product with identifier " + productIdentifier + " doesn't exist."));
+ if (!productService.existsByIdentifier(productIdentifier))
+ throw ServiceException.notFound("Product with identifier ''{0}'' doesn''t exist.", productIdentifier);
}
//TODO: check that case parameters are within product parameters in put and post.