-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mohammad Ghazanfar Ali Danish <[email protected]>
- Loading branch information
Showing
4 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...stry/feature/authorization/rbac/backend/submodel/AasRegistryTargetInformationAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2024 the Eclipse BaSyx Authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
******************************************************************************/ | ||
|
||
package org.eclipse.digitaltwin.basyx.aasregistry.feature.authorization.rbac.backend.submodel; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.digitaltwin.aas4j.v3.model.Property; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultProperty; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementCollection; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementList; | ||
import org.eclipse.digitaltwin.basyx.aasregistry.feature.authorization.AasRegistryTargetInformation; | ||
import org.eclipse.digitaltwin.basyx.authorization.rbac.TargetInformation; | ||
import org.eclipse.digitaltwin.basyx.authorization.rules.rbac.backend.submodel.TargetInformationAdapter; | ||
import org.eclipse.digitaltwin.basyx.core.exceptions.InvalidTargetInformationException; | ||
|
||
/** | ||
* An implementation of the {@link TargetInformationAdapter} to adapt with Aas | ||
* {@link TargetInformation} | ||
* | ||
* @author danish | ||
*/ | ||
public class AasRegistryTargetInformationAdapter implements TargetInformationAdapter { | ||
|
||
@Override | ||
public SubmodelElementCollection adapt(TargetInformation targetInformation) { | ||
|
||
SubmodelElementCollection targetInformationSMC = new DefaultSubmodelElementCollection.Builder().idShort("targetInformation").build(); | ||
|
||
SubmodelElementList aasId = new DefaultSubmodelElementList.Builder().idShort("aasIds").build(); | ||
|
||
List<SubmodelElement> aasIds = ((AasRegistryTargetInformation) targetInformation).getAasIds().stream().map(this::transform).collect(Collectors.toList()); | ||
aasId.setValue(aasIds); | ||
|
||
targetInformationSMC.setValue(Arrays.asList(aasId)); | ||
|
||
return targetInformationSMC; | ||
} | ||
|
||
@Override | ||
public TargetInformation adapt(SubmodelElementCollection targetInformation) { | ||
|
||
SubmodelElement aasIdSubmodelElement = targetInformation.getValue().stream().filter(sme -> sme.getIdShort().equals("aasIds")).findAny().orElseThrow( | ||
() -> new InvalidTargetInformationException("The TargetInformation defined in the SubmodelElementCollection Rule with id: " + targetInformation.getIdShort() + " is not compatible with the " + getClass().getName())); | ||
|
||
if (!(aasIdSubmodelElement instanceof SubmodelElementList)) | ||
throw new InvalidTargetInformationException("The TargetInformation defined in the SubmodelElementCollection Rule with id: " + targetInformation.getIdShort() + " is not compatible with the " + getClass().getName()); | ||
|
||
SubmodelElementList aasIdList = (SubmodelElementList) aasIdSubmodelElement; | ||
|
||
List<String> aasIds = aasIdList.getValue().stream().map(Property.class::cast).map(Property::getValue).collect(Collectors.toList()); | ||
|
||
return new AasRegistryTargetInformation(aasIds); | ||
} | ||
|
||
private Property transform(String aasId) { | ||
return new DefaultProperty.Builder().value(aasId).build(); | ||
} | ||
|
||
} |
143 changes: 143 additions & 0 deletions
143
...aasregistry/regression/feature/authorization/AasRegistryTargetInformationAdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2024 the Eclipse BaSyx Authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
******************************************************************************/ | ||
|
||
package org.eclipse.digitaltwin.basyx.aasregistry.regression.feature.authorization; | ||
|
||
import static org.junit.Assert.*; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.Property; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElement; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementCollection; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.SubmodelElementList; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultProperty; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementCollection; | ||
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultSubmodelElementList; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.digitaltwin.basyx.aasregistry.feature.authorization.AasRegistryTargetInformation; | ||
import org.eclipse.digitaltwin.basyx.aasregistry.feature.authorization.rbac.backend.submodel.AasRegistryTargetInformationAdapter; | ||
import org.eclipse.digitaltwin.basyx.authorization.rbac.TargetInformation; | ||
import org.eclipse.digitaltwin.basyx.core.exceptions.InvalidTargetInformationException; | ||
|
||
/** | ||
* Tests {@link AasRegistryTargetInformationAdapter} | ||
* | ||
* @author danish | ||
*/ | ||
public class AasRegistryTargetInformationAdapterTest { | ||
|
||
private AasRegistryTargetInformationAdapter aasRegistryTargetInformationAdapter; | ||
|
||
@Before | ||
public void setUp() { | ||
aasRegistryTargetInformationAdapter = new AasRegistryTargetInformationAdapter(); | ||
} | ||
|
||
@Test | ||
public void testAdaptTargetInformationToSubmodelElementCollection() { | ||
|
||
List<String> aasIds = Arrays.asList("aasId1", "aasId2"); | ||
TargetInformation targetInformation = new AasRegistryTargetInformation(aasIds); | ||
|
||
SubmodelElementCollection result = aasRegistryTargetInformationAdapter.adapt(targetInformation); | ||
|
||
assertEquals("targetInformation", result.getIdShort()); | ||
|
||
List<SubmodelElement> elements = result.getValue(); | ||
assertEquals(1, elements.size()); | ||
|
||
SubmodelElementList aasIdList = (SubmodelElementList) elements.get(0); | ||
assertEquals("aasIds", aasIdList.getIdShort()); | ||
|
||
List<String> actualAasIds = aasIdList.getValue().stream().map(Property.class::cast).map(Property::getValue).map(String::valueOf).collect(Collectors.toList()); | ||
assertEquals(aasIds, actualAasIds); | ||
} | ||
|
||
@Test | ||
public void testAdaptSubmodelElementCollectionToTargetInformation() { | ||
|
||
List<String> expectedAasIds = Arrays.asList("aasId1", "aasId2"); | ||
List<SubmodelElement> aasIdProperties = expectedAasIds.stream().map(aasId -> new DefaultProperty.Builder().value(aasId).build()).collect(Collectors.toList()); | ||
|
||
SubmodelElementList aasIdList = new DefaultSubmodelElementList.Builder().idShort("aasIds").value(aasIdProperties).build(); | ||
|
||
SubmodelElementCollection targetInformationSMC = new DefaultSubmodelElementCollection.Builder().idShort("targetInformation").value(Collections.singletonList(aasIdList)).build(); | ||
|
||
TargetInformation result = aasRegistryTargetInformationAdapter.adapt(targetInformationSMC); | ||
|
||
assertTrue(result instanceof AasRegistryTargetInformation); | ||
assertEquals(expectedAasIds, ((AasRegistryTargetInformation) result).getAasIds()); | ||
} | ||
|
||
@Test | ||
public void testAdaptTargetInformationWithEmptyAasIds() { | ||
|
||
List<String> aasIds = Collections.emptyList(); | ||
TargetInformation targetInformation = new AasRegistryTargetInformation(aasIds); | ||
|
||
SubmodelElementCollection result = aasRegistryTargetInformationAdapter.adapt(targetInformation); | ||
|
||
assertEquals("targetInformation", result.getIdShort()); | ||
|
||
List<SubmodelElement> elements = result.getValue(); | ||
assertEquals(1, elements.size()); | ||
|
||
SubmodelElementList aasIdList = (SubmodelElementList) elements.get(0); | ||
assertEquals("aasIds", aasIdList.getIdShort()); | ||
|
||
List<String> actualAasIds = aasIdList.getValue().stream() | ||
.map(Property.class::cast) | ||
.map(Property::getValue) | ||
.map(String::valueOf) | ||
.collect(Collectors.toList()); | ||
assertTrue(actualAasIds.isEmpty()); | ||
} | ||
|
||
@Test(expected = InvalidTargetInformationException.class) | ||
public void testAdaptSubmodelElementCollectionWithInvalidStructure() { | ||
|
||
SubmodelElementCollection targetInformationSMC = new DefaultSubmodelElementCollection.Builder().idShort("targetInformation") | ||
.value(Collections.singletonList(new DefaultProperty.Builder().idShort("invalidElement").value("value").build())) | ||
.build(); | ||
|
||
aasRegistryTargetInformationAdapter.adapt(targetInformationSMC); | ||
} | ||
|
||
@Test(expected = InvalidTargetInformationException.class) | ||
public void testAdaptSubmodelElementCollectionWithoutAasIds() { | ||
|
||
SubmodelElementCollection targetInformationSMC = new DefaultSubmodelElementCollection.Builder().idShort("targetInformation") | ||
.value(Collections.emptyList()) | ||
.build(); | ||
|
||
aasRegistryTargetInformationAdapter.adapt(targetInformationSMC); | ||
} | ||
|
||
} |