use of io.adminshell.aas.v3.model.SubmodelElementCollection in project FAAAST-Service by FraunhoferIOSB.
the class RequestHandlerManagerTest method testReadValueFromAssetConnectionAndUpdatePersistence.
@Test
public void testReadValueFromAssetConnectionAndUpdatePersistence() throws AssetConnectionException, ResourceNotFoundException {
RequestHandler requestHandler = new DeleteSubmodelByIdRequestHandler(persistence, messageBus, assetConnectionManager);
Reference parentRef = new DefaultReference.Builder().key(new DefaultKey.Builder().value("sub").idType(KeyType.IRI).type(KeyElements.SUBMODEL).build()).build();
SubmodelElement prop1 = new DefaultProperty.Builder().idShort("prop1").value("test").valueType("string").build();
SubmodelElement range = new DefaultRange.Builder().idShort("range1").max("1.0").min("0").valueType("double").build();
SubmodelElement prop2 = new DefaultProperty.Builder().idShort("prop2").value("test").valueType("string").build();
SubmodelElementCollection collection = new DefaultSubmodelElementCollection.Builder().idShort("col1").value(prop2).build();
SubmodelElement prop1_new = new DefaultProperty.Builder().idShort("prop1").value("testNew").valueType("string").build();
SubmodelElement range_new = new DefaultRange.Builder().idShort("range1").max("1.0").min("0").valueType("double").build();
SubmodelElement prop2_new = new DefaultProperty.Builder().idShort("prop2").value("testNew").valueType("string").build();
List<SubmodelElement> submodelElements = List.of(prop1, range, collection);
AssetValueProvider provider_prop1 = mock(AssetValueProvider.class);
AssetValueProvider provider_prop2 = mock(AssetValueProvider.class);
AssetValueProvider provider_range1 = mock(AssetValueProvider.class);
when(assetConnectionManager.hasValueProvider(any())).thenReturn(true);
// mock value prop1
when(assetConnectionManager.getValueProvider(AasUtils.toReference(parentRef, prop1))).thenReturn(provider_prop1);
when(provider_prop1.getValue()).thenReturn(ElementValueMapper.toValue(prop1_new));
// mock value prop2
when(assetConnectionManager.getValueProvider(AasUtils.toReference(AasUtils.toReference(parentRef, collection), prop2))).thenReturn(provider_prop2);
when(provider_prop2.getValue()).thenReturn(ElementValueMapper.toValue(prop2_new));
// mock value range
when(assetConnectionManager.getValueProvider(AasUtils.toReference(parentRef, range))).thenReturn(provider_range1);
when(provider_range1.getValue()).thenReturn(ElementValueMapper.toValue(range_new));
requestHandler.readValueFromAssetConnectionAndUpdatePersistence(parentRef, submodelElements);
verify(persistence).put(null, AasUtils.toReference(parentRef, prop1), prop1_new);
verify(persistence).put(null, AasUtils.toReference(AasUtils.toReference(parentRef, collection), prop2), prop2_new);
Assert.assertEquals(prop1_new, prop1);
Assert.assertEquals(prop2_new, prop2);
Assert.assertEquals(range_new, range);
}
use of io.adminshell.aas.v3.model.SubmodelElementCollection in project FAAAST-Service by FraunhoferIOSB.
the class ReferenceHelper method completeReferenceWithProperKeyElements.
/**
* Browse the keys of a reference and try to find the referenced element in the
* asset administration shell environment to set the right {@link io.adminshell.aas.v3.model.KeyElements}
* of the key.
* All key types must be null or SUBMODEL_ELEMENT.
*
* @param reference with keys which should be completed
* @param env the asset administration shell environment which contains the referenced elements
* @throws ResourceNotFoundException if an element referenced by a key could not be found
*/
public static void completeReferenceWithProperKeyElements(Reference reference, AssetAdministrationShellEnvironment env) throws ResourceNotFoundException {
if (reference == null) {
return;
}
List<Key> keys = reference.getKeys();
if (keys.stream().allMatch(x -> x.getType() != null && x.getType() != KeyElements.SUBMODEL_ELEMENT)) {
return;
}
final Referable[] parent = { null };
for (Key k : keys) {
if (env.getAssetAdministrationShells().stream().anyMatch(x -> x.getIdentification().getIdentifier().equalsIgnoreCase(k.getValue()) || x.getIdShort().equalsIgnoreCase(k.getValue()))) {
k.setType(KeyElements.ASSET_ADMINISTRATION_SHELL);
continue;
}
env.getSubmodels().forEach(x -> {
if (x.getIdentification().getIdentifier().equalsIgnoreCase(k.getValue()) || x.getIdShort().equalsIgnoreCase(k.getValue())) {
k.setType(KeyElements.SUBMODEL);
parent[0] = x;
}
});
if (k.getType() != null && k.getType() != KeyElements.SUBMODEL_ELEMENT) {
continue;
}
if (env.getConceptDescriptions().stream().anyMatch(x -> x.getIdentification().getIdentifier().equalsIgnoreCase(k.getValue()) || x.getIdShort().equalsIgnoreCase(k.getValue()))) {
k.setType(KeyElements.CONCEPT_DESCRIPTION);
continue;
}
if (env.getAssets().stream().anyMatch(x -> x.getIdentification().getIdentifier().equalsIgnoreCase(k.getValue()) || x.getIdShort().equalsIgnoreCase(k.getValue()))) {
k.setType(KeyElements.ASSET);
continue;
}
if (parent[0] != null && Submodel.class.isAssignableFrom(parent[0].getClass())) {
Submodel submodel = (Submodel) parent[0];
submodel.getSubmodelElements().forEach(y -> {
if (y.getIdShort().equalsIgnoreCase(k.getValue())) {
k.setType(AasUtils.referableToKeyType(y));
parent[0] = y;
}
});
} else if (SubmodelElementCollection.class.isAssignableFrom(parent[0].getClass())) {
((SubmodelElementCollection) parent[0]).getValues().forEach(x -> {
if (x.getIdShort().equalsIgnoreCase(k.getValue())) {
k.setType(AasUtils.referableToKeyType(x));
parent[0] = x;
}
});
} else if (Operation.class.isAssignableFrom(parent[0].getClass())) {
Operation operation = (Operation) parent[0];
Stream.concat(Stream.concat(operation.getInoutputVariables().stream(), operation.getInputVariables().stream()), operation.getOutputVariables().stream()).forEach(x -> {
if (x.getValue().getIdShort().equalsIgnoreCase(k.getValue())) {
k.setType(AasUtils.referableToKeyType(x.getValue()));
parent[0] = x.getValue();
}
});
}
if (k.getType() == null) {
throw new ResourceNotFoundException("Resource with ID " + k.getValue() + " was not found!");
}
}
}
use of io.adminshell.aas.v3.model.SubmodelElementCollection in project FAAAST-Service by FraunhoferIOSB.
the class IntegrationTestHttpEndpoint method testGetSpecificSubmodelLevel.
@Test
public void testGetSpecificSubmodelLevel() throws SerializationException, IOException, DeserializationException {
Submodel expected = environment.getSubmodels().get(2);
String identifier = Base64.getUrlEncoder().encodeToString(expected.getIdentification().getIdentifier().getBytes(StandardCharsets.UTF_8));
String baseUrl = HTTP_SUBMODELS + "/" + identifier + "/submodel";
// Level = deep
String url = baseUrl + "?level=deep";
HttpResponse response = getCall(url);
Submodel actual = retrieveResourceFromResponse(response, Submodel.class);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals(expected, actual);
String finalUrl = url;
setUpEventCheck(expected, ElementReadEventMessage.class, () -> getCall(finalUrl));
// Level = core
url = baseUrl + "?level=core";
response = getCall(url);
actual = retrieveResourceFromResponse(response, Submodel.class);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotEquals(expected, actual);
Assert.assertTrue(((SubmodelElementCollection) actual.getSubmodelElements().stream().filter(x -> x.getIdShort().equalsIgnoreCase("ExampleSubmodelCollectionOrdered")).findFirst().get()).getValues().size() == 0);
String finalUrl2 = url;
SubmodelElementCollection submodelElementCollection = ((SubmodelElementCollection) expected.getSubmodelElements().get(5));
submodelElementCollection.setValues(null);
submodelElementCollection = ((SubmodelElementCollection) expected.getSubmodelElements().get(6));
submodelElementCollection.setValues(null);
setUpEventCheck(expected, ElementReadEventMessage.class, () -> getCall(finalUrl2));
}
use of io.adminshell.aas.v3.model.SubmodelElementCollection in project FAAAST-Service by FraunhoferIOSB.
the class PersistenceInMemoryTest method putSubmodelElementChangeInSubmodelElementCollectionTest.
@Test
public void putSubmodelElementChangeInSubmodelElementCollectionTest() throws ResourceNotFoundException {
String AAS_IDENTIFIER = "https://acplt.org/Test_AssetAdministrationShell_Mandatory";
String SUBMODEL_IDENTIFIER = "https://acplt.org/Test_Submodel_Mandatory";
String SUBMODEL_ELEMENT_COLLECTION_IDSHORT = "ExampleSubmodelCollectionUnordered";
SubmodelElement submodelElement = ((SubmodelElementCollection) this.environment.getSubmodels().stream().filter(x -> x.getIdentification().getIdentifier().equalsIgnoreCase(SUBMODEL_IDENTIFIER)).findFirst().get().getSubmodelElements().stream().filter(y -> y.getIdShort().equalsIgnoreCase(SUBMODEL_ELEMENT_COLLECTION_IDSHORT)).findFirst().orElse(null)).getValues().stream().findFirst().orElse(null);
SubmodelElement changedSubmodelElement = DeepCopyHelper.deepCopy(submodelElement, submodelElement.getClass());
String category = "NewCategory";
changedSubmodelElement.setCategory(category);
Reference reference = Util.createReference(AAS_IDENTIFIER, SUBMODEL_IDENTIFIER, SUBMODEL_ELEMENT_COLLECTION_IDSHORT, submodelElement.getIdShort());
this.persistence.put(null, reference, changedSubmodelElement);
SubmodelElement actualSubmodelelement = this.persistence.get(reference, new QueryModifier.Builder().extend(Extend.WithBLOBValue).build());
Assert.assertEquals(changedSubmodelElement, actualSubmodelelement);
}
use of io.adminshell.aas.v3.model.SubmodelElementCollection in project FAAAST-Service by FraunhoferIOSB.
the class PersistenceInMemoryTest method testQueryModifierLevel.
@Test
public void testQueryModifierLevel() throws ResourceNotFoundException {
String SUBMODEL_IDENTIFIER = "https://acplt.org/Test_Submodel_Mandatory";
QueryModifier queryModifier = new QueryModifier.Builder().level(Level.Deep).build();
Identifier submodelId = new DefaultIdentifier.Builder().idType(IdentifierType.IRI).identifier(SUBMODEL_IDENTIFIER).build();
Submodel expected = this.environment.getSubmodels().stream().filter(x -> x.getIdentification().equals(submodelId)).findFirst().get();
Submodel actual = (Submodel) this.persistence.get(submodelId, queryModifier);
Assert.assertEquals(expected, actual);
queryModifier = new QueryModifier.Builder().level(Level.Core).build();
actual = (Submodel) this.persistence.get(submodelId, queryModifier);
List<SubmodelElement> submodelElementCollections = actual.getSubmodelElements().stream().filter(x -> SubmodelElementCollection.class.isAssignableFrom(x.getClass())).collect(Collectors.toList());
Assert.assertTrue(submodelElementCollections.stream().allMatch(x -> ((SubmodelElementCollection) x).getValues() == null));
}
Aggregations