433240f4b2822d0caed298a1d42fa627ce210f53
[dcaegen2/platform.git] / mod / bpgenerator / onap / src / test / java / org / onap / blueprintgenerator / test / TlsInfoTest.java
1 /*============LICENSE_START=======================================================
2 org.onap.dcae
3 ================================================================================
4 Copyright (c) 2020 Nokia. All rights reserved.
5 Copyright (c) 2020 AT&T. 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.onap.blueprintgenerator.test;
22
23 import org.junit.Test;
24 import org.junit.runners.Parameterized;
25 import org.onap.blueprintgenerator.model.common.Input;
26 import org.onap.blueprintgenerator.model.common.Node;
27 import org.onap.blueprintgenerator.model.common.OnapBlueprint;
28 import org.onap.blueprintgenerator.model.common.ExternalCertificateParameters;
29 import org.onap.blueprintgenerator.model.common.ExternalTlsInfo;
30 import org.onap.blueprintgenerator.model.dmaap.TlsInfo;
31
32 import java.nio.file.Paths;
33 import java.util.Arrays;
34 import java.util.LinkedHashMap;
35 import java.util.List;
36
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertFalse;
39 import static org.junit.Assert.assertNotNull;
40 import static org.junit.Assert.assertNull;
41
42 /**
43  * Test Case for Tls Info
44  *
45  */
46 // @RunWith(Parameterized.class)
47 public class TlsInfoTest extends BlueprintGeneratorTests {
48
49     @Parameterized.Parameter
50     public char bpType;
51
52     @Parameterized.Parameters(name = "Blueprint type: {0}")
53     public static List<Character> data() {
54         return Arrays.asList('o', 'd');
55     }
56
57     /**
58      * Test Case for Tls True and External TLS True
59      *
60      */
61     @Test
62     public void useTlsTrueAndUseExternalTlsTrueTest() {
63         OnapBlueprint bp =
64             createBlueprintFromFile(
65                 Paths.get(
66                     "src",
67                     "test",
68                     "resources",
69                     "componentspecs",
70                     useTlsTrueAndUseExternalTlsTrueTest)
71                     .toFile()
72                     .getAbsolutePath());
73
74         assertBlueprintContainsExternalTlsInfoWithUseFlagDefault(bp, true);
75         assertBlueprintContainsTlsInfoWithUseFlagDefault(bp, true);
76     }
77     /**
78      * Test Case for Tls False and External TLS False
79      *
80      */
81     @Test
82     public void useTlsFalseAndUseExternalTlsFalseTest() {
83         OnapBlueprint bp =
84             createBlueprintFromFile(
85                 Paths.get(
86                     "src",
87                     "test",
88                     "resources",
89                     "componentspecs",
90                     useTlsFalseAndUseExternalTlsFalseTest)
91                     .toFile()
92                     .getAbsolutePath());
93
94         assertBlueprintContainsExternalTlsInfoWithUseFlagDefault(bp, false);
95         assertBlueprintContainsTlsInfoWithUseFlagDefault(bp, false);
96     }
97
98     /**
99      * Test Case for Tls True and No External TLS Flag
100      *
101      */
102     @Test
103     public void useTlsTrueAndNoExternalTlsFlagTest() {
104         OnapBlueprint bp =
105             createBlueprintFromFile(
106                 Paths.get(
107                     "src",
108                     "test",
109                     "resources",
110                     "componentspecs",
111                     useTlsTrueAndNoExternalTlsFlagTest)
112                     .toFile()
113                     .getAbsolutePath());
114
115         assertBlueprintContainsTlsInfoWithUseFlagDefault(bp, true);
116         assertBlueprintHasNoExternalTlsInfo(bp);
117     }
118
119     /**
120      * Test Case for No Tls Info
121      *
122      */
123     @Test
124     public void noTlsInfo() {
125         OnapBlueprint bp =
126             createBlueprintFromFile(
127                 Paths.get("src", "test", "resources", "componentspecs", noTlsInfo)
128                     .toFile()
129                     .getAbsolutePath());
130
131         assertBlueprintHasNoTlsInfo(bp);
132         assertBlueprintHasNoExternalTlsInfo(bp);
133     }
134
135     private void assertBlueprintContainsExternalTlsInfoWithUseFlagDefault(
136         OnapBlueprint bp, boolean useFlagDefault) {
137         // should create proper inputs
138         assertContainsInputWithDefault(bp, "external_cert_use_external_tls", useFlagDefault);
139         assertContainsInputWithDefault(bp, "external_cert_ca_name", "\"RA\"");
140         assertContainsInputWithDefault(bp, "external_cert_cert_type", "\"P12\"");
141         assertContainsInputWithDefault(bp, "external_cert_common_name", "\"sample.onap.org\"");
142         assertContainsInputWithDefault(
143             bp, "external_cert_sans", "\"sample.onap.org,component.sample.onap.org\"");
144
145         Node node = bp.getNode_templates().get("test.component.spec");
146
147         // should create proper externalTlsInfo object in node properties
148         ExternalTlsInfo externalTlsInfo = node.getProperties().getExternal_cert();
149         assertNotNull(externalTlsInfo);
150
151         assertEquals("external_cert_ca_name", externalTlsInfo.getCaName().getBpInputName());
152         assertEquals("external_cert_cert_type", externalTlsInfo.getCertType().getBpInputName());
153         assertEquals(
154             "external_cert_use_external_tls", externalTlsInfo.getUseExternalTls().getBpInputName());
155         assertEquals("/opt/app/dcae-certificate/", externalTlsInfo.getExternalCertDirectory());
156
157         ExternalCertificateParameters extCertParams =
158             externalTlsInfo.getExternalCertificateParameters();
159         assertNotNull(extCertParams);
160
161         assertEquals("external_cert_common_name", extCertParams.getCommonName().getBpInputName());
162         assertEquals("external_cert_sans", extCertParams.getSans().getBpInputName());
163     }
164
165     private void assertBlueprintContainsTlsInfoWithUseFlagDefault(
166         OnapBlueprint bp, boolean useFlagDefault) {
167         // shold create proper inputs
168         assertContainsInputWithDefault(bp, "use_tls", useFlagDefault);
169
170         Node node = bp.getNode_templates().get("test.component.spec");
171
172         // should create proper tlsInfo object in node properties
173         TlsInfo tlsInfo = node.getProperties().getTls_info();
174         assertEquals("use_tls", tlsInfo.getUseTls().getBpInputName());
175         assertEquals("/opt/app/dcae-certificate/", tlsInfo.getCertDirectory());
176     }
177
178     private void assertBlueprintHasNoExternalTlsInfo(OnapBlueprint bp) {
179         // should not create inputs for external tls
180         assertFalse(bp.getInputs().containsKey("external_cert_use_external_tls"));
181         assertFalse(bp.getInputs().containsKey("external_cert_common_name"));
182         assertFalse(bp.getInputs().containsKey("external_cert_ca_name"));
183         assertFalse(bp.getInputs().containsKey("external_cert_sans"));
184
185         Node node = bp.getNode_templates().get("test.component.spec");
186
187         // should not create externalTlsInfo object in node properties
188         ExternalTlsInfo externalTlsInfo = node.getProperties().getExternal_cert();
189         assertNull(externalTlsInfo);
190     }
191
192     private void assertBlueprintHasNoTlsInfo(OnapBlueprint bp) {
193         // should not create inputs for tls
194         assertFalse(bp.getInputs().containsKey("use_tls"));
195
196         Node node = bp.getNode_templates().get("test.component.spec");
197
198         // should not create tlsInfo object in node properties
199         assertNull(node.getProperties().getTls_info());
200     }
201
202     private void assertContainsInputWithDefault(
203         OnapBlueprint bp, String inputName, Object defaultValue) {
204         LinkedHashMap<String, Object> input = bp.getInputs().get(inputName);
205         assertNotNull(input);
206         assertEquals(defaultValue, input.get("default"));
207     }
208
209     private OnapBlueprint createBlueprintFromFile(String path) {
210         onapComponentSpec = onapComponentSpecService.createComponentSpecFromFile(path);
211         Input input = onapTestUtils.getInput(path, "", "", "", "o", "");
212         OnapBlueprint onapBlueprint = onapBlueprintCreatorService.createBlueprint(onapComponentSpec, input);
213         return onapBlueprint;
214     }
215 }