Refactor Widget creation methods
[aai/babel.git] / src / test / java / org / onap / aai / babel / xml / generator / model / TestWidget.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.Collections;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.onap.aai.babel.util.ArtifactTestUtils;
33 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
34 import org.onap.aai.babel.xml.generator.types.ModelType;
35
36 /**
37  * Direct tests of the Widget class for code coverage.
38  */
39 public class TestWidget {
40
41     /**
42      * Load the Widget Configuration, including the type mappings and the UUID mappings.
43      *
44      * @throws IOException
45      *             if the mappings configuration cannot be loaded
46      */
47     @BeforeClass
48     public static void setup() throws IOException {
49         ArtifactTestUtils util = new ArtifactTestUtils();
50         util.loadWidgetToUuidMappings();
51         util.loadWidgetMappings();
52     }
53
54     @Test
55     public void testGetWidgets() throws XmlArtifactGenerationException {
56         Widget widget = Widget.createWidget("SERVICE");
57         assertThat(widget.getType(), is(ModelType.WIDGET));
58         assertThat(widget.getName(), is("service-instance"));
59         assertThat(widget.getDeleteFlag(), is(true));
60
61         widget = Widget.createWidget("VF");
62         assertThat(widget.getType(), is(ModelType.WIDGET));
63         assertThat(widget.getName(), is("generic-vnf"));
64         assertThat(widget.getDeleteFlag(), is(false));
65
66         widget = Widget.createWidget("VFC");
67         assertThat(widget.getType(), is(ModelType.WIDGET));
68         assertThat(widget.getName(), is("vnfc"));
69         assertThat(widget.getDeleteFlag(), is(true));
70
71         widget = Widget.createWidget("VSERVER");
72         assertThat(widget.getType(), is(ModelType.WIDGET));
73         assertThat(widget.getName(), is("vserver"));
74         assertThat(widget.getDeleteFlag(), is(true));
75
76         widget = Widget.createWidget("VOLUME");
77         assertThat(widget.getType(), is(ModelType.WIDGET));
78         assertThat(widget.getName(), is("volume"));
79         assertThat(widget.getDeleteFlag(), is(true));
80
81         widget = Widget.createWidget("FLAVOR");
82         assertThat(widget.getType(), is(ModelType.WIDGET));
83         assertThat(widget.getName(), is("flavor"));
84         assertThat(widget.getDeleteFlag(), is(false));
85
86         widget = Widget.createWidget("TENANT");
87         assertThat(widget.getType(), is(ModelType.WIDGET));
88         assertThat(widget.getName(), is("tenant"));
89         assertThat(widget.getDeleteFlag(), is(false));
90
91         widget = Widget.createWidget("VOLUME_GROUP");
92         assertThat(widget.getType(), is(ModelType.WIDGET));
93         assertThat(widget.getName(), is("volume-group"));
94         assertThat(widget.getDeleteFlag(), is(true));
95
96         widget = Widget.createWidget("LINT");
97         assertThat(widget.getType(), is(ModelType.WIDGET));
98         assertThat(widget.getName(), is("l-interface"));
99         assertThat(widget.getDeleteFlag(), is(true));
100
101         widget = Widget.createWidget("L3_NET");
102         assertThat(widget.getType(), is(ModelType.WIDGET));
103         assertThat(widget.getName(), is("l3-network"));
104         assertThat(widget.getDeleteFlag(), is(true));
105
106         widget = Widget.createWidget("VFMODULE");
107         assertThat(widget.getType(), is(ModelType.WIDGET));
108         assertThat(widget.getName(), is("vf-module"));
109         assertThat(widget.getDeleteFlag(), is(true));
110
111         widget = Widget.createWidget("IMAGE");
112         assertThat(widget.getType(), is(ModelType.WIDGET));
113         assertThat(widget.getName(), is("image"));
114         assertThat(widget.getDeleteFlag(), is(false));
115
116         widget = Widget.createWidget("OAM_NETWORK");
117         assertThat(widget.getType(), is(ModelType.WIDGET));
118         assertThat(widget.getName(), is("oam-network"));
119         assertThat(widget.getDeleteFlag(), is(true));
120
121         widget = Widget.createWidget("ALLOTTED_RESOURCE");
122         assertThat(widget.getType(), is(ModelType.WIDGET));
123         assertThat(widget.getName(), is("allotted-resource"));
124         assertThat(widget.getDeleteFlag(), is(true));
125
126         widget = Widget.createWidget("TUNNEL_XCONNECT");
127         assertThat(widget.getType(), is(ModelType.WIDGET));
128         assertThat(widget.getName(), is("tunnel-xconnect"));
129         assertThat(widget.getDeleteFlag(), is(true));
130
131         widget = Widget.createWidget("CONFIGURATION");
132         assertThat(widget.getType(), is(ModelType.WIDGET));
133         assertThat(widget.getName(), is("configuration"));
134         assertThat(widget.getDeleteFlag(), is(true));
135     }
136
137     @Test
138     public void testWidgetMethods() throws XmlArtifactGenerationException {
139         Widget widget = Widget.createWidget("SERVICE");
140         assertThat(widget.getType(), is(ModelType.WIDGET));
141         assertThat(widget.getWidgetId(), is("service-instance-invariant-id"));
142         assertThat(widget.addWidget(Widget.createWidget("TENANT")), is(true));
143         assertThat(widget.memberOf(null), is(false));
144         assertThat(widget.memberOf(Collections.emptyList()), is(false));
145     }
146
147     /**
148      * Call equals() method for code coverage.
149      *
150      * @throws XmlArtifactGenerationException
151      *             if there is no configuration defined for the test Widget Type
152      */
153     @Test
154     public void testEquals() throws XmlArtifactGenerationException {
155         Widget widgetModel = Widget.createWidget("OAM_NETWORK");
156
157         // equals() is reflexive
158         assertThat(widgetModel.equals(widgetModel), is(true));
159
160         // equals() is symmetric
161         Widget widgetModelB = Widget.createWidget("OAM_NETWORK");
162         assertThat(widgetModel.equals(widgetModelB), is(true));
163         assertThat(widgetModelB.equals(widgetModel), is(true));
164
165         assertThat(widgetModel.equals(null), is(false));
166         assertThat(widgetModel.equals(Widget.createWidget("VSERVER")), is(false));
167     }
168
169     @Test(expected = IllegalArgumentException.class)
170     public void testGetUnknownWidget() throws XmlArtifactGenerationException {
171         WidgetType.valueOf("invalid-widget-name");
172     }
173
174     /**
175      * Try to get the Widget object for an unsupported (non-configured) type.
176      *
177      * @throws XmlArtifactGenerationException
178      *             if there is no configuration defined for the specified Widget type
179      */
180     @Test(expected = XmlArtifactGenerationException.class)
181     public void testGetDynamicWidget() throws XmlArtifactGenerationException {
182         Widget.createWidget(new WidgetType(null));
183     }
184
185     @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class)
186     public void testAddResourceIsUnsupported() throws XmlArtifactGenerationException {
187         Widget.createWidget("OAM_NETWORK").addResource(null);
188     }
189
190     @Test(expected = IllegalArgumentException.class)
191     public void testGetVersionIdForUknownWidget() {
192         new Widget(new WidgetType("test"), null, false).getId();
193     }
194
195     @Test(expected = IllegalArgumentException.class)
196     public void testGetInvariantIdForUknownWidget() {
197         new Widget(new WidgetType("test"), null, false).getWidgetId();
198     }
199
200     // Call Widget methods which are not supported, purely for code coverage.
201
202     @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class)
203     public void testGetModelNameVersionIdIsUnsupported() throws XmlArtifactGenerationException {
204         Widget widgetModel = Widget.createWidget("OAM_NETWORK");
205         assertThat(widgetModel.getModelNameVersionId(), is(nullValue()));
206     }
207
208     @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class)
209     public void testGetModelTypeNameIsUnsupported() throws XmlArtifactGenerationException {
210         Widget widgetModel = Widget.createWidget("OAM_NETWORK");
211         assertThat(widgetModel.getModelTypeName(), is(nullValue()));
212     }
213
214     @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class)
215     public void testGetModelIdIsUnsupported() throws XmlArtifactGenerationException {
216         Widget widgetModel = Widget.createWidget("OAM_NETWORK");
217         assertThat(widgetModel.getModelId(), is(nullValue()));
218     }
219
220 }