From 3fcb2edf0b6b6bd91b88ee566af3e38a8d0c3565 Mon Sep 17 00:00:00 2001 From: Sebastian-Ion TINCU <Sebastian-Ion.TINCU@ext.ec.europa.eu> Date: Mon, 26 Feb 2024 09:24:13 +0100 Subject: [PATCH] EDELIVERY-11317 User should be able to check connection with SMP when changing certificate. Add unit tests. Replace SmlIntegrationConfiguration with MockBeans on the WS clients. --- .../conversion/SmlIdentifierConverter.java | 25 +- .../smp/services/ui/UIDomainService.java | 3 +- .../ec/edelivery/smp/sml/SmlConnector.java | 33 +- .../config/SmlIntegrationConfiguration.java | 151 ---------- .../SmlIdentifierConverterTest.java | 79 ++++- .../edelivery/smp/data/dao/TestUtilsDao.java | 20 +- .../smp/services/DomainServiceTest.java | 27 +- ...ntegrationServiceNoSMLIntegrationTest.java | 100 ------- .../services/SMLIntegrationServiceTest.java | 159 +++++++--- .../ui/UIDomainPublicServiceTest.java | 2 +- .../smp/services/ui/UIDomainServiceTest.java | 62 +++- .../smp/sml/SmlConnectorDomainTest.java | 222 +++++++++----- .../smp/sml/SmlConnectorParticipantTest.java | 282 +++++++++++++----- .../edelivery/smp/testutil/TestConstants.java | 1 + 14 files changed, 649 insertions(+), 517 deletions(-) delete mode 100644 smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmlIntegrationConfiguration.java delete mode 100644 smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/SMLIntegrationServiceNoSMLIntegrationTest.java diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/conversion/SmlIdentifierConverter.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/conversion/SmlIdentifierConverter.java index 0e60154ab..67e76057b 100644 --- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/conversion/SmlIdentifierConverter.java +++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/conversion/SmlIdentifierConverter.java @@ -23,6 +23,7 @@ import ec.services.wsdl.bdmsl.data._1.ParticipantsType; import ec.services.wsdl.bdmsl.data._1.SMPAdvancedServiceForParticipantType; import eu.europa.ec.edelivery.smp.identifiers.Identifier; import org.busdox.servicemetadata.locator._1.ServiceMetadataPublisherServiceForParticipantType; +import org.busdox.transport.identifiers._1.ParticipantIdentifierType; import static org.apache.commons.lang3.StringUtils.isBlank; @@ -34,11 +35,10 @@ public class SmlIdentifierConverter { public static ServiceMetadataPublisherServiceForParticipantType toBusdoxParticipantId(Identifier participantId, String smpId) { validate(participantId, smpId); + ParticipantIdentifierType parId = toParticipantIdentifierType(participantId); + ServiceMetadataPublisherServiceForParticipantType busdoxIdentifier = new ServiceMetadataPublisherServiceForParticipantType(); busdoxIdentifier.setServiceMetadataPublisherID(smpId); - org.busdox.transport.identifiers._1.ParticipantIdentifierType parId = new org.busdox.transport.identifiers._1.ParticipantIdentifierType(); - parId.setScheme(participantId.getScheme()); - parId.setValue(participantId.getValue()); busdoxIdentifier.setParticipantIdentifier(parId); return busdoxIdentifier; } @@ -46,10 +46,9 @@ public class SmlIdentifierConverter { public static ParticipantsType toParticipantsType(Identifier participantId, String smpId) { validate(participantId, smpId); + ParticipantIdentifierType parId = toParticipantIdentifierType(participantId); + ParticipantsType participantsType = new ParticipantsType(); - org.busdox.transport.identifiers._1.ParticipantIdentifierType parId = new org.busdox.transport.identifiers._1.ParticipantIdentifierType(); - parId.setScheme(participantId.getScheme()); - parId.setValue(participantId.getValue()); participantsType.setParticipantIdentifier(parId); participantsType.setServiceMetadataPublisherID(smpId); return participantsType; @@ -59,15 +58,16 @@ public class SmlIdentifierConverter { public static SMPAdvancedServiceForParticipantType toBDMSLAdvancedParticipantId(Identifier participantId, String smpId, String serviceMetadata) { validate(participantId, smpId); + ServiceMetadataPublisherServiceForParticipantType busdoxIdentifier = toBusdoxParticipantId(participantId, smpId); + SMPAdvancedServiceForParticipantType bdmslRequest = new SMPAdvancedServiceForParticipantType(); bdmslRequest.setServiceName(serviceMetadata); - ServiceMetadataPublisherServiceForParticipantType bdxlRequest = toBusdoxParticipantId(participantId, smpId); - bdmslRequest.setCreateParticipantIdentifier(bdxlRequest); + bdmslRequest.setCreateParticipantIdentifier(busdoxIdentifier); return bdmslRequest; } - private static void validate(Identifier participantId, String smpId) { + protected static void validate(Identifier participantId, String smpId) { if (isBlank(smpId)) { throw new IllegalStateException("SMP ID is null or empty"); } @@ -75,4 +75,11 @@ public class SmlIdentifierConverter { throw new IllegalStateException("Participant Scheme or Id is null or empty"); } } + + private static ParticipantIdentifierType toParticipantIdentifierType(Identifier participantId) { + ParticipantIdentifierType parId = new ParticipantIdentifierType(); + parId.setScheme(participantId.getScheme()); + parId.setValue(participantId.getValue()); + return parId; + } } diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainService.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainService.java index f2394bf4e..2ea8bf73d 100644 --- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainService.java +++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainService.java @@ -74,7 +74,8 @@ public class UIDomainService extends UIServiceBase<DBDomain, DomainRO> { ResourceDefDao resourceDefDao, DomainResourceDefDao domainResourceDefDao, GroupDao groupDao, - GroupMemberDao groupMemberDao, SMLIntegrationService smlIntegrationService) { + GroupMemberDao groupMemberDao, + SMLIntegrationService smlIntegrationService) { this.conversionService = conversionService; this.domainDao = domainDao; this.resourceDao = resourceDao; 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 2fa54ea76..fbcdd429a 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 @@ -104,7 +104,7 @@ public class SmlConnector implements ApplicationContextAware { } String normalizedParticipantString = identifierService.formatParticipant(normalizedParticipantId); if (!domain.isSmlRegistered()) { - LOG.warn("Participant {} is not registered to SML because domain {} is not registered!", + LOG.info("Participant {} is not registered to SML because domain {} is not registered!", normalizedParticipantString, domain.getDomainCode()); return false; } @@ -144,7 +144,7 @@ public class SmlConnector implements ApplicationContextAware { String normalizedParticipantString = identifierService.formatParticipant(normalizedParticipantId); if (!domain.isSmlRegistered()) { - LOG.warn("Cannot check if Participant {} exists when domain {} is not registered!", + LOG.info("Cannot check if Participant {} exists when domain {} is not registered!", normalizedParticipantString, domain.getDomainCode()); return false; } @@ -154,9 +154,7 @@ public class SmlConnector implements ApplicationContextAware { ParticipantsType smlRequest = toParticipantsType(normalizedParticipantId, domain.getSmlSmpId()); ExistsParticipantResponseType existsParticipantResponseType = getBDMSLWSClient(domain).existsParticipantIdentifier(smlRequest); return existsParticipantResponseType.isExist(); - } catch (BadRequestFault e) { - return processSMLErrorMessage(e, normalizedParticipantId); - } catch (NotFoundFault e) { + } catch (BadRequestFault | NotFoundFault e) { return processSMLErrorMessage(e, normalizedParticipantId); } catch (Exception e) { LOG.error(e.getClass().getName() + e.getMessage(), e); @@ -176,16 +174,7 @@ public class SmlConnector implements ApplicationContextAware { getBDMSLWSClient(domain).createParticipantIdentifier(smlRequest); } - protected boolean processSMLErrorMessage(BadRequestFault e, Identifier participantIdentifierType) { - if (!isOkMessage(participantIdentifierType, e.getMessage())) { - LOG.error(e.getMessage(), e); - throw new SMPRuntimeException(ErrorCode.SML_INTEGRATION_EXCEPTION, e, ExceptionUtils.getRootCauseMessage(e)); - } - LOG.warn(e.getMessage(), e); - return true; - } - - protected boolean processSMLErrorMessage(NotFoundFault e, Identifier participantIdentifierType) { + protected boolean processSMLErrorMessage(Exception e, Identifier participantIdentifierType) { if (!isOkMessage(participantIdentifierType, e.getMessage())) { LOG.error(e.getMessage(), e); throw new SMPRuntimeException(ErrorCode.SML_INTEGRATION_EXCEPTION, e, ExceptionUtils.getRootCauseMessage(e)); @@ -249,9 +238,7 @@ public class SmlConnector implements ApplicationContextAware { try { ServiceMetadataPublisherServiceType smlSmpRequest = getServiceMetadataPublisherServiceType(smlSmpId); getSMPManagerWSClient(domain).read(smlSmpRequest); - } catch (BadRequestFault e) { - processSMLErrorMessage(e, domain); - } catch (NotFoundFault e) { + } catch (BadRequestFault | NotFoundFault e) { processSMLErrorMessage(e, domain); } catch (Exception e) { LOG.error(e.getClass().getName() + e.getMessage(), e); @@ -274,15 +261,7 @@ public class SmlConnector implements ApplicationContextAware { return smlSmpRequest; } - private void processSMLErrorMessage(BadRequestFault e, DBDomain domain) { - if (!isOkMessage(domain, e.getMessage())) { - LOG.error(e.getMessage(), e); - throw new SMPRuntimeException(ErrorCode.SML_INTEGRATION_EXCEPTION, e, ExceptionUtils.getRootCauseMessage(e)); - } - LOG.warn(e.getMessage(), e); - } - - private void processSMLErrorMessage(NotFoundFault e, DBDomain domain) { + private void processSMLErrorMessage(Exception e, DBDomain domain) { if (!isOkMessage(domain, e.getMessage())) { LOG.error(e.getMessage(), e); throw new SMPRuntimeException(ErrorCode.SML_INTEGRATION_EXCEPTION, e, ExceptionUtils.getRootCauseMessage(e)); 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 deleted file mode 100644 index 317469bc5..000000000 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmlIntegrationConfiguration.java +++ /dev/null @@ -1,151 +0,0 @@ -/*- - * #START_LICENSE# - * smp-server-library - * %% - * Copyright (C) 2017 - 2023 European Commission | eDelivery | DomiSMP - * %% - * 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 at: - * - * [PROJECT_HOME]\license\eupl-1.2\license.txt or https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 - * - * 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. - * #END_LICENSE# - */ -package eu.europa.ec.edelivery.smp.config; - -import eu.europa.ec.bdmsl.ws.soap.*; -import eu.europa.ec.edelivery.smp.data.model.DBDomain; -import org.mockito.Mockito; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Primary; -import org.springframework.context.annotation.Scope; -import org.springframework.stereotype.Component; - -import javax.xml.ws.http.HTTPException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.willThrow; -import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE; - - -@Component -public class SmlIntegrationConfiguration { - - protected DBDomain defaultDomain; - - - protected List<IManageServiceMetadataWS> smpManagerClientMocks = new ArrayList<>(); - protected Map<IManageServiceMetadataWS, AuthenticationTestDataHolder> smpManagerClientMocksData = new HashMap<>(); - - protected List<IManageParticipantIdentifierWS> smlClientMocks = new ArrayList<>(); - protected Map<IManageParticipantIdentifierWS, AuthenticationTestDataHolder> smlClientMocksData = new HashMap<>(); - protected int throwExceptionAfterParticipantCallCount = -1; - - protected Throwable throwException; - - public void reset() { - smpManagerClientMocks.clear(); - smpManagerClientMocksData.clear(); - smlClientMocks.clear(); - smlClientMocksData.clear(); - - - defaultDomain = new DBDomain(); - defaultDomain.setDomainCode("default_domain_id"); - defaultDomain.setSmlSmpId("SAMPLE-SMP-ID"); - defaultDomain.setSmlRegistered(false); - defaultDomain.setSmlClientCertAuth(false); - defaultDomain.setSmlClientKeyAlias("clientAlias"); - setThrowExceptionAfterParticipantCallCount(-1); - setThrowException(null); - } - - @Bean("MockIManageServiceMetadataWS") - @Primary - @Scope(SCOPE_PROTOTYPE) - public IManageServiceMetadataWS smpManagerClient() 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(); - smpManagerClientMocks.add(clientMock); - smpManagerClientMocksData.put(clientMock, dh); - return clientMock; - } - - @Bean("MockIManageParticipantIdentifierWS") - @Scope(SCOPE_PROTOTYPE) - @Primary - public IManageParticipantIdentifierWS smpParticipantClient() 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(); - smlClientMocks.add(clientMock); - smlClientMocksData.put(clientMock, dh); - return clientMock; - } - - public List<IManageServiceMetadataWS> getSmpManagerClientMocks() { - return smpManagerClientMocks; - } - - public Map<IManageServiceMetadataWS, AuthenticationTestDataHolder> getSmpManagerClientMocksData() { - return smpManagerClientMocksData; - } - - public List<IManageParticipantIdentifierWS> getParticipantManagmentClientMocks() { - return smlClientMocks; - } - - public Map<IManageParticipantIdentifierWS, AuthenticationTestDataHolder> getParticipantManagmentClientMocksData() { - return smlClientMocksData; - } - - public int getThrowExceptionAfterParticipantCallCount() { - return throwExceptionAfterParticipantCallCount; - } - - 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/SmlIdentifierConverterTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/conversion/SmlIdentifierConverterTest.java index 01d89a775..0a6b06630 100644 --- 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 @@ -19,6 +19,8 @@ package eu.europa.ec.edelivery.smp.conversion; +import ec.services.wsdl.bdmsl.data._1.ParticipantsType; +import ec.services.wsdl.bdmsl.data._1.SMPAdvancedServiceForParticipantType; import eu.europa.ec.edelivery.smp.identifiers.Identifier; import org.busdox.servicemetadata.locator._1.ServiceMetadataPublisherServiceForParticipantType; import org.junit.Test; @@ -31,12 +33,13 @@ import static org.junit.Assert.assertNull; */ 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"; + private static final String SMP_ID = "SMP-ID"; + private static final String ID_VALUE = "sample:value"; + private static final String ID_SCHEME = "sample:scheme"; + private static final String SERVICE_NAME = "naptrService"; @Test - public void positiveCase() { + public void toBusdoxParticipantId() { //given Identifier participantId = new Identifier(ID_VALUE, ID_SCHEME); @@ -49,22 +52,67 @@ public class SmlIdentifierConverterTest { assertEquals(ID_VALUE, result.getParticipantIdentifier().getValue()); } - @Test(expected = IllegalStateException.class) - public void negativeCaseMissingSmpId() { + @Test + public void toBusdoxParticipantId_NullScheme() { + //given + Identifier participantId = new Identifier(ID_VALUE, null); + + //when + ServiceMetadataPublisherServiceForParticipantType result = SmlIdentifierConverter.toBusdoxParticipantId(participantId, SMP_ID); + //then + assertEquals(SMP_ID, result.getServiceMetadataPublisherID()); + assertNull(result.getParticipantIdentifier().getScheme()); + assertEquals(ID_VALUE, result.getParticipantIdentifier().getValue()); + } + + @Test + public void toBDMSLAdvancedParticipantId() { //given Identifier participantId = new Identifier(ID_VALUE, ID_SCHEME); //when - SmlIdentifierConverter.toBusdoxParticipantId(participantId, null); + SMPAdvancedServiceForParticipantType result = SmlIdentifierConverter.toBDMSLAdvancedParticipantId(participantId, SMP_ID, SERVICE_NAME); + + //then + assertEquals(SERVICE_NAME, result.getServiceName()); + assertEquals(ID_SCHEME, result.getCreateParticipantIdentifier().getParticipantIdentifier().getScheme()); + assertEquals(ID_VALUE, result.getCreateParticipantIdentifier().getParticipantIdentifier().getValue()); } @Test - public void positiveCaseWithNullScheme() { + public void toBDMSLAdvancedParticipantId_NullScheme() { //given Identifier participantId = new Identifier(ID_VALUE, null); //when - ServiceMetadataPublisherServiceForParticipantType result = SmlIdentifierConverter.toBusdoxParticipantId(participantId, SMP_ID); + SMPAdvancedServiceForParticipantType result = SmlIdentifierConverter.toBDMSLAdvancedParticipantId(participantId, SMP_ID, SERVICE_NAME); + //then + assertEquals(SERVICE_NAME, result.getServiceName()); + assertNull(result.getCreateParticipantIdentifier().getParticipantIdentifier().getScheme()); + assertEquals(ID_VALUE, result.getCreateParticipantIdentifier().getParticipantIdentifier().getValue()); + } + + @Test + public void toParticipantsType() { + //given + Identifier participantId = new Identifier(ID_VALUE, ID_SCHEME); + + //when + ParticipantsType result = SmlIdentifierConverter.toParticipantsType(participantId, SMP_ID); + + //then + assertEquals(SMP_ID, result.getServiceMetadataPublisherID()); + assertEquals(ID_SCHEME, result.getParticipantIdentifier().getScheme()); + assertEquals(ID_VALUE, result.getParticipantIdentifier().getValue()); + } + + @Test + public void toParticipantsType_NullScheme() { + //given + Identifier participantId = new Identifier(ID_VALUE, null); + + //when + ParticipantsType result = SmlIdentifierConverter.toParticipantsType(participantId, SMP_ID); //then assertEquals(SMP_ID, result.getServiceMetadataPublisherID()); assertNull(result.getParticipantIdentifier().getScheme()); @@ -72,11 +120,20 @@ public class SmlIdentifierConverterTest { } @Test(expected = IllegalStateException.class) - public void negativeCaseMissingValue() { + public void validate_negativeCaseMissingSmpId() { + //given + Identifier participantId = new Identifier(ID_VALUE, ID_SCHEME); + + //when + SmlIdentifierConverter.validate(participantId, null); + } + + @Test(expected = IllegalStateException.class) + public void validate_negativeCaseMissingValue() { //given Identifier participantId = new Identifier(null, ID_SCHEME); //when - SmlIdentifierConverter.toBusdoxParticipantId(participantId, SMP_ID); + SmlIdentifierConverter.validate(participantId, SMP_ID); } } diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/data/dao/TestUtilsDao.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/data/dao/TestUtilsDao.java index 98089cdb4..d03e2d626 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/data/dao/TestUtilsDao.java +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/data/dao/TestUtilsDao.java @@ -68,6 +68,7 @@ public class TestUtilsDao { DBDomain d1; DBDomain d2; + DBDomain d3; DBResourceDef resourceDefSmp; DBSubresourceDef subresourceDefSmp; DBResourceDef resourceDefCpp; @@ -115,9 +116,10 @@ public class TestUtilsDao { /** * Database can be cleaned by script before the next test; clean also the objects */ - public void clearData(){ + public void clearData() { d1 = null; d2 = null; + d3 = null; resourceDefSmp = null; subresourceDefSmp = null; resourceDefCpp = null; @@ -223,9 +225,11 @@ public class TestUtilsDao { } d1 = createDomain(TEST_DOMAIN_CODE_1); d2 = createDomain(TEST_DOMAIN_CODE_2); + d3 = createRegisteredDomain(TEST_DOMAIN_CODE_3); assertNotNull(d1.getId()); - assertNotNull(d1.getId()); + assertNotNull(d2.getId()); + assertNotNull(d3.getId()); } @Transactional @@ -587,6 +591,14 @@ public class TestUtilsDao { return createDomain(domainCode, VisibilityType.PUBLIC); } + @Transactional + public DBDomain createRegisteredDomain(String domainCode) { + DBDomain d = TestDBUtils.createDBDomain(domainCode); + d.setSmlRegistered(true); + persistFlushDetach(d); + return d; + } + @Transactional public DBDomain createDomain(String domainCode, VisibilityType visibility) { DBDomain d = TestDBUtils.createDBDomain(domainCode); @@ -654,6 +666,10 @@ public class TestUtilsDao { return d2; } + public DBDomain getD3() { + return d3; + } + public DBResourceDef getResourceDefSmp() { return resourceDefSmp; } diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/DomainServiceTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/DomainServiceTest.java index 391cfa7ca..80989dbef 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/DomainServiceTest.java +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/DomainServiceTest.java @@ -19,7 +19,8 @@ package eu.europa.ec.edelivery.smp.services; -import eu.europa.ec.edelivery.smp.config.SmlIntegrationConfiguration; +import eu.europa.ec.bdmsl.ws.soap.IManageParticipantIdentifierWS; +import eu.europa.ec.bdmsl.ws.soap.IManageServiceMetadataWS; import eu.europa.ec.edelivery.smp.config.enums.SMPPropertyEnum; import eu.europa.ec.edelivery.smp.conversion.IdentifierService; import eu.europa.ec.edelivery.smp.data.dao.AbstractJunit5BaseDao; @@ -39,6 +40,7 @@ import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.util.ReflectionTestUtils; import java.io.IOException; @@ -59,21 +61,19 @@ import static org.mockito.ArgumentMatchers.any; public class DomainServiceTest extends AbstractJunit5BaseDao { @Autowired - IdentifierService identifierService; + private IdentifierService identifierService; @Autowired - SmlIntegrationConfiguration integrationMock; - @Autowired - SmlConnector smlConnector; + private SmlConnector smlConnector; @Autowired private SMLIntegrationService smlIntegrationService; - - @Autowired - protected DomainDao domainDao; - + private DomainDao domainDao; @Autowired - protected DomainService testInstance; - + private DomainService testInstance; + @MockBean + private IManageParticipantIdentifierWS iManageParticipantIdentifierWS; + @MockBean + private IManageServiceMetadataWS iManageServiceMetadataWS; @BeforeEach public void prepareDatabase() throws IOException { @@ -91,17 +91,12 @@ public class DomainServiceTest extends AbstractJunit5BaseDao { setDatabaseProperty(SMPPropertyEnum.SML_LOGICAL_ADDRESS, "http://localhost/smp"); setDatabaseProperty(SMPPropertyEnum.SML_URL, "http://localhost/edelivery-sml"); setDatabaseProperty(SMPPropertyEnum.SML_ENABLED, "true"); - - integrationMock.reset(); - - } @ParameterizedTest @NullSource @ValueSource(strings = {"", " "}) public void getDomainForBlankCodeForSingleDomain(String searchCode) { - // given DBDomain testDomain01 = testUtilsDao.createDomain(TEST_DOMAIN_CODE_1); assertEquals(1, domainDao.getAllDomains().size()); diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/SMLIntegrationServiceNoSMLIntegrationTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/SMLIntegrationServiceNoSMLIntegrationTest.java deleted file mode 100644 index eb00da838..000000000 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/SMLIntegrationServiceNoSMLIntegrationTest.java +++ /dev/null @@ -1,100 +0,0 @@ -/*- - * #START_LICENSE# - * smp-webapp - * %% - * Copyright (C) 2017 - 2023 European Commission | eDelivery | DomiSMP - * %% - * 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 at: - * - * [PROJECT_HOME]\license\eupl-1.2\license.txt or https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 - * - * 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. - * #END_LICENSE# - */ - -package eu.europa.ec.edelivery.smp.services; - -import eu.europa.ec.edelivery.smp.config.enums.SMPPropertyEnum; -import eu.europa.ec.edelivery.smp.data.model.DBDomain; -import eu.europa.ec.edelivery.smp.data.model.doc.DBResource; -import eu.europa.ec.edelivery.smp.exceptions.SMPRuntimeException; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.transaction.annotation.Transactional; - -/** - * Purpose of class is to test ServiceGroupService base methods - * - * @author Joze Rihtarsic - * @since 4.1 - */ -@ContextConfiguration(classes = {SMLIntegrationService.class}) -public class SMLIntegrationServiceNoSMLIntegrationTest extends AbstractServiceIntegrationTest { - - - @Autowired - protected SMLIntegrationService testInstance; - - @Before - @Transactional - public void prepareDatabase() { - configurationDao.setPropertyToDatabase(SMPPropertyEnum.SML_ENABLED, "false", ""); - configurationDao.refreshProperties(); - - - testUtilsDao.clearData(); - testUtilsDao.createResources(); - } - - @Test - public void registerOnlyDomainToSml() { - - // given - DBDomain testDomain01 = testUtilsDao.getD1(); - testDomain01.setSmlRegistered(false); - - // when - SMPRuntimeException result = Assert.assertThrows(SMPRuntimeException.class, () -> testInstance.registerDomain(testDomain01)); - Assert.assertEquals("Configuration error: [SML integration is not enabled!]!", result.getMessage()); - } - - @Test - public void unregisterOnlyDomainToSml() { - - // given - DBDomain testDomain01 = testUtilsDao.getD1(); - testDomain01.setSmlRegistered(true); - - // when - SMPRuntimeException result = Assert.assertThrows(SMPRuntimeException.class, () -> testInstance.unRegisterDomain(testDomain01)); - Assert.assertEquals("Configuration error: [SML integration is not enabled!]!", result.getMessage()); - } - - @Test - public void registerParticipant() { - - DBDomain testDomain01 = testUtilsDao.getD1(); - DBResource resource = testUtilsDao.getResourceD1G1RD1(); - // nothing is expected to be thrown - testInstance.registerParticipant(resource, testDomain01); - } - - @Test - public void unregisterParticipant() { - - DBDomain testDomain01 = testUtilsDao.getD1(); - DBResource resource = testUtilsDao.getResourceD1G1RD1(); - // nothing is expected to be thrown - testInstance.unregisterParticipant(resource, testDomain01); - } - - -} diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/SMLIntegrationServiceTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/SMLIntegrationServiceTest.java index cf8d2caa0..d94d518a6 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/SMLIntegrationServiceTest.java +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/SMLIntegrationServiceTest.java @@ -19,29 +19,31 @@ package eu.europa.ec.edelivery.smp.services; -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.SmlIntegrationConfiguration; +import eu.europa.ec.bdmsl.ws.soap.*; import eu.europa.ec.edelivery.smp.conversion.IdentifierService; import eu.europa.ec.edelivery.smp.data.model.DBDomain; import eu.europa.ec.edelivery.smp.data.model.doc.DBResource; +import eu.europa.ec.edelivery.smp.exceptions.SMPRuntimeException; +import eu.europa.ec.edelivery.smp.identifiers.Identifier; import eu.europa.ec.edelivery.smp.sml.SmlConnector; +import org.busdox.servicemetadata.locator._1.ServiceMetadataPublisherServiceForParticipantType; +import org.busdox.servicemetadata.locator._1.ServiceMetadataPublisherServiceType; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.transaction.annotation.Transactional; import java.util.regex.Pattern; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** @@ -50,40 +52,33 @@ import static org.mockito.Mockito.verify; * @author Joze Rihtarsic * @since 4.1 */ -@RunWith(SpringRunner.class) -@ContextConfiguration(classes = {SmlIntegrationConfiguration.class, - SMLIntegrationService.class}) public class SMLIntegrationServiceTest extends AbstractServiceIntegrationTest { @Autowired IdentifierService identifierService; - @Autowired - SmlIntegrationConfiguration integrationMock; - @Autowired + @MockBean + private IManageServiceMetadataWS iManageServiceMetadataWS; + @MockBean + private IManageParticipantIdentifierWS iManageParticipantIdentifierWS; + @SpyBean protected SmlConnector smlConnector; @Autowired protected SMLIntegrationService testInstance; - @Autowired + @SpyBean ConfigurationService configurationService; @Before @Transactional public void prepareDatabase() { - ReflectionTestUtils.setField(testInstance, "identifierService", identifierService); - identifierService.configureParticipantIdentifierFormatter(null, false, Pattern.compile(".*")); - configurationService = Mockito.spy(configurationService); - smlConnector = Mockito.spy(smlConnector); - Mockito.doNothing().when(smlConnector).configureClient(any(), any(), any()); - - ReflectionTestUtils.setField(testInstance, "configurationService", configurationService); ReflectionTestUtils.setField(smlConnector, "configurationService", configurationService); + ReflectionTestUtils.setField(testInstance, "configurationService", configurationService); ReflectionTestUtils.setField(testInstance, "smlConnector", smlConnector); + ReflectionTestUtils.setField(testInstance, "identifierService", identifierService); - Mockito.doReturn(true).when(configurationService).isSMLIntegrationEnabled(); - - integrationMock.reset(); + Mockito.reset(smlConnector); + Mockito.doNothing().when(smlConnector).configureClient(any(), any(), any()); testUtilsDao.clearData(); testUtilsDao.createResources(); @@ -91,52 +86,136 @@ public class SMLIntegrationServiceTest extends AbstractServiceIntegrationTest { @Test public void registerDomainToSml() throws UnauthorizedFault, InternalErrorFault, BadRequestFault { - // given DBDomain testDomain01 = testUtilsDao.getD1(); testDomain01.setSmlRegistered(false); testUtilsDao.merge(testDomain01); + givenSmlIntegrationEnabled(true); // when testInstance.registerDomain(testDomain01); assertTrue(testDomain01.isSmlRegistered()); - assertEquals(1, integrationMock.getSmpManagerClientMocks().size()); - verify(integrationMock.getSmpManagerClientMocks().get(0)).create(any()); - Mockito.verifyNoMoreInteractions(integrationMock.getSmpManagerClientMocks().toArray()); - + verify(iManageServiceMetadataWS, times(1)).create(any(ServiceMetadataPublisherServiceType.class)); } @Test public void unregisterDomainToSml() throws UnauthorizedFault, InternalErrorFault, BadRequestFault, NotFoundFault { - // given DBDomain testDomain01 = testUtilsDao.getD1(); testDomain01.setSmlRegistered(true); + givenSmlIntegrationEnabled(true); // when testInstance.unRegisterDomain(testDomain01); assertFalse(testDomain01.isSmlRegistered()); - assertEquals(1, integrationMock.getSmpManagerClientMocks().size()); - verify(integrationMock.getSmpManagerClientMocks().get(0)).delete(testDomain01.getSmlSmpId()); - Mockito.verifyNoMoreInteractions(integrationMock.getSmpManagerClientMocks().toArray()); + verify(iManageServiceMetadataWS, times(1)).delete(testDomain01.getSmlSmpId()); } @Test - public void registerParticipant() throws NotFoundFault, UnauthorizedFault, InternalErrorFault, BadRequestFault { + public void registerParticipant() throws Exception { DBDomain testDomain01 = testUtilsDao.getD1(); testDomain01.setSmlRegistered(true); DBResource resource = testUtilsDao.getResourceD1G1RD1(); resource.setSmlRegistered(false); + givenSmlIntegrationEnabled(true); + + // when + testInstance.registerParticipant(resource, testDomain01); + + //then + verify(iManageParticipantIdentifierWS, times(1)).create(any(ServiceMetadataPublisherServiceForParticipantType.class)); + } + + @Test + public void participantExists() { + // given + DBDomain domain = testUtilsDao.getD1(); + DBResource resource = testUtilsDao.getResourceD1G1RD1(); + givenSmlIntegrationEnabled(true); + + Identifier identifier = identifierService.normalizeParticipant(resource.getIdentifierScheme(), resource.getIdentifierValue()); + Mockito.doReturn(true).when(smlConnector).participantExists(identifier, domain); + + // when + boolean participantExists = testInstance.participantExists(resource, domain); + + // then + Assert.assertTrue(participantExists); + } + + @Test + public void registerOnlyDomainToSml_smlIntegrationDisabled() { + // given + DBDomain testDomain01 = testUtilsDao.getD1(); + testDomain01.setSmlRegistered(false); + + givenSmlIntegrationEnabled(false); + + // when + SMPRuntimeException result = Assert.assertThrows(SMPRuntimeException.class, () -> testInstance.registerDomain(testDomain01)); + Assert.assertEquals("Configuration error: [SML integration is not enabled!]!", result.getMessage()); + } + + @Test + public void unregisterOnlyDomainToSml_smlIntegrationDisabled() { + // given + DBDomain testDomain01 = testUtilsDao.getD1(); + testDomain01.setSmlRegistered(true); + + givenSmlIntegrationEnabled(false); + // when + SMPRuntimeException result = Assert.assertThrows(SMPRuntimeException.class, () -> testInstance.unRegisterDomain(testDomain01)); + Assert.assertEquals("Configuration error: [SML integration is not enabled!]!", result.getMessage()); + } + + @Test + public void registerParticipant_smlIntegrationDisabled() { + DBDomain testDomain01 = testUtilsDao.getD1(); + DBResource resource = testUtilsDao.getResourceD1G1RD1(); + + givenSmlIntegrationEnabled(false); + + // nothing is expected to be thrown testInstance.registerParticipant(resource, testDomain01); + } - //then -- expect on call - assertEquals(1, integrationMock.getParticipantManagmentClientMocks().size()); - verify(integrationMock.getParticipantManagmentClientMocks().get(0)).create(any()); - Mockito.verifyNoMoreInteractions(integrationMock.getParticipantManagmentClientMocks().toArray()); + @Test + public void unregisterParticipant_smlIntegrationDisabled() { + DBDomain testDomain01 = testUtilsDao.getD1(); + DBResource resource = testUtilsDao.getResourceD1G1RD1(); + + givenSmlIntegrationEnabled(false); + + // nothing is expected to be thrown + testInstance.unregisterParticipant(resource, testDomain01); + } + + @Test + public void participantExists_smlIntegrationDisabled() { + DBDomain domain = testUtilsDao.getD1(); + DBResource resource = testUtilsDao.getResourceD1G1RD1(); + + givenSmlIntegrationEnabled(false); + + SMPRuntimeException result = Assert.assertThrows(SMPRuntimeException.class, () -> testInstance.participantExists(resource, domain)); + Assert.assertEquals("Configuration error: [SML integration is not enabled!]!", result.getMessage()); + } + + @Test + public void isDomainValid_smlIntegrationDisabled() { + DBDomain domain = testUtilsDao.getD1(); + + givenSmlIntegrationEnabled(false); + + SMPRuntimeException result = Assert.assertThrows(SMPRuntimeException.class, () -> testInstance.isDomainValid(domain)); + Assert.assertEquals("Configuration error: [SML integration is not enabled!]!", result.getMessage()); + } + private void givenSmlIntegrationEnabled(boolean enabled) { + Mockito.doReturn(enabled).when(configurationService).isSMLIntegrationEnabled(); } } diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainPublicServiceTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainPublicServiceTest.java index 45257b8f3..f306e7cb5 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainPublicServiceTest.java +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainPublicServiceTest.java @@ -50,7 +50,7 @@ class UIDomainPublicServiceTest extends AbstractJunit5BaseDao { @Test public void testGetTableList() { ServiceResult<DomainPublicRO> result = testInstance.getTableList(-1, -1, null, null, null); - assertEquals(2, result.getCount().intValue()); + assertEquals(3, result.getCount().intValue()); } @Test diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainServiceTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainServiceTest.java index 974cd4e91..9d6eed1ce 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainServiceTest.java +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/services/ui/UIDomainServiceTest.java @@ -22,10 +22,17 @@ import eu.europa.ec.edelivery.smp.data.dao.DomainDao; import eu.europa.ec.edelivery.smp.data.enums.VisibilityType; import eu.europa.ec.edelivery.smp.data.model.DBDomain; import eu.europa.ec.edelivery.smp.data.ui.DomainRO; +import eu.europa.ec.edelivery.smp.exceptions.BadRequestException; import eu.europa.ec.edelivery.smp.services.AbstractServiceTest; +import eu.europa.ec.edelivery.smp.services.SMLIntegrationService; +import org.apache.commons.lang3.StringUtils; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.test.util.ReflectionTestUtils; import java.util.Collections; import java.util.List; @@ -35,25 +42,30 @@ import static org.junit.Assert.*; public class UIDomainServiceTest extends AbstractServiceTest { @Autowired - UIDomainService testInstance; + private UIDomainService testInstance; @Autowired - DomainDao domainDao; + private DomainDao domainDao; +// @Autowired + @SpyBean + private SMLIntegrationService smlIntegrationService; @Before public void prepareDatabase() { testUtilsDao.clearData(); testUtilsDao.createResourceDefinitionsForDomains(); + +// smlIntegrationService = Mockito.spy(smlIntegrationService); + ReflectionTestUtils.setField(testInstance, "smlIntegrationService", smlIntegrationService); } @Test public void getAllDomains() { List<DomainRO> domainROS = testInstance.getAllDomains(); - assertEquals(2, domainROS.size()); + assertEquals(3, domainROS.size()); } @Test public void updateDomainData() { - DomainRO domainRO = new DomainRO(); domainRO.setDomainCode("NewCode"); domainRO.setVisibility(VisibilityType.INTERNAL); @@ -71,22 +83,60 @@ public class UIDomainServiceTest extends AbstractServiceTest { @Test public void updateSMLDomainData() { - DomainRO domainRO = new DomainRO(); domainRO.setSmlSubdomain("New SmlSubdomain"); domainRO.setSmlSmpId("NewSmlSmpId"); domainRO.setSmlClientKeyAlias("NewClientKeyAlias"); domainRO.setSmlClientCertAuth(false); DBDomain domain = testUtilsDao.getD1(); + testInstance.updateDomainSmlIntegrationData(domain.getId(), domainRO); - DBDomain result = domainDao.find(domain.getId()); + DBDomain result = domainDao.find(domain.getId()); assertEquals(domainRO.getSmlSubdomain(), result.getSmlSubdomain()); assertEquals(domainRO.getSmlSmpId(), result.getSmlSmpId()); assertEquals(domainRO.getSmlClientKeyAlias(), result.getSmlClientKeyAlias()); assertEquals(domainRO.isSmlClientCertAuth(), result.isSmlClientCertAuth()); } + @Test + public void updateSMLDomainData_domainNotFound() { + BadRequestException result = Assert.assertThrows(BadRequestException.class, () -> + testInstance.updateDomainSmlIntegrationData(-1l, new DomainRO())); + Assert.assertEquals("Domain does not exist in database!", result.getMessage()); + } + + @Test + public void updateSMLDomainData_registeredDomainSmpIdChangeNotAllowed() { + DBDomain domain = testUtilsDao.getD3(); + + DomainRO domainRO = new DomainRO(); + domainRO.setSmlSmpId("utestRegistered03"); + + BadRequestException result = Assert.assertThrows(BadRequestException.class, () -> + testInstance.updateDomainSmlIntegrationData(domain.getId(), domainRO)); + Assert.assertEquals("SMP-SML identifier must not change for registered domain [utestRegistered03]!", result.getMessage()); + } + + @Test + public void updateSMLDomainData_invalidSmlIntegrationCertificate() { + DBDomain domain = testUtilsDao.getD3(); + + // Ensure domain DTO doesn't update domain existing values or #isDomainValid(domain) below won't match + // As a workaround, we can use #isDomainValid(Mockito.any()) but this would be less clean + DomainRO domainRO = new DomainRO(); + domainRO.setSmlSmpId(StringUtils.trim(domain.getSmlSmpId())); + domainRO.setSmlSubdomain(domain.getSmlSubdomain()); + domainRO.setSmlClientKeyAlias(domain.getSmlClientKeyAlias()); + domainRO.setSmlClientCertAuth(domain.isSmlClientCertAuth()); + + Mockito.doReturn(false).when(smlIntegrationService).isDomainValid(domain); + + BadRequestException result = Assert.assertThrows(BadRequestException.class, () -> + testInstance.updateDomainSmlIntegrationData(domain.getId(), domainRO)); + Assert.assertEquals("The SML-SMP certificate for domain [utestRegistered03] is not valid!", result.getMessage()); + } + @Test public void updateDomainResourceListClear() { DBDomain testDomain = testUtilsDao.getD1(); diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorDomainTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorDomainTest.java index d507add2a..0b21fa695 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorDomainTest.java +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorDomainTest.java @@ -21,22 +21,23 @@ package eu.europa.ec.edelivery.smp.sml; import eu.europa.ec.bdmsl.ws.soap.BadRequestFault; +import eu.europa.ec.bdmsl.ws.soap.IManageServiceMetadataWS; 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.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.AbstractServiceTest; import eu.europa.ec.edelivery.smp.services.ConfigurationService; +import org.busdox.servicemetadata.locator._1.ServiceMetadataPublisherServiceType; +import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; import org.junit.jupiter.api.Assertions; -import org.junit.rules.ExpectedException; +import org.mockito.Mock; import org.mockito.Mockito; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.test.util.ReflectionTestUtils; import java.util.UUID; @@ -44,6 +45,8 @@ import java.util.UUID; import static eu.europa.ec.edelivery.smp.sml.SmlConnectorTestConstants.*; import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** @@ -52,174 +55,251 @@ import static org.mockito.Mockito.verify; */ public class SmlConnectorDomainTest extends AbstractServiceTest { - @Autowired - protected ConfigurationService configurationService; + // Beans + @SpyBean + private ConfigurationService configurationService; + @MockBean + private IManageServiceMetadataWS iManageServiceMetadataWS; + @SpyBean + private SmlConnector testInstance; - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Autowired - protected SmlConnector testInstance; - @Autowired - SmlIntegrationConfiguration mockSml; + // Mocks + @Mock + private DBDomain domain; @Before public void setup() { - - configurationService = Mockito.spy(configurationService); - testInstance = Mockito.spy(testInstance); // default behaviour Mockito.doNothing().when(testInstance).configureClient(any(), any(), any()); - ReflectionTestUtils.setField(testInstance, "configurationService", configurationService); Mockito.doReturn(true).when(configurationService).isSMLIntegrationEnabled(); - mockSml.reset(); + + ReflectionTestUtils.setField(testInstance, "configurationService", configurationService); } @Test - public void testRegisterDomainInDns() throws UnauthorizedFault, InternalErrorFault, BadRequestFault { + public void testRegisterDomainInDns() throws Exception { //when - boolean result = testInstance.registerDomain(DEFAULT_DOMAIN); //then assertTrue(result); - assertEquals(1, mockSml.getSmpManagerClientMocks().size()); - verify(mockSml.getSmpManagerClientMocks().get(0)).create(any()); - Mockito.verifyNoMoreInteractions(mockSml.getSmpManagerClientMocks().toArray()); + verify(iManageServiceMetadataWS, times(1)).create(any(ServiceMetadataPublisherServiceType.class)); } @Test - public void testRegisterDomainInDnsAlreadyExists() throws UnauthorizedFault, InternalErrorFault, BadRequestFault { + public void testRegisterDomainInDnsAlreadyExists() throws Exception { + //given + Mockito.doThrow(new BadRequestFault(ERROR_SMP_ALREADY_EXISTS)).when(iManageServiceMetadataWS).create(any(ServiceMetadataPublisherServiceType.class)); + //when - BadRequestFault ex = new BadRequestFault(ERROR_SMP_ALREADY_EXISTS); - mockSml.setThrowException(ex); boolean result = testInstance.registerDomain(DEFAULT_DOMAIN); //then assertTrue(result); - assertEquals(1, mockSml.getSmpManagerClientMocks().size()); - verify(mockSml.getSmpManagerClientMocks().get(0)).create(any()); - Mockito.verifyNoMoreInteractions(mockSml.getSmpManagerClientMocks().toArray()); + verify(iManageServiceMetadataWS, times(1)).create(any(ServiceMetadataPublisherServiceType.class)); } @Test - public void testRegisterDomainInDnsUnknownException() { - //when + public void testRegisterDomainInDnsUnknownException() throws Exception { + //given String message = "something unexpected"; - Exception ex = new Exception(message); - mockSml.setThrowException(ex); - expectedException.expectMessage(message); - expectedException.expect(SMPRuntimeException.class); + Mockito.doThrow(new InternalErrorFault(message)).when(iManageServiceMetadataWS).create(any(ServiceMetadataPublisherServiceType.class)); - testInstance.registerDomain(DEFAULT_DOMAIN); + //when + SMPRuntimeException smpRuntimeException = assertThrows(SMPRuntimeException.class, () -> + testInstance.registerDomain(DEFAULT_DOMAIN)); + + //then + Assert.assertEquals("SML integration error! Error: InternalErrorFault: " + message, smpRuntimeException.getMessage().trim()); + verify(iManageServiceMetadataWS, times(1)).create(any(ServiceMetadataPublisherServiceType.class)); } @Test - public void testRegisterDomainInDnsNewClientIsAlwaysCreated() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + public void testRegisterDomainInDnsNewClientIsAlwaysCreated() throws Exception { //when testInstance.registerDomain(DEFAULT_DOMAIN); testInstance.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()); + verify(iManageServiceMetadataWS, times(2)).create(any(ServiceMetadataPublisherServiceType.class)); } @Test - public void testDomainUnregisterFromDns() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + public void testDomainUnregisterFromDns() throws Exception { //when testInstance.unregisterDomain(DEFAULT_DOMAIN); //then - assertEquals(1, mockSml.getSmpManagerClientMocks().size()); - verify(mockSml.getSmpManagerClientMocks().get(0)).delete(any()); - Mockito.verifyNoMoreInteractions(mockSml.getSmpManagerClientMocks().toArray()); + verify(iManageServiceMetadataWS, times(1)).delete(anyString()); } @Test - public void testUnregisterDomainFromDnsNewClientIsAlwaysCreated() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + public void testUnregisterDomainFromDnsNewClientIsAlwaysCreated() throws Exception { //when testInstance.unregisterDomain(DEFAULT_DOMAIN); testInstance.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()); + verify(iManageServiceMetadataWS, times(2)).delete(anyString()); } @Test - public void testUnregisterDomainFromDnsThrowUnknownBadRequestFault() { + public void testUnregisterDomainFromDnsThrowUnknownBadRequestFault() throws Exception { + // given + Mockito.doThrow(new BadRequestFault(ERROR_UNEXPECTED_MESSAGE)).when(iManageServiceMetadataWS).delete(anyString()); + //when - BadRequestFault ex = new BadRequestFault(ERROR_UNEXPECTED_MESSAGE); - mockSml.setThrowException(ex); - expectedException.expectMessage(ERROR_UNEXPECTED_MESSAGE); - expectedException.expect(SMPRuntimeException.class); + SMPRuntimeException smpRuntimeException = assertThrows(SMPRuntimeException.class, () -> + testInstance.unregisterDomain(DEFAULT_DOMAIN)); + + //then + Assert.assertEquals("SML integration error! Error: BadRequestFault: " + ERROR_UNEXPECTED_MESSAGE, smpRuntimeException.getMessage().trim()); + verify(iManageServiceMetadataWS, times(1)).delete(anyString()); - testInstance.unregisterDomain(DEFAULT_DOMAIN); } @Test - public void testUnregisterDomainFromDnsThrowUnknownException() { + public void testUnregisterDomainFromDnsThrowUnknownException() throws Exception { + //given + Mockito.doThrow(new InternalErrorFault("something unexpected")).when(iManageServiceMetadataWS).delete(anyString()); + //when - String message = "something unexpected"; - Exception ex = new Exception(message); - mockSml.setThrowException(ex); - expectedException.expectMessage(message); - expectedException.expect(SMPRuntimeException.class); + SMPRuntimeException smpRuntimeException = assertThrows(SMPRuntimeException.class, () -> + testInstance.unregisterDomain(DEFAULT_DOMAIN)); - testInstance.unregisterDomain(DEFAULT_DOMAIN); + //then + Assert.assertEquals("SML integration error! Error: InternalErrorFault: something unexpected", smpRuntimeException.getMessage().trim()); + verify(iManageServiceMetadataWS, times(1)).delete(anyString()); } @Test - public void testUnregisterDomainFromDnsNotExists() { - //when - BadRequestFault ex = new BadRequestFault(ERROR_SMP_NOT_EXISTS); - mockSml.setThrowException(ex); + public void testUnregisterDomainFromDnsNotExists() throws Exception { + //given + Mockito.doThrow(new BadRequestFault(ERROR_SMP_NOT_EXISTS)).when(iManageServiceMetadataWS).delete(anyString()); + //when Assertions.assertDoesNotThrow(() -> testInstance.unregisterDomain(DEFAULT_DOMAIN)); } @Test public void testIsOkMessageForDomainNull() { + //when boolean suc = testInstance.isOkMessage(DEFAULT_DOMAIN, null); + //then assertFalse(suc); } @Test public void testIsOkMessageForDomainFalse() { - + //when boolean suc = testInstance.isOkMessage(DEFAULT_DOMAIN, ERROR_UNEXPECTED_MESSAGE); + //then assertFalse(suc); } @Test public void testGetSmlClientKeyAliasForDomain() { - + //given DBDomain domain = new DBDomain(); domain.setSmlClientKeyAlias(UUID.randomUUID().toString()); domain.setSmlClientCertAuth(false); + //when String alias = testInstance.getSmlClientKeyAliasForDomain(domain); + //then assertEquals(domain.getSmlClientKeyAlias(), alias); } @Test @Ignore("Randomly fails on bamboo ") public void testGetSmlClientKeyAliasForDomainNulForSingleKey() { - + //given DBDomain domain = new DBDomain(); domain.setSmlClientKeyAlias(null); domain.setSmlClientCertAuth(false); + //when String alias = testInstance.getSmlClientKeyAliasForDomain(domain); + //then assertEquals("single_domain_key", alias); } + + @Test + public void isDomainValid() throws Exception { + //given + ServiceMetadataPublisherServiceType existingDomain = new ServiceMetadataPublisherServiceType(); + Mockito.when(iManageServiceMetadataWS.read(any(ServiceMetadataPublisherServiceType.class))).thenReturn(existingDomain); + + //when + boolean result = testInstance.isDomainValid(domain); + + //then + Assert.assertTrue("Should have returned true when the participant exists", result); + } + + @Test + public void isDomainValid_wrapsBadRequestFaultIntoSmpRuntimeException() throws Exception { + //given + String errorMessage = UUID.randomUUID().toString(); + Mockito.when(iManageServiceMetadataWS.read(any(ServiceMetadataPublisherServiceType.class))).thenThrow(new BadRequestFault(errorMessage)); + + //when + SMPRuntimeException smpRuntimeException = assertThrows(SMPRuntimeException.class, () -> + testInstance.isDomainValid(domain)); + + //then + Assert.assertEquals("Should have returned an SMPRuntimeException wrapping the original BadRequestFault when thrown while reading a domain", + "SML integration error! Error: BadRequestFault: " + errorMessage, + smpRuntimeException.getMessage().trim()); + } + + @Test + public void isDomainValid_wrapsNotFoundFaultIntoSmpRuntimeException() throws Exception { + //given + String errorMessage = UUID.randomUUID().toString(); + Mockito.when(iManageServiceMetadataWS.read(any(ServiceMetadataPublisherServiceType.class))).thenThrow(new NotFoundFault(errorMessage)); + + //when + SMPRuntimeException smpRuntimeException = assertThrows(SMPRuntimeException.class, () -> + testInstance.isDomainValid(domain)); + + //then + Assert.assertEquals("Should have returned an SMPRuntimeException wrapping the original NotFoundFault when thrown while reading a domain", + "SML integration error! Error: NotFoundFault: " + errorMessage, + smpRuntimeException.getMessage().trim()); + } + + @Test + public void isDomainValid_wrapsCheckedExceptionsIntoSmpRuntimeException() throws Exception { + //given + String errorMessage = UUID.randomUUID().toString(); + // We need to match one of the checked exceptions present in the method signature, so we throw InternalErrorFault which will be handled aside + Mockito.when(iManageServiceMetadataWS.read(any(ServiceMetadataPublisherServiceType.class))).thenThrow(new InternalErrorFault(errorMessage)); + + //when + SMPRuntimeException smpRuntimeException = assertThrows(SMPRuntimeException.class, () -> + testInstance.isDomainValid(domain)); + + //then + Assert.assertEquals("Should have returned an SMPRuntimeException wrapping the original Exception when thrown while reading a domain", + "SML integration error! Error: InternalErrorFault: " + errorMessage, + smpRuntimeException.getMessage().trim()); + } + + @Test + public void isDomainValid_smlIntegrationDisabled() { + //given + Mockito.doReturn(false).when(configurationService).isSMLIntegrationEnabled(); + + //when + boolean result = testInstance.isDomainValid(domain); + + //then + Assert.assertFalse("Should have returned the domain as not valid when the SML integration is not enabled", result); + Mockito.verifyNoMoreInteractions(iManageServiceMetadataWS); + } } diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorParticipantTest.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorParticipantTest.java index a871e2ef2..8c0a8aaea 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorParticipantTest.java +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/sml/SmlConnectorParticipantTest.java @@ -8,9 +8,9 @@ * 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 at: - * + * * [PROJECT_HOME]\license\eupl-1.2\license.txt or https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 - * + * * 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. @@ -19,230 +19,348 @@ 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.SmlIntegrationConfiguration; +import ec.services.wsdl.bdmsl.data._1.ExistsParticipantResponseType; +import ec.services.wsdl.bdmsl.data._1.ParticipantsType; +import eu.europa.ec.bdmsl.ws.soap.*; +import eu.europa.ec.edelivery.smp.data.model.DBDomain; import eu.europa.ec.edelivery.smp.exceptions.SMPRuntimeException; +import eu.europa.ec.edelivery.smp.identifiers.Identifier; import eu.europa.ec.edelivery.smp.services.AbstractServiceIntegrationTest; import eu.europa.ec.edelivery.smp.services.ConfigurationService; +import org.busdox.servicemetadata.locator._1.ServiceMetadataPublisherServiceForParticipantType; import org.hamcrest.CoreMatchers; import org.hamcrest.MatcherAssert; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.test.util.ReflectionTestUtils; +import java.util.UUID; + import static eu.europa.ec.edelivery.smp.sml.SmlConnectorTestConstants.*; import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.verify; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.*; /** * Created by JRC * since 4.1. */ -@RunWith(SpringRunner.class) -@ContextConfiguration(classes = {SmlConnector.class, SmlIntegrationConfiguration.class}) public class SmlConnectorParticipantTest extends AbstractServiceIntegrationTest { - - @Autowired - protected ConfigurationService configurationService; - - @Autowired - protected SmlConnector testInstance; - - @Autowired - SmlIntegrationConfiguration mockSml; + // Beans + @SpyBean + private ConfigurationService configurationService; + @MockBean + private IBDMSLServiceWS ibdmslServiceWS; + @MockBean + private IManageParticipantIdentifierWS iManageParticipantIdentifierWS; + @SpyBean + private SmlConnector testInstance; + + // Mocks + @Mock + private DBDomain domain; + @Mock + private Identifier identifier; @Before public void setup() { - testInstance = Mockito.spy(testInstance); // default behaviour Mockito.doNothing().when(testInstance).configureClient(any(), any(), any()); + Mockito.doReturn(true).when(configurationService).isSMLIntegrationEnabled(); - - configurationService = Mockito.spy(configurationService); ReflectionTestUtils.setField(testInstance, "configurationService", configurationService); - Mockito.doReturn(true).when(configurationService).isSMLIntegrationEnabled(); DEFAULT_DOMAIN.setSmlRegistered(true); - mockSml.reset(); } @Test - public void testRegisterInDns() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + public void testRegisterInDns() throws Exception { //when boolean result = testInstance.registerInDns(PARTICIPANT_ID, DEFAULT_DOMAIN, null); //then assertTrue(result); - assertEquals(1, mockSml.getParticipantManagmentClientMocks().size()); - verify(mockSml.getParticipantManagmentClientMocks().get(0)).create(any()); - Mockito.verifyNoMoreInteractions(mockSml.getParticipantManagmentClientMocks().toArray()); + verify(iManageParticipantIdentifierWS, times(1)).create(any(ServiceMetadataPublisherServiceForParticipantType.class)); + Mockito.verifyNoMoreInteractions(iManageParticipantIdentifierWS); } @Test - public void testRegisterInDnsAlreadyExists() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + public void testRegisterInDnsAlreadyExists() throws Exception { + //given + Mockito.doThrow(new BadRequestFault(ERROR_PI_ALREADY_EXISTS)).when(iManageParticipantIdentifierWS).create(any(ServiceMetadataPublisherServiceForParticipantType.class)); + //when - BadRequestFault ex = new BadRequestFault(ERROR_PI_ALREADY_EXISTS); - mockSml.setThrowException(ex); boolean result = testInstance.registerInDns(PARTICIPANT_ID, DEFAULT_DOMAIN, null); //then assertTrue(result); - assertEquals(1, mockSml.getParticipantManagmentClientMocks().size()); - verify(mockSml.getParticipantManagmentClientMocks().get(0)).create(any()); - Mockito.verifyNoMoreInteractions(mockSml.getParticipantManagmentClientMocks().toArray()); + verify(iManageParticipantIdentifierWS, times(1)).create(any(ServiceMetadataPublisherServiceForParticipantType.class)); + Mockito.verifyNoMoreInteractions(iManageParticipantIdentifierWS); } @Test - public void testRegisterInDnsUnknownException() { - //when + public void testRegisterInDnsUnknownException() throws Exception { + //given String message = "something unexpected"; - Exception ex = new Exception(message); - mockSml.setThrowException(ex); + Mockito.doThrow(new InternalErrorFault(message)).when(iManageParticipantIdentifierWS).create(any(ServiceMetadataPublisherServiceForParticipantType.class)); + //when SMPRuntimeException result = assertThrows(SMPRuntimeException.class, () -> testInstance.registerInDns(PARTICIPANT_ID, DEFAULT_DOMAIN, null)); - MatcherAssert.assertThat(result.getMessage(), CoreMatchers.containsString(message)); + + //then + MatcherAssert.assertThat(result.getMessage(), CoreMatchers.containsStringIgnoringCase(message)); } @Test - public void testRegisterInDnsNewClientIsAlwaysCreated() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + public void testRegisterInDnsNewClientIsAlwaysCreated() throws Exception { //when testInstance.registerInDns(PARTICIPANT_ID, DEFAULT_DOMAIN, null); testInstance.registerInDns(PARTICIPANT_ID, DEFAULT_DOMAIN, null); //then - assertEquals(2, mockSml.getParticipantManagmentClientMocks().size()); - verify(mockSml.getParticipantManagmentClientMocks().get(0)).create(any()); - verify(mockSml.getParticipantManagmentClientMocks().get(1)).create(any()); - Mockito.verifyNoMoreInteractions(mockSml.getParticipantManagmentClientMocks().toArray()); + verify(iManageParticipantIdentifierWS, times(2)).create(any(ServiceMetadataPublisherServiceForParticipantType.class)); + Mockito.verifyNoMoreInteractions(iManageParticipantIdentifierWS); } @Test - public void testUnregisterFromDns() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + public void testUnregisterFromDns() throws Exception { //when boolean result = testInstance.unregisterFromDns(PARTICIPANT_ID, DEFAULT_DOMAIN); //then assertTrue(result); - assertEquals(1, mockSml.getParticipantManagmentClientMocks().size()); - verify(mockSml.getParticipantManagmentClientMocks().get(0)).delete(any()); - Mockito.verifyNoMoreInteractions(mockSml.getParticipantManagmentClientMocks().toArray()); + verify(iManageParticipantIdentifierWS, times(1)).delete(any(ServiceMetadataPublisherServiceForParticipantType.class)); + Mockito.verifyNoMoreInteractions(iManageParticipantIdentifierWS); } @Test - public void testUnregisterFromDnsNewClientIsAlwaysCreated() throws UnauthorizedFault, NotFoundFault, InternalErrorFault, BadRequestFault { + public void testUnregisterFromDnsNewClientIsAlwaysCreated() throws Exception { //when testInstance.unregisterFromDns(PARTICIPANT_ID, DEFAULT_DOMAIN); testInstance.unregisterFromDns(PARTICIPANT_ID, DEFAULT_DOMAIN); //then - assertEquals(2, mockSml.getParticipantManagmentClientMocks().size()); - verify(mockSml.getParticipantManagmentClientMocks().get(0)).delete(any()); - verify(mockSml.getParticipantManagmentClientMocks().get(1)).delete(any()); - Mockito.verifyNoMoreInteractions(mockSml.getParticipantManagmentClientMocks().toArray()); + verify(iManageParticipantIdentifierWS, times(2)).delete(any(ServiceMetadataPublisherServiceForParticipantType.class)); + Mockito.verifyNoMoreInteractions(iManageParticipantIdentifierWS); } @Test - public void testUnregisterFromDnsThrowUnknownBadRequestFault() { - //when - BadRequestFault ex = new BadRequestFault(ERROR_UNEXPECTED_MESSAGE); - mockSml.setThrowException(ex); + public void testUnregisterFromDnsThrowUnknownBadRequestFault() throws Exception { + doThrow(new BadRequestFault(ERROR_UNEXPECTED_MESSAGE)).when(iManageParticipantIdentifierWS).delete(any(ServiceMetadataPublisherServiceForParticipantType.class)); + //when SMPRuntimeException result = assertThrows(SMPRuntimeException.class, () -> testInstance.unregisterFromDns(PARTICIPANT_ID, DEFAULT_DOMAIN)); - MatcherAssert.assertThat(result.getMessage(), CoreMatchers.containsString(ERROR_UNEXPECTED_MESSAGE)); - + MatcherAssert.assertThat(result.getMessage(), CoreMatchers.containsStringIgnoringCase(ERROR_UNEXPECTED_MESSAGE)); } @Test - public void testUnregisterFromDnsThrowUnknownException() { - //when + public void testUnregisterFromDnsThrowUnknownException() throws Exception { String message = "something unexpected"; - Exception ex = new Exception(message); - mockSml.setThrowException(ex); + doThrow(new InternalErrorFault(ERROR_UNEXPECTED_MESSAGE)).when(iManageParticipantIdentifierWS).delete(any(ServiceMetadataPublisherServiceForParticipantType.class)); + //when SMPRuntimeException result = assertThrows(SMPRuntimeException.class, () -> testInstance.unregisterFromDns(PARTICIPANT_ID, DEFAULT_DOMAIN)); - MatcherAssert.assertThat(result.getMessage(), CoreMatchers.containsString(message)); + MatcherAssert.assertThat(result.getMessage(), CoreMatchers.containsStringIgnoringCase(message)); } @Test - public void testUnregisterFromDnsNotExists() { + public void testUnregisterFromDnsNotExists() throws Exception { + //given + Mockito.doThrow(new BadRequestFault(ERROR_PI_NO_EXISTS)).when(iManageParticipantIdentifierWS).delete(any(ServiceMetadataPublisherServiceForParticipantType.class)); + //when - BadRequestFault ex = new BadRequestFault(ERROR_PI_NO_EXISTS); - mockSml.setThrowException(ex); boolean suc = testInstance.unregisterFromDns(PARTICIPANT_ID, DEFAULT_DOMAIN); + //then assertTrue(suc); } - @Test public void testIsOkMessageForParticipantNull() { - + //when boolean suc = testInstance.isOkMessage(PARTICIPANT_ID, null); + //then assertFalse(suc); } @Test public void testIsOkMessageForParticipantOk() { + //when boolean suc = testInstance.isOkMessage(PARTICIPANT_ID, ERROR_PI_ALREADY_EXISTS); + //then assertTrue(suc); } @Test public void testIsOkMessageForParticipantFalse() { + //when boolean suc = testInstance.isOkMessage(PARTICIPANT_ID, ERROR_UNEXPECTED_MESSAGE); + //then assertFalse(suc); } - @Test public void testProcessSMLErrorMessageBadRequestFaultIgnore() { - + //given BadRequestFault ex = new BadRequestFault(ERROR_PI_ALREADY_EXISTS); + + //when boolean suc = testInstance.processSMLErrorMessage(ex, PARTICIPANT_ID); + //then assertTrue(suc); } @Test public void testProcessSMLErrorMessageBadRequestFaultFailed() { - + //given BadRequestFault ex = new BadRequestFault(ERROR_UNEXPECTED_MESSAGE); + //when SMPRuntimeException result = assertThrows(SMPRuntimeException.class, () -> testInstance.processSMLErrorMessage(ex, PARTICIPANT_ID)); - MatcherAssert.assertThat(result.getMessage(), CoreMatchers.containsString(ERROR_UNEXPECTED_MESSAGE)); - } + //then + MatcherAssert.assertThat(result.getMessage(), CoreMatchers.containsStringIgnoringCase(ERROR_UNEXPECTED_MESSAGE)); + } @Test public void testProcessSMLErrorMessageNoFoundFaultFailed() { - + //given NotFoundFault ex = new NotFoundFault(ERROR_UNEXPECTED_MESSAGE); + //when SMPRuntimeException result = assertThrows(SMPRuntimeException.class, () -> testInstance.processSMLErrorMessage(ex, PARTICIPANT_ID)); - MatcherAssert.assertThat(result.getMessage(), CoreMatchers.containsString(ERROR_UNEXPECTED_MESSAGE)); - + //then + MatcherAssert.assertThat(result.getMessage(), CoreMatchers.containsStringIgnoringCase(ERROR_UNEXPECTED_MESSAGE)); } @Test public void testProcessSMLErrorMessageNoFoundFaultOk() { - + //given NotFoundFault ex = new NotFoundFault(ERROR_PI_NO_EXISTS); + //when testInstance.processSMLErrorMessage(ex, PARTICIPANT_ID); } + @Test + public void participantExists() throws Exception { + // given + ExistsParticipantResponseType existingParticipant = new ExistsParticipantResponseType(); + existingParticipant.setExist(true); + Mockito.when(domain.isSmlRegistered()).thenReturn(true); + Mockito.when(domain.getSmlSmpId()).thenReturn("smlSmpId"); + Mockito.when(identifier.getValue()).thenReturn("identifierValue"); + Mockito.when(identifier.getScheme()).thenReturn("identifierScheme"); + Mockito.when(ibdmslServiceWS.existsParticipantIdentifier(any(ParticipantsType.class))).thenReturn(existingParticipant); + + Mockito.doNothing().when(testInstance).configureClient(anyString(), any(), any(DBDomain.class)); + + // when + boolean result = testInstance.participantExists(identifier, domain); + + // then + Assert.assertTrue("Should have returned true when the participant exists", result); + } + + @Test + public void participantExists_wrapsBadRequestFaultIntoSmpRuntimeException() throws Exception { + // given + String errorMessage = UUID.randomUUID().toString(); + Mockito.when(domain.isSmlRegistered()).thenReturn(true); + Mockito.when(domain.getSmlSmpId()).thenReturn("smlSmpId"); + Mockito.when(identifier.getValue()).thenReturn("identifierValue"); + Mockito.when(identifier.getScheme()).thenReturn("identifierScheme"); + Mockito.when(ibdmslServiceWS.existsParticipantIdentifier(any(ParticipantsType.class))).thenThrow(new BadRequestFault(errorMessage)); + + Mockito.doNothing().when(testInstance).configureClient(anyString(), any(), any(DBDomain.class)); + + // when + SMPRuntimeException smpRuntimeException = assertThrows(SMPRuntimeException.class, () -> + testInstance.participantExists(identifier, domain)); + + // then + Assert.assertEquals("Should have returned an SMPRuntimeException wrapping the original BadRequestFault when thrown while looking up whether a participant exists or not", + "SML integration error! Error: BadRequestFault: " + errorMessage, + smpRuntimeException.getMessage().trim()); + } + + @Test + public void participantExists_wrapsNotFoundFaultIntoSmpRuntimeException() throws Exception { + // given + String errorMessage = UUID.randomUUID().toString(); + Mockito.when(domain.isSmlRegistered()).thenReturn(true); + Mockito.when(domain.getSmlSmpId()).thenReturn("smlSmpId"); + Mockito.when(identifier.getValue()).thenReturn("identifierValue"); + Mockito.when(identifier.getScheme()).thenReturn("identifierScheme"); + Mockito.when(ibdmslServiceWS.existsParticipantIdentifier(any(ParticipantsType.class))).thenThrow(new NotFoundFault(errorMessage)); + + Mockito.doNothing().when(testInstance).configureClient(anyString(), any(), any(DBDomain.class)); + + // when + SMPRuntimeException smpRuntimeException = assertThrows(SMPRuntimeException.class, () -> + testInstance.participantExists(identifier, domain)); + + // then + Assert.assertEquals("Should have returned an SMPRuntimeException wrapping the original NotFoundFault when thrown while looking up whether a participant exists or not", + "SML integration error! Error: NotFoundFault: " + errorMessage, + smpRuntimeException.getMessage().trim()); + } + + @Test + public void participantExists_wrapsCheckedExceptionsIntoSmpRuntimeException() throws Exception { + // given + String errorMessage = UUID.randomUUID().toString(); + Mockito.when(domain.isSmlRegistered()).thenReturn(true); + Mockito.when(domain.getSmlSmpId()).thenReturn("smlSmpId"); + Mockito.when(identifier.getValue()).thenReturn("identifierValue"); + Mockito.when(identifier.getScheme()).thenReturn("identifierScheme"); + // We need to match one of the checked exceptions present in the method signature, so we throw InternalErrorFault which will be handled aside + Mockito.when(ibdmslServiceWS.existsParticipantIdentifier(any(ParticipantsType.class))).thenThrow(new InternalErrorFault(errorMessage)); + + Mockito.doNothing().when(testInstance).configureClient(anyString(), any(), any(DBDomain.class)); + + // when + SMPRuntimeException smpRuntimeException = assertThrows(SMPRuntimeException.class, () -> + testInstance.participantExists(identifier, domain)); + + // then + Assert.assertEquals("Should have returned an SMPRuntimeException wrapping the original Exception when thrown while looking up whether a participant exists or not", + "SML integration error! Error: InternalErrorFault: " + errorMessage, + smpRuntimeException.getMessage().trim()); + } + + @Test + public void participantExists_smlIntegrationDisabled() { + // given + Mockito.doReturn(false).when(configurationService).isSMLIntegrationEnabled(); + + // when + boolean result = testInstance.participantExists(identifier, domain); + + // then + Assert.assertFalse("The participant should have been returned as non-existing when the SML integration is not enabled", result); + } + @Test + public void participantExists_unregisteredDomain() { + // given + Mockito.when(domain.isSmlRegistered()).thenReturn(false); + + // when + boolean result = testInstance.participantExists(identifier, domain); + + // then + Assert.assertFalse("The participant should have been returned as non-existing when the domain is not registered in SML", result); + } } diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/testutil/TestConstants.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/testutil/TestConstants.java index 0177ef7eb..c93a83fee 100644 --- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/testutil/TestConstants.java +++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/testutil/TestConstants.java @@ -36,6 +36,7 @@ public class TestConstants { public static final String TEST_DOMAIN_CODE_1 = "utestPeppol01"; public static final String TEST_DOMAIN_CODE_2 = "utestEHhealth02"; + public static final String TEST_DOMAIN_CODE_3 = "utestRegistered03"; public static final String TEST_SML_SUBDOMAIN_CODE_1 = ""; // peppol subdomain is empty string public static final String TEST_SML_SUBDOMAIN_CODE_2 = "ehealth"; -- GitLab