diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SMLErrorMessages.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SMLErrorMessages.java index 96712a814dab02a2085292b14e3b2c0750fba5b3..03f7449bcdf9d1e5fe516ab1dd47975aba386405 100644 --- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SMLErrorMessages.java +++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/sml/SMLErrorMessages.java @@ -3,6 +3,7 @@ package eu.europa.ec.edelivery.smp.sml; public class SMLErrorMessages { public static final String ERR_PARTICIPANT_ALREADY_EXISTS ="[ERR-106] The participant identifier '%s' does already exist for the scheme %s"; + public static final String ERR_PARTICIPANT_NOT_EXISTS="[ERR-100] The participant identifier '%s' doesn't exist for the scheme %s"; public static final String ERR_DOMAIN_ALREADY_EXISTS="[ERR-106] The SMP '%s' already exists"; public static final String ERR_DOMAIN_NOT_EXISTS="[ERR-100] The SMP '%s' doesn't exist"; 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 8b188563ebda9919a5e0c0604525a9a8810bf96c..ede7ffbfd68c517b407021eb93bf0d93e225f3e7 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 @@ -34,9 +34,7 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import static eu.europa.ec.edelivery.smp.conversion.SmlIdentifierConverter.toBusdoxParticipantId; -import static eu.europa.ec.edelivery.smp.sml.SMLErrorMessages.ERR_DOMAIN_ALREADY_EXISTS; -import static eu.europa.ec.edelivery.smp.sml.SMLErrorMessages.ERR_DOMAIN_NOT_EXISTS; -import static eu.europa.ec.edelivery.smp.sml.SMLErrorMessages.ERR_PARTICIPANT_ALREADY_EXISTS; +import static eu.europa.ec.edelivery.smp.sml.SMLErrorMessages.*; import static eu.europa.ec.smp.api.Identifiers.asString; /** @@ -114,7 +112,8 @@ public class SmlConnector implements ApplicationContextAware { return false; } String exp = String.format(ERR_PARTICIPANT_ALREADY_EXISTS, patId.getValue(), patId.getScheme()); - return errorMessage.startsWith(exp); + String exp2 = String.format(ERR_PARTICIPANT_NOT_EXISTS, patId.getValue(), patId.getScheme()); + return errorMessage.startsWith(exp)|| errorMessage.startsWith(exp2); } diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmlIntegrationConfiguration.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmlIntegrationConfiguration.java index f24af073fb0e395ffecebaab6347a49975cb9d48..d2597fc34dc8a9ffe908b255145f5009ef4fe0eb 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmlIntegrationConfiguration.java +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmlIntegrationConfiguration.java @@ -1,8 +1,9 @@ package eu.europa.ec.edelivery.smp.config; -import eu.europa.ec.bdmsl.ws.soap.IManageParticipantIdentifierWS; -import eu.europa.ec.bdmsl.ws.soap.IManageServiceMetadataWS; +import eu.europa.ec.bdmsl.ws.soap.*; import eu.europa.ec.edelivery.smp.data.model.DBDomain; +import org.mockito.ArgumentMatchers; +import org.mockito.BDDMockito; import org.mockito.Mockito; import org.oasis_open.docs.bdxr.ns.smp._2016._05.ParticipantIdentifierType; import org.springframework.context.annotation.Bean; @@ -15,6 +16,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.BDDMockito.*; import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE; @@ -32,6 +35,8 @@ public class SmlIntegrationConfiguration { protected Map<IManageParticipantIdentifierWS, AuthenticationTestDataHolder> smlClientMocksData = new HashMap<>(); protected int throwExceptionAfterParticipantCallCount = -1; + protected Throwable throwException; + public void reset() { smpManagerClientMocks.clear(); smpManagerClientMocksData.clear(); @@ -47,12 +52,23 @@ public class SmlIntegrationConfiguration { defaultDomain.setSmlClientKeyAlias("clientAlias"); defaultDomain.setSmlClientCertHeader("blueCoatClientHeader"); setThrowExceptionAfterParticipantCallCount(-1); + setThrowException(null); } @Bean @Scope(SCOPE_PROTOTYPE) - public IManageServiceMetadataWS smpManagerClient(String clientKeyAlias, String clientCertHttpHeader, boolean authBlueCoat) { + public IManageServiceMetadataWS smpManagerClient(String clientKeyAlias, String clientCertHttpHeader, boolean authBlueCoat) throws BadRequestFault, UnauthorizedFault, InternalErrorFault, NotFoundFault { + + + IManageServiceMetadataWS clientMock = Mockito.mock(IManageServiceMetadataWS.class); + if (throwException!= null) { + willThrow(throwException).given(clientMock).create(any()); + willThrow(throwException).given(clientMock).delete(any()); + willThrow(throwException).given(clientMock).read(any()); + willThrow(throwException).given(clientMock).update(any()); + } + AuthenticationTestDataHolder dh = new AuthenticationTestDataHolder(); dh.setAlias(clientKeyAlias); dh.setBlueCoatHeader(clientCertHttpHeader); @@ -63,12 +79,24 @@ public class SmlIntegrationConfiguration { @Bean @Scope(SCOPE_PROTOTYPE) - public IManageParticipantIdentifierWS smpParticipantClient(String clientKeyAlias, String clientCertHttpHeader,boolean authBlueCoat) { + public IManageParticipantIdentifierWS smpParticipantClient(String clientKeyAlias, String clientCertHttpHeader,boolean authBlueCoat) throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + if (throwExceptionAfterParticipantCallCount >0 && throwExceptionAfterParticipantCallCount <= smlClientMocks.size()){ throw new HTTPException(400); } IManageParticipantIdentifierWS clientMock = Mockito.mock(IManageParticipantIdentifierWS.class); + if (throwException!= null) { + willThrow(throwException).given(clientMock).create(any()); + willThrow(throwException).given(clientMock).delete(any()); + willThrow(throwException).given(clientMock).list(any()); + willThrow(throwException).given(clientMock).createList(any()); + willThrow(throwException).given(clientMock).deleteList(any()); + willThrow(throwException).given(clientMock).migrate(any()); + willThrow(throwException).given(clientMock).prepareToMigrate(any()); + } + + AuthenticationTestDataHolder dh = new AuthenticationTestDataHolder(); dh.setAlias(clientKeyAlias); dh.setBlueCoatHeader(clientCertHttpHeader); @@ -108,4 +136,12 @@ public class SmlIntegrationConfiguration { public void setThrowExceptionAfterParticipantCallCount(int throwExceptionAfterParticipantCallCount) { this.throwExceptionAfterParticipantCallCount = throwExceptionAfterParticipantCallCount; } + + public Throwable getThrowException() { + return throwException; + } + + public void setThrowException(Throwable throwException) { + this.throwException = throwException; + } } diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/conversion/X509CertificateToCertificateROConverterTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/conversion/X509CertificateToCertificateROConverterTest.java new file mode 100644 index 0000000000000000000000000000000000000000..9fd77d52e4467127feaed54949350831914ffd61 --- /dev/null +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/conversion/X509CertificateToCertificateROConverterTest.java @@ -0,0 +1,70 @@ +package eu.europa.ec.edelivery.smp.conversion; + +import eu.europa.ec.edelivery.smp.data.ui.CertificateRO; +import eu.europa.ec.smp.api.Identifiers; +import junitparams.JUnitParamsRunner; +import junitparams.Parameters; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.oasis_open.docs.bdxr.ns.smp._2016._05.ParticipantIdentifierType; + +import java.security.cert.CertificateEncodingException; +import java.security.cert.X509Certificate; +import java.util.Base64; + +import static org.junit.Assert.*; + + +@RunWith(JUnitParamsRunner.class) +public class X509CertificateToCertificateROConverterTest { + + private static final Object[] testCases() { + return new Object[][]{ + // alias, subject, issuer, serial number, blueCoatHeader, certificateId + {"alias", "subject", "issuer","serialNumber","blueCoat","certificateId"}, + }; + } + + + X509CertificateToCertificateROConverter testInstance = new X509CertificateToCertificateROConverter(); + + @Test + @Parameters(method = "testCases") + public void testconvert(String alias, + String subject, + String issuer, + String serialNumber, + String blueCoat, + String certificateId) throws CertificateEncodingException { + /* + // given + X509Certificate certificate = getCertificate(alias); + + // when + CertificateRO certRo = testInstance.convert(certificate); + + //then + assertEquals(subject, certRo.getSubject()); + assertEquals(issuer, certRo.getIssuer()); + assertEquals(serialNumber, certRo.getSerialNumber()); + assertEquals(blueCoat, certRo.getBlueCoatHeader()); + assertEquals(certificateId, certRo.getCertificateId()); + assertEquals(Base64.getEncoder().encode(certificate.getEncoded()), certRo.getEncodedValue()); + assertEquals(certificate.getNotBefore(), certRo.getValidFrom()); + assertEquals(certificate.getNotAfter(), certRo.getValidTo()); + */ + } + + @Test + public void convert() { + } + + @Test + public void getCertificateIdFromCertificate() { + } + + X509Certificate getCertificate(String alias){ + X509Certificate cert = null; + return cert; + } +} \ No newline at end of file diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorTestBase.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorTestBase.java new file mode 100644 index 0000000000000000000000000000000000000000..a825c9507715b30a48c48825d9de6e8faefd9dc7 --- /dev/null +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorTestBase.java @@ -0,0 +1,55 @@ +package eu.europa.ec.edelivery.smp.sml; + +import eu.europa.ec.edelivery.smp.config.ConversionTestConfig; +import eu.europa.ec.edelivery.smp.config.PropertiesSingleDomainTestConfig; +import eu.europa.ec.edelivery.smp.config.SmlIntegrationConfiguration; +import eu.europa.ec.edelivery.smp.data.model.DBDomain; +import eu.europa.ec.edelivery.smp.services.SecurityUtilsServices; +import eu.europa.ec.edelivery.smp.services.ui.UIKeystoreService; +import org.junit.Rule; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.oasis_open.docs.bdxr.ns.smp._2016._05.ParticipantIdentifierType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { SmlConnector.class,SmlIntegrationConfiguration.class, + SecurityUtilsServices.class, UIKeystoreService.class, + ConversionTestConfig.class, PropertiesSingleDomainTestConfig.class}) +@Configuration +@TestPropertySource(properties = { + "bdmsl.integration.enabled=true"}) +public class SmlConnectorTestBase { + protected static final ParticipantIdentifierType PARTICIPANT_ID = new ParticipantIdentifierType("sample:value", "sample:scheme"); + protected static final DBDomain DEFAULT_DOMAIN; + static { + DEFAULT_DOMAIN = new DBDomain(); + DEFAULT_DOMAIN.setDomainCode("default_domain_id"); + DEFAULT_DOMAIN.setSmlSmpId("SAMPLE-SMP-ID"); + } + + protected static final String ERROR_UNEXPECTED_MESSAGE ="[ERR-106] Something unexpected happend"; + protected static final String ERROR_SMP_NOT_EXISTS ="[ERR-100] The SMP '"+DEFAULT_DOMAIN.getSmlSmpId()+"' doesn't exist"; + protected static final String ERROR_SMP_ALREADY_EXISTS ="[ERR-106] The SMP '"+DEFAULT_DOMAIN.getSmlSmpId()+"' already exists"; + protected static final String ERROR_PI_ALREADY_EXISTS = "[ERR-106] The participant identifier 'sample:value' does already exist for the scheme sample:scheme"; + protected static final String ERROR_PI_NO_EXISTS = "[ERR-100] The participant identifier 'sample:value' doesn't exist for the scheme sample:scheme"; + + + + @Rule + public ExpectedException expectedExeption = ExpectedException.none(); + @Autowired + protected SmlConnector smlConnector; + @Autowired + SmlIntegrationConfiguration mockSml; + + @Autowired + public void setup(){ + mockSml.reset(); + } +} diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorTestDomain.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorTestDomain.java new file mode 100644 index 0000000000000000000000000000000000000000..44bf260efc027303a0a8230df5ce1708e45a0f40 --- /dev/null +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorTestDomain.java @@ -0,0 +1,202 @@ +/* + * 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.sml; + +import eu.europa.ec.bdmsl.ws.soap.BadRequestFault; +import eu.europa.ec.bdmsl.ws.soap.InternalErrorFault; +import eu.europa.ec.bdmsl.ws.soap.NotFoundFault; +import eu.europa.ec.bdmsl.ws.soap.UnauthorizedFault; +import eu.europa.ec.edelivery.smp.config.ConversionTestConfig; +import eu.europa.ec.edelivery.smp.config.PropertiesSingleDomainTestConfig; +import eu.europa.ec.edelivery.smp.config.SmlIntegrationConfiguration; +import eu.europa.ec.edelivery.smp.data.model.DBDomain; +import eu.europa.ec.edelivery.smp.exceptions.SMPRuntimeException; +import eu.europa.ec.edelivery.smp.services.SecurityUtilsServices; +import eu.europa.ec.edelivery.smp.services.ui.UIKeystoreService; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +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.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +/** + * Created by JRC + * since 4.1. + */ +public class SmlConnectorTestDomain extends SmlConnectorTestBase{ + + // private static List<IManageParticipantIdentifierWS> smlClientMocks = new ArrayList<>(); + private static final ParticipantIdentifierType PARTICIPANT_ID = new ParticipantIdentifierType("sample:value", "sample:scheme"); + private static final DBDomain DEFAULT_DOMAIN; + + static { + DEFAULT_DOMAIN = new DBDomain(); + DEFAULT_DOMAIN.setDomainCode("default_domain_id"); + DEFAULT_DOMAIN.setSmlSmpId("SAMPLE-SMP-ID"); + } + + private static final String ERROR_UNEXPECTED_MESSAGE ="[ERR-106] Something unexpected happend"; + private static final String ERROR_SMP_NOT_EXISTS ="[ERR-100] The SMP '"+DEFAULT_DOMAIN.getSmlSmpId()+"' doesn't exist"; + private static final String ERROR_SMP_ALREADY_EXISTS ="[ERR-106] The SMP '"+DEFAULT_DOMAIN.getSmlSmpId()+"' already exists"; + private static final String ERROR_PI_ALREADY_EXISTS = "[ERR-106] The participant identifier 'sample:value' does already exist for the scheme sample:scheme"; + private static final String ERROR_PI_NO_EXISTS = "[ERR-100] The participant identifier 'sample:value' doesn't exist for the scheme sample:scheme"; + + @Rule + public ExpectedException expectedExeption = ExpectedException.none(); + + @Autowired + SmlIntegrationConfiguration mockSml; + + @Autowired + private SmlConnector smlConnector; + + @Autowired + public void setup(){ + mockSml.reset(); + } + + @Test + public void testRegisterDomainInDns() throws UnauthorizedFault, InternalErrorFault, BadRequestFault { + //when + boolean result = smlConnector.registerDomain(DEFAULT_DOMAIN); + + //then + assertTrue(result); + assertEquals(1, mockSml.getSmpManagerClientMocks().size()); + verify(mockSml.getSmpManagerClientMocks().get(0)).create(any()); + Mockito.verifyNoMoreInteractions(mockSml.getSmpManagerClientMocks().toArray()); + } + + @Test + public void testRegisterDomainInDnsAlreadyExists() throws UnauthorizedFault, InternalErrorFault, BadRequestFault { + //when + BadRequestFault ex = new BadRequestFault(ERROR_SMP_ALREADY_EXISTS); + mockSml.setThrowException(ex); + boolean result = smlConnector.registerDomain(DEFAULT_DOMAIN); + + //then + assertTrue(result); + assertEquals(1, mockSml.getSmpManagerClientMocks().size()); + verify(mockSml.getSmpManagerClientMocks().get(0)).create(any()); + Mockito.verifyNoMoreInteractions(mockSml.getSmpManagerClientMocks().toArray()); + } + + @Test + public void testRegisterDomainInDnsUnknownException(){ + //when + String message = "something unexpected"; + Exception ex = new Exception(message); + mockSml.setThrowException(ex); + expectedExeption.expectMessage(message); + expectedExeption.expect(SMPRuntimeException .class); + + smlConnector.registerDomain(DEFAULT_DOMAIN); + } + + @Test + public void testRegisterDomainInDnsNewClientIsAlwaysCreated() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + //when + smlConnector.registerDomain(DEFAULT_DOMAIN); + smlConnector.registerDomain(DEFAULT_DOMAIN); + + //then + assertEquals(2, mockSml.getSmpManagerClientMocks().size()); + verify(mockSml.getSmpManagerClientMocks().get(0)).create(any()); + verify(mockSml.getSmpManagerClientMocks().get(1)).create(any()); + Mockito.verifyNoMoreInteractions(mockSml.getSmpManagerClientMocks().toArray()); + } + + @Test + public void testDomainUnregisterFromDns() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + //when + boolean result = smlConnector.unregisterDomain(DEFAULT_DOMAIN); + + //then + assertTrue(result); + assertEquals(1, mockSml.getSmpManagerClientMocks().size()); + verify(mockSml.getSmpManagerClientMocks().get(0)).delete(any()); + Mockito.verifyNoMoreInteractions(mockSml.getSmpManagerClientMocks().toArray()); + } + + @Test + public void testUnregisterDomainFromDnsNewClientIsAlwaysCreated() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + //when + smlConnector.unregisterDomain(DEFAULT_DOMAIN); + smlConnector.unregisterDomain(DEFAULT_DOMAIN); + + //then + assertEquals(2, mockSml.getSmpManagerClientMocks().size()); + verify(mockSml.getSmpManagerClientMocks().get(0)).delete(any()); + verify(mockSml.getSmpManagerClientMocks().get(1)).delete(any()); + Mockito.verifyNoMoreInteractions(mockSml.getSmpManagerClientMocks().toArray()); + } + + @Test + public void testUnregisterDomainFromDnsThrowUnknownBadRequestFault() { + //when + BadRequestFault ex = new BadRequestFault(ERROR_UNEXPECTED_MESSAGE); + mockSml.setThrowException(ex); + expectedExeption.expectMessage(ERROR_UNEXPECTED_MESSAGE); + expectedExeption.expect(SMPRuntimeException .class); + + smlConnector.unregisterDomain(DEFAULT_DOMAIN); + } + + @Test + public void testUnregisterDomainFromDnsThrowUnknownException() { + //when + String message = "something unexpected"; + Exception ex = new Exception(message); + mockSml.setThrowException(ex); + expectedExeption.expectMessage(message); + expectedExeption.expect(SMPRuntimeException .class); + + smlConnector.unregisterDomain(DEFAULT_DOMAIN); + } + + @Test + public void testUnregisterDomainFromDnsNotExists() { + //when + BadRequestFault ex = new BadRequestFault(ERROR_SMP_NOT_EXISTS); + mockSml.setThrowException(ex); + boolean suc = smlConnector.unregisterDomain(DEFAULT_DOMAIN); + + assertTrue(suc); + } + + @Test + public void testIsOkMessageForDomainNull(){ + boolean suc = smlConnector.isOkMessage(DEFAULT_DOMAIN, null); + + assertFalse(suc); + } + + @Test + public void testIsOkMessageForDomainFalse(){ + + boolean suc = smlConnector.isOkMessage(DEFAULT_DOMAIN, ERROR_UNEXPECTED_MESSAGE); + + assertFalse(suc); + } +} diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorTestParticipant.java similarity index 70% rename from smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorTest.java rename to smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorTestParticipant.java index 04597ba821ba8ac859cb6def605b40c18680466a..2249cbade86bd7bfc85caaa1cee81d7c3cf9a11c 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorTest.java +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorTestParticipant.java @@ -38,52 +38,34 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.booleanThat; import static org.mockito.Mockito.verify; /** - * Created by gutowpa on 08/01/2018. + * Created by JRC + * since 4.1. */ -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { SmlConnector.class,SmlIntegrationConfiguration.class, - SecurityUtilsServices.class, UIKeystoreService.class, - ConversionTestConfig.class, PropertiesSingleDomainTestConfig.class}) -@Configuration -@TestPropertySource(properties = { - "bdmsl.integration.enabled=true"}) -public class SmlConnectorTest { +public class SmlConnectorTestParticipant extends SmlConnectorTestBase { - // private static List<IManageParticipantIdentifierWS> smlClientMocks = new ArrayList<>(); - private static final ParticipantIdentifierType PARTICIPANT_ID = new ParticipantIdentifierType("sample:value", "sample:scheme"); - private static final DBDomain DEFAULT_DOMAIN; - static { - DEFAULT_DOMAIN = new DBDomain(); - DEFAULT_DOMAIN.setDomainCode("default_domain_id"); - DEFAULT_DOMAIN.setSmlSmpId("SAMPLE-SMP-ID"); - } - - private static final String ERROR_UNEXPECTED_MESSAGE ="[ERR-106] Something unexpected happend"; - private static final String ERROR_SMP_NOT_EXISTS ="[ERR-100] The SMP '"+DEFAULT_DOMAIN.getSmlSmpId()+"' doesn't exist"; - private static final String ERROR_SMP_ALREADY_EXISTS ="[ERR-106] The SMP '"+DEFAULT_DOMAIN.getSmlSmpId()+"' already exists"; - private static final String ERROR_PI_ALREADY_EXISTS = "[ERR-106] The participant identifier 'sample:value' does already exist for the scheme sample:scheme"; - - @Rule - public ExpectedException expectedExeption = ExpectedException.none(); - - @Autowired - SmlIntegrationConfiguration mockSml; - @Autowired - private SmlConnector smlConnector; + @Test + public void testRegisterInDns() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + //when + boolean result = smlConnector.registerInDns(PARTICIPANT_ID, DEFAULT_DOMAIN); - @Autowired - public void setup(){ - mockSml.reset(); + //then + assertTrue(result); + assertEquals(1, mockSml.getParticipantManagmentClientMocks().size()); + verify(mockSml.getParticipantManagmentClientMocks().get(0)).create(any()); + Mockito.verifyNoMoreInteractions(mockSml.getParticipantManagmentClientMocks().toArray()); } @Test - public void testRegisterInDns() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + public void testRegisterInDnsAlreadyExists() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { //when + BadRequestFault ex = new BadRequestFault(ERROR_PI_ALREADY_EXISTS); + mockSml.setThrowException(ex); boolean result = smlConnector.registerInDns(PARTICIPANT_ID, DEFAULT_DOMAIN); //then @@ -93,6 +75,18 @@ public class SmlConnectorTest { Mockito.verifyNoMoreInteractions(mockSml.getParticipantManagmentClientMocks().toArray()); } + @Test + public void testRegisterInDnsUnknownException() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + //when + String message = "something unexpected"; + Exception ex = new Exception(message); + mockSml.setThrowException(ex); + expectedExeption.expectMessage(message); + expectedExeption.expect(SMPRuntimeException .class); + + smlConnector.registerInDns(PARTICIPANT_ID, DEFAULT_DOMAIN); + } + @Test public void testRegisterInDnsNewClientIsAlwaysCreated() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { //when @@ -131,57 +125,63 @@ public class SmlConnectorTest { Mockito.verifyNoMoreInteractions(mockSml.getParticipantManagmentClientMocks().toArray()); } - @Test - public void testIsOkMessageForParticipantNull(){ - - boolean suc = smlConnector.isOkMessage(PARTICIPANT_ID, null); + public void testUnregisterFromDnsThrowUnknownBadRequestFault() { + //when + BadRequestFault ex = new BadRequestFault(ERROR_UNEXPECTED_MESSAGE); + mockSml.setThrowException(ex); + expectedExeption.expectMessage(ERROR_UNEXPECTED_MESSAGE); + expectedExeption.expect(SMPRuntimeException .class); - assertFalse(suc); + smlConnector.unregisterFromDns(PARTICIPANT_ID, DEFAULT_DOMAIN); } @Test - public void testIsOkMessageForParticipantOk(){ - boolean suc = smlConnector.isOkMessage(PARTICIPANT_ID, ERROR_PI_ALREADY_EXISTS); + public void testUnregisterFromDnsThrowUnknownException() { + //when + String message = "something unexpected"; + Exception ex = new Exception(message); + mockSml.setThrowException(ex); + expectedExeption.expectMessage(message); + expectedExeption.expect(SMPRuntimeException .class); - assertTrue(suc); + smlConnector.unregisterFromDns(PARTICIPANT_ID, DEFAULT_DOMAIN); } @Test - public void testIsOkMessageForParticipantFalse(){ - boolean suc = smlConnector.isOkMessage(PARTICIPANT_ID, ERROR_UNEXPECTED_MESSAGE); + public void testUnregisterFromDnsNotExists() { + //when + BadRequestFault ex = new BadRequestFault(ERROR_PI_NO_EXISTS); + mockSml.setThrowException(ex); + boolean suc = smlConnector.unregisterFromDns(PARTICIPANT_ID, DEFAULT_DOMAIN); - assertFalse(suc); + assertTrue(suc); } + @Test - public void testIsOkMessageForDomainNull(){ - boolean suc = smlConnector.isOkMessage(DEFAULT_DOMAIN, null); + public void testIsOkMessageForParticipantNull(){ + + boolean suc = smlConnector.isOkMessage(PARTICIPANT_ID, null); assertFalse(suc); } @Test - public void testIsOkMessageForParticipantOkAdd(){ - boolean suc = smlConnector.isOkMessage(DEFAULT_DOMAIN, ERROR_SMP_ALREADY_EXISTS); - - assertTrue(suc); - } - @Test - public void testIsOkMessageForParticipantOkDelete(){ - boolean suc = smlConnector.isOkMessage(DEFAULT_DOMAIN, ERROR_SMP_NOT_EXISTS); + public void testIsOkMessageForParticipantOk(){ + boolean suc = smlConnector.isOkMessage(PARTICIPANT_ID, ERROR_PI_ALREADY_EXISTS); assertTrue(suc); } @Test - public void testIsOkMessageForDomainFalse(){ - - boolean suc = smlConnector.isOkMessage(DEFAULT_DOMAIN, ERROR_UNEXPECTED_MESSAGE); + public void testIsOkMessageForParticipantFalse(){ + boolean suc = smlConnector.isOkMessage(PARTICIPANT_ID, ERROR_UNEXPECTED_MESSAGE); assertFalse(suc); } + @Test public void testProcessSMLErrorMessageBadRequestFaultIgnore(){ @@ -212,6 +212,14 @@ public class SmlConnectorTest { smlConnector.processSMLErrorMessage(ex, PARTICIPANT_ID); } + @Test + public void testProcessSMLErrorMessageNoFoundFaultOk(){ + + NotFoundFault ex = new NotFoundFault(ERROR_PI_NO_EXISTS); + + smlConnector.processSMLErrorMessage(ex, PARTICIPANT_ID); + } + }