|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e. V. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.adminshell.aas.v3.dataformat.json; |
| 17 | + |
| 18 | +import io.adminshell.aas.v3.dataformat.DeserializationException; |
| 19 | +import io.adminshell.aas.v3.dataformat.Deserializer; |
| 20 | +import io.adminshell.aas.v3.model.*; |
| 21 | +import io.adminshell.aas.v3.model.impl.DefaultProperty; |
| 22 | +import io.adminshell.aas.v3.model.impl.DefaultSubmodel; |
| 23 | +import org.junit.Test; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | + |
| 27 | +import java.io.File; |
| 28 | +import java.io.IOException; |
| 29 | +import java.nio.file.Files; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +import static org.junit.Assert.assertEquals; |
| 34 | + |
| 35 | +public class JsonReferableDeserializerTest { |
| 36 | + |
| 37 | + private static final Logger logger = LoggerFactory.getLogger(JsonReferableDeserializerTest.class); |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testReadAAS() throws IOException, DeserializationException { |
| 41 | + File fileExpected = new File("src/test/resources/assetAdministrationShell.json"); |
| 42 | + String expected = Files.readString(fileExpected.toPath()); |
| 43 | + AssetAdministrationShell aas = new JsonDeserializer().readReferable(expected, AssetAdministrationShell.class); |
| 44 | + AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT; |
| 45 | + AssetAdministrationShell aasExpected = environment.getAssetAdministrationShells().get(0); |
| 46 | + |
| 47 | + assertEquals(aasExpected, aas); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testReadAASs() throws IOException, DeserializationException { |
| 52 | + File fileExpected = new File("src/test/resources/assetAdministrationShellList.json"); |
| 53 | + String expected = Files.readString(fileExpected.toPath()); |
| 54 | + List<AssetAdministrationShell> aas = new JsonDeserializer().readReferables(expected, AssetAdministrationShell.class); |
| 55 | + AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT; |
| 56 | + List<AssetAdministrationShell> aasExpected = Arrays.asList(environment.getAssetAdministrationShells().get(0) |
| 57 | + ,environment.getAssetAdministrationShells().get(1)) ; |
| 58 | + |
| 59 | + assertEquals(aasExpected, aas); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testReadSubmodel() throws IOException, DeserializationException { |
| 64 | + File fileExpected = new File("src/test/resources/submodel.json"); |
| 65 | + String expected = Files.readString(fileExpected.toPath()); |
| 66 | + Submodel submodel = new JsonDeserializer().readReferable(expected,Submodel.class); |
| 67 | + AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT; |
| 68 | + Submodel submodelExpected = environment.getSubmodels().get(0); |
| 69 | + |
| 70 | + assertEquals(submodelExpected, submodel); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testReadSubmodels() throws IOException, DeserializationException { |
| 75 | + File fileExpected = new File("src/test/resources/submodelList.json"); |
| 76 | + String expected = Files.readString(fileExpected.toPath()); |
| 77 | + List<Submodel> submodels = new JsonDeserializer().readReferables(expected,Submodel.class); |
| 78 | + AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT; |
| 79 | + List<Submodel> submodelsExpected = Arrays.asList(environment.getSubmodels().get(0),environment.getSubmodels().get(1)); |
| 80 | + |
| 81 | + assertEquals(submodelsExpected, submodels); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testReadSubmodelElement() throws IOException, DeserializationException { |
| 86 | + File fileExpected = new File("src/test/resources/submodelElement.json"); |
| 87 | + String expected = Files.readString(fileExpected.toPath()); |
| 88 | + SubmodelElement submodelElement = new JsonDeserializer().readReferable(expected,SubmodelElement.class); |
| 89 | + AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT; |
| 90 | + SubmodelElement submodelElementExpected = environment.getSubmodels().get(0).getSubmodelElements().get(0); |
| 91 | + |
| 92 | + assertEquals(submodelElementExpected, submodelElement); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testReadSubmodelElements() throws IOException, DeserializationException { |
| 97 | + File fileExpected = new File("src/test/resources/submodelElementList.json"); |
| 98 | + String expected = Files.readString(fileExpected.toPath()); |
| 99 | + List<SubmodelElement> submodelElements = new JsonDeserializer().readReferables(expected,SubmodelElement.class); |
| 100 | + AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT; |
| 101 | + List<SubmodelElement> submodelElementsExpected = Arrays.asList( |
| 102 | + environment.getSubmodels().get(0).getSubmodelElements().get(0), |
| 103 | + environment.getSubmodels().get(0).getSubmodelElements().get(1)); ; |
| 104 | + |
| 105 | + assertEquals(submodelElementsExpected, submodelElements); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testReadSubmodelElementCollection() throws IOException, DeserializationException { |
| 110 | + File fileExpected = new File("src/test/resources/submodelElementCollection.json"); |
| 111 | + String expected = Files.readString(fileExpected.toPath()); |
| 112 | + SubmodelElementCollection submodelElementCollection = new JsonDeserializer().readReferable(expected,SubmodelElementCollection.class); |
| 113 | + AssetAdministrationShellEnvironment environment = AASFull.ENVIRONMENT; |
| 114 | + SubmodelElement submodelElementCollectionExpected = environment.getSubmodels().get(6).getSubmodelElements().get(5); ; |
| 115 | + |
| 116 | + assertEquals(submodelElementCollectionExpected, submodelElementCollection); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments