From 194753a1eaa901f1e550bae717ace45b1a641558 Mon Sep 17 00:00:00 2001
From: Pawel GUTOWSKI <Pawel.GUTOWSKI@ext.ec.europa.eu>
Date: Mon, 8 Jan 2018 19:13:21 +0100
Subject: [PATCH] EDELIVERY-1978 Added some tests

---
 .../edelivery/smp/sml/SmlClientFactory.java   |  18 +--
 .../ec/edelivery/smp/sml/SmlConnector.java    |   4 +-
 .../smp/sml/SmlIntegrationException.java      |   2 +-
 .../SmlIdentifierConverterTest.java           |  71 +++++++++++
 .../smlintegration/SmlClientFactoryTest.java  |  66 ++++++++++
 .../smp/smlintegration/SmlConnectorTest.java  | 120 ++++++++++++++++++
 6 files changed, 269 insertions(+), 12 deletions(-)
 create mode 100644 smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/conversion/SmlIdentifierConverterTest.java
 create mode 100644 smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/smlintegration/SmlClientFactoryTest.java
 create mode 100644 smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/smlintegration/SmlConnectorTest.java

diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SmlClientFactory.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SmlClientFactory.java
index 971292520..f7dffcf39 100644
--- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SmlClientFactory.java
+++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SmlClientFactory.java
@@ -62,33 +62,33 @@ public class SmlClientFactory {
 
     private static final String CLIENT_CERT_HEADER_KEY = "Client-Cert";
 
-    @Value("${bdmsl.integration.url}")
+    @Value("${bdmsl.integration.url:}")
     private URL smlUrl;
 
-    @Value("${bdmsl.integration.keystore.path}")
+    @Value("${bdmsl.integration.keystore.path:}")
     private String smlClientKeyStorePath;
 
-    @Value("${bdmsl.integration.keystore.password}")
+    @Value("${bdmsl.integration.keystore.password:}")
     private String smlClientKeyStorePassword;
 
-    @Value("${bdmsl.integration.keystore.alias}")
+    @Value("${bdmsl.integration.keystore.alias:}")
     private String smlClientKeyAlias;
 
-    @Value("${bdmsl.integration.http.header.client.cert}")
+    @Value("${bdmsl.integration.http.header.client.cert:}")
     private String smlClientCertHttpHeader;
 
     private KeyManager[] keyManagers;
 
-    @Value("${bdmsl.integration.proxy.server}")
+    @Value("${bdmsl.integration.proxy.server:}")
     private String proxyServer;
 
-    @Value("${bdmsl.integration.proxy.port}")
+    @Value("${bdmsl.integration.proxy.port:}")
     private Optional<Integer> proxyPort;
 
-    @Value("${bdmsl.integration.proxy.user}")
+    @Value("${bdmsl.integration.proxy.user:}")
     private String proxyUser;
 
-    @Value("${bdmsl.integration.proxy.password}")
+    @Value("${bdmsl.integration.proxy.password:}")
     private String proxyPassword;
 
 
diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SmlConnector.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SmlConnector.java
index 0073c6ad6..272724b52 100644
--- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SmlConnector.java
+++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SmlConnector.java
@@ -38,8 +38,8 @@ public class SmlConnector implements ApplicationContextAware {
 
     private static final Logger log = LoggerFactory.getLogger(SmlConnector.class);
 
-    @Value("${bdmsl.integration.enabled}")
-    boolean smlIntegrationEnabled;
+    @Value("${bdmsl.integration.enabled:false}")
+    private boolean smlIntegrationEnabled;
 
     @Value("${bdmsl.integration.smp.id}")
     private String smpId;
diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SmlIntegrationException.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SmlIntegrationException.java
index b7bc290db..489d0f444 100644
--- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SmlIntegrationException.java
+++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SmlIntegrationException.java
@@ -14,7 +14,7 @@
 package eu.europa.ec.edelivery.smp.sml;
 
 /**
- * Problem occured when integrating with SML
+ * Problem occurred when integrating with SML
  *
  * Created by gutowpa on 18/12/2017.
  */
diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/conversion/SmlIdentifierConverterTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/conversion/SmlIdentifierConverterTest.java
new file mode 100644
index 000000000..c066be5f2
--- /dev/null
+++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/conversion/SmlIdentifierConverterTest.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2018 European Commission | CEF eDelivery
+ *
+ * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
+ * You may not use this work except in compliance with the Licence.
+ *
+ * You may obtain a copy of the Licence attached in file: LICENCE-EUPL-v1.2.pdf
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing permissions and limitations under the Licence.
+ */
+
+package eu.europa.ec.edelivery.smp.conversion;
+
+import org.busdox.servicemetadata.locator._1.ServiceMetadataPublisherServiceForParticipantType;
+import org.junit.Test;
+import org.oasis_open.docs.bdxr.ns.smp._2016._05.ParticipantIdentifierType;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Created by gutowpa on 08/01/2018.
+ */
+public class SmlIdentifierConverterTest {
+
+    public static final String SMP_ID = "SMP-ID";
+    public static final String ID_VALUE = "sample:value";
+    public static final String ID_SCHEME = "sample:scheme";
+
+    @Test
+    public void positiveCase() {
+        //given
+        ParticipantIdentifierType participantId = new ParticipantIdentifierType(ID_VALUE, ID_SCHEME);
+
+        //when
+        ServiceMetadataPublisherServiceForParticipantType result = SmlIdentifierConverter.toBusdoxParticipantId(participantId, SMP_ID);
+
+        //then
+        assertEquals(SMP_ID, result.getServiceMetadataPublisherID());
+        assertEquals(ID_SCHEME, result.getParticipantIdentifier().getScheme());
+        assertEquals(ID_VALUE, result.getParticipantIdentifier().getValue());
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void negativeCaseMissingSmpId() {
+        //given
+        ParticipantIdentifierType participantId = new ParticipantIdentifierType(ID_VALUE, ID_SCHEME);
+
+        //when
+        SmlIdentifierConverter.toBusdoxParticipantId(participantId, null);
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void negativeCaseMissingScheme() {
+        //given
+        ParticipantIdentifierType participantId = new ParticipantIdentifierType(ID_VALUE, null);
+
+        //when
+        SmlIdentifierConverter.toBusdoxParticipantId(participantId, SMP_ID);
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void negativeCaseMissingValue() {
+        //given
+        ParticipantIdentifierType participantId = new ParticipantIdentifierType(null, ID_SCHEME);
+
+        //when
+        SmlIdentifierConverter.toBusdoxParticipantId(participantId, SMP_ID);
+    }
+}
diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/smlintegration/SmlClientFactoryTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/smlintegration/SmlClientFactoryTest.java
new file mode 100644
index 000000000..3f0344084
--- /dev/null
+++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/smlintegration/SmlClientFactoryTest.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2018 European Commission | CEF eDelivery
+ *
+ * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
+ * You may not use this work except in compliance with the Licence.
+ *
+ * You may obtain a copy of the Licence attached in file: LICENCE-EUPL-v1.2.pdf
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing permissions and limitations under the Licence.
+ */
+
+package eu.europa.ec.edelivery.smp.smlintegration;
+
+import eu.europa.ec.bdmsl.ws.soap.IManageParticipantIdentifierWS;
+import eu.europa.ec.edelivery.smp.sml.SmlClientFactory;
+import org.apache.cxf.endpoint.Client;
+import org.apache.cxf.frontend.ClientProxy;
+import org.apache.cxf.message.Message;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Created by gutowpa on 08/01/2018.
+ */
+@RunWith(SpringRunner.class)
+@ContextConfiguration(classes = SmlClientFactoryTest.class)
+@Configuration
+@ComponentScan("eu.europa.ec.edelivery.smp.sml")
+@TestPropertySource(properties = {
+        "bdmsl.integration.url=https://sml.url.pl",
+        "bdmsl.integration.http.header.client.cert=value_of_ClientCert_HTTP_header"})
+public class SmlClientFactoryTest {
+
+    @Autowired
+    private SmlClientFactory smlClientFactory;
+
+    @Test
+    public void factoryProducedPreconfiguredCxfClient() {
+        //when
+        IManageParticipantIdentifierWS client = smlClientFactory.create();
+
+        //then
+        assertNotNull(client);
+        Client cxfClient = ClientProxy.getClient(client);
+        Map<String, Object> requestContext = cxfClient.getRequestContext();
+        Map httpHeaders = (Map) requestContext.get(Message.PROTOCOL_HEADERS);
+        List clientCerts = (List) httpHeaders.get("Client-Cert");
+        assertEquals(1, clientCerts.size());
+        assertEquals("value_of_ClientCert_HTTP_header", clientCerts.get(0));
+        assertEquals("https://sml.url.pl", requestContext.get(Message.ENDPOINT_ADDRESS));
+    }
+}
diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/smlintegration/SmlConnectorTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/smlintegration/SmlConnectorTest.java
new file mode 100644
index 000000000..343d0a1f7
--- /dev/null
+++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/smlintegration/SmlConnectorTest.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2018 European Commission | CEF eDelivery
+ *
+ * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
+ * You may not use this work except in compliance with the Licence.
+ *
+ * You may obtain a copy of the Licence attached in file: LICENCE-EUPL-v1.2.pdf
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing permissions and limitations under the Licence.
+ */
+
+package eu.europa.ec.edelivery.smp.smlintegration;
+
+import eu.europa.ec.bdmsl.ws.soap.*;
+import eu.europa.ec.edelivery.smp.sml.SmlConnector;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.oasis_open.docs.bdxr.ns.smp._2016._05.ParticipantIdentifierType;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Scope;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.verify;
+import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;
+
+/**
+ * Created by gutowpa on 08/01/2018.
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(classes = SmlConnectorTest.class)
+@Configuration
+@TestPropertySource(properties = {
+        "bdmsl.integration.enabled=true"})
+public class SmlConnectorTest {
+
+    private static List<IManageParticipantIdentifierWS> smlClientMocks = new ArrayList<>();
+    private static final ParticipantIdentifierType PARTICIPANT_ID = new ParticipantIdentifierType("sample:value", "sample:scheme");
+
+    @Autowired
+    private SmlConnector smlConnector;
+
+    @Before
+    public void setup() {
+        smlClientMocks = new ArrayList<>();
+    }
+
+    @Bean
+    public SmlConnector smlConnector() {
+        return new SmlConnector();
+    }
+
+    @Bean
+    @Scope(SCOPE_PROTOTYPE)
+    public IManageParticipantIdentifierWS smlClientMock() {
+        IManageParticipantIdentifierWS clientMock = Mockito.mock(IManageParticipantIdentifierWS.class);
+        smlClientMocks.add(clientMock);
+        return clientMock;
+    }
+
+    @Test
+    public void testRegisterInDns() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault {
+        //when
+        smlConnector.registerInDns(PARTICIPANT_ID);
+
+        //then
+        assertEquals(1, smlClientMocks.size());
+        verify(smlClientMocks.get(0)).create(any());
+        Mockito.verifyNoMoreInteractions(smlClientMocks.toArray());
+    }
+
+    @Test
+    public void testRegisterInDnsNewClientIsAlwaysCreated() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault {
+        //when
+        smlConnector.registerInDns(PARTICIPANT_ID);
+        smlConnector.registerInDns(PARTICIPANT_ID);
+
+        //then
+        assertEquals(2, smlClientMocks.size());
+        verify(smlClientMocks.get(0)).create(any());
+        verify(smlClientMocks.get(1)).create(any());
+        Mockito.verifyNoMoreInteractions(smlClientMocks.toArray());
+    }
+
+    @Test
+    public void testUnregisterFromDns() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault {
+        //when
+        smlConnector.unregisterFromDns(PARTICIPANT_ID);
+
+        //then
+        assertEquals(1, smlClientMocks.size());
+        verify(smlClientMocks.get(0)).delete(any());
+        Mockito.verifyNoMoreInteractions(smlClientMocks.toArray());
+    }
+
+    @Test
+    public void testUnregisterFromDnsNewClientIsAlwaysCreated() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault {
+        //when
+        smlConnector.unregisterFromDns(PARTICIPANT_ID);
+        smlConnector.unregisterFromDns(PARTICIPANT_ID);
+
+        //then
+        assertEquals(2, smlClientMocks.size());
+        verify(smlClientMocks.get(0)).delete(any());
+        verify(smlClientMocks.get(1)).delete(any());
+        Mockito.verifyNoMoreInteractions(smlClientMocks.toArray());
+    }
+}
-- 
GitLab