summaryrefslogtreecommitdiffstats
path: root/mod/bpgenerator/onap/src/test/java/org/onap/blueprintgenerator/test/TlsInfoTest.java
blob: 433240f4b2822d0caed298a1d42fa627ce210f53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*============LICENSE_START=======================================================
org.onap.dcae
================================================================================
Copyright (c) 2020 Nokia. All rights reserved.
Copyright (c) 2020 AT&T. All rights reserved.
================================================================================
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
============LICENSE_END=========================================================
*/

package org.onap.blueprintgenerator.test;

import org.junit.Test;
import org.junit.runners.Parameterized;
import org.onap.blueprintgenerator.model.common.Input;
import org.onap.blueprintgenerator.model.common.Node;
import org.onap.blueprintgenerator.model.common.OnapBlueprint;
import org.onap.blueprintgenerator.model.common.ExternalCertificateParameters;
import org.onap.blueprintgenerator.model.common.ExternalTlsInfo;
import org.onap.blueprintgenerator.model.dmaap.TlsInfo;

import java.nio.file.Paths;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

/**
 * Test Case for Tls Info
 *
 */
// @RunWith(Parameterized.class)
public class TlsInfoTest extends BlueprintGeneratorTests {

    @Parameterized.Parameter
    public char bpType;

    @Parameterized.Parameters(name = "Blueprint type: {0}")
    public static List<Character> data() {
        return Arrays.asList('o', 'd');
    }

    /**
     * Test Case for Tls True and External TLS True
     *
     */
    @Test
    public void useTlsTrueAndUseExternalTlsTrueTest() {
        OnapBlueprint bp =
            createBlueprintFromFile(
                Paths.get(
                    "src",
                    "test",
                    "resources",
                    "componentspecs",
                    useTlsTrueAndUseExternalTlsTrueTest)
                    .toFile()
                    .getAbsolutePath());

        assertBlueprintContainsExternalTlsInfoWithUseFlagDefault(bp, true);
        assertBlueprintContainsTlsInfoWithUseFlagDefault(bp, true);
    }
    /**
     * Test Case for Tls False and External TLS False
     *
     */
    @Test
    public void useTlsFalseAndUseExternalTlsFalseTest() {
        OnapBlueprint bp =
            createBlueprintFromFile(
                Paths.get(
                    "src",
                    "test",
                    "resources",
                    "componentspecs",
                    useTlsFalseAndUseExternalTlsFalseTest)
                    .toFile()
                    .getAbsolutePath());

        assertBlueprintContainsExternalTlsInfoWithUseFlagDefault(bp, false);
        assertBlueprintContainsTlsInfoWithUseFlagDefault(bp, false);
    }

    /**
     * Test Case for Tls True and No External TLS Flag
     *
     */
    @Test
    public void useTlsTrueAndNoExternalTlsFlagTest() {
        OnapBlueprint bp =
            createBlueprintFromFile(
                Paths.get(
                    "src",
                    "test",
                    "resources",
                    "componentspecs",
                    useTlsTrueAndNoExternalTlsFlagTest)
                    .toFile()
                    .getAbsolutePath());

        assertBlueprintContainsTlsInfoWithUseFlagDefault(bp, true);
        assertBlueprintHasNoExternalTlsInfo(bp);
    }

    /**
     * Test Case for No Tls Info
     *
     */
    @Test
    public void noTlsInfo() {
        OnapBlueprint bp =
            createBlueprintFromFile(
                Paths.get("src", "test", "resources", "componentspecs", noTlsInfo)
                    .toFile()
                    .getAbsolutePath());

        assertBlueprintHasNoTlsInfo(bp);
        assertBlueprintHasNoExternalTlsInfo(bp);
    }

    private void assertBlueprintContainsExternalTlsInfoWithUseFlagDefault(
        OnapBlueprint bp, boolean useFlagDefault) {
        // should create proper inputs
        assertContainsInputWithDefault(bp, "external_cert_use_external_tls", useFlagDefault);
        assertContainsInputWithDefault(bp, "external_cert_ca_name", "\"RA\"");
        assertContainsInputWithDefault(bp, "external_cert_cert_type", "\"P12\"");
        assertContainsInputWithDefault(bp, "external_cert_common_name", "\"sample.onap.org\"");
        assertContainsInputWithDefault(
            bp, "external_cert_sans", "\"sample.onap.org,component.sample.onap.org\"");

        Node node = bp.getNode_templates().get("test.component.spec");

        // should create proper externalTlsInfo object in node properties
        ExternalTlsInfo externalTlsInfo = node.getProperties().getExternal_cert();
        assertNotNull(externalTlsInfo);

        assertEquals("external_cert_ca_name", externalTlsInfo.getCaName().getBpInputName());
        assertEquals("external_cert_cert_type", externalTlsInfo.getCertType().getBpInputName());
        assertEquals(
            "external_cert_use_external_tls", externalTlsInfo.getUseExternalTls().getBpInputName());
        assertEquals("/opt/app/dcae-certificate/", externalTlsInfo.getExternalCertDirectory());

        ExternalCertificateParameters extCertParams =
            externalTlsInfo.getExternalCertificateParameters();
        assertNotNull(extCertParams);

        assertEquals("external_cert_common_name", extCertParams.getCommonName().getBpInputName());
        assertEquals("external_cert_sans", extCertParams.getSans().getBpInputName());
    }

    private void assertBlueprintContainsTlsInfoWithUseFlagDefault(
        OnapBlueprint bp, boolean useFlagDefault) {
        // shold create proper inputs
        assertContainsInputWithDefault(bp, "use_tls", useFlagDefault);

        Node node = bp.getNode_templates().get("test.component.spec");

        // should create proper tlsInfo object in node properties
        TlsInfo tlsInfo = node.getProperties().getTls_info();
        assertEquals("use_tls", tlsInfo.getUseTls().getBpInputName());
        assertEquals("/opt/app/dcae-certificate/", tlsInfo.getCertDirectory());
    }

    private void assertBlueprintHasNoExternalTlsInfo(OnapBlueprint bp) {
        // should not create inputs for external tls
        assertFalse(bp.getInputs().containsKey("external_cert_use_external_tls"));
        assertFalse(bp.getInputs().containsKey("external_cert_common_name"));
        assertFalse(bp.getInputs().containsKey("external_cert_ca_name"));
        assertFalse(bp.getInputs().containsKey("external_cert_sans"));

        Node node = bp.getNode_templates().get("test.component.spec");

        // should not create externalTlsInfo object in node properties
        ExternalTlsInfo externalTlsInfo = node.getProperties().getExternal_cert();
        assertNull(externalTlsInfo);
    }

    private void assertBlueprintHasNoTlsInfo(OnapBlueprint bp) {
        // should not create inputs for tls
        assertFalse(bp.getInputs().containsKey("use_tls"));

        Node node = bp.getNode_templates().get("test.component.spec");

        // should not create tlsInfo object in node properties
        assertNull(node.getProperties().getTls_info());
    }

    private void assertContainsInputWithDefault(
        OnapBlueprint bp, String inputName, Object defaultValue) {
        LinkedHashMap<String, Object> input = bp.getInputs().get(inputName);
        assertNotNull(input);
        assertEquals(defaultValue, input.get("default"));
    }

    private OnapBlueprint createBlueprintFromFile(String path) {
        onapComponentSpec = onapComponentSpecService.createComponentSpecFromFile(path);
        Input input = onapTestUtils.getInput(path, "", "", "", "o", "");
        OnapBlueprint onapBlueprint = onapBlueprintCreatorService.createBlueprint(onapComponentSpec, input);
        return onapBlueprint;
    }
}