Code development platform for open source projects from the European Union institutions :large_blue_circle: EU Login authentication by SMS will be completely phased out by mid-2025. To see alternatives please check here

Skip to content
Snippets Groups Projects
Commit 964fe698 authored by Joze RIHTARSIC's avatar Joze RIHTARSIC
Browse files

fix unit test build

parent 52955d5f
No related branches found
No related tags found
No related merge requests found
Pipeline #172943 failed
Showing
with 118 additions and 109 deletions
version: '3.8'
services:
##
## UI Tests
......@@ -12,5 +11,5 @@ services:
- test-network
networks:
test-network:
external:
name: ${DOCKER_NETWORK_NAME:-domismp-springboot-mysql_default}
name: ${DOCKER_NETWORK_NAME:-domismp-springboot-mysql_default}
external: true
version: '3.8'
services:
smp-springboot:
image: ${IMAGE_TAG:-edeliverytest}/${IMAGE_SMP_SPRINGBOOT_MYSQL}:${SMP_VERSION}
......
#
# The intention of the compose file is to override the default configuration of the docker-compose.yml file,
# such as the ports to be exposed, to be used in the local development/test environment.
version: "3.8"
services:
domismp-service:
ports:
......
version: '3.8'
services:
##
## API Tests
......@@ -12,5 +11,5 @@ services:
- test-network
networks:
test-network:
external:
name: ${DOCKER_NETWORK_NAME:-domismp-tomcat-mysql_default}
name: ${DOCKER_NETWORK_NAME:-domismp-tomcat-mysql_default}
external: true
version: '3.8'
services:
##
## UI Tests
......@@ -13,5 +12,5 @@ services:
- test-network
networks:
test-network:
external:
name: ${DOCKER_NETWORK_NAME:-domismp-tomcat-mysql_default}
name: ${DOCKER_NETWORK_NAME:-domismp-tomcat-mysql_default}
external: true
version: "3.8"
services:
domismp-service:
image: ${IMAGE_TAG:-edeliverytest}/${IMAGE_SMP_TOMCAT_MYSQL}:${SMP_VERSION}
......
version: '3.8'
services:
##
## API Tests
......@@ -12,5 +11,5 @@ services:
- test-network
networks:
test-network:
external:
name: ${DOCKER_NETWORK_NAME:-domismp-tomcat-mysql_default}
name: ${DOCKER_NETWORK_NAME:-domismp-tomcat-mysql_default}
external: true
version: "3.8"
services:
smp-oracle-db:
image: ${IMAGE_TAG:-edeliverytest}/${IMAGE_SMP_DB_ORACLE}-${ORA_VERSION}-${ORA_EDITION}:${SMP_VERSION}
......
version: '3.8'
services:
##
## API Tests
......@@ -12,5 +11,5 @@ services:
- test-network
networks:
test-network:
external:
name: ${DOCKER_NETWORK_NAME:-domismp-tomcat-mysql_default}
name: ${DOCKER_NETWORK_NAME:-domismp-tomcat-mysql_default}
external: true
services:
smp-oracle-db:
# image: ${IMAGE_TAG:-edeliverytest}/${IMAGE_SMP_DB_ORACLE}-${ORA_VERSION}-${ORA_EDITION}:${SMP_VERSION}
image: container-registry.oracle.com/database/express:21.3.0-xe
......
......@@ -82,6 +82,7 @@ See the Licence for the specific language governing permissions and limitations
<commons-validator.version>1.8.0</commons-validator.version>
<cxf-xjc-runtime.version>3.3.2</cxf-xjc-runtime.version>
<cxf.version>3.5.8</cxf.version>
<ehcache.version>3.10.8</ehcache.version>
<h2.version>2.2.224</h2.version>
<hamcrest.version>2.2</hamcrest.version>
<hibernate-jpa.version>1.0.2.Final</hibernate-jpa.version>
......@@ -216,6 +217,13 @@ See the Licence for the specific language governing permissions and limitations
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Ehcache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>${ehcache.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
......
......@@ -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.
......@@ -23,12 +23,6 @@ import eu.europa.ec.smp.spi.api.model.ResourceIdentifier;
import eu.europa.ec.smp.spi.exceptions.ResourceException;
import eu.europa.ec.smp.spi.resource.ResourceHandlerSpi;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* The abstract class with common methods for implementation of the ResourceHandlerSpi.
......@@ -38,33 +32,10 @@ import java.io.InputStream;
*/
public abstract class AbstractHandler implements ResourceHandlerSpi {
static final Logger LOG = LoggerFactory.getLogger(AbstractHandler.class);
private static final String DISALLOW_DOCTYPE_FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
public byte[] readFromInputStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] data = new byte[4096];
int nRead;
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
}
public ResourceIdentifier getResourceIdentifier(RequestData resourceData) throws ResourceException {
if (resourceData == null || resourceData.getResourceIdentifier() == null || StringUtils.isEmpty(resourceData.getResourceIdentifier().getValue())) {
throw new ResourceException(ResourceException.ErrorCode.INVALID_PARAMETERS, "Missing resource identifier for the resource CPP ");
}
return resourceData.getResourceIdentifier();
}
public ResourceIdentifier getSubresourceIdentifier(RequestData resourceData) throws ResourceException {
if (resourceData == null || resourceData.getSubresourceIdentifier() == null || StringUtils.isEmpty(resourceData.getSubresourceIdentifier().getValue())) {
throw new ResourceException(ResourceException.ErrorCode.INVALID_PARAMETERS, "Missing sub-resource identifier for the resource service metadata!");
}
return resourceData.getSubresourceIdentifier();
}
}
......@@ -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.
......@@ -55,7 +55,6 @@ import java.time.OffsetDateTime;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import static eu.europa.ec.smp.spi.exceptions.ResourceException.ErrorCode.*;
......@@ -66,7 +65,6 @@ public class DomiSMPJSONHandlerExample extends AbstractHandler {
private static final Logger LOG = LoggerFactory.getLogger(DomiSMPJSONHandlerExample.class);
final SmpDataServiceApi smpDataApi;
final SmpIdentifierServiceApi smpIdentifierApi;
......@@ -87,13 +85,15 @@ public class DomiSMPJSONHandlerExample extends AbstractHandler {
try {
String identifierString = smpIdentifierApi.formatResourceIdentifier(identifier);
String identifierString = smpIdentifierApi.formatResourceIdentifier(
resourceData.getDomainCode(),
identifier);
ExampleEntityRo exampleEntityRo = new ExampleEntityRo();
exampleEntityRo.setIdentifier(identifierString);
exampleEntityRo.setUrl("http://example.local/test");
exampleEntityRo.setEmail("test.address@example.local");
X509Certificate cert = createX509Certificate("CN="+identifierString+",O=edelivery,C=EU");
X509Certificate cert = createX509Certificate("CN=" + identifierString + ",O=edelivery,C=EU");
exampleEntityRo.setCertificate(Base64.getEncoder().encodeToString(cert.getEncoded()));
ObjectMapper mapper = new ObjectMapper();
......@@ -113,7 +113,8 @@ public class DomiSMPJSONHandlerExample extends AbstractHandler {
key.getPrivate(), false, -1, null,
Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
} catch (NoSuchProviderException | NoSuchAlgorithmException | InvalidAlgorithmParameterException | IOException |
} catch (NoSuchProviderException | NoSuchAlgorithmException |
InvalidAlgorithmParameterException | IOException |
CertificateException | OperatorCreationException e) {
throw new ResourceException(INTERNAL_ERROR, "Error occurred at sample certificate generation!", e);
}
......@@ -160,12 +161,12 @@ public class DomiSMPJSONHandlerExample extends AbstractHandler {
try {
bios = new ByteArrayInputStream(StreamUtils.copyToByteArray(inputStream));
} catch (IOException e) {
throw new RuntimeException(e);
throw new ResourceException(ResourceException.ErrorCode.PROCESS_ERROR, ExceptionUtils.getRootCauseMessage(e), e);
}
inputStream.mark(Integer.MAX_VALUE - 2);
validateAndParse(bios, getResourceIdentifier(resourceData));
validateAndParse(bios, getResourceIdentifier(resourceData), resourceData.getDomainCode());
try {
bios.reset();
StreamUtils.copy(bios, responseData.getOutputStream());
......@@ -187,13 +188,12 @@ public class DomiSMPJSONHandlerExample extends AbstractHandler {
public ExampleEntityRo validateAndParse(RequestData resourceData) throws ResourceException {
// get service group identifier
ResourceIdentifier identifier = getResourceIdentifier(resourceData);
return validateAndParse(resourceData.getResourceInputStream(), identifier);
return validateAndParse(resourceData.getResourceInputStream(), identifier, resourceData.getDomainCode());
}
public ExampleEntityRo validateAndParse(InputStream inputStream, ResourceIdentifier identifier ) throws ResourceException {
public ExampleEntityRo validateAndParse(InputStream inputStream, ResourceIdentifier identifier, String domainCode) throws ResourceException {
// get service group identifier
Properties properties = new Properties();
// validate by schema
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
......@@ -204,31 +204,31 @@ public class DomiSMPJSONHandlerExample extends AbstractHandler {
throw new ResourceException(INVALID_RESOURCE, "Error occurred while reading example property document: [" + identifier + "] with error: " + ExceptionUtils.getRootCauseMessage(ex), ex);
}
if ( StringUtils.isBlank(entityRo.getIdentifier())){
throw new ResourceException(INVALID_RESOURCE, "Missing property [identifier]!" );
if (StringUtils.isBlank(entityRo.getIdentifier())) {
throw new ResourceException(INVALID_RESOURCE, "Missing property [identifier]!");
}
if ( StringUtils.isBlank(entityRo.getUrl())){
throw new ResourceException(INVALID_RESOURCE, "Missing property [url]!" );
if (StringUtils.isBlank(entityRo.getUrl())) {
throw new ResourceException(INVALID_RESOURCE, "Missing property [url]!");
}
if ( StringUtils.isBlank(entityRo.getEmail())){
throw new ResourceException(INVALID_RESOURCE, "Missing property [email]!" );
if (StringUtils.isBlank(entityRo.getEmail())) {
throw new ResourceException(INVALID_RESOURCE, "Missing property [email]!");
}
if ( StringUtils.isBlank(entityRo.getCertificate())){
throw new ResourceException(INVALID_RESOURCE, "Missing property [certificate]" );
if (StringUtils.isBlank(entityRo.getCertificate())) {
throw new ResourceException(INVALID_RESOURCE, "Missing property [certificate]");
}
String identifierString = smpIdentifierApi.formatResourceIdentifier(identifier);
if (!StringUtils.equalsIgnoreCase(entityRo.getIdentifier(),identifierString )){
throw new ResourceException(INVALID_RESOURCE, "Property: [identifier] does not match value for the resource ["+identifierString+"]" );
String identifierString = smpIdentifierApi.formatResourceIdentifier(domainCode, identifier);
if (!StringUtils.equalsIgnoreCase(entityRo.getIdentifier(), identifierString)) {
throw new ResourceException(INVALID_RESOURCE, "Property: [identifier] does not match value for the resource [" + identifierString + "]");
}
try {
new URL(entityRo.getUrl());
} catch (MalformedURLException e) {
throw new ResourceException(INVALID_RESOURCE, "Bad property value: [url]!. Value ["+entityRo.getUrl()+"] is not URL" );
throw new ResourceException(INVALID_RESOURCE, "Bad property value: [url]!. Value [" + entityRo.getUrl() + "] is not URL");
}
return entityRo;
}
......
......@@ -86,7 +86,7 @@ public class DomiSMPPropertyHandlerExample extends AbstractHandler {
ResourceIdentifier identifier = getResourceIdentifier(resourceData);
try {
String identifierString = smpIdentifierApi.formatResourceIdentifier(identifier);
String identifierString = smpIdentifierApi.formatResourceIdentifier(resourceData.getDomainCode(), identifier);
Properties properties = new Properties();
properties.setProperty(PROPERTY_IDENTIFIER, identifierString);
properties.setProperty(PROPERTY_URL, "http://example.local/test");
......@@ -201,7 +201,7 @@ public class DomiSMPPropertyHandlerExample extends AbstractHandler {
if ( !properties.containsKey(PROPERTY_CERTIFICATE)){
throw new ResourceException(INVALID_RESOURCE, "Missing property document: [" + PROPERTY_CERTIFICATE + "]" );
}
String identifierString = smpIdentifierApi.formatResourceIdentifier(identifier);
String identifierString = smpIdentifierApi.formatResourceIdentifier(resourceData.getDomainCode(), identifier);
if (!StringUtils.equalsIgnoreCase(properties.getProperty(PROPERTY_IDENTIFIER),identifierString )){
throw new ResourceException(INVALID_RESOURCE, "Property: [" + PROPERTY_IDENTIFIER + "] does not match value for the resource ["+identifierString+"]" );
}
......
......@@ -25,6 +25,7 @@ import eu.europa.ec.smp.spi.api.model.RequestData;
import eu.europa.ec.smp.spi.api.model.ResourceIdentifier;
import eu.europa.ec.smp.spi.api.model.ResponseData;
import eu.europa.ec.smp.spi.exceptions.ResourceException;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import java.io.ByteArrayInputStream;
......@@ -34,6 +35,8 @@ import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertTrue;
abstract class AbstractHandlerTest {
private static final String TEST_DOMAIN_CODE = "TestDomainCode";
protected SmpDataServiceApi mockSmpDataApi = Mockito.mock(SmpDataServiceApi.class);
protected SmpIdentifierServiceApi mockSmpIdentifierServiceApi = Mockito.mock(SmpIdentifierServiceApi.class);
protected SmpXmlSignatureApi mockSignatureApi = Mockito.mock(SmpXmlSignatureApi.class);
......@@ -52,11 +55,13 @@ abstract class AbstractHandlerTest {
Mockito.doReturn(baos).when(responseData).getOutputStream();
Mockito.doReturn(AbstractHandlerTest.class.getResourceAsStream(resourceName)).when(requestData).getResourceInputStream();
Mockito.doReturn(resourceIdentifier).when(requestData).getResourceIdentifier();
Mockito.doReturn(TEST_DOMAIN_CODE).when(requestData).getDomainCode();
if (subresourceIdentifier != null) {
Mockito.doReturn(subresourceIdentifier).when(requestData).getSubresourceIdentifier();
}
Mockito.when(mockSmpIdentifierServiceApi.normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString())).thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[0], (String) i.getArguments()[1]));
Mockito.when(mockSmpIdentifierServiceApi.normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[1], (String) i.getArguments()[2]));
getTestInstance().readResource(requestData, responseData);
assertTrue(baos.size() > 0);
......@@ -72,12 +77,17 @@ abstract class AbstractHandlerTest {
Mockito.doReturn(baos).when(responseData).getOutputStream();
Mockito.doReturn(AbstractHandlerTest.class.getResourceAsStream(resourceName)).when(requestData).getResourceInputStream();
Mockito.doReturn(resourceIdentifier).when(requestData).getResourceIdentifier();
Mockito.doReturn(TEST_DOMAIN_CODE).when(requestData).getDomainCode();
if (subresourceIdentifier != null) {
Mockito.doReturn(subresourceIdentifier).when(requestData).getSubresourceIdentifier();
Mockito.when(mockSmpIdentifierServiceApi.normalizeSubresourceIdentifier(Mockito.anyString(), Mockito.anyString())).thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[0], (String) i.getArguments()[1]));
Mockito.when(mockSmpIdentifierServiceApi.normalizeSubresourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[1], (String) i.getArguments()[2]));
}
Mockito.when(mockSmpIdentifierServiceApi.normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString())).thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[0], (String) i.getArguments()[1]));
Mockito.doReturn(resourceIdentifier.getScheme() + "::" + resourceIdentifier.getValue()).when(mockSmpIdentifierServiceApi).formatResourceIdentifier(resourceIdentifier);
Mockito.when(mockSmpIdentifierServiceApi.normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[1], (String) i.getArguments()[2]));
Mockito.doReturn(resourceIdentifier.getScheme() + "::"
+ resourceIdentifier.getValue()).when(mockSmpIdentifierServiceApi)
.formatResourceIdentifier(ArgumentMatchers.anyString(), ArgumentMatchers.any(ResourceIdentifier.class));
getTestInstance().storeResource(requestData, responseData);
}
......@@ -90,12 +100,18 @@ abstract class AbstractHandlerTest {
// validate
if (subresourceIdentifier != null) {
Mockito.doReturn(subresourceIdentifier).when(requestData).getSubresourceIdentifier();
Mockito.when(mockSmpIdentifierServiceApi.normalizeSubresourceIdentifier(Mockito.anyString(), Mockito.anyString())).thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[0], (String) i.getArguments()[1]));
Mockito.when(mockSmpIdentifierServiceApi.normalizeSubresourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).
thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[1], (String) i.getArguments()[2]));
}
Mockito.doReturn(AbstractHandlerTest.class.getResourceAsStream(resourceName)).when(requestData).getResourceInputStream();
Mockito.doReturn(resourceIdentifier).when(requestData).getResourceIdentifier();
Mockito.when(mockSmpIdentifierServiceApi.normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString())).thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[0], (String) i.getArguments()[1]));
Mockito.doReturn(resourceIdentifier.getScheme() + "::" + resourceIdentifier.getValue()).when(mockSmpIdentifierServiceApi).formatResourceIdentifier(resourceIdentifier);
Mockito.doReturn(TEST_DOMAIN_CODE).when(requestData).getDomainCode();
Mockito.when(mockSmpIdentifierServiceApi.normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[1], (String) i.getArguments()[2]));
Mockito.doReturn(resourceIdentifier.getScheme() + "::"
+ resourceIdentifier.getValue()).when(mockSmpIdentifierServiceApi)
.formatResourceIdentifier(ArgumentMatchers.anyString(), ArgumentMatchers.any(ResourceIdentifier.class));
getTestInstance().validateResource(requestData);
......@@ -109,22 +125,26 @@ abstract class AbstractHandlerTest {
void generateResourceAction(ResourceIdentifier resourceIdentifier, ResourceIdentifier subresourceIdentifier) throws ResourceException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Mockito.doReturn(resourceIdentifier).when(requestData).getResourceIdentifier();
Mockito.doReturn(resourceIdentifier.getScheme() + "::" + resourceIdentifier.getValue()).when(mockSmpIdentifierServiceApi).formatResourceIdentifier(resourceIdentifier);
Mockito.doReturn(TEST_DOMAIN_CODE).when(requestData).getDomainCode();
if (subresourceIdentifier != null) {
Mockito.doReturn(subresourceIdentifier).when(requestData).getSubresourceIdentifier();
Mockito.when(mockSmpIdentifierServiceApi.normalizeSubresourceIdentifier(Mockito.anyString(), Mockito.anyString())).thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[0], (String) i.getArguments()[1]));
Mockito.when(mockSmpIdentifierServiceApi.normalizeSubresourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[1], (String) i.getArguments()[2]));
}
Mockito.doReturn(baos).when(responseData).getOutputStream();
Mockito.doReturn(resourceIdentifier.getScheme() + "::"
+ resourceIdentifier.getValue()).when(mockSmpIdentifierServiceApi)
.formatResourceIdentifier(ArgumentMatchers.anyString(), ArgumentMatchers.any(ResourceIdentifier.class));
getTestInstance().generateResource(requestData, responseData, Collections.emptyList());
assertTrue(baos.size() > 0);
// The generated resource should be valid
ByteArrayInputStream bios = new ByteArrayInputStream(baos.toByteArray());
Mockito.doReturn(bios).when(requestData).getResourceInputStream();
Mockito.doReturn(resourceIdentifier).when(mockSmpIdentifierServiceApi).normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString());
Mockito.doReturn(resourceIdentifier).when(mockSmpIdentifierServiceApi).normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
getTestInstance().validateResource(requestData);
}
......
......@@ -216,7 +216,9 @@ public class OasisCppa3CppHandler extends AbstractHandler {
boolean hasMatchingPartyId = false;
final PartyInfoType partyInfo = cppDocument.getPartyInfo();
for (PartyIdType partyId : partyInfo.getPartyIds()) {
ResourceIdentifier xmlResourceIdentifier = smpIdentifierApi.normalizeResourceIdentifier(partyId.getValue(), partyId.getType());
ResourceIdentifier xmlResourceIdentifier = smpIdentifierApi.normalizeResourceIdentifier(
resourceData.getDomainCode(),
partyId.getValue(), partyId.getType());
if (xmlResourceIdentifier.equals(identifier)) {
hasMatchingPartyId = true;
break;
......
......@@ -34,6 +34,8 @@ import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertTrue;
abstract class AbstractHandlerTest {
private static final String TEST_DOMAIN_CODE = "TestDomain";
protected SmpDataServiceApi mockSmpDataApi = Mockito.mock(SmpDataServiceApi.class);
protected SmpIdentifierServiceApi mockSmpIdentifierServiceApi = Mockito.mock(SmpIdentifierServiceApi.class);
protected SmpXmlSignatureApi mockSignatureApi = Mockito.mock(SmpXmlSignatureApi.class);
......@@ -51,12 +53,14 @@ abstract class AbstractHandlerTest {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Mockito.doReturn(baos).when(responseData).getOutputStream();
Mockito.doReturn(AbstractHandlerTest.class.getResourceAsStream(resourceName)).when(requestData).getResourceInputStream();
Mockito.doReturn(TEST_DOMAIN_CODE).when(requestData).getDomainCode();
Mockito.doReturn(resourceIdentifier).when(requestData).getResourceIdentifier();
if (subresourceIdentifier != null) {
Mockito.doReturn(subresourceIdentifier).when(requestData).getSubresourceIdentifier();
}
Mockito.when(mockSmpIdentifierServiceApi.normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString())).thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[0], (String) i.getArguments()[1]));
Mockito.when(mockSmpIdentifierServiceApi.normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[1], (String) i.getArguments()[2]));
getTestInstance().readResource(requestData, responseData);
assertTrue(baos.size() > 0);
......@@ -71,12 +75,15 @@ abstract class AbstractHandlerTest {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Mockito.doReturn(baos).when(responseData).getOutputStream();
Mockito.doReturn(AbstractHandlerTest.class.getResourceAsStream(resourceName)).when(requestData).getResourceInputStream();
Mockito.doReturn(TEST_DOMAIN_CODE).when(requestData).getDomainCode();
Mockito.doReturn(resourceIdentifier).when(requestData).getResourceIdentifier();
if (subresourceIdentifier != null) {
Mockito.doReturn(subresourceIdentifier).when(requestData).getSubresourceIdentifier();
Mockito.when(mockSmpIdentifierServiceApi.normalizeSubresourceIdentifier(Mockito.anyString(), Mockito.anyString())).thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[0], (String) i.getArguments()[1]));
Mockito.when(mockSmpIdentifierServiceApi.normalizeSubresourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[1], (String) i.getArguments()[2]));
}
Mockito.when(mockSmpIdentifierServiceApi.normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString())).thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[0], (String) i.getArguments()[1]));
Mockito.when(mockSmpIdentifierServiceApi.normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[1], (String) i.getArguments()[2]));
getTestInstance().storeResource(requestData, responseData);
}
......@@ -89,12 +96,14 @@ abstract class AbstractHandlerTest {
// validate
if (subresourceIdentifier != null) {
Mockito.doReturn(subresourceIdentifier).when(requestData).getSubresourceIdentifier();
Mockito.when(mockSmpIdentifierServiceApi.normalizeSubresourceIdentifier(Mockito.anyString(), Mockito.anyString())).thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[0], (String) i.getArguments()[1]));
Mockito.when(mockSmpIdentifierServiceApi.normalizeSubresourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[1], (String) i.getArguments()[2]));
}
Mockito.doReturn(AbstractHandlerTest.class.getResourceAsStream(resourceName)).when(requestData).getResourceInputStream();
Mockito.doReturn(resourceIdentifier).when(requestData).getResourceIdentifier();
Mockito.when(mockSmpIdentifierServiceApi.normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString())).thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[0], (String) i.getArguments()[1]));
Mockito.doReturn(TEST_DOMAIN_CODE).when(requestData).getDomainCode();
Mockito.when(mockSmpIdentifierServiceApi.normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[1], (String) i.getArguments()[2]));
getTestInstance().validateResource(requestData);
}
......@@ -107,9 +116,11 @@ abstract class AbstractHandlerTest {
void generateResourceAction(ResourceIdentifier resourceIdentifier, ResourceIdentifier subresourceIdentifier) throws ResourceException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Mockito.doReturn(resourceIdentifier).when(requestData).getResourceIdentifier();
Mockito.doReturn(TEST_DOMAIN_CODE).when(requestData).getDomainCode();
if (subresourceIdentifier != null) {
Mockito.doReturn(subresourceIdentifier).when(requestData).getSubresourceIdentifier();
Mockito.when(mockSmpIdentifierServiceApi.normalizeSubresourceIdentifier(Mockito.anyString(), Mockito.anyString())).thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[0], (String) i.getArguments()[1]));
Mockito.when(mockSmpIdentifierServiceApi.normalizeSubresourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))
.thenAnswer(i -> new ResourceIdentifier((String) i.getArguments()[1], (String) i.getArguments()[2]));
}
Mockito.doReturn(baos).when(responseData).getOutputStream();
......@@ -120,7 +131,7 @@ abstract class AbstractHandlerTest {
// The generated resource should be valid
ByteArrayInputStream bios = new ByteArrayInputStream(baos.toByteArray());
Mockito.doReturn(bios).when(requestData).getResourceInputStream();
Mockito.doReturn(resourceIdentifier).when(mockSmpIdentifierServiceApi).normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString());
Mockito.doReturn(resourceIdentifier).when(mockSmpIdentifierServiceApi).normalizeResourceIdentifier(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
getTestInstance().validateResource(requestData);
}
......
......@@ -107,7 +107,7 @@ public class OasisSMPResource10Handler extends AbstractOasisSMPHandler {
}
// get references
resource.setServiceMetadataReferenceCollection(new ServiceMetadataReferenceCollectionType());
List<ServiceMetadataReferenceType> referenceTypes = buildReferences(identifier);
List<ServiceMetadataReferenceType> referenceTypes = buildReferences(resourceData.getDomainCode(), identifier);
resource.getServiceMetadataReferenceCollection().getServiceMetadataReferences().addAll(referenceTypes);
try {
......@@ -118,14 +118,14 @@ public class OasisSMPResource10Handler extends AbstractOasisSMPHandler {
}
private List<ServiceMetadataReferenceType> buildReferences(ResourceIdentifier resourceIdentifier) throws ResourceException {
private List<ServiceMetadataReferenceType> buildReferences(final String domainCode, ResourceIdentifier resourceIdentifier) throws ResourceException {
LOG.debug("Build build References identifier [{}].", resourceIdentifier);
// get subresource identifiers for document type
List<ResourceIdentifier> subResourceIdentifier = smpDataApi.getSubResourceIdentifiers(resourceIdentifier, OasisSMPSubresource10.RESOURCE_IDENTIFIER);
List<ServiceMetadataReferenceType> referenceIds = new ArrayList<>();
for (ResourceIdentifier subresId : subResourceIdentifier) {
URI url = buildSMPURLForParticipantAndDocumentIdentifier(resourceIdentifier, subresId);
URI url = buildSMPURLForParticipantAndDocumentIdentifier(domainCode, resourceIdentifier, subresId);
ServiceMetadataReferenceType referenceType = new ServiceMetadataReferenceType();
referenceType.setHref(url.toString());
referenceIds.add(referenceType);
......@@ -133,13 +133,13 @@ public class OasisSMPResource10Handler extends AbstractOasisSMPHandler {
return referenceIds;
}
public URI buildSMPURLForParticipantAndDocumentIdentifier(ResourceIdentifier resourceIdentifier, ResourceIdentifier subresourceIdentifier) throws ResourceException {
public URI buildSMPURLForParticipantAndDocumentIdentifier(final String domainCode, ResourceIdentifier resourceIdentifier, ResourceIdentifier subresourceIdentifier) throws ResourceException {
LOG.debug("Build SMP url for participant identifier: [{}] and document identifier [{}].", resourceIdentifier, subresourceIdentifier);
String pathSegment = smpDataApi.getURIPathSegmentForSubresource(OasisSMPSubresource10.RESOURCE_IDENTIFIER);
String baseUrl = smpDataApi.getResourceUrl();
String formattedParticipant = smpIdentifierApi.formatResourceIdentifier(resourceIdentifier);
String formattedDocument = smpIdentifierApi.formatSubresourceIdentifier(subresourceIdentifier);
String formattedParticipant = smpIdentifierApi.formatResourceIdentifier(domainCode, resourceIdentifier);
String formattedDocument = smpIdentifierApi.formatSubresourceIdentifier(domainCode, subresourceIdentifier);
LOG.debug("Build SMP url from base path [{}], participant identifier: [{}] and document identifier [{}].",
baseUrl, formattedParticipant, formattedDocument);
......@@ -228,11 +228,13 @@ public class OasisSMPResource10Handler extends AbstractOasisSMPHandler {
throw new ResourceException(INVALID_RESOURCE, "Error occurred while parsing Oasis SMP 1.0 ServiceGroup with error: " + ExceptionUtils.getRootCauseMessage(e), e);
}
final ParticipantIdentifierType participantId = resource.getParticipantIdentifier();
ResourceIdentifier xmlResourceIdentifier = smpIdentifierApi.normalizeResourceIdentifier(participantId.getValue(), participantId.getScheme());
ResourceIdentifier xmlResourceIdentifier = smpIdentifierApi.normalizeResourceIdentifier(resourceData.getDomainCode(),
participantId.getValue(), participantId.getScheme());
if (!xmlResourceIdentifier.equals(identifier)) {
// Business identifier must equal path
throw new ResourceException(INVALID_PARAMETERS, "Participant identifiers don't match between URL parameter [" + identifier + "] and XML body: ['" + xmlResourceIdentifier + "']");
throw new ResourceException(INVALID_PARAMETERS,
"Participant identifiers don't match between URL parameter [" + identifier + "] and XML body: ['" + xmlResourceIdentifier + "']");
}
......
......@@ -28,10 +28,10 @@ import eu.europa.ec.smp.spi.api.SmpXmlSignatureApi;
import eu.europa.ec.smp.spi.api.model.RequestData;
import eu.europa.ec.smp.spi.api.model.ResourceIdentifier;
import eu.europa.ec.smp.spi.api.model.ResponseData;
import eu.europa.ec.smp.spi.utils.DomUtils;
import eu.europa.ec.smp.spi.def.OasisSMPSubresource20;
import eu.europa.ec.smp.spi.exceptions.ResourceException;
import eu.europa.ec.smp.spi.exceptions.SignatureException;
import eu.europa.ec.smp.spi.utils.DomUtils;
import gen.eu.europa.ec.ddc.api.smp20.ServiceGroup;
import gen.eu.europa.ec.ddc.api.smp20.aggregate.ServiceReference;
import gen.eu.europa.ec.ddc.api.smp20.basic.ID;
......@@ -119,7 +119,8 @@ public class OasisSMPResource20Handler extends AbstractOasisSMPHandler {
Document doc = reader.objectToDocument(resource);
signatureApi.createEnvelopedSignature(resourceData, doc.getDocumentElement(), Collections.emptyList());
DomUtils.serialize(doc, responseData.getOutputStream());
} catch (SignatureException | TechnicalException | TransformerException e) {
} catch (SignatureException | TechnicalException |
TransformerException e) {
throw new ResourceException(PROCESS_ERROR, "Error occurred while signing the service group 2.0 message!: ["
+ identifier + "]. Error: " + ExceptionUtils.getRootCauseMessage(e), e);
}
......@@ -219,7 +220,9 @@ public class OasisSMPResource20Handler extends AbstractOasisSMPHandler {
throw new ResourceException(INVALID_RESOURCE, "Error occurred while reading the Oasis SMP 2.0 ServiceGroup with error: " + ExceptionUtils.getRootCauseMessage(e), e);
}
final ParticipantID participantId = resource.getParticipantID();
ResourceIdentifier xmlResourceIdentifier = smpIdentifierApi.normalizeResourceIdentifier(participantId.getValue(), participantId.getSchemeID());
ResourceIdentifier xmlResourceIdentifier = smpIdentifierApi.normalizeResourceIdentifier(
resourceData.getDomainCode(),
participantId.getValue(), participantId.getSchemeID());
if (!xmlResourceIdentifier.equals(identifier)) {
// Business identifier must equal path
......
......@@ -187,7 +187,9 @@ public class OasisSMPSubresource10Handler extends AbstractOasisSMPHandler {
} catch (TechnicalException e) {
throw new ResourceException(INVALID_RESOURCE, "Error occurred while validation Oasis SMP 1.0 ServiceMetadata: [" + identifier + "] with error: " + ExceptionUtils.getRootCauseMessage(e), e);
}
serviceMetadataValidator.validate(identifier, documentIdentifier, subresource);
serviceMetadataValidator.validate(
resourceData.getDomainCode(),
identifier, documentIdentifier, subresource);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment