912a50526f0aca4c902888d55bf967b05970cf9e
[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-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 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.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.IOException;
30 import java.util.Arrays;
31 import java.util.List;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.aai.babel.parser.ArtifactGeneratorToscaParser;
35 import org.onap.aai.babel.util.ArtifactTestUtils;
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 List<Resource> resourceModels = Arrays.asList(new VirtualFunction(), new InstanceGroup());
47     private Widget widgetModel = new OamNetwork();
48     private Model anonymousModel;
49
50     static {
51         System.setProperty("APP_HOME", ".");
52     }
53
54     /**
55      * Initialise the Artifact Generator with filtering and mapping configuration. Also Load the Widget to UUID mappings
56      * from the Artifact Generator properties.
57      *
58      * @throws IOException
59      *             if the Artifact Generator properties file is not loaded
60      */
61     @Before
62     public void setup() throws IOException {
63         ArtifactTestUtils utils = new ArtifactTestUtils();
64         utils.setGeneratorSystemProperties();
65
66         ArtifactGeneratorToscaParser.initGroupFilterConfiguration();
67         utils.loadWidgetToUuidMappings();
68
69         anonymousModel = new Model() {
70             @Override
71             public boolean addResource(Resource resource) {
72                 return false;
73             }
74
75             @Override
76             public boolean addWidget(Widget resource) {
77                 return false;
78             }
79
80             @Override
81             public Type getWidgetType() {
82                 return null;
83             }
84         };
85     }
86
87     @Test
88     public void testGetModels() {
89         assertThat(Model.getModelFor(null), is(nullValue()));
90         assertThat(Model.getModelFor(""), is(nullValue()));
91         assertThat(Model.getModelFor("any.unknown.type"), is(nullValue()));
92
93         assertThat(Model.getModelFor("org.openecomp.resource.vf.allottedResource"), instanceOf(AllotedResource.class));
94         assertThat(Model.getModelFor("org.openecomp.resource.vf.allottedResource.with.sub.type"),
95                 instanceOf(AllotedResource.class));
96         assertThat(Model.getModelFor("org.openecomp.resource.vfc.AllottedResource"),
97                 instanceOf(ProvidingService.class));
98         assertThat(Model.getModelFor("org.openecomp.resource.vfc"), instanceOf(VServerWidget.class));
99         assertThat(Model.getModelFor("org.openecomp.resource.cp"), instanceOf(LIntfWidget.class));
100         assertThat(Model.getModelFor("org.openecomp.cp"), instanceOf(LIntfWidget.class));
101         assertThat(Model.getModelFor("org.openecomp.cp.some.suffix"), instanceOf(LIntfWidget.class));
102         assertThat(Model.getModelFor("org.openecomp.resource.vl"), instanceOf(L3Network.class));
103         assertThat(Model.getModelFor("org.openecomp.resource.vf"), instanceOf(VirtualFunction.class));
104         assertThat(Model.getModelFor("org.openecomp.groups.vfmodule"), instanceOf(VfModule.class));
105         assertThat(Model.getModelFor("org.openecomp.groups.VfModule"), instanceOf(VfModule.class));
106         assertThat(Model.getModelFor("org.openecomp.resource.vfc.nodes.heat.cinder"), instanceOf(VolumeWidget.class));
107         assertThat(Model.getModelFor("org.openecomp.nodes.PortMirroringConfiguration"),
108                 instanceOf(Configuration.class));
109         assertThat(Model.getModelFor("org.openecomp.nodes.PortMirroringConfiguration", "Configuration"),
110                 instanceOf(Configuration.class));
111         assertThat(Model.getModelFor("any.string", "Configuration"), instanceOf(Configuration.class));
112         assertThat(Model.getModelFor("org.openecomp.resource.cr.Kk1806Cr1", "CR"), instanceOf(CR.class));
113         assertThat(Model.getModelFor("any.string", "CR"), instanceOf(CR.class));
114
115         assertThat(Model.getModelFor("org.openecomp.resource.vfc", "an.unknown.type"), instanceOf(VServerWidget.class));
116     }
117
118     @Test
119     public void testGetCardinality() {
120         resourceModels.get(0).getCardinality();
121     }
122
123     @Test
124     public void testGetModelType() {
125         assertThat(serviceModel.getModelType(), is(ModelType.SERVICE));
126         for (Resource resourceModel : resourceModels) {
127             assertThat(resourceModel.getModelType(), is(ModelType.RESOURCE));
128         }
129         assertThat(widgetModel.getModelType(), is(ModelType.WIDGET));
130         assertThat(anonymousModel.getModelType(), is(nullValue()));
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         assertThat(resourceModels.get(0).getModelType(), is(ModelType.RESOURCE));
142         assertThat(widgetModel.getModelType(), is(ModelType.WIDGET));
143         assertThat(anonymousModel.getModelType(), is(nullValue()));
144     }
145
146 }