aed94453bdacd9c45914b31648ad45dd0d7c7bc9
[sdc/sdc-tosca.git] / sdc-tosca / src / test / java / org / onap / sdc / impl / ToscaParserDataTypeTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ================================================================================
4  * Copyright (C) 2019 Fujitsu Limited. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.sdc.impl;
21
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
25 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
26 import org.onap.sdc.tosca.parser.impl.SdcToscaParserFactory;
27 import org.onap.sdc.toscaparser.api.elements.DataType;
28 import org.onap.sdc.toscaparser.api.elements.PropertyDef;
29 import org.onap.sdc.toscaparser.api.elements.constraints.Schema;
30
31 import java.net.URL;
32 import java.util.HashSet;
33 import java.util.LinkedHashMap;
34
35 import static org.junit.Assert.assertThat;
36 import static org.hamcrest.CoreMatchers.is;
37 import static org.hamcrest.core.IsNull.notNullValue;
38
39 public class ToscaParserDataTypeTest {
40
41     private static ISdcCsarHelper helper = null;
42     private static final String TEST_DATATYPE_FILENAME = "csars/dataTypes-test-service.csar";
43     private static final String TEST_DATATYPE_TEST1 = "TestType1";
44     private static final String TEST_DATATYPE_TEST2 = "TestType2";
45     private static final String TEST_DATATYPE_PROPERTY_STR = "strdata";
46     private static final String TEST_DATATYPE_PROPERTY_INT = "intdata";
47     private static final String TEST_DATATYPE_PROPERTY_LIST = "listdata";
48     private static final String TEST_DATATYPE_PROPERTY_TYPE = "type";
49     private static final String TEST_DATATYPE_PROPERTY_ENTRY_SCHEMA = "entry_schema";
50
51     @BeforeClass
52     public static void setUpClass() {
53         try {
54             URL resource = GetEntityPortMirroringTest.class.getClassLoader()
55                     .getResource(TEST_DATATYPE_FILENAME);
56             if (resource != null) {
57                 helper = SdcToscaParserFactory.getInstance().getSdcCsarHelper(resource.getFile());
58             }
59
60         } catch (SdcToscaParserException e) {
61             e.printStackTrace();
62         }
63     }
64
65     @Test
66     public void getDataTypes() {
67         HashSet<DataType> dataTypes = helper.getDataTypes();
68         assertThat(dataTypes, notNullValue());
69         assertThat(dataTypes.size(), is(2));
70
71         for(DataType dataType: dataTypes){
72             LinkedHashMap<String, PropertyDef> properties;
73             PropertyDef property;
74             if(dataType.getType().equals(TEST_DATATYPE_TEST1)){
75                 properties = dataType.getAllProperties();
76                 property = properties.get(TEST_DATATYPE_PROPERTY_STR);
77                 assertThat(property,notNullValue());
78                 assertThat(property.getName(),is(TEST_DATATYPE_PROPERTY_STR));
79                 assertThat( property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE),is(Schema.STRING));
80             }
81             if(dataType.getType().equals(TEST_DATATYPE_TEST2)) {
82                 properties = dataType.getAllProperties();
83                 property = properties.get(TEST_DATATYPE_PROPERTY_INT);
84                 assertThat(property, notNullValue());
85                 assertThat(property.getName(), is(TEST_DATATYPE_PROPERTY_INT));
86                 assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE), is(Schema.INTEGER));
87
88                 property = properties.get(TEST_DATATYPE_PROPERTY_LIST);
89                 assertThat(property, notNullValue());
90                 assertThat(property.getName(), is(TEST_DATATYPE_PROPERTY_LIST));
91                 assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_TYPE), is(Schema.LIST));
92                 assertThat(property.getSchema().get(TEST_DATATYPE_PROPERTY_ENTRY_SCHEMA), is(TEST_DATATYPE_TEST1));
93             }
94         }
95     }
96
97
98 }