Re-implement model type value for Resource Mapping
[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 org.junit.Before;
30 import org.junit.Test;
31 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
32 import org.onap.aai.babel.util.ArtifactTestUtils;
33 import org.onap.aai.babel.xml.generator.model.Widget.Type;
34
35 /**
36  * Direct tests of the Model abstract class (to improve code coverage). Not all methods are tested here. Some are
37  * covered by the tests of derived classes.
38  */
39 public class TestModel {
40
41     private Widget widgetModel = new Widget(Type.OAM_NETWORK, "oam-network", true);
42     private Model anonymousModel;
43
44     /**
45      * Initialize the Artifact Generator with filtering and mapping configuration. Also load the Widget to UUID mappings
46      * from the Artifact Generator properties.
47      *
48      * @throws IOException
49      *             if the mappings configuration cannot be loaded
50      */
51     @Before
52     public void setup() throws IOException {
53         ArtifactTestUtils utils = new ArtifactTestUtils();
54         utils.setGeneratorSystemProperties();
55
56         String configLocation = System.getProperty(ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE);
57         if (configLocation == null) {
58             throw new IllegalArgumentException(
59                     String.format(ArtifactGeneratorToscaParser.GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND,
60                             ArtifactGeneratorToscaParser.PROPERTY_TOSCA_MAPPING_FILE));
61         }
62
63         ArtifactGeneratorToscaParser.initToscaMappingsConfiguration(configLocation);
64         utils.loadWidgetToUuidMappings();
65
66         anonymousModel = new Model() {
67             @Override
68             public boolean addWidget(Widget resource) {
69                 return false;
70             }
71
72             @Override
73             public Type getWidgetType() {
74                 return null;
75             }
76
77             @Override
78             public String getModelTypeName() {
79                 return null;
80             }
81         };
82     }
83
84     @Test
85     public void testGetModels() {
86         assertThat(Model.getModelFor(null), is(nullValue()));
87         assertThat(Model.getModelFor(""), is(nullValue()));
88         assertThat(Model.getModelFor("any.unknown.type"), is(nullValue()));
89
90         assertMapping("org.openecomp.resource.vfc", Type.VSERVER);
91         assertMapping("org.openecomp.resource.cp", Type.LINT);
92         assertMapping("org.openecomp.cp", Type.LINT);
93         assertMapping("org.openecomp.cp.some.suffix", Type.LINT);
94         assertMapping("org.openecomp.resource.vl", Type.L3_NET);
95         assertMapping("org.openecomp.resource.vf", Type.VF);
96         assertMapping("org.openecomp.groups.vfmodule", Type.VFMODULE);
97         assertMapping("org.openecomp.groups.VfModule", Type.VFMODULE);
98         assertMapping("org.openecomp.resource.vfc.nodes.heat.cinder", Type.VOLUME);
99         assertMapping("org.openecomp.nodes.PortMirroringConfiguration", "Configuration", Type.CONFIGURATION);
100         assertMapping("any.string", "Configuration", Type.CONFIGURATION);
101         assertMapping("org.openecomp.resource.cr.Kk1806Cr1", "CR", Type.CR);
102         assertMapping("any.string", "CR", Type.CR);
103
104         assertMapping("org.openecomp.resource.vfc", "an.unknown.type", Type.VSERVER);
105     }
106
107     /**
108      * Assert that the TOSCA type String is mapped to the expected Widget Type.
109      * 
110      * @param toscaType
111      *            the TOSCA type or prefix
112      * @param widgetType
113      *            the type of Widget expected from the mappings
114      */
115     private void assertMapping(String toscaType, Type widgetType) {
116         assertThat(Model.getModelFor(toscaType).getWidgetType(), is(widgetType));
117     }
118
119     /**
120      * Assert that the TOSCA metadata type is mapped to the expected Widget Type.
121      * 
122      * @param toscaType
123      *            the name (or name prefix) of the TOSCA type
124      * @param metadataType
125      *            the type specified in the TOSCA metadata
126      * @param widgetType
127      *            the type of Widget expected from the mappings
128      */
129     private void assertMapping(String toscaType, String metadataType, Type widgetType) {
130         assertThat(Model.getModelFor(toscaType, metadataType).getWidgetType(), is(widgetType));
131     }
132
133     @Test
134     public void testGetModelNameVersionId() {
135         assertThat(anonymousModel.getModelNameVersionId(), is(nullValue()));
136     }
137
138     @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class)
139     public void testGetModelNameVersionIdIsUnsupported() {
140         assertThat(widgetModel.getModelNameVersionId(), is(nullValue()));
141     }
142
143 }