4c5ff20082062525f234fca3b78b42d2621b87ec
[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 Widget(Type.OAM_NETWORK, "oam-network", true);
50     private Model anonymousModel;
51
52     static {
53         System.setProperty("APP_HOME", ".");
54     }
55
56     /**
57      * Initialize 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,
72                             ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE));
73         }
74
75         ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(configLocation);
76         utils.loadWidgetToUuidMappings();
77
78         anonymousModel = new Model() {
79             @Override
80             public boolean addResource(Resource resource) {
81                 return false;
82             }
83
84             @Override
85             public boolean addWidget(Widget resource) {
86                 return false;
87             }
88
89             @Override
90             public Type getWidgetType() {
91                 return null;
92             }
93
94             @Override
95             public Map<String, Object> getProperties() {
96                 return Collections.emptyMap();
97             }
98
99             @Override
100             public boolean isResource() {
101                 return false;
102             }
103         };
104     }
105
106     @Test
107     public void testGetModels() {
108         assertThat(Model.getModelFor(null), is(nullValue()));
109         assertThat(Model.getModelFor(""), is(nullValue()));
110         assertThat(Model.getModelFor("any.unknown.type"), is(nullValue()));
111
112         assertMapping("org.openecomp.resource.vfc", Type.VSERVER);
113         assertMapping("org.openecomp.resource.cp", Type.LINT);
114         assertMapping("org.openecomp.cp", Type.LINT);
115         assertMapping("org.openecomp.cp.some.suffix", Type.LINT);
116         assertMapping("org.openecomp.resource.vl", Type.L3_NET);
117         assertMapping("org.openecomp.resource.vf", Type.VF);
118         assertMapping("org.openecomp.groups.vfmodule", Type.VFMODULE);
119         assertMapping("org.openecomp.groups.VfModule", Type.VFMODULE);
120         assertMapping("org.openecomp.resource.vfc.nodes.heat.cinder", Type.VOLUME);
121         assertMapping("org.openecomp.nodes.PortMirroringConfiguration", "Configuration", Type.CONFIGURATION);
122         assertMapping("any.string", "Configuration", Type.CONFIGURATION);
123         assertMapping("org.openecomp.resource.cr.Kk1806Cr1", "CR", Type.CR);
124         assertMapping("any.string", "CR", Type.CR);
125
126         assertMapping("org.openecomp.resource.vfc", "an.unknown.type", Type.VSERVER);
127     }
128
129     /**
130      * Assert that the TOSCA type String is mapped to the expected Widget Type.
131      * 
132      * @param toscaType
133      *            the TOSCA type or prefix
134      * @param widgetType
135      *            the type of Widget expected from the mappings
136      */
137     private void assertMapping(String toscaType, Type widgetType) {
138         assertThat(Model.getModelFor(toscaType).getWidgetType(), is(widgetType));
139     }
140
141     /**
142      * Assert that the TOSCA metadata type is mapped to the expected Widget Type.
143      * 
144      * @param toscaType
145      *            the name (or name prefix) of the TOSCA type
146      * @param metadataType
147      *            the type specified in the TOSCA metadata
148      * @param widgetType
149      *            the type of Widget expected from the mappings
150      */
151     private void assertMapping(String toscaType, String metadataType, Type widgetType) {
152         assertThat(Model.getModelFor(toscaType, metadataType).getWidgetType(), is(widgetType));
153     }
154
155     @Test
156     public void testGetModelType() {
157         assertThat(serviceModel.getModelType(), is(ModelType.SERVICE));
158         for (Resource resourceModel : resourceModels) {
159             assertThat(resourceModel.getModelType(), is(ModelType.RESOURCE));
160         }
161         assertThat(widgetModel.getModelType(), is(ModelType.WIDGET));
162         assertThat(anonymousModel.getModelType(), is(nullValue()));
163     }
164
165     @Test
166     public void testGetModelNameVersionId() {
167         assertThat(anonymousModel.getModelNameVersionId(), is(nullValue()));
168     }
169
170     @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class)
171     public void testGetModelNameVersionIdIsUnsupported() {
172         assertThat(widgetModel.getModelNameVersionId(), is(nullValue()));
173         assertThat(resourceModels.get(0).getModelType(), is(ModelType.RESOURCE));
174         assertThat(widgetModel.getModelType(), is(ModelType.WIDGET));
175         assertThat(anonymousModel.getModelType(), is(nullValue()));
176     }
177
178 }