From 90f3477a68368f738b674e73d71b1f77f656ddfc Mon Sep 17 00:00:00 2001 From: Mohammad Ghazanfar Ali Danish <62088117+mdanish98@users.noreply.github.com> Date: Tue, 9 Apr 2024 14:09:49 +0200 Subject: [PATCH] Removes mandatory inclusion of Submodel IDs during serialization (#250) Signed-off-by: Mohammad Ghazanfar Ali Danish --- .../TestAASEnvironmentSerialization.java | 39 ++++++----- .../http/AasEnvironmentApiHTTPController.java | 13 +++- .../http/TestAasEnvironmentHTTP.java | 66 +++++++++++++++++-- 3 files changed, 94 insertions(+), 24 deletions(-) diff --git a/basyx.aasenvironment/basyx.aasenvironment-core/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/TestAASEnvironmentSerialization.java b/basyx.aasenvironment/basyx.aasenvironment-core/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/TestAASEnvironmentSerialization.java index b798a176c..e672c2902 100644 --- a/basyx.aasenvironment/basyx.aasenvironment-core/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/TestAASEnvironmentSerialization.java +++ b/basyx.aasenvironment/basyx.aasenvironment-core/src/test/java/org/eclipse/digitaltwin/basyx/aasenvironment/TestAASEnvironmentSerialization.java @@ -100,9 +100,11 @@ public static Collection createDummyShells() { @Test public void testAASEnviromentSerializationWithJSON() throws SerializationException, IOException, DeserializationException { boolean includeConceptDescription = true; + boolean aasIdsIncluded = true; + boolean submodelIdsIncluded = true; String jsonSerialization = aasEnvironment.createJSONAASEnvironmentSerialization(getShellIds(createDummyShells()), getSubmodelIds(createDummySubmodels()), includeConceptDescription); - validateJSON(jsonSerialization, includeConceptDescription); + validateJSON(jsonSerialization, aasIdsIncluded, submodelIdsIncluded, includeConceptDescription); validateRepositoriesState(); } @@ -110,9 +112,11 @@ public void testAASEnviromentSerializationWithJSON() throws SerializationExcepti @Test public void testAASEnviromentSerializationWithXML() throws SerializationException, IOException, SAXException, DeserializationException { boolean includeConceptDescription = true; + boolean aasIdsIncluded = true; + boolean submodelIdsIncluded = true; String xmlSerialization = aasEnvironment.createXMLAASEnvironmentSerialization(getShellIds(createDummyShells()), getSubmodelIds(createDummySubmodels()), includeConceptDescription); - validateXml(xmlSerialization, includeConceptDescription); + validateXml(xmlSerialization, aasIdsIncluded, submodelIdsIncluded, includeConceptDescription); validateRepositoriesState(); } @@ -120,9 +124,11 @@ public void testAASEnviromentSerializationWithXML() throws SerializationExceptio @Test public void testAASEnviromentSerializationWithAASX() throws SerializationException, IOException, InvalidOperationException, InvalidFormatException, DeserializationException { boolean includeConceptDescription = true; + boolean aasIdsIncluded = true; + boolean submodelIdsIncluded = true; byte[] serialization = aasEnvironment.createAASXAASEnvironmentSerialization(getShellIds(createDummyShells()), getSubmodelIds(createDummySubmodels()), includeConceptDescription); - checkAASX(new ByteArrayInputStream(serialization), includeConceptDescription); + checkAASX(new ByteArrayInputStream(serialization), aasIdsIncluded, submodelIdsIncluded, includeConceptDescription); validateRepositoriesState(); } @@ -130,31 +136,33 @@ public void testAASEnviromentSerializationWithAASX() throws SerializationExcepti @Test public void testAASEnviromentSerializationWithJSONExcludeCD() throws SerializationException, IOException, DeserializationException { boolean includeConceptDescription = false; + boolean aasIdsIncluded = true; + boolean submodelIdsIncluded = true; String jsonSerialization = aasEnvironment.createJSONAASEnvironmentSerialization(getShellIds(createDummyShells()), getSubmodelIds(createDummySubmodels()), includeConceptDescription); - validateJSON(jsonSerialization, includeConceptDescription); + validateJSON(jsonSerialization, aasIdsIncluded, submodelIdsIncluded, includeConceptDescription); validateRepositoriesState(); } - public static void validateJSON(String actual, boolean includeConceptDescription) throws DeserializationException { + public static void validateJSON(String actual, boolean areAASsIncluded, boolean areSubmodelsIncluded, boolean includeConceptDescription) throws DeserializationException { JsonDeserializer jsonDeserializer = new JsonDeserializer(); Environment aasEnvironment = jsonDeserializer.read(actual, Environment.class); - checkAASEnvironment(aasEnvironment, includeConceptDescription); + checkAASEnvironment(aasEnvironment, areAASsIncluded, areSubmodelsIncluded, includeConceptDescription); } - public static void validateXml(String actual, boolean includeConceptDescription) throws DeserializationException { + public static void validateXml(String actual, boolean areAASsIncluded, boolean areSubmodelsIncluded, boolean includeConceptDescription) throws DeserializationException { XmlDeserializer xmlDeserializer = new XmlDeserializer(); Environment aasEnvironment = xmlDeserializer.read(actual); - checkAASEnvironment(aasEnvironment, includeConceptDescription); + checkAASEnvironment(aasEnvironment, areAASsIncluded, areSubmodelsIncluded, includeConceptDescription); } - public static void checkAASX(InputStream inputStream, boolean includeConceptDescription) throws IOException, InvalidFormatException, DeserializationException { + public static void checkAASX(InputStream inputStream, boolean areAASsIncluded, boolean areSubmodelsIncluded, boolean includeConceptDescription) throws IOException, InvalidFormatException, DeserializationException { AASXDeserializer aasxDeserializer = new AASXDeserializer(inputStream); Environment environment = aasxDeserializer.read(); - checkAASEnvironment(environment, includeConceptDescription); + checkAASEnvironment(environment, areAASsIncluded, areSubmodelsIncluded, includeConceptDescription); inputStream.close(); } @@ -168,14 +176,15 @@ public static Collection createDummyConceptDescriptions() { return conceptDescriptions; } - private static void checkAASEnvironment(Environment aasEnvironment, boolean areConceptDescriptionsIncluded) { - assertAasIds(aasEnvironment); + private static void checkAASEnvironment(Environment aasEnvironment, boolean areAASsIncluded, boolean areSubmodelsIncluded, boolean areConceptDescriptionsIncluded) { + if (areAASsIncluded) + assertAasIds(aasEnvironment); - assertSubmodelIds(aasEnvironment); + if (areSubmodelsIncluded) + assertSubmodelIds(aasEnvironment); - if (areConceptDescriptionsIncluded) { + if (areConceptDescriptionsIncluded) assertConceptDescriptionIds(aasEnvironment); - } } private static void assertConceptDescriptionIds(Environment aasEnvironment) { diff --git a/basyx.aasenvironment/basyx.aasenvironment-http/src/main/java/org/eclipse/digitaltwin/basyx/aasenvironment/http/AasEnvironmentApiHTTPController.java b/basyx.aasenvironment/basyx.aasenvironment-http/src/main/java/org/eclipse/digitaltwin/basyx/aasenvironment/http/AasEnvironmentApiHTTPController.java index cc227b308..3a1819c1a 100644 --- a/basyx.aasenvironment/basyx.aasenvironment-http/src/main/java/org/eclipse/digitaltwin/basyx/aasenvironment/http/AasEnvironmentApiHTTPController.java +++ b/basyx.aasenvironment/basyx.aasenvironment-http/src/main/java/org/eclipse/digitaltwin/basyx/aasenvironment/http/AasEnvironmentApiHTTPController.java @@ -121,16 +121,25 @@ public ResponseEntity uploadEnvironment(MultipartFile envFile) { private List getOriginalIds(List ids) { List results = new ArrayList<>(); + + if (!areValidIds(ids)) + return results; + ids.forEach(id -> { results.add(Base64UrlEncodedIdentifier.fromEncodedValue(id).getIdentifier()); }); + return results; } private boolean areParametersValid(String accept, @Valid List aasIds, @Valid List submodelIds) { - if (aasIds.isEmpty() || submodelIds.isEmpty()) { + if (!areValidIds(aasIds) && !areValidIds(submodelIds)) return false; - } + return (accept.equals(ACCEPT_AASX) || accept.equals(ACCEPT_JSON) || accept.equals(ACCEPT_XML)); } + + private boolean areValidIds(List identifiers) { + return identifiers != null && !identifiers.isEmpty(); + } } diff --git a/basyx.aasenvironment/basyx.aasenvironment-http/src/test/java/org/eclipse/basyx/digitaltwin/aasenvironment/http/TestAasEnvironmentHTTP.java b/basyx.aasenvironment/basyx.aasenvironment-http/src/test/java/org/eclipse/basyx/digitaltwin/aasenvironment/http/TestAasEnvironmentHTTP.java index 51e64b3ff..a79bd2e30 100644 --- a/basyx.aasenvironment/basyx.aasenvironment-http/src/test/java/org/eclipse/basyx/digitaltwin/aasenvironment/http/TestAasEnvironmentHTTP.java +++ b/basyx.aasenvironment/basyx.aasenvironment-http/src/test/java/org/eclipse/basyx/digitaltwin/aasenvironment/http/TestAasEnvironmentHTTP.java @@ -96,41 +96,73 @@ public void baSyxResponseHeader() throws IOException, ProtocolException { @Test public void testAASEnvironmentSertializationWithJSON() throws IOException, ParseException, DeserializationException { boolean includeConceptDescription = true; + boolean aasIdsIncluded = true; + boolean submodelIdsIncluded = true; CloseableHttpResponse response = executeGetOnURL(createSerializationURL(includeConceptDescription), JSON_MIMETYPE); String actual = BaSyxHttpTestUtils.getResponseAsString(response); - TestAASEnvironmentSerialization.validateJSON(actual, includeConceptDescription); + TestAASEnvironmentSerialization.validateJSON(actual, aasIdsIncluded, submodelIdsIncluded, includeConceptDescription); } @Test public void testAASEnvironmentSertializationWithXML() throws IOException, ParseException, DeserializationException { boolean includeConceptDescription = true; + boolean aasIdsIncluded = true; + boolean submodelIdsIncluded = true; CloseableHttpResponse response = executeGetOnURL(createSerializationURL(includeConceptDescription), XML_MIMETYPE); String actual = BaSyxHttpTestUtils.getResponseAsString(response); - TestAASEnvironmentSerialization.validateXml(actual, includeConceptDescription); + TestAASEnvironmentSerialization.validateXml(actual, aasIdsIncluded, submodelIdsIncluded, includeConceptDescription); } @Test public void testAASEnvironmentSertializationWithAASX() throws IOException, ParseException, DeserializationException, InvalidFormatException { boolean includeConceptDescription = true; + boolean aasIdsIncluded = true; + boolean submodelIdsIncluded = true; CloseableHttpResponse response = executeGetOnURL(createSerializationURL(includeConceptDescription), AASX_MIMETYPE); assertEquals(HttpStatus.OK.value(), response.getCode()); - TestAASEnvironmentSerialization.checkAASX(response.getEntity() - .getContent(), includeConceptDescription); + TestAASEnvironmentSerialization.checkAASX(response.getEntity().getContent(), aasIdsIncluded, submodelIdsIncluded, includeConceptDescription); } @Test public void testAASEnvironmentSertializationWithAASXExcludeCD() throws IOException, ParseException, DeserializationException, InvalidFormatException { boolean includeConceptDescription = false; + boolean aasIdsIncluded = true; + boolean submodelIdsIncluded = true; CloseableHttpResponse response = executeGetOnURL(createSerializationURL(includeConceptDescription), AASX_MIMETYPE); assertEquals(HttpStatus.OK.value(), response.getCode()); - TestAASEnvironmentSerialization.checkAASX(response.getEntity() - .getContent(), includeConceptDescription); + TestAASEnvironmentSerialization.checkAASX(response.getEntity().getContent(), aasIdsIncluded, submodelIdsIncluded, includeConceptDescription); + } + + @Test + public void aasEnvironmentSertializationOnlyAasIds() throws IOException, ParseException, DeserializationException { + boolean includeConceptDescription = false; + boolean aasIdsIncluded = true; + boolean submodelIdsIncluded = false; + + CloseableHttpResponse response = executeGetOnURL(getSerializationURLOnlyAas(createIdCollection(DummyAASEnvironmentComponent.AAS_TECHNICAL_DATA_ID, DummyAASEnvironmentComponent.AAS_OPERATIONAL_DATA_ID), includeConceptDescription), JSON_MIMETYPE); + assertEquals(HttpStatus.OK.value(), response.getCode()); + + String actual = BaSyxHttpTestUtils.getResponseAsString(response); + TestAASEnvironmentSerialization.validateJSON(actual, aasIdsIncluded, submodelIdsIncluded, includeConceptDescription); + } + + @Test + public void aasEnvironmentSertializationOnlySubmodelIds() throws IOException, ParseException, DeserializationException { + boolean includeConceptDescription = true; + boolean aasIdsIncluded = false; + boolean submodelIdsIncluded = true; + + CloseableHttpResponse response = executeGetOnURL(getSerializationURLOnlySubmodels(createIdCollection(DummyAASEnvironmentComponent.SUBMODEL_OPERATIONAL_DATA_ID, DummyAASEnvironmentComponent.SUBMODEL_TECHNICAL_DATA_ID), includeConceptDescription), JSON_MIMETYPE); + assertEquals(HttpStatus.OK.value(), response.getCode()); + + String actual = BaSyxHttpTestUtils.getResponseAsString(response); + TestAASEnvironmentSerialization.validateJSON(actual, aasIdsIncluded, submodelIdsIncluded, includeConceptDescription); } @Test @@ -237,7 +269,27 @@ public static String getSerializationURL(Collection aasIds, Collection