Skip to content

Commit

Permalink
Merge pull request #359 from catenax-ng/feature/TRI-1332-fix-decentra…
Browse files Browse the repository at this point in the history
…l-flow

feat(irs-api): Fix DiscoveryResponse
  • Loading branch information
ds-jhartmann authored Jun 14, 2023
2 parents a160cee + 51466bc commit 68a6aea
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public AssetAdministrationShellDescriptor getAAShellDescriptor(final DigitalTwin
log.info("Retrieved AAS Identification for DigitalTwinRegistryKey: {}", key);
final DiscoveryFinderRequest onlyBpn = new DiscoveryFinderRequest(List.of("bpn"));
final List<String> providedBpn = List.of(key.bpn());
final List<DiscoveryEndpoint> discoveryEndpoints = discoveryFinderClient.findDiscoveryEndpoints(onlyBpn);
final List<DiscoveryEndpoint> discoveryEndpoints = discoveryFinderClient.findDiscoveryEndpoints(onlyBpn)
.endpoints();
final List<String> connectorEndpoints = discoveryEndpoints.stream()
.map(discoveryEndpoint -> discoveryFinderClient.findConnectorEndpoints(
discoveryEndpoint.endpointAddress(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package org.eclipse.tractusx.irs.aaswrapper.registry.domain;

/**
* A result of finding Discovery Endpoint
* A single Discovery Endpoint.
*/
public record DiscoveryEndpoint(String type, String description, String endpointAddress, String documentation,
String resourceId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
*/
public interface DiscoveryFinderClient {

List<DiscoveryEndpoint> findDiscoveryEndpoints(DiscoveryFinderRequest request);
DiscoveryResponse findDiscoveryEndpoints(DiscoveryFinderRequest request);

List<EdcDiscoveryResult> findConnectorEndpoints(String endpointAddress, List<String> bpns);

}
Expand All @@ -61,14 +62,16 @@ class DiscoveryFinderClientImpl implements DiscoveryFinderClient {

@Override
@Retry(name = "registry")
public List<DiscoveryEndpoint> findDiscoveryEndpoints(final DiscoveryFinderRequest request) {
return restTemplate.postForObject(discoveryFinderUrl, request, List.class);
public DiscoveryResponse findDiscoveryEndpoints(final DiscoveryFinderRequest request) {
return restTemplate.postForObject(discoveryFinderUrl, request, DiscoveryResponse.class);
}

@Override
@Retry(name = "registry")
public List<EdcDiscoveryResult> findConnectorEndpoints(final String endpointAddress,
final List<String> bpns) {
return restTemplate.postForObject(endpointAddress, bpns, List.class);
public List<EdcDiscoveryResult> findConnectorEndpoints(final String endpointAddress, final List<String> bpns) {
final EdcDiscoveryResult[] edcDiscoveryResults = restTemplate.postForObject(endpointAddress, bpns,
EdcDiscoveryResult[].class);

return edcDiscoveryResults == null ? List.of() : List.of(edcDiscoveryResults);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/********************************************************************************
* 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,2022,2023 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.aaswrapper.registry.domain;

import java.util.List;

/**
* The result of searching the Discovery Finder. Contains a list of {@link DiscoveryEndpoint}s.
*/
public record DiscoveryResponse(List<DiscoveryEndpoint> endpoints) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,28 @@ class DecentralDigitalTwinRegistryServiceTest {
private final DecentralDigitalTwinRegistryClient decentralDigitalTwinRegistryClient = mock(
DecentralDigitalTwinRegistryClient.class);

private final DecentralDigitalTwinRegistryService decentralDigitalTwinRegistryService = new DecentralDigitalTwinRegistryService(discoveryFinderClient,
endpointDataForConnectorsService, decentralDigitalTwinRegistryClient);
private final DecentralDigitalTwinRegistryService decentralDigitalTwinRegistryService = new DecentralDigitalTwinRegistryService(
discoveryFinderClient, endpointDataForConnectorsService, decentralDigitalTwinRegistryClient);

@Test
void shouldReturnExpectedShell() {
// given
final DigitalTwinRegistryKey digitalTwinRegistryKey = new DigitalTwinRegistryKey("urn:uuid:4132cd2b-cbe7-4881-a6b4-39fdc31cca2b", "bpn");
final DigitalTwinRegistryKey digitalTwinRegistryKey = new DigitalTwinRegistryKey(
"urn:uuid:4132cd2b-cbe7-4881-a6b4-39fdc31cca2b", "bpn");
final AssetAdministrationShellDescriptor expectedShell = shellDescriptor(Collections.emptyList());
EndpointDataReference endpointDataReference = EndpointDataReference.Builder.newInstance().endpoint("url.to.host").build();
EndpointDataReference endpointDataReference = EndpointDataReference.Builder.newInstance()
.endpoint("url.to.host")
.build();
final List<DiscoveryEndpoint> discoveryEndpoints = List.of(
new DiscoveryEndpoint("type", "desc", "address", "doc", "resId"));
when(discoveryFinderClient.findDiscoveryEndpoints(any(DiscoveryFinderRequest.class))).thenReturn(
Collections.singletonList(new DiscoveryEndpoint("type", "desc", "address", "doc", "resId")));
when(endpointDataForConnectorsService.findEndpointDataForConnectors(anyList())).thenReturn(List.of(endpointDataReference));
when(decentralDigitalTwinRegistryClient.getAllAssetAdministrationShellIdsByAssetLink(any(), anyList())).thenReturn(Collections.emptyList());
when(decentralDigitalTwinRegistryClient.getAssetAdministrationShellDescriptor(any(), any())).thenReturn(expectedShell);
new DiscoveryResponse(discoveryEndpoints));
when(endpointDataForConnectorsService.findEndpointDataForConnectors(anyList())).thenReturn(
List.of(endpointDataReference));
when(decentralDigitalTwinRegistryClient.getAllAssetAdministrationShellIdsByAssetLink(any(),
anyList())).thenReturn(Collections.emptyList());
when(decentralDigitalTwinRegistryClient.getAssetAdministrationShellDescriptor(any(), any())).thenReturn(
expectedShell);

// when
final AssetAdministrationShellDescriptor actualShell = decentralDigitalTwinRegistryService.getAAShellDescriptor(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/********************************************************************************
* 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,2022,2023 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.aaswrapper.registry.domain;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.web.client.RestTemplate;

@ExtendWith(MockitoExtension.class)
class DiscoveryFinderClientImplTest {
@Mock
private RestTemplate restTemplate;

@Test
void shouldReturnEmptyListWhenDiscoveryReturnsNull() {
// Arrange
final String discoveryFinderUrl = "https://discovery.mock/api/administration/connectors/discovery/search";
final String providerUrl = "https://discovery.mock/api/administration/connectors/discovery";
final List<String> bpns = List.of("BPN123");
final DiscoveryFinderClientImpl discoveryFinderClient = new DiscoveryFinderClientImpl(discoveryFinderUrl,
restTemplate);
when(restTemplate.postForObject(providerUrl, bpns, EdcDiscoveryResult[].class)).thenReturn(null);

// Act
final List<EdcDiscoveryResult> connectorEndpoints = discoveryFinderClient.findConnectorEndpoints(providerUrl,
bpns);

// Assert
assertThat(connectorEndpoints).isNotNull().isEmpty();
}

@Test
void shouldReturnEmptyListWhenDiscoveryReturnsEmpty() {
// Arrange
final String discoveryFinderUrl = "https://discovery.mock/api/administration/connectors/discovery/search";
final String providerUrl = "https://discovery.mock/api/administration/connectors/discovery";
final List<String> bpns = List.of("BPN123");
final DiscoveryFinderClientImpl discoveryFinderClient = new DiscoveryFinderClientImpl(discoveryFinderUrl,
restTemplate);
final EdcDiscoveryResult[] discoveryResponse = { new EdcDiscoveryResult("BPN123",
List.of("https://provider.edc"))
};

when(restTemplate.postForObject(providerUrl, bpns, EdcDiscoveryResult[].class)).thenReturn(discoveryResponse);

// Act
final List<EdcDiscoveryResult> connectorEndpoints = discoveryFinderClient.findConnectorEndpoints(providerUrl,
bpns);

// Assert
assertThat(connectorEndpoints).isNotNull().hasSize(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public class ContractNegotiationService {

private final EdcControlPlaneClient edcControlPlaneClient;

private final EdcConfiguration config;

private final PolicyCheckerService policyCheckerService;

public NegotiationResponse negotiate(final String providerConnectorUrl, final CatalogItem catalogItem)
Expand All @@ -72,8 +70,7 @@ public NegotiationResponse negotiate(final String providerConnectorUrl, final Ca

final NegotiationRequest negotiationRequest = NegotiationRequest.builder()
.connectorId(catalogItem.getConnectorId())
.connectorAddress(providerConnectorUrl
+ config.getControlplane().getProviderSuffix())
.connectorAddress(providerConnectorUrl)
.offer(contractOfferRequest)
.build();

Expand All @@ -94,7 +91,7 @@ public NegotiationResponse negotiate(final String providerConnectorUrl, final Ca
.managedResources(TransferProcessRequest.DEFAULT_MANAGED_RESOURCES)
.connectorId(catalogItem.getConnectorId())
.connectorAddress(
providerConnectorUrl + config.getControlplane().getProviderSuffix())
providerConnectorUrl)
.contractId(response.getContractAgreementId())
.assetId(catalogItem.getAssetPropId())
.dataDestination(destination)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ public class EdcControlPlaneClient {

/* package */ Catalog getCatalog(final String providerConnectorUrl, final int offset) {
final var catalogUrl = config.getControlplane().getEndpoint().getData() + "/catalog/request";
final var providerUrl = providerConnectorUrl + config.getControlplane().getProviderSuffix();
final var limit = config.getControlplane().getCatalogPageSize();

final CatalogRequest request = buildCatalogRequest(offset, providerUrl, limit);
final CatalogRequest request = buildCatalogRequest(offset, providerConnectorUrl, limit);
return edcRestTemplate.exchange(catalogUrl, HttpMethod.POST, new HttpEntity<>(request, headers()),
Catalog.class).getBody();
}
Expand All @@ -80,9 +79,9 @@ private CatalogRequest buildCatalogRequest(final int offset, final String provid
return CatalogRequest.builder().providerUrl(providerUrl).querySpec(querySpec.build()).build();
}

/* package */ Catalog getCatalogWithFilter(final String providerConnectorUrl, final String key,
final String value) {
/* package */ Catalog getCatalogWithFilter(final String providerConnectorUrl, final String key, final String value) {
final var catalogUrl = config.getControlplane().getEndpoint().getData() + "/catalog/request";

final var querySpec = QuerySpec.Builder.newInstance().filter(List.of(new Criterion(key, "=", value)));
final var catalogRequest = CatalogRequest.builder()
.providerUrl(providerConnectorUrl)
Expand Down
Loading

0 comments on commit 68a6aea

Please sign in to comment.