a00989900346a1f069ecd71f02fe398aa38f2f3a
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.tosca.services;
22
23 import org.junit.Assert;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.junit.runner.RunWith;
28 import org.mockito.runners.MockitoJUnitRunner;
29 import org.onap.sdc.tosca.datatypes.model.*;
30 import org.openecomp.sdc.common.errors.CoreException;
31
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.Optional;
35
36 /**
37  * @author shiria
38  * @since September 15, 2016.
39  */
40 @RunWith(MockitoJUnitRunner.class)
41 public class DataModelUtilTest {
42
43   @Rule
44   public ExpectedException thrown = ExpectedException.none();
45
46   @Test
47   public void testAddSubstitutionMapping() throws Exception {
48     thrown.expect(CoreException.class);
49     thrown.expectMessage(
50         "Invalid action, can't add 'Substitution Mapping' to 'Service Template', 'Service Template' entity is NULL.");
51     DataModelUtil.addSubstitutionMapping(null, new SubstitutionMapping());
52   }
53
54   @Test
55   public void testAddSubstitutionMappingReq() throws Exception {
56     thrown.expect(CoreException.class);
57     thrown.expectMessage(
58         "Invalid action, can't add 'Substitution Mapping Requirements' to 'Service Template', 'Service Template' entity is NULL.");
59     DataModelUtil.addSubstitutionMappingReq(null, "123", new ArrayList<>());
60   }
61
62   @Test
63   public void testAddNodeTemplate() throws Exception {
64     thrown.expect(CoreException.class);
65     thrown.expectMessage(
66         "Invalid action, can't add 'Node Template' to 'Service Template', 'Service Template' entity is NULL.");
67     DataModelUtil.addNodeTemplate(null, "123", new NodeTemplate());
68   }
69
70   @Test
71   public void testAddPolicyDefinition() throws Exception {
72     thrown.expect(CoreException.class);
73     thrown.expectMessage(
74         "Invalid action, can't add 'Policy Definition' to 'Service Template', 'Service Template' entity is NULL.");
75     DataModelUtil.addPolicyDefinition(null, "123", new PolicyDefinition());
76   }
77
78   @Test
79   public void testAddNodeType() throws Exception {
80     thrown.expect(CoreException.class);
81     thrown.expectMessage(
82         "Invalid action, can't add 'Node Type' to 'Service Template', 'Service Template' entity is NULL.");
83     DataModelUtil.addNodeType(null, "123", new NodeType());
84   }
85
86   @Test
87   public void testAddRelationshipTemplate() throws Exception {
88     thrown.expect(CoreException.class);
89     thrown.expectMessage(
90         "Invalid action, can't add 'Relationship Template' to 'Service Template', 'Service Template' entity is NULL.");
91     DataModelUtil.addRelationshipTemplate(null, "123", new RelationshipTemplate());
92   }
93
94   @Test
95   public void testAddRequirementAssignment() throws Exception {
96     thrown.expect(CoreException.class);
97     thrown.expectMessage(
98         "Invalid action, can't add 'Requirement Assignment' to 'Node Template', 'Node Template' entity is NULL.");
99     DataModelUtil.addRequirementAssignment(null, "123", new RequirementAssignment());
100   }
101
102   @Test
103   public void testGetNodeTemplate() throws Exception {
104     thrown.expect(CoreException.class);
105     thrown.expectMessage(
106         "Invalid action, can't add 'Node Template' to 'Service Template', 'Service Template' entity is NULL.");
107     DataModelUtil.addNodeTemplate(null, "123", new NodeTemplate());
108   }
109
110   @Test
111   public void testGetNodeType() throws Exception {
112     thrown.expect(CoreException.class);
113     thrown.expectMessage(
114         "Invalid action, can't add 'Node Type' to 'Service Template', 'Service Template' entity is NULL.");
115     DataModelUtil.addNodeType(null, "123", new NodeType());
116   }
117
118   @Test
119   public void testAddGroupToTopologyTemplate() throws Exception {
120     thrown.expect(CoreException.class);
121     thrown.expectMessage(
122         "Invalid action, can't add 'Group Definition' to 'Service Template', 'Service Template' entity is NULL.");
123     DataModelUtil.addGroupDefinitionToTopologyTemplate(null, "123", new GroupDefinition());
124   }
125
126
127   @Test
128   public void testGetRelationshipTemplate(){
129     RelationshipTemplate relationshipTemplate = new RelationshipTemplate();
130     String testingRelationshipType = "testingRelationshipType";
131     relationshipTemplate.setType(testingRelationshipType);
132     TopologyTemplate topologyTemplate = new TopologyTemplate();
133     topologyTemplate.setRelationship_templates(new HashMap<>());
134     String relationId = "rtest";
135     topologyTemplate.getRelationship_templates().put(relationId, relationshipTemplate);
136     ServiceTemplate serviceTemplate = new ServiceTemplate();
137     serviceTemplate.setTopology_template(topologyTemplate);
138
139     Optional<RelationshipTemplate> relationshipTemplateOut =
140             DataModelUtil.getRelationshipTemplate(serviceTemplate, relationId);
141     Assert.assertNotNull(relationshipTemplateOut);
142     Assert.assertEquals(true,relationshipTemplateOut.isPresent());
143     Assert.assertEquals(testingRelationshipType, relationshipTemplateOut.get().getType());
144   }
145
146   @Test
147   public void testGetEmptyRelationshipTemplate(){
148     ServiceTemplate serviceTemplate = new ServiceTemplate();
149     String relationId = "rtest";
150     Optional<RelationshipTemplate> relationshipTemplateOut =
151             DataModelUtil.getRelationshipTemplate(serviceTemplate, relationId);
152     Assert.assertNotNull(relationshipTemplateOut);
153     Assert.assertEquals(false,relationshipTemplateOut.isPresent());
154   }
155 }