82636940c33c199d5a862d53e663b36d18fc0218
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / utils / ToscaUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.utils;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28
29 import org.junit.Test;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes;
32 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
36
37 /**
38  * Import the {@link ToscaUtils} class.
39  *
40  * @author Liam Fallon (liam.fallon@est.tech)
41  */
42 public class ToscaUtilsTest {
43
44     @Test
45     public void testAssertDataTypes() {
46         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
47
48         assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
49         assertEquals("no data types specified on service template",
50                 ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
51         assertThatThrownBy(() -> {
52             ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
53         }).hasMessage("no data types specified on service template");
54
55         jpaToscaServiceTemplate.setDataTypes(new JpaToscaDataTypes());
56
57         assertFalse(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
58         assertEquals("list of data types specified on service template is empty",
59                 ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
60         assertThatThrownBy(() -> {
61             ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
62         }).hasMessage("list of data types specified on service template is empty");
63
64         jpaToscaServiceTemplate.getDataTypes().getConceptMap().put(new PfConceptKey(), null);
65
66         assertTrue(ToscaUtils.doDataTypesExist(jpaToscaServiceTemplate));
67         assertEquals(null, ToscaUtils.checkDataTypesExist(jpaToscaServiceTemplate));
68         assertThatCode(() -> {
69             ToscaUtils.assertDataTypesExist(jpaToscaServiceTemplate);
70         }).doesNotThrowAnyException();
71
72     }
73
74     @Test
75     public void testAssertPolicyTypes() {
76         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
77
78         assertFalse(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
79         assertEquals("no policy types specified on service template",
80                 ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
81         assertThatThrownBy(() -> {
82             ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
83         }).hasMessage("no policy types specified on service template");
84
85         jpaToscaServiceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
86
87         assertFalse(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
88         assertEquals("list of policy types specified on service template is empty",
89                 ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
90         assertThatThrownBy(() -> {
91             ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
92         }).hasMessage("list of policy types specified on service template is empty");
93
94         jpaToscaServiceTemplate.getPolicyTypes().getConceptMap().put(new PfConceptKey(), null);
95
96         assertTrue(ToscaUtils.doPolicyTypesExist(jpaToscaServiceTemplate));
97         assertEquals(null, ToscaUtils.checkPolicyTypesExist(jpaToscaServiceTemplate));
98         assertThatCode(() -> {
99             ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate);
100         }).doesNotThrowAnyException();
101     }
102
103     @Test
104     public void testAssertPolicies() {
105         JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate();
106
107         assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
108         assertEquals("topology template not specified on service template",
109                 ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
110         assertThatThrownBy(() -> {
111             ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
112         }).hasMessage("topology template not specified on service template");
113
114         jpaToscaServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
115
116         assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
117         assertEquals("no policies specified on topology template of service template",
118                 ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
119         assertThatThrownBy(() -> {
120             ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
121         }).hasMessage("no policies specified on topology template of service template");
122
123         jpaToscaServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
124
125         assertFalse(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
126         assertEquals("list of policies specified on topology template of service template is empty",
127                 ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
128         assertThatThrownBy(() -> {
129             ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
130         }).hasMessage("list of policies specified on topology template of service template is empty");
131
132         jpaToscaServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(new PfConceptKey(), null);
133
134         assertTrue(ToscaUtils.doPoliciesExist(jpaToscaServiceTemplate));
135         assertEquals(null, ToscaUtils.checkPoliciesExist(jpaToscaServiceTemplate));
136         assertThatCode(() -> {
137             ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate);
138         }).doesNotThrowAnyException();
139     }
140 }