8d6fe3e576d7fac773af553e9f37e8a83a2efc3b
[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-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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.instanceOf;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.hamcrest.CoreMatchers.nullValue;
27 import static org.junit.Assert.assertThat;
28
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.util.Properties;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
36 import org.onap.aai.babel.xml.generator.model.Widget.Type;
37 import org.onap.aai.babel.xml.generator.types.ModelType;
38
39 /**
40  * Direct tests of the Model abstract class (to improve code coverage). Not all methods are tested here. Some are
41  * covered by the tests of derived classes.
42  */
43 public class TestModel {
44
45     private Service serviceModel = new Service();
46     private Resource resourceModel = new VirtualFunction();
47     private Widget widgetModel = new OamNetwork();
48     private Model anonymousModel;
49
50     static {
51         System.setProperty("APP_HOME", ".");
52     }
53
54     /**
55      * Load the Widget to UUID mappings from the Artifact Generator properties.
56      *
57      * @throws FileNotFoundException if the properties file is missing
58      * @throws IOException if the properties file is not loaded
59      */
60     @Before
61     public void setup() throws FileNotFoundException, IOException {
62         InputStream in = TestModel.class.getClassLoader().getResourceAsStream("artifact-generator.properties");
63         Properties properties = new Properties();
64         properties.load(in);
65         in.close();
66         WidgetConfigurationUtil.setConfig(properties);
67
68         anonymousModel = new Model() {
69             @Override
70             public boolean addResource(Resource resource) {
71                 return false;
72             }
73
74             @Override
75             public boolean addWidget(Widget resource) {
76                 return false;
77             }
78
79             @Override
80             public Type getWidgetType() {
81                 return null;
82             }
83         };
84     }
85
86     @Test
87     public void testGetModels() {
88         assertThat(Model.getModelFor(null), is(nullValue()));
89         assertThat(Model.getModelFor(""), is(nullValue()));
90         assertThat(Model.getModelFor("any.unknown.type"), is(nullValue()));
91
92         assertThat(Model.getModelFor("org.openecomp.resource.vf.allottedResource"), instanceOf(AllotedResource.class));
93         assertThat(Model.getModelFor("org.openecomp.resource.vfc.AllottedResource"),
94                 instanceOf(ProvidingService.class));
95         assertThat(Model.getModelFor("org.openecomp.resource.vfc"), instanceOf(VServerWidget.class));
96         assertThat(Model.getModelFor("org.openecomp.resource.cp"), instanceOf(LIntfWidget.class));
97         assertThat(Model.getModelFor("org.openecomp.cp"), instanceOf(LIntfWidget.class));
98         assertThat(Model.getModelFor("org.openecomp.cp.some.suffix"), instanceOf(LIntfWidget.class));
99         assertThat(Model.getModelFor("org.openecomp.resource.vl"), instanceOf(L3Network.class));
100         assertThat(Model.getModelFor("org.openecomp.resource.vf"), instanceOf(VirtualFunction.class));
101         assertThat(Model.getModelFor("org.openecomp.groups.vfmodule"), instanceOf(VfModule.class));
102         assertThat(Model.getModelFor("org.openecomp.groups.VfModule"), instanceOf(VfModule.class));
103         assertThat(Model.getModelFor("org.openecomp.resource.vfc.nodes.heat.cinder"), instanceOf(VolumeWidget.class));
104         assertThat(Model.getModelFor("org.openecomp.nodes.PortMirroringConfiguration"),
105                 instanceOf(Configuration.class));
106     }
107
108     @Test
109     public void testGetCardinality() {
110         resourceModel.getCardinality();
111     }
112
113     @Test
114     public void testGetModelType() {
115         assertThat(serviceModel.getModelType(), is(ModelType.SERVICE));
116         assertThat(resourceModel.getModelType(), is(ModelType.RESOURCE));
117         assertThat(widgetModel.getModelType(), is(ModelType.WIDGET));
118         assertThat(anonymousModel.getModelType(), is(nullValue()));
119     }
120
121     @Test
122     public void testGetModelNameVersionId() {
123         assertThat(anonymousModel.getModelNameVersionId(), is(nullValue()));
124     }
125
126     @Test(expected = org.onap.aai.babel.xml.generator.error.IllegalAccessException.class)
127     public void testGetModelNameVersionIdIsUnsupported() {
128         assertThat(widgetModel.getModelNameVersionId(), is(nullValue()));
129     }
130
131 }