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