Skip to content

Commit

Permalink
feat(edc-client):[#344] Add IrsWireMock integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
ds-jhartmann committed Jan 23, 2024
1 parent 588b7bd commit 4125ea9
Show file tree
Hide file tree
Showing 4 changed files with 789 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
Expand All @@ -66,6 +67,7 @@
@Configuration
@RequiredArgsConstructor
@SuppressWarnings("PMD.ExcessiveImports")
@Profile("!integrationtest")
public class RestTemplateConfig {

public static final String DTR_REST_TEMPLATE = "dtrRestTemplate";
Expand Down
150 changes: 150 additions & 0 deletions irs-api/src/test/java/org/eclipse/tractusx/irs/IrsWireMockIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/********************************************************************************
* Copyright (c) 2021,2022,2023
* 2022: ZF Friedrichshafen AG
* 2022: ISTOS GmbH
* 2022,2023: Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
* 2022,2023: BOSCH AG
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
package org.eclipse.tractusx.irs;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.givenThat;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import org.eclipse.tractusx.irs.component.JobHandle;
import org.eclipse.tractusx.irs.component.PartChainIdentificationKey;
import org.eclipse.tractusx.irs.component.RegisterJob;
import org.eclipse.tractusx.irs.component.enums.Direction;
import org.eclipse.tractusx.irs.semanticshub.AspectModels;
import org.eclipse.tractusx.irs.services.IrsItemGraphQueryService;
import org.eclipse.tractusx.irs.services.SemanticHubService;
import org.eclipse.tractusx.irs.services.validation.SchemaNotFoundException;
import org.eclipse.tractusx.irs.testing.containers.MinioContainer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.testcontainers.junit.jupiter.Testcontainers;

@WireMockTest(httpPort = 8085)
@Testcontainers
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WireMockTestConfig.class
/*, properties = "spring.main.allow-bean-definition-overriding=true"*/)
@ContextConfiguration(initializers = IrsWireMockIT.MinioConfigInitializer.class)
//@Import({ WireMockTestConfig.class })
@ActiveProfiles("integrationtest")
class IrsWireMockIT {
public static final String BPDM_TEST = "http://bpdm.test/legal-entities/{partnerId}?idType={idType}";
public static final String DISCOVERY_TEST = "http://discovery.test";
public static final String SEMANTIC_HUB_URL = "http://semantic.hub/models";
public static final String SEMANTIC_HUB_SCHEMA_URL = "http://semantic.hub/models/{urn}/json-schema";
private static final String ACCESS_KEY = "accessKey";
private static final String SECRET_KEY = "secretKey";
private static final MinioContainer minioContainer = new MinioContainer(
new MinioContainer.CredentialsProvider(ACCESS_KEY, SECRET_KEY)).withReuse(true);
public static final String EDC_URL = "http://edc.test:8081/management";
@Autowired
private IrsItemGraphQueryService irsService;

@Autowired
private SemanticHubService semanticHubService;

@BeforeAll
static void startContainer() {
minioContainer.start();
givenThat(get(urlPathEqualTo("/models")).willReturn(aResponse().withStatus(200)
.withHeader("Content-Type",
"application/json;charset=UTF-8")
.withBodyFile("all-models-page-IT.json")));
}

@AfterAll
static void stopContainer() {
minioContainer.stop();
}

@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("bpdm.bpnEndpoint", () -> BPDM_TEST);
registry.add("digitalTwinRegistry.discoveryFinderUrl", () -> DISCOVERY_TEST);
registry.add("semanticshub.url", () -> SEMANTIC_HUB_URL);
registry.add("semanticshub.modelJsonSchemaEndpoint", () -> SEMANTIC_HUB_SCHEMA_URL);
registry.add("irs-edc-client.controlplane.endpoint.data", () -> EDC_URL);
}

@Test
void shouldStartApplicationAndCollectSemanticModels() throws SchemaNotFoundException {
// Act
final AspectModels allAspectModels = semanticHubService.getAllAspectModels();

// Assert
assertThat(allAspectModels.models()).hasSize(78);
}

@Test
void shouldStartJob() {
// Arrange
final RegisterJob request = RegisterJob.builder()
.key(PartChainIdentificationKey.builder()
.bpn("BPNTEST")
.globalAssetId("globalAssetId")
.build())
.depth(2)
.aspects(List.of("SerialPart", "Batch", "SingleLevelBomAsBuilt"))
.collectAspects(true)
.lookupBPNs(true)
.direction(Direction.DOWNWARD)
.build();

// Act
final JobHandle jobHandle = irsService.registerItemJob(request);

// Assert
assertThat(jobHandle.getId()).isNotNull();
}

public static class MinioConfigInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
final String hostAddress = minioContainer.getHostAddress();
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(applicationContext,
"blobstore.endpoint=http://" + hostAddress, "blobstore.accessKey=" + ACCESS_KEY,
"blobstore.secretKey=" + SECRET_KEY, "policystore.persistence.endpoint=http://" + hostAddress,
"policystore.persistence.accessKey=" + ACCESS_KEY,
"policystore.persistence.secretKey=" + SECRET_KEY,
"policystore.persistence.bucketName=policy-test");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/********************************************************************************
* Copyright (c) 2021,2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
package org.eclipse.tractusx.irs;

import static org.eclipse.tractusx.irs.configuration.RestTemplateConfig.BPDM_REST_TEMPLATE;
import static org.eclipse.tractusx.irs.configuration.RestTemplateConfig.DISCOVERY_REST_TEMPLATE;
import static org.eclipse.tractusx.irs.configuration.RestTemplateConfig.DTR_REST_TEMPLATE;
import static org.eclipse.tractusx.irs.configuration.RestTemplateConfig.EDC_REST_TEMPLATE;
import static org.eclipse.tractusx.irs.configuration.RestTemplateConfig.NO_ERROR_REST_TEMPLATE;
import static org.eclipse.tractusx.irs.configuration.RestTemplateConfig.SEMHUB_REST_TEMPLATE;
import static org.eclipse.tractusx.irs.testing.wiremock.WireMockConfig.restTemplateProxy;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.web.client.RestTemplate;

@TestConfiguration
public class WireMockTestConfig {
public static final int HTTP_PORT = 8085;
private static final String PROXY_SERVER_HOST = "127.0.0.1";

@Primary
@Profile("integrationtest")
@Bean(DTR_REST_TEMPLATE)
RestTemplate dtrRestTemplate() {
return restTemplateProxy(PROXY_SERVER_HOST, HTTP_PORT);
}

@Primary
@Profile("integrationtest")
@Bean(EDC_REST_TEMPLATE)
RestTemplate edcRestTemplate() {
return restTemplateProxy(PROXY_SERVER_HOST, HTTP_PORT);
}

@Primary
@Profile("integrationtest")
@Bean(NO_ERROR_REST_TEMPLATE)
RestTemplate noErrorRestTemplate() {
return restTemplateProxy(PROXY_SERVER_HOST, HTTP_PORT);
}

@Primary
@Profile("integrationtest")
@Bean(DISCOVERY_REST_TEMPLATE)
RestTemplate discoveryRestTemplate() {
return restTemplateProxy(PROXY_SERVER_HOST, HTTP_PORT);
}

@Primary
@Profile("integrationtest")
@Bean(BPDM_REST_TEMPLATE)
RestTemplate bpdmRestTemplate() {
return restTemplateProxy(PROXY_SERVER_HOST, HTTP_PORT);
}

@Primary
@Profile("integrationtest")
@Bean(SEMHUB_REST_TEMPLATE)
@Qualifier(SEMHUB_REST_TEMPLATE)
RestTemplate semanticHubRestTemplate() {
return restTemplateProxy(PROXY_SERVER_HOST, HTTP_PORT);
}
}
Loading

0 comments on commit 4125ea9

Please sign in to comment.