Rename the groupfilter.config System Property
[aai/babel.git] / src / test / java / org / onap / aai / babel / xml / generator / model / TestModel.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2019 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.aai.babel.xml.generator.model;
23
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.nullValue;
26 import static org.junit.Assert.assertThat;
27
28 import java.io.IOException;
29 import java.util.Arrays;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Map;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
36 import org.onap.aai.babel.util.ArtifactTestUtils;
37 import org.onap.aai.babel.xml.generator.model.Widget.Type;
38 import org.onap.aai.babel.xml.generator.types.ModelType;
39
40 /**
41  * Direct tests of the Model abstract class (to improve code coverage). Not all methods are tested here. Some are
42  * covered by the tests of derived classes.
43  */
44 public class TestModel {
45
46     private Service serviceModel = new Service();
47     private List<Resource> resourceModels =
48             Arrays.asList(new Resource(Type.CR, true), new Resource(Type.INSTANCE_GROUP, true));
49     private Widget widgetModel = new OamNetwork();
50     private Model anonymousModel;
51
52     static {
53         System.setProperty("APP_HOME", ".");
54     }
55
56     /**
57      * Initialise the Artifact Generator with filtering and mapping configuration. Also Load the Widget to UUID mappings
58      * from the Artifact Generator properties.
59      *
60      * @throws IOException
61      *     if the Artifact Generator properties file is not loaded
62      */
63     @Before
64     public void setup() throws IOException {
65         ArtifactTestUtils utils = new ArtifactTestUtils();
66         utils.setGeneratorSystemProperties();
67
68         String configLocation = System.getProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE);
69         if (configLocation == null) {
70             throw new IllegalArgumentException(
71                     String.format(ArtifactGeneratorToscaParser.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND, ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE));
72         }
73         
74         ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(configLocation);
75         utils.loadWidgetToUuidMappings();
76
77         anonymousModel = new Model() {
78             @Override
79             public boolean addResource(Resource resource) {
80                 return false;
81             }
82
83             @Override
84             public boolean addWidget(Widget resource) {
85                 return false;
86             }
87
88             @Override
89             public Type getWidgetType() {
90                 return null;
91             }
92
93             @Override
94             public Map<String, Object> getProperties() {
95                 return Collections.emptyMap();
96             }
97
98             @Override
99             public boolean isResource() {
100                 return false;
101             }
102         };
103     }
104
105     @Test
106     public void testGetModels() {
107         assertThat(Model.getModelFor(null), is(nullValue()));
108         assertThat(Model.getModelFor(""), is(nullValue()));
109         assertThat(Model.getModelFor("any.unknown.type"), is(nullValue()));
110
111         assertMapping("org.openecomp.resource.vfc", Type.VSERVER);
112         assertMapping("org.openecomp.resource.cp", Type.LINT);
113         assertMapping("org.openecomp.cp", Type.LINT);
114         assertMapping("org.openecomp.cp.some.suffix", Type.LINT);
115         assertMapping("org.openecomp.resource.vl", Type.L3_NET);
116         assertMapping("org.openecomp.resource.vf", Type.VF);
117         assertMapping("org.openecomp.groups.vfmodule", Type.VFMODULE);
118         assertMapping("org.openecomp.groups.VfModule", Type.VFMODULE);
119         assertMapping("org.openecomp.resource.vfc.nodes.heat.cinder", Type.VOLUME);
120         assertMapping("org.openecomp.nodes.PortMirroringConfiguration", "Configuration", Type.CONFIGURATION);
121         assertMapping("any.string", "Configuration", Type.CONFIGURATION);
122         assertMapping("org.openecomp.resource.cr.Kk1806Cr1", "CR", Type.CR);
123         assertMapping("any.string", "CR", Type.CR);
124
125         assertMapping("org.openecomp.resource.vfc", "an.unknown.type", Type.VSERVER);
126     }
127
128     /**
129      * Assert that the TOSCA type String is mapped to the expected Widget Type.
130      * 
131      * @param toscaType
132      *     the TOSCA type or prefix
133      * @param widgetType
134      *     the type of Widget expected from the mappings
135      */
136     private void assertMapping(String toscaType, Type widgetType) {
137         assertThat(Model.getModelFor(toscaType).getWidgetType(), is(widgetType));
138     }
139
140     /**
141      * Assert that the TOSCA metadata type is mapped to the expected Widget Type.
142      * 
143      * @param toscaType
144      *     the name (or name prefix) of the TOSCA type
145      * @param metadataType
146      *     the type specified in the TOSCA metadata
147      * @param widgetType
148      *     the type of Widget expected from the mappings
149      */
150     private void assertMapping(String toscaType, String metadataType, Type widgetType) {
151         assertThat(Model.getModelFor(toscaType, metadataType).getWidgetType(), is(widgetType));
152     }
153
154     @Test
155     public void testGetModelType() {
156         assertThat(serviceModel.getModelType(), is(ModelType.SERVICE));
157         for (Resource resourceModel : resourceModels) {
158             assertThat(resourceModel.getModelType(), is(ModelType.RESOURCE));
159         }
160         assertThat(widgetModel.getModelType(), is(ModelType.WIDGET));
161         assertThat(anonymousModel.getModelType(), is(nullValue()));
162     }
163
164     @Test
165     public void testGetModelNameVersionId() {
166         assertThat(anonymousModel.getModelNameVersionId(), is(nullValue()));
167     }
168
169     @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class)
170     public void testGetModelNameVersionIdIsUnsupported() {
171         assertThat(widgetModel.getModelNameVersionId(), is(nullValue()));
172         assertThat(resourceModels.get(0).getModelType(), is(ModelType.RESOURCE));
173         assertThat(widgetModel.getModelType(), is(ModelType.WIDGET));
174         assertThat(anonymousModel.getModelType(), is(nullValue()));
175     }
176
177 }