summaryrefslogtreecommitdiffstats
path: root/dictionaryvalidation/src/test/java/org/onap/validation/cli/MainCITest.java
blob: 307d3887ae0669d8a2ccaaaeefcb0dfa4eb35e35 (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
/*
 *Copyright 2020 Nokia
 *
 * 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.
 *
 */
package org.onap.validation.cli;


import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.onap.validation.cli.command.validate.OutputFilePathGenerator;
import org.onap.validation.cli.command.validate.ResponseModel;
import org.onap.validation.cli.command.validate.ResponseStorage;
import org.onap.validation.cli.core.Cli;
import org.onap.validation.cli.core.CommandException;
import org.onap.validation.cli.core.Console;
import org.onap.validation.cli.command.validate.ToResponseModelConverter;
import org.onap.validation.yaml.YamlLoadingUtils;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
class MainCITest {

    public static final int JSON_RESPONSE_INDEX = 3;
    public static final int OPERATION_INFO_MSG_INDEX = 5;
    public static final int NO_ERROR = 0;
    @Mock
    private Console console;
    @Mock
    private ResponseStorage responseStorage;
    private final OutputFilePathGenerator filePathGenerator = new OutputFilePathGenerator();

    @Test
    void shouldReportThatPathToFileWasNotProvided() {
        // when
        final int statusCode = Main.run(new String[0], console, responseStorage);

        // then
        assertThat(statusCode).isEqualTo(Cli.INTERNAL_ERROR_STATUS_CODE);
        final ArgumentCaptor<CommandException> exceptionCaptor = ArgumentCaptor.forClass(CommandException.class);
        verify(console).error(exceptionCaptor.capture());
        verify(console).error(Cli.APPLICATION_INTERNAL_ERROR_MSG);
        assertThat(exceptionCaptor.getValue().getMessage()).isEqualTo("Command argument is missing: provide a path to file");

    }

    @Test
    void shouldReportThatFileDoseNotExist() throws IOException {
        // when
        final String pathToYaml = "/path/to/nonExisting.yaml";
        final int statusCode = Main.run(new String[]{pathToYaml}, console, new ResponseStorage());

        // then
        assertThat(statusCode).isEqualTo(Cli.INTERNAL_ERROR_STATUS_CODE);
        final ArgumentCaptor<CommandException> exceptionCaptor = ArgumentCaptor.forClass(CommandException.class);
        verify(console).error(exceptionCaptor.capture());
        verify(console).error(Cli.APPLICATION_INTERNAL_ERROR_MSG);
        assertThat(exceptionCaptor.getValue().getMessage()).isEqualTo("File '/path/to/nonExisting.yaml' does not exist!");
        verify(responseStorage, never()).store(eq(Path.of(pathToYaml)), anyString());
    }

    @Test
    void shouldReportThatFileIsBroken() throws IOException {
        // when
        final String pathToFile = YamlLoadingUtils.getUrlForGivenPath("Not_Yaml_File.txt").getPath();
        final int statusCode = Main.run(new String[]{pathToFile}, console, new ResponseStorage());

        // then
        assertThat(statusCode).isEqualTo(Cli.INTERNAL_ERROR_STATUS_CODE);
        final ArgumentCaptor<CommandException> exceptionCaptor = ArgumentCaptor.forClass(CommandException.class);
        verify(console).error(exceptionCaptor.capture());
        verify(console).error(Cli.APPLICATION_INTERNAL_ERROR_MSG);
        assertThat(exceptionCaptor.getValue().getMessage()).isEqualTo("Provided yaml file has invalid structure!");
        verify(responseStorage, never()).store(eq(Path.of(pathToFile)), anyString());
    }

    @Test
    void shouldValidateProperYamlFile() throws IOException {
        // when
        final String path = YamlLoadingUtils.getUrlForGivenPath(YamlLoadingUtils.PATH_TO_SIMPLE_VALID_SCHEMA).getPath();
        final int statusCode = Main.run(new String[]{path}, console, responseStorage);

        // then
        assertThat(statusCode).isEqualTo(Cli.PASS_STATUS_CODE);
        assertThatResponseWasLoggedAtConsole(
                path,
                ResponseModel.ResponseStatus.PASS,
                NO_ERROR
        );
    }

    @Test
    void shouldValidateYamlWithErrors() throws IOException {
        // when
        final String path = YamlLoadingUtils.getUrlForGivenPath(YamlLoadingUtils.PATH_TO_YAML_WITH_WRONG_VALUES).getPath();
        final int statusCode = Main.run(new String[]{path}, console, responseStorage);

        // then
        assertThat(statusCode).isEqualTo(Cli.FAILED_STATUS_CODE);
        assertThatResponseWasLoggedAtConsole(
                path,
                ResponseModel.ResponseStatus.FAILED,
                4
        );
    }

    private void assertThatResponseWasLoggedAtConsole(String pathToFile, ResponseModel.ResponseStatus responseStatus, int expectedErrors) throws IOException {
        ArgumentCaptor<String> consoleInfoCaptor = ArgumentCaptor.forClass(String.class);
        verify(console, times(6)).info(consoleInfoCaptor.capture());
        final List<String> allValues = consoleInfoCaptor.getAllValues();
        final String json = allValues.get(JSON_RESPONSE_INDEX);
        final ResponseModel responseModel = ToResponseModelConverter.toModel(json);
        assertThat(responseModel.getFile()).isEqualTo(pathToFile);
        assertThat(responseModel.getStatus()).isEqualTo(responseStatus);
        assertThat(responseModel.getErrors()).hasSize(expectedErrors);
        assertThat(consoleInfoCaptor.getAllValues().get(OPERATION_INFO_MSG_INDEX)).isEqualTo(Cli.APPLICATION_EXIT_SUCCESSFULLY_MSG);
        verify(responseStorage).store(eq(filePathGenerator.responsePathFor(Path.of(pathToFile))), eq(json));
    }

}