From 0bee9572e6ec78c2d05caf72cb433ed2dcd5342f Mon Sep 17 00:00:00 2001
From: Joze RIHTARSIC <joze.rihtarsic@ext.ec.europa.eu>
Date: Wed, 28 Nov 2018 08:21:08 +0100
Subject: [PATCH] - add unit test for keystore - fix application resource
 attribute smlIntegration

---
 pom.xml                                       |   2 +-
 .../edelivery/smp/ui/ApplicationResource.java |  10 +-
 .../edelivery/smp/ui/DomainResourceTest.java  |   1 -
 .../smp/ui/KeystoreResourceTest.java          |  95 ++++++++++++++++--
 .../smp-keystore_multiple_domains.jks         | Bin 2658 -> 7870 bytes
 5 files changed, 95 insertions(+), 13 deletions(-)

diff --git a/pom.xml b/pom.xml
index 230bba27a..bb4ccab81 100644
--- a/pom.xml
+++ b/pom.xml
@@ -34,7 +34,7 @@
         <project.scm.id>edelivery-scm</project.scm.id>
         <sonar.host.url>http://edelquality.westeurope.cloudapp.azure.com:9000/sonar/</sonar.host.url>
         <sonar.exclusions>**/smp/data/model/*,**/smp/data/ui/*, **/smp/ui/exception/*,**/smp/services/ui/filters/*,
-            **/smp/exceptions/*, **/to_be_removed/*
+            **/smp/exceptions/*, **/smp/data/ui/enums/*, **/to_be_removed/*
         </sonar.exclusions>
         <sonar.language>java</sonar.language>
         <!-- jacoco remote agent settings start -->
diff --git a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/ui/ApplicationResource.java b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/ui/ApplicationResource.java
index 0d79da20d..6d094c76c 100644
--- a/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/ui/ApplicationResource.java
+++ b/smp-webapp/src/main/java/eu/europa/ec/edelivery/smp/ui/ApplicationResource.java
@@ -4,7 +4,9 @@ package eu.europa.ec.edelivery.smp.ui;
 import eu.europa.ec.edelivery.smp.data.ui.ServiceGroupRO;
 import eu.europa.ec.edelivery.smp.data.ui.ServiceResult;
 import eu.europa.ec.edelivery.smp.data.ui.SmpInfoRO;
+import eu.europa.ec.edelivery.smp.services.SMLIntegrationService;
 import eu.europa.ec.edelivery.smp.services.ui.UIServiceGroupService;
+import eu.europa.ec.edelivery.smp.sml.SmlConnector;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -30,6 +32,9 @@ public class ApplicationResource {
     @Autowired
     private Environment env;
 
+    @Autowired
+    SmlConnector smlConnector;
+
     @Value("${smp.artifact.name:eDelivery SMP}")
     String artifactName;
     @Value("${smp.artifact.version:}")
@@ -37,9 +42,6 @@ public class ApplicationResource {
     @Value("${smp.artifact.build.time:}")
     String buildTime;
 
-    @Value("${bdmsl.integration.enabled}")
-    boolean smlIntegrationEnabled;
-
 
     @RequestMapping(method = RequestMethod.GET, path = "name")
     public  String getName() {
@@ -55,7 +57,7 @@ public class ApplicationResource {
     public SmpInfoRO getApplicationInfo() {
         SmpInfoRO info = new SmpInfoRO();
         info.setVersion(getDisplayVersion());
-        info.setSmlIntegrationOn(smlIntegrationEnabled);
+        info.setSmlIntegrationOn(smlConnector.isSmlIntegrationEnabled());
         info.setContextPath(getRootContext());
         return info;
     }
diff --git a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/DomainResourceTest.java b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/DomainResourceTest.java
index 3c091601d..4e6d99fb8 100644
--- a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/DomainResourceTest.java
+++ b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/DomainResourceTest.java
@@ -64,7 +64,6 @@ public class DomainResourceTest {
     DomainDao domainDao;
 
     private MockMvc mvc;
-    private static final RequestPostProcessor ADMIN_CREDENTIALS = httpBasic("smp_admin", "test123");
     private static final RequestPostProcessor SYSTEM_CREDENTIALS = httpBasic("sys_admin", "test123");
 
     @Before
diff --git a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/KeystoreResourceTest.java b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/KeystoreResourceTest.java
index 8e5bdc2da..fae6f0527 100644
--- a/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/KeystoreResourceTest.java
+++ b/smp-webapp/src/test/java/eu/europa/ec/edelivery/smp/ui/KeystoreResourceTest.java
@@ -6,7 +6,9 @@ import eu.europa.ec.edelivery.smp.config.PropertiesTestConfig;
 import eu.europa.ec.edelivery.smp.config.SmpAppConfig;
 import eu.europa.ec.edelivery.smp.config.SmpWebAppConfig;
 import eu.europa.ec.edelivery.smp.config.SpringSecurityConfig;
-import eu.europa.ec.edelivery.smp.data.ui.SmpInfoRO;
+import eu.europa.ec.edelivery.smp.data.ui.*;
+import eu.europa.ec.edelivery.smp.services.ui.UIKeystoreService;
+import org.apache.commons.io.FileUtils;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -20,6 +22,7 @@ import org.springframework.test.context.jdbc.SqlConfig;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.test.context.web.WebAppConfiguration;
 import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
 import org.springframework.test.web.servlet.request.RequestPostProcessor;
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 import org.springframework.web.context.ContextLoaderListener;
@@ -28,9 +31,18 @@ import org.springframework.web.context.WebApplicationContext;
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
 
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Arrays;
+
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
 
@@ -51,16 +63,18 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
         "bdmsl.integration.enabled=true"})
 
 public class KeystoreResourceTest {
-    private static final String PATH = "/ui/rest/application";
+    private static final String PATH = "/ui/rest/keystore";
+
+    Path resourceDirectory = Paths.get("src", "test", "resources",  "keystores", "smp-keystore.jks");
 
     @Autowired
     private WebApplicationContext webAppContext;
 
-
-
+    @Autowired
+    private UIKeystoreService uiKeystoreService;
 
     private MockMvc mvc;
-    private static final RequestPostProcessor ADMIN_CREDENTIALS = httpBasic("smp_admin", "test123");
+    private static final RequestPostProcessor SYSTEM_CREDENTIALS = httpBasic("sys_admin", "test123");
 
     @Before
     public void setup() {
@@ -79,11 +93,78 @@ public class KeystoreResourceTest {
     }
 
     @Test
-    public void getKeyCertificateList() {
+    public void getKeyCertificateList() throws Exception {
+        // given when
+        int countStart = uiKeystoreService.getKeystoreEntriesList().size();
+        MvcResult result = mvc.perform(get(PATH).with(SYSTEM_CREDENTIALS)).
+                andExpect(status().isOk()).andReturn();
+
+        //them
+        ObjectMapper mapper = new ObjectMapper();
+        ServiceResult res = mapper.readValue(result.getResponse().getContentAsString(), ServiceResult.class);
+
+
+        assertNotNull(res);
+        assertEquals(countStart, res.getServiceEntities().size());
+        res.getServiceEntities().forEach(sgMap -> {
+            CertificateRO cert = mapper.convertValue(sgMap, CertificateRO.class);
+            assertNotNull(cert.getAlias());
+            assertNotNull(cert.getCertificateId());
+            assertNotNull(cert.getBlueCoatHeader());
+            assertNull(cert.getEncodedValue()); // submit only metadata
+        });
+    }
+
+    @Test
+    public void uploadKeystoreFailed() throws Exception {
+        // given when
+        MvcResult result = mvc.perform(post(PATH+"/3/upload/JKS/test123")
+                .with(SYSTEM_CREDENTIALS)
+                .content("invalid keystore")).
+                andExpect(status().isOk()).andReturn();
+
+        //them
+        ObjectMapper mapper = new ObjectMapper();
+        KeystoreImportResult res = mapper.readValue(result.getResponse().getContentAsString(), KeystoreImportResult.class);
+
+        assertNotNull(res);
+        assertEquals("IOException occurred while reading the keystore: Invalid keystore format", res.getErrorMessage());
+    }
+
+    @Test
+    public void uploadKeystoreInvalidPassword() throws Exception {
+
+        // given when
+        MvcResult result = mvc.perform(post(PATH+"/3/upload/JKS/NewPassword1234")
+                .with(SYSTEM_CREDENTIALS)
+                .content(Files.readAllBytes(resourceDirectory)) )
+                .andExpect(status().isOk()).andReturn();
+
+        //them
+        ObjectMapper mapper = new ObjectMapper();
+        KeystoreImportResult res = mapper.readValue(result.getResponse().getContentAsString(), KeystoreImportResult.class);
+
+        assertNotNull(res);
+        assertEquals("IOException occurred while reading the keystore: Keystore was tampered with, or password was incorrect", res.getErrorMessage());
     }
 
     @Test
-    public void uploadKeystore() {
+    public void uploadKeystoreOK() throws Exception {
+
+        int countStart = uiKeystoreService.getKeystoreEntriesList().size();
+        // given when
+        MvcResult result = mvc.perform(post(PATH+"/3/upload/JKS/test123")
+                .with(SYSTEM_CREDENTIALS)
+                .content(Files.readAllBytes(resourceDirectory)) )
+                .andExpect(status().isOk()).andReturn();
+
+        //them
+        ObjectMapper mapper = new ObjectMapper();
+        KeystoreImportResult res = mapper.readValue(result.getResponse().getContentAsString(), KeystoreImportResult.class);
+
+        assertNotNull(res);
+        assertNull(res.getErrorMessage());
+        assertEquals(countStart+1, uiKeystoreService.getKeystoreEntriesList().size());
     }
 
 
diff --git a/smp-webapp/src/test/resources/keystores/smp-keystore_multiple_domains.jks b/smp-webapp/src/test/resources/keystores/smp-keystore_multiple_domains.jks
index 75810c018b1203530f3d02fff46545cf184a71da..716ca6ad4f15ec132c0bc17648b0eafb2cd2ee9f 100644
GIT binary patch
delta 2858
zcmajf_dgU41Hkd)@FgRhowLUoclIXEHz(_C5ofOxC!3Oskd=&Noa~*ga7JXWIKoA^
zNM^D#%k%yA^YeP1A3p!V`}J92d8a}5d*}BK001EWC#nWIc}gDE05rzK+s(n%*T)Hs
zak%Fe<{&LC4*-yP*ht%@rb4-~-gO@uw`M=|#<knrd_%pDT~W)odNoVRJbrtA>`^_b
z3(O>#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