From 8e5b537d0ab4b1f35cbb2cd4bb40a979eeb99f46 Mon Sep 17 00:00:00 2001
From: Pawel GUTOWSKI <Pawel.GUTOWSKI@ext.ec.europa.eu>
Date: Fri, 19 Jan 2018 16:33:40 +0100
Subject: [PATCH] EDELIVERY-1871 EDELIVERY-1893 Added missing test and fixed
 minor bug

---
 smp-parent-pom/pom.xml                           |  7 +++++++
 .../smp/services/ServiceGroupService.java        |  6 +++++-
 smp-webapp/pom.xml                               |  4 ++++
 .../controllers/ServiceGroupControllerTest.java  | 16 +++++++++++++++-
 4 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/smp-parent-pom/pom.xml b/smp-parent-pom/pom.xml
index c78c33b38..8d2dc847a 100644
--- a/smp-parent-pom/pom.xml
+++ b/smp-parent-pom/pom.xml
@@ -91,6 +91,7 @@
 		<junitparams.version>1.0.5</junitparams.version>
 		<lombok.version>1.16.16</lombok.version>
 		<xmlunit.version>2.5.1</xmlunit.version>
+		<hamcrest.version>2.0.0.0</hamcrest.version>
 	</properties>
 
 	<!-- Where are we looking for JARs? -->
@@ -503,6 +504,12 @@
 				<version>${xmlunit.version}</version>
 				<scope>test</scope>
 			</dependency>
+			<dependency>
+				<groupId>org.hamcrest</groupId>
+				<artifactId>hamcrest-junit</artifactId>
+				<version>${hamcrest.version}</version>
+				<scope>test</scope>
+			</dependency>
 		</dependencies>
 	</dependencyManagement>
 
diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ServiceGroupService.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ServiceGroupService.java
index 4b03bcf47..5859da329 100644
--- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ServiceGroupService.java
+++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ServiceGroupService.java
@@ -111,7 +111,11 @@ public class ServiceGroupService {
 
     private DBDomain findDomain(String domain) {
         if (isNotBlank(domain)) {
-            return domainDao.find(domain);
+            DBDomain dbDomain = domainDao.find(domain);
+            if(dbDomain == null){
+                throw new WrongInputFieldException("Requested domain does not exist: " + domain);
+            }
+            return dbDomain;
         }
         Optional<DBDomain> dbDomain = domainDao.getTheOnlyDomain();
         if (dbDomain.isPresent()) {
diff --git a/smp-webapp/pom.xml b/smp-webapp/pom.xml
index 96455bfcf..393ad41b0 100644
--- a/smp-webapp/pom.xml
+++ b/smp-webapp/pom.xml
@@ -77,6 +77,10 @@
             <groupId>org.springframework</groupId>
             <artifactId>spring-test</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.hamcrest</groupId>
+            <artifactId>hamcrest-junit</artifactId>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/controllers/ServiceGroupControllerTest.java b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/controllers/ServiceGroupControllerTest.java
index af3470071..bce99f650 100644
--- a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/controllers/ServiceGroupControllerTest.java
+++ b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/controllers/ServiceGroupControllerTest.java
@@ -40,6 +40,7 @@ import javax.servlet.ServletContextListener;
 
 import static eu.europa.ec.edelivery.smp.ServiceGroupBodyUtil.getSampleServiceGroupBodyWithScheme;
 import static java.lang.String.format;
+import static org.hamcrest.Matchers.stringContainsInOrder;
 import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;
 import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
@@ -64,7 +65,8 @@ public class ServiceGroupControllerTest {
     private static final String PARTICIPANT_SCHEME = "ehealth-participantid-qns";
     private static final String PARTICIPANT_ID = "urn:poland:ncpb";
     private static final String URL_PATH = format("/%s::%s", PARTICIPANT_SCHEME, PARTICIPANT_ID);
-    private final String SERVICE_GROUP_INPUT_BODY = getSampleServiceGroupBodyWithScheme(PARTICIPANT_SCHEME);
+    private static final String SERVICE_GROUP_INPUT_BODY = getSampleServiceGroupBodyWithScheme(PARTICIPANT_SCHEME);
+    private static final String HTTP_HEADER_KEY_DOMAIN = "Domain";
 
     private static final RequestPostProcessor ADMIN_CREDENTIALS = httpBasic("test_admin", "gutek123");
 
@@ -179,4 +181,16 @@ public class ServiceGroupControllerTest {
                 .content(getSampleServiceGroupBodyWithScheme(scheme)))
                 .andExpect(status().isBadRequest());
     }
+
+    @Test
+    public void creatingServiceGroupUnderNotExistingDomainReturnsBadRequest() throws Exception {
+        mvc.perform(put(URL_PATH)
+                .with(ADMIN_CREDENTIALS)
+                .contentType(APPLICATION_XML_VALUE)
+                .header(HTTP_HEADER_KEY_DOMAIN, "not-existing-domain")
+                .content(SERVICE_GROUP_INPUT_BODY))
+                .andExpect(status().isBadRequest())
+                .andExpect(content().string(stringContainsInOrder("WRONG_FIELD")));
+    }
+
 }
-- 
GitLab