c564a93f39121f35077d38956345ef022599666d
[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 (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright (c) 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     /**
53      * Initialize the Artifact Generator with filtering and mapping configuration. Also load the Widget to UUID mappings
54      * from the Artifact Generator properties.
55      *
56      * @throws IOException
57      *             if the mappings configuration cannot be loaded
58      */
59     @Before
60     public void setup() throws IOException {
61         ArtifactTestUtils utils = new ArtifactTestUtils();
62         utils.setGeneratorSystemProperties();
63
64         String configLocation = System.getProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE);
65         if (configLocation == null) {
66             throw new IllegalArgumentException(
67                     String.format(ArtifactGeneratorToscaParser.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND,
68                             ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE));
69         }
70
71         ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(configLocation);
72         utils.loadWidgetToUuidMappings();
73
74         anonymousModel = new Model() {
75             @Override
76             public boolean addResource(Resource resource) {
77                 return false;
78             }
79
80             @Override
81             public boolean addWidget(Widget resource) {
82                 return false;
83             }
84
85             @Override
86             public Type getWidgetType() {
87                 return null;
88             }
89
90             @Override
91             public Map<String, Object> getProperties() {
92                 return Collections.emptyMap();
93             }
94
95             @Override
96             public boolean isResource() {
97                 return false;
98             }
99         };
100     }
101
102     @Test
103     public void testGetModels() {
104         assertThat(Model.getModelFor(null), is(nullValue()));
105         assertThat(Model.getModelFor(""), is(nullValue()));
106         assertThat(Model.getModelFor("any.unknown.type"), is(nullValue()));
107
108         assertMapping("org.openecomp.resource.vfc", Type.VSERVER);
109         assertMapping("org.openecomp.resource.cp", Type.LINT);
110         assertMapping("org.openecomp.cp", Type.LINT);
111         assertMapping("org.openecomp.cp.some.suffix", Type.LINT);
112         assertMapping("org.openecomp.resource.vl", Type.L3_NET);
113         assertMapping("org.openecomp.resource.vf", Type.VF);
114         assertMapping("org.openecomp.groups.vfmodule", Type.VFMODULE);
115         assertMapping("org.openecomp.groups.VfModule", Type.VFMODULE);
116         assertMapping("org.openecomp.resource.vfc.nodes.heat.cinder", Type.VOLUME);
117         assertMapping("org.openecomp.nodes.PortMirroringConfiguration", "Configuration", Type.CONFIGURATION);
118         assertMapping("any.string", "Configuration", Type.CONFIGURATION);
119         assertMapping("org.openecomp.resource.cr.Kk1806Cr1", "CR", Type.CR);
120         assertMapping("any.string", "CR", Type.CR);
121
122         assertMapping("org.openecomp.resource.vfc", "an.unknown.type", Type.VSERVER);
123     }
124
125     /**
126      * Assert that the TOSCA type String is mapped to the expected Widget Type.
127      * 
128      * @param toscaType
129      *            the TOSCA type or prefix
130      * @param widgetType
131      *            the type of Widget expected from the mappings
132      */
133     private void assertMapping(String toscaType, Type widgetType) {
134         assertThat(Model.getModelFor(toscaType).getWidgetType(), is(widgetType));
135     }
136
137     /**
138      * Assert that the TOSCA metadata type is mapped to the expected Widget Type.
139      * 
140      * @param toscaType
141      *            the name (or name prefix) of the TOSCA type
142      * @param metadataType
143      *            the type specified in the TOSCA metadata
144      * @param widgetType
145      *            the type of Widget expected from the mappings
146      */
147     private void assertMapping(String toscaType, String metadataType, Type widgetType) {
148         assertThat(Model.getModelFor(toscaType, metadataType).getWidgetType(), is(widgetType));
149     }
150
151     @Test
152     public void testGetModelType() {
153         assertThat(serviceModel.getModelType(), is(ModelType.SERVICE));
154         for (Resource resourceModel : resourceModels) {
155             assertThat(resourceModel.getModelType(), is(ModelType.RESOURCE));
156         }
157         assertThat(widgetModel.getModelType(), is(ModelType.WIDGET));
158         assertThat(anonymousModel.getModelType(), is(nullValue()));
159     }
160
161     @Test
162     public void testGetModelNameVersionId() {
163         assertThat(anonymousModel.getModelNameVersionId(), is(nullValue()));
164     }
165
166     @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class)
167     public void testGetModelNameVersionIdIsUnsupported() {
168         assertThat(widgetModel.getModelNameVersionId(), is(nullValue()));
169         assertThat(resourceModels.get(0).getModelType(), is(ModelType.RESOURCE));
170         assertThat(widgetModel.getModelType(), is(ModelType.WIDGET));
171         assertThat(anonymousModel.getModelType(), is(nullValue()));
172     }
173
174 }