Skip to content

Commit

Permalink
Fix referredIdSemanticId not being set in client - createSubmodelInAas (
Browse files Browse the repository at this point in the history
eclipse-basyx#369)

* Fix referredIdSemanticId not being set in client - createSubmodelInAas

* Add link to related aas4j issue

* chore: Fix formatting issues
  • Loading branch information
mateusmolina-iese authored Aug 1, 2024
1 parent 9ca510f commit cad48b8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
import org.eclipse.digitaltwin.basyx.aasregistry.client.model.AssetAdministrationShellDescriptor;
import org.eclipse.digitaltwin.basyx.aasrepository.client.ConnectedAasRepository;
import org.eclipse.digitaltwin.basyx.aasrepository.feature.registry.integration.AasDescriptorFactory;
import org.eclipse.digitaltwin.basyx.client.internal.resolver.DescriptorResolver;
import org.eclipse.digitaltwin.basyx.aasservice.client.ConnectedAasService;
import org.eclipse.digitaltwin.basyx.client.internal.resolver.DescriptorResolver;
import org.eclipse.digitaltwin.basyx.submodelregistry.client.api.SubmodelRegistryApi;
import org.eclipse.digitaltwin.basyx.submodelregistry.client.model.SubmodelDescriptor;
import org.eclipse.digitaltwin.basyx.submodelrepository.client.ConnectedSubmodelRepository;
Expand Down Expand Up @@ -212,6 +212,8 @@ public void createAas(AssetAdministrationShell aas) {
* The Submodel object to create under the specified AAS.
*/
public void createSubmodelInAas(String aasIdentifier, Submodel submodel) {
AssetAdministrationShell shell = getAasService(aasIdentifier).getAAS();

smRepository.createSubmodel(submodel);
SubmodelDescriptor descriptor = smDescriptorFactory.create(submodel);

Expand All @@ -221,7 +223,12 @@ public void createSubmodelInAas(String aasIdentifier, Submodel submodel) {
throw new RegistryHttpRequestException(aasIdentifier, e);
}

aasRepository.addSubmodelReference(aasIdentifier, AasUtils.toReference(submodel));
Reference smRef = AasUtils.toReference(AasUtils.toReference(shell), submodel);

// TODO See https://github.com/eclipse-aas4j/aas4j/issues/308
smRef.setReferredSemanticId(submodel.getSemanticId());

aasRepository.addSubmodelReference(aasIdentifier, smRef);
}

private String extractSubmodelIdentifierFromReference(Reference submodelReference) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.exceptions.NoValidEndpointFoundException;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.AasDescriptorResolver;
Expand Down Expand Up @@ -73,16 +75,16 @@
*
*/
public class TestConnectedAasManager {
protected final static String AAS_REPOSITORY_BASE_PATH = "http://localhost:8081";
protected final static String SM_REPOSITORY_BASE_PATH = "http://localhost:8081";
protected final static String AAS_REGISTRY_BASE_PATH = "http://localhost:8050";
protected final static String SM_REGISTRY_BASE_PATH = "http://localhost:8060";
protected static final String AAS_REPOSITORY_BASE_PATH = "http://localhost:8081";
protected static final String SM_REPOSITORY_BASE_PATH = "http://localhost:8081";
protected static final String AAS_REGISTRY_BASE_PATH = "http://localhost:8050";
protected static final String SM_REGISTRY_BASE_PATH = "http://localhost:8060";

protected static ConfigurableApplicationContext appContext;
protected static AasRepository aasRepository;
protected static SubmodelRepository smRepository;

protected final static TestFixture FIXTURE = new TestFixture(AAS_REPOSITORY_BASE_PATH, SM_REPOSITORY_BASE_PATH);
protected static final TestFixture FIXTURE = new TestFixture(AAS_REPOSITORY_BASE_PATH, SM_REPOSITORY_BASE_PATH);

protected static ConnectedAasRepository connectedAasRepository;
protected static ConnectedSubmodelRepository connectedSmRepository;
Expand Down Expand Up @@ -146,20 +148,19 @@ public void createAas() throws ApiException {
public void createSubmodelInAas() throws Exception {
Submodel expectedSm = FIXTURE.buildSmPos1();
SubmodelDescriptor expectedDescriptor = FIXTURE.buildSmPos1Descriptor();
Reference expectedRef = FIXTURE.buildSmPos1Ref();

aasManager.createSubmodelInAas(TestFixture.AAS_PRE1_ID, expectedSm);

InOrder inOrder = inOrder(connectedSmRepository, smRegistryApi, connectedAasRepository);

inOrder.verify(connectedSmRepository, times(1))
.createSubmodel(expectedSm);
inOrder.verify(smRegistryApi, times(1))
.postSubmodelDescriptor(expectedDescriptor);
inOrder.verify(connectedAasRepository, times(1))
.addSubmodelReference(eq(TestFixture.AAS_PRE1_ID), any());
inOrder.verify(connectedSmRepository, times(1)).createSubmodel(expectedSm);
inOrder.verify(smRegistryApi, times(1)).postSubmodelDescriptor(expectedDescriptor);
inOrder.verify(connectedAasRepository, times(1)).addSubmodelReference(eq(TestFixture.AAS_PRE1_ID), any());

assertEquals(expectedSm, getSubmodelFromRepo(TestFixture.SM_POS1_ID));
assertEquals(expectedDescriptor, getDescriptorFromSubmodelRegistry(TestFixture.SM_POS1_ID));
assertEquals(expectedRef, getSubmodelRefFromAasRepository(TestFixture.AAS_PRE1_ID, TestFixture.SM_POS1_ID).get());
}

@Test
Expand Down Expand Up @@ -195,7 +196,7 @@ public void deleteSubmodelOfAas() throws Exception {
}

@Test
public void getAas() throws ApiException, NoValidEndpointFoundException {
public void getAas() throws NoValidEndpointFoundException {
AssetAdministrationShell expectedAas = FIXTURE.buildAasPre1();

AssetAdministrationShell actualAas = aasManager.getAasService(TestFixture.AAS_PRE1_ID)
Expand All @@ -205,7 +206,7 @@ public void getAas() throws ApiException, NoValidEndpointFoundException {
}

@Test
public void getSubmodel() throws Exception {
public void getSubmodel() {
Submodel expectedSm = FIXTURE.buildSmPre1();

Submodel actualSm = aasManager.getSubmodelService(TestFixture.SM_PRE1_ID)
Expand Down Expand Up @@ -340,4 +341,14 @@ protected void cleanUpRepositories() {
}
}

private Optional<Reference> getSubmodelRefFromAasRepository(String aasId, String smId) {
return aasManager.getAasService(aasId)
.getAAS()
.getSubmodels()
.stream()
.filter(ref -> ref.getKeys().stream()
.anyMatch(key -> key.getValue().equals(smId)))
.findAny();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.eclipse.digitaltwin.basyx.http.Base64UrlEncoder;
import org.eclipse.digitaltwin.basyx.submodelregistry.client.model.SubmodelDescriptor;
import org.eclipse.digitaltwin.basyx.submodelrepository.feature.registry.integration.DummySubmodelDescriptorFactory;
import org.eclipse.digitaltwin.basyx.submodelrepository.feature.registry.integration.mapper.AttributeMapper;

/**
* Test fixture for {@link ConnectedAasManager} and related Components
Expand All @@ -68,6 +69,7 @@ public class TestFixture {
public static final String SM_POS1_ID = "smPos1";
public static final String SM_POS1_ID_ENCODED = Base64UrlEncoder.encode(SM_POS1_ID);
public static final String SM_POS1_IDSHORT = "smPos1IdShort";
public static final String SM_POS1_SEMANTICID = "smPos1SemanticId";

private final String aasRepositoryBasePath;
private final String smRepositoryBasePath;
Expand Down Expand Up @@ -114,11 +116,19 @@ public AssetAdministrationShellDescriptor buildAasPos1Descriptor() {
}

public SubmodelDescriptor buildSmPos1Descriptor() {
return DummySubmodelDescriptorFactory.createDummyDescriptor(SM_POS1_ID, SM_POS1_IDSHORT, smRepositoryBasePath, null);
return DummySubmodelDescriptorFactory.createDummyDescriptor(SM_POS1_ID, SM_POS1_IDSHORT, smRepositoryBasePath, new AttributeMapper(ConnectedAasManagerHelper.buildObjectMapper()).mapSemanticId(buildSmPos1SemanticId()));
}

public Reference buildSmPos1SemanticId() {
return new DefaultReference.Builder().type(ReferenceTypes.EXTERNAL_REFERENCE).keys(new DefaultKey.Builder().type(KeyTypes.GLOBAL_REFERENCE).value("https://admin-shell.io/aas/3/0/CustomDataSpecification").build()).build();
}

public Submodel buildSmPos1() {
return new DefaultSubmodel.Builder().id(SM_POS1_ID).idShort(SM_POS1_IDSHORT).build();
return new DefaultSubmodel.Builder().id(SM_POS1_ID).idShort(SM_POS1_IDSHORT).semanticId(buildSmPos1SemanticId()).build();
}

public Reference buildSmPos1Ref() {
return new DefaultReference.Builder().type(ReferenceTypes.MODEL_REFERENCE).referredSemanticId(buildSmPos1SemanticId()).keys(new DefaultKey.Builder().type(KeyTypes.SUBMODEL).value(SM_POS1_ID).build()).build();
}

}

0 comments on commit cad48b8

Please sign in to comment.