Merge "Add factories for Ext tls parameters"
[dcaegen2/platform.git] / mod / bpgenerator / src / test / java / org / onap / blueprintgenerator / core / TlsInfoTest.java
1 /*============LICENSE_START=======================================================
2  org.onap.dcae
3  ================================================================================
4  Copyright (c) 2020 Nokia. All rights reserved.
5  ================================================================================
6  Licensed under the Apache License, Version 2.0 (the "License");
7  you may not use this file except in compliance with the License.
8  You may obtain a copy of the License at
9
10  http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  See the License for the specific language governing permissions and
16  limitations under the License.
17  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.blueprintgenerator.core;
21
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.Parameterized;
25 import org.onap.blueprintgenerator.models.blueprint.Blueprint;
26 import org.onap.blueprintgenerator.models.blueprint.Node;
27 import org.onap.blueprintgenerator.models.blueprint.tls.TlsInfo;
28 import org.onap.blueprintgenerator.models.blueprint.tls.impl.ExternalCertificateParameters;
29 import org.onap.blueprintgenerator.models.blueprint.tls.impl.ExternalTlsInfo;
30 import org.onap.blueprintgenerator.models.componentspec.ComponentSpec;
31
32 import java.util.Arrays;
33 import java.util.LinkedHashMap;
34 import java.util.List;
35
36 import static org.junit.Assert.assertEquals;
37 import static org.junit.Assert.assertFalse;
38 import static org.junit.Assert.assertNotNull;
39 import static org.junit.Assert.assertNull;
40
41 @RunWith(Parameterized.class)
42 public class TlsInfoTest {
43
44         @Parameterized.Parameter
45         public char bpType;
46
47         @Parameterized.Parameters(name = "Blueprint type: {0}")
48         public static List<Character> data() {
49                 return Arrays.asList('o', 'd');
50         }
51
52         @Test
53         public void useTlsTrueAndUseExternalTlsTrueTest(){
54                 Blueprint bp = createBlueprintFromFile("TestCases/TlsInfo/testComponentSpec_withTlsTrueAndExternalTlsTrue.json");
55
56                 assertBlueprintContainsExternalTlsInfoWithUseFlagDefault(bp, true);
57                 assertBlueprintContainsTlsInfoWithUseFlagDefault(bp, true);
58         }
59
60         @Test
61         public void useTlsFalseAndUseExternalTlsFalseTest(){
62                 Blueprint bp = createBlueprintFromFile("TestCases/TlsInfo/testComponentSpec_withTlsFalseAndExternalTlsFalse.json");
63
64                 assertBlueprintContainsExternalTlsInfoWithUseFlagDefault(bp, false);
65                 assertBlueprintContainsTlsInfoWithUseFlagDefault(bp, false);
66         }
67
68         @Test
69         public void useTlsTrueAndNoExternalTlsFlagTest(){
70                 Blueprint bp = createBlueprintFromFile("TestCases/TlsInfo/testComponentSpec_withTlsTrueAndNoExternalTls.json");
71
72                 assertBlueprintContainsTlsInfoWithUseFlagDefault(bp, true);
73                 assertBlueprintHasNoExternalTlsInfo(bp);
74         }
75
76         @Test
77         public void noTlsInfo(){
78                 Blueprint bp = createBlueprintFromFile("TestCases/TlsInfo/testComponentSpec_withoutTlsInfo.json");
79
80                 assertBlueprintHasNoTlsInfo(bp);
81                 assertBlueprintHasNoExternalTlsInfo(bp);
82         }
83
84         private void assertBlueprintContainsExternalTlsInfoWithUseFlagDefault(Blueprint bp, boolean useFlagDefault) {
85                 //should create proper inputs
86                 assertContainsInputWithDefault(bp, "external_cert_use_external_tls", useFlagDefault);
87                 assertContainsInputWithDefault(bp, "external_cert_ca_name", "\"RA\"");
88                 assertContainsInputWithDefault(bp, "external_cert_cert_type", "\"P12\"");
89                 assertContainsInputWithDefault(bp, "external_cert_common_name", "\"sample.onap.org\"");
90                 assertContainsInputWithDefault(bp, "external_cert_sans",
91                                 "\"sample.onap.org:component.sample.onap.org\"");
92
93                 Node node = bp.getNode_templates().get("test.component.spec");
94
95                 //should create proper externalTlsInfo object in node properties
96                 ExternalTlsInfo externalTlsInfo = node.getProperties().getExternal_cert();
97                 assertNotNull(externalTlsInfo);
98
99                 assertEquals("external_cert_ca_name", externalTlsInfo.getCaName().getBpInputName());
100                 assertEquals("external_cert_cert_type", externalTlsInfo.getCertType().getBpInputName());
101                 assertEquals("external_cert_use_external_tls", externalTlsInfo.getUseExternalTls().getBpInputName());
102                 assertEquals("/opt/app/dcae-certificate/", externalTlsInfo.getExternalCertDirectory());
103
104                 ExternalCertificateParameters extCertParams = externalTlsInfo.getExternalCertificateParameters();
105                 assertNotNull(extCertParams);
106
107                 assertEquals("external_cert_common_name", extCertParams.getCommonName().getBpInputName());
108                 assertEquals("external_cert_sans", extCertParams.getSans().getBpInputName());
109         }
110
111         private void assertBlueprintContainsTlsInfoWithUseFlagDefault(Blueprint bp, boolean useFlagDefault) {
112                 //shold create proper inputs
113                 assertContainsInputWithDefault(bp, "use_tls", useFlagDefault);
114
115                 Node node = bp.getNode_templates().get("test.component.spec");
116
117                 //should create proper tlsInfo object in node properties
118                 TlsInfo tlsInfo = node.getProperties().getTls_info();
119                 assertEquals("use_tls", tlsInfo.getUseTls().getBpInputName());
120                 assertEquals("/opt/app/dcae-certificate/", tlsInfo.getCertDirectory());
121
122         }
123
124         private void assertBlueprintHasNoExternalTlsInfo(Blueprint bp) {
125                 //should not create inputs for external tls
126                 assertFalse(bp.getInputs().containsKey("external_cert_use_external_tls"));
127                 assertFalse(bp.getInputs().containsKey("external_cert_common_name"));
128                 assertFalse(bp.getInputs().containsKey("external_cert_ca_name"));
129                 assertFalse(bp.getInputs().containsKey("external_cert_sans"));
130
131                 Node node = bp.getNode_templates().get("test.component.spec");
132
133                 //should not create externalTlsInfo object in node properties
134                 ExternalTlsInfo externalTlsInfo = node.getProperties().getExternal_cert();
135                 assertNull(externalTlsInfo);
136         }
137
138
139         private void assertBlueprintHasNoTlsInfo(Blueprint bp) {
140                 //should not create inputs for tls
141                 assertFalse(bp.getInputs().containsKey("use_tls"));
142
143                 Node node = bp.getNode_templates().get("test.component.spec");
144
145                 //should not create tlsInfo object in node properties
146                 assertNull(node.getProperties().getTls_info());
147         }
148
149         private void assertContainsInputWithDefault(Blueprint bp, String inputName, Object defaultValue) {
150                 LinkedHashMap<String, Object> input = bp.getInputs().get(inputName);
151                 assertNotNull(input);
152                 assertEquals(defaultValue, input.get("default"));
153         }
154
155         private Blueprint createBlueprintFromFile(String path) {
156                 ComponentSpec cs = new ComponentSpec();
157                 cs.createComponentSpecFromFile(path);
158
159                 Blueprint bp = new Blueprint();
160                 bp = bp.createBlueprint(cs, "", this.bpType, "", "");
161                 return bp;
162         }
163 }