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