Create 1.13.0 maven release
[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.hamcrest.MatcherAssert.assertThat;
27 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
28 import static org.junit.jupiter.api.Assertions.assertThrows;
29
30 import java.io.IOException;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33 import org.onap.aai.babel.util.ArtifactTestUtils;
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     /**
42      * Load the Widget mapping configuration.
43      *
44      * @throws IOException
45      *             if the mappings configuration cannot be loaded
46      */
47     @BeforeAll
48     public static void setup() throws IOException {
49         new ArtifactTestUtils().loadWidgetMappings();
50     }
51
52     @Test
53     public void testGetModels() {
54         assertThat(Model.getModelFor(null), is(nullValue()));
55         assertThat(Model.getModelFor(""), is(nullValue()));
56         assertThat(Model.getModelFor("any.unknown.type"), is(nullValue()));
57
58         assertMapping("org.openecomp.resource.vfc", WidgetType.valueOf("VSERVER"));
59         assertMapping("org.openecomp.resource.cp", WidgetType.valueOf("LINT"));
60         assertMapping("org.openecomp.cp", WidgetType.valueOf("LINT"));
61         assertMapping("org.openecomp.cp.some.suffix", WidgetType.valueOf("LINT"));
62         assertMapping("org.openecomp.resource.vl", WidgetType.valueOf("L3_NET"));
63         assertMapping("org.openecomp.resource.vf", WidgetType.valueOf("VF"));
64         assertMapping("org.openecomp.groups.vfmodule", WidgetType.valueOf("VFMODULE"));
65         assertMapping("org.openecomp.groups.VfModule", WidgetType.valueOf("VFMODULE"));
66         assertMapping("org.openecomp.resource.vfc.nodes.heat.cinder", WidgetType.valueOf("VOLUME"));
67         assertMapping("org.openecomp.nodes.PortMirroringConfiguration", "Configuration",
68                 WidgetType.valueOf("CONFIGURATION"));
69         assertMapping("any.string", "Configuration", WidgetType.valueOf("CONFIGURATION"));
70         assertMapping("org.openecomp.resource.cr.Kk1806Cr1", "CR", WidgetType.valueOf("CR"));
71         assertMapping("any.string", "CR", WidgetType.valueOf("CR"));
72
73         assertMapping("org.openecomp.resource.vfc", "an.unknown.type", WidgetType.valueOf("VSERVER"));
74     }
75
76     /**
77      * Test that there is no exception if processing a Model that has no metadata properties.
78      */
79     @Test
80     public void testNullIdentProperties() {
81         assertDoesNotThrow(() -> {
82             createTestModel().populateModelIdentificationInformation(null);
83         });
84     }
85
86     /**
87      * Test that an exception occurs if calling code passes an unsupported Widget Type value to the base implementation
88      * of the hasWidgetType() method.
89      */
90     @Test
91     public void testUnknownWidgetType() {
92         assertThrows(IllegalArgumentException.class, () -> {
93             createTestModel().hasWidgetType(null);
94         });
95     }
96
97     /**
98      * Create any Model with a valid WidgetType, for method testing.
99      *
100      * @return a valid Model for testing purposes
101      */
102     private Model createTestModel() {
103         return new Resource(WidgetType.valueOf("VSERVER"), false);
104     }
105
106     /**
107      * Assert that the TOSCA type String is mapped to the expected Widget Type.
108      *
109      * @param toscaType
110      *            the TOSCA type or prefix
111      * @param widgetType
112      *            the type of Widget expected from the mappings
113      */
114     private void assertMapping(String toscaType, WidgetType widgetType) {
115         assertThat(Model.getModelFor(toscaType).getWidgetType(), is(widgetType));
116     }
117
118     /**
119      * Assert that the TOSCA metadata type is mapped to the expected Widget Type.
120      *
121      * @param toscaType
122      *            the name (or name prefix) of the TOSCA type
123      * @param metadataType
124      *            the type specified in the TOSCA metadata
125      * @param widgetType
126      *            the type of Widget expected from the mappings
127      */
128     private void assertMapping(String toscaType, String metadataType, WidgetType widgetType) {
129         assertThat(Model.getModelFor(toscaType, metadataType).getWidgetType(), is(widgetType));
130     }
131
132 }