summaryrefslogtreecommitdiffstats
path: root/csarvalidation/src/test/java/org/onap/cvc/csar/CsarValidatorTest.java
blob: 299aff2189083ed77e7ba9bbef7deb87e58ad4c4 (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
/**
 * Copyright 2017 Huawei Technologies Co., Ltd.
 * Copyright 2020 Nokia
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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.
 */

package org.onap.cvc.csar;

import org.apache.commons.lang3.StringUtils;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.onap.cli.fw.cmd.OnapCommand;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.output.OnapCommandResult;
import org.onap.cli.fw.output.OnapCommandResultAttribute;
import org.onap.cli.main.OnapCli;

import java.net.URISyntaxException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.onap.cvc.csar.cc.sol004.IntegrationTestUtils.absoluteFilePath;


public class CsarValidatorTest {

    private static final String NO_CERTIFICATE_RULE = "r130206";
    private static final String OPERATION_STATUS_FAILED = "FAILED";

    @Test
    public void shouldReportErrorAsWarningWhenErrorIsIgnored() throws URISyntaxException {
        // given
        OnapCliWrapper cli = new OnapCliWrapper(new String[]{
            "--product", "onap-dublin",
            "csar-validate",
            "--format", "json",
            "--pnf",
            "--csar", absoluteFilePath("pnf/r130206/csar-option1-warning-2.csar")});

        // when
        cli.handle();

        // then
        final OnapCommandResult onapCommandResult = cli.getCommandResult();
        assertTrue(onapCommandResult.getOutput().toString().contains(
            "\"warnings\":[{\"vnfreqNo\":\"R130206\",\"code\":\"0x1006\",\"message\":\"Warning. Consider adding package "
                + "integrity and authenticity assurance according to ETSI NFV-SOL 004 Security Option 1\",\"file\":\"\",\"lineNumber\":-1}]}"));
    }

    @Test
    public void shouldReportThanVnfValidationFailed() throws URISyntaxException {
        // given
        OnapCliWrapper cli = new OnapCliWrapper(new String[]{
            "--product", "onap-dublin",
            "csar-validate",
            "--format", "json",
            "--csar", absoluteFilePath("VoLTE.csar")});

        // when
        cli.handle();

        // then
        final OnapCommandResult onapCommandResult = cli.getCommandResult();
        verifyThatOperation(onapCommandResult, OPERATION_STATUS_FAILED);
        verifyThatXRulesFails(onapCommandResult, 7);
        verifyThatOperationFinishedWithoutAnyError(cli);
    }


    @Test
    public void shouldReportThatPnfValidationFailedWhenCsarDoNotHaveCertificate_allOtherRulesShouldPass() throws URISyntaxException {
        // given
        OnapCliWrapper cli = new OnapCliWrapper(new String[]{
            "--product", "onap-dublin",
            "csar-validate",
            "--format", "json",
            "--pnf",
            "--csar", absoluteFilePath("pnf/r972082/validFile.csar")});
        // when
        cli.handle();

        // then
        final OnapCommandResult onapCommandResult = cli.getCommandResult();
        verifyThatOperation(onapCommandResult, OPERATION_STATUS_FAILED);
        verifyThatXRulesFails(onapCommandResult, 1);
        verifyThatRuleFails(onapCommandResult, NO_CERTIFICATE_RULE);
        verifyThatOperationFinishedWithoutAnyError(cli);
    }

    @Test
    public void shouldReportThatPnfValidationFailedWhenZipDoNotHaveCertificate_allOtherRulesShouldPass() throws URISyntaxException {
        // given
        OnapCliWrapper cli = new OnapCliWrapper(new String[]{
            "--product", "onap-dublin",
            "csar-validate",
            "--format", "json",
            "--pnf",
            "--csar", absoluteFilePath("pnf/signed-package-valid-signature.zip")});

        // when
        cli.handle();

        // then
        final OnapCommandResult onapCommandResult = cli.getCommandResult();
        verifyThatOperation(onapCommandResult, OPERATION_STATUS_FAILED);
        verifyThatXRulesFails(onapCommandResult, 1);
        verifyThatRuleFails(onapCommandResult, NO_CERTIFICATE_RULE);
        verifyThatOperationFinishedWithoutAnyError(cli);
    }

    private void verifyThatXRulesFails(OnapCommandResult onapCommandResult, int noOfRules) {
        verifyDataOccurrenceInResult(onapCommandResult, "\"passed\":false", noOfRules);
    }

    private void verifyThatRuleFails(OnapCommandResult onapCommandResult, String nameOfRule) {
        final String value = String.format("\"passed\":false,\"vnfreqName\":\"%s\"", nameOfRule);
        verifyDataOccurrenceInResult(onapCommandResult, value, 1);
    }

    private void verifyDataOccurrenceInResult(OnapCommandResult onapCommandResult, String data, int dataOccurrence) {
        final String json = getResultValue(onapCommandResult, "results");
        int occurrence = StringUtils.countMatches(json, data);
        Assertions.assertThat(occurrence).isEqualTo(dataOccurrence);
    }

    private void verifyThatOperation(OnapCommandResult onapCommandResult, String operationStatus) {
        final String value = getResultValue(onapCommandResult, "criteria");
        Assertions.assertThat(value).isEqualTo(operationStatus);
    }

    private String getResultValue(OnapCommandResult onapCommandResult, String attributeName) {
        final OnapCommandResultAttribute results = getOnapCommandResultAttribute(onapCommandResult, attributeName);
        return results.getValues().get(0);
    }

    private OnapCommandResultAttribute getOnapCommandResultAttribute(OnapCommandResult onapCommandResult, String name) {
        return onapCommandResult.getRecords().stream()
                .filter(it -> it.getName().equals(name))
                .findFirst()
                .orElseThrow(() -> new RuntimeException(String.format("Unable to find '%s' attribute!", name)));
    }

    private void verifyThatOperationFinishedWithoutAnyError(OnapCliWrapper cli) {
        assertEquals(0, cli.getExitCode());
    }

    static class OnapCliWrapper extends OnapCli {

        private OnapCommandResult commandResult;

        OnapCliWrapper(String[] args) {
            super(args);
        }

        @Override
        public void handleTracking(OnapCommand cmd) throws OnapCommandException {
            super.handleTracking(cmd);
            this.commandResult = cmd.getResult();
        }

        OnapCommandResult getCommandResult() {
            return this.commandResult;
        }
    }
}