aboutsummaryrefslogtreecommitdiffstats
path: root/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java
blob: d24fe1e7154115e0823ef80a179b4f01bbd7cc1d (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright 2017 Huawei Technologies Co., Ltd.
 *
 * 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.cli.validation;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandInvalidSample;
import org.onap.cli.fw.error.OnapCommandProductVersionInvalid;
import org.onap.cli.fw.registrar.OnapCommandRegistrar;
import org.onap.cli.fw.schema.OnapCommandSchemaInfo;
import org.onap.cli.main.OnapCli;
import org.onap.cli.main.conf.OnapCliConstants;
import org.onap.cli.moco.OnapCommandSample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.onap.cli.fw.error.OnapCommandInvalidSchema;
import org.onap.cli.fw.utils.OnapCommandDiscoveryUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class OnapValidationTest {

    public static final String SAMPLE_VERSION = "open_cli_sample_version";
    public static final String SAMPLE_VERSION_1_0 = "1.0";

    public static final String SAMPLE_COMMAND_NAME = "name";
    public static final String SAMPLE_PRODUCT = "version";
    public static final String SAMPLE_LIST = "samples";
    public static final String SAMPLE_DESCRIPTION = "name";
    public static final String SAMPLE_INPUT = "input";
    public static final String SAMPLE_OUTPUT = "output";
    public static final String SAMPLE_MOCO = "moco";

    OnapCli cli = new OnapCli();

    private static Logger LOG = LoggerFactory.getLogger(OnapValidationTest.class);
    private static Gson gson = new GsonBuilder().serializeNulls().create();

    private void handle(String[] args) {
        cli.resetExitCode();
        cli.setArgs(args);
        cli.handle();
    }

    /**
     * Add Test annotation to this method for running the stability and perf testing
     * @throws OnapCommandProductVersionInvalid
     * @throws OnapCommandException
     * @throws InterruptedException
     */
    //@Test
    public void stablitityPerformanceBeijingTest() throws OnapCommandProductVersionInvalid, OnapCommandException {
        System.out.println(new Date());
        while (true) {
            OnapCommandRegistrar.getRegistrar().setEnabledProductVersion("onap-beijing");
            for (String cmd : OnapCommandRegistrar.getRegistrar().listCommandsForEnabledProductVersion()) {
                System.out.println(cmd + ":");
                Date start = new Date();
                this.handle(new String[] { cmd, "-V"});
                Date end = new Date();
                System.out.println("[ Total time " + (end.getTime() - start.getTime()) + " ms ]\n");
                try {
                    Thread.sleep(2000); //NOSONAR
                } catch (InterruptedException e) {
                    System.out.println(new Date());
                }
            }
        }
    }

    @Test
    public void validateCommandSchemas() throws OnapCommandException {
        for (String version: OnapCommandRegistrar.getRegistrar().getAvailableProductVersions()) {
            OnapCommandRegistrar.getRegistrar().setEnabledProductVersion(version);
            System.out.println(version);
            System.out.println("==========================\n\n");
            for (OnapCommandSchemaInfo sch : OnapCommandRegistrar.getRegistrar().listCommandInfo()) {
                if (sch.isIgnore()) {
                    continue;
                }
                if (sch.getProduct() != null && sch.getProduct().equals(version)) {
                    System.out.println(
                    "************************* validate '" + sch.getCmdName() + "' *******************************");
                    OnapCommandRegistrar.getRegistrar().setEnabledProductVersion("open-cli");
                    this.handle(new String[] { "schema-validate", "-l", sch.getSchemaName(), "-i"});
                }
            }
        }
    }

    @Test
    public void genReadTheDocs() throws OnapCommandException {
        for (String version: OnapCommandRegistrar.getRegistrar().getAvailableProductVersions()) {
            OnapCommandRegistrar.getRegistrar().setEnabledProductVersion(version);
            System.out.println(version);
            System.out.println("==========================\n\n");
            int i = 1;
            List<OnapCommandSchemaInfo> cmds = OnapCommandRegistrar.getRegistrar().listCommandInfo();
            Collections.sort(cmds);
            for (OnapCommandSchemaInfo sch : cmds) {
                if (sch.isIgnore()) {
                    continue;
                }
                if (sch.getProduct() != null && sch.getProduct().equals(version)) {
                    System.out.println("[" + i++ + "] " + sch.getCmdName());
                    System.out.println("-----------------------------------------------\n\n");
                    this.handle(new String[] { sch.getCmdName(), "-h"});
                    System.out.println("\n");
                }
            }
        }
    }

    @Test
    public void collectSampleYamlTest() {
        try {
            File root = new File("../../");
            String sampleFileName = "target/sample.rst";

            FileUtils.deleteQuietly(new File(sampleFileName));
            Map<String, List<OnapCommandSample>> discoveredYamls = discoverYamls(root);

            writeSamples(new File(sampleFileName), discoveredYamls);
        } catch (IOException e) {
            fail();
        }
    }

    private void writeSamples(File dest, Map<String, List<OnapCommandSample>> cliProductSamples) throws IOException {

        for (String product: cliProductSamples.keySet()) {
            FileUtils.write(dest, "\n" + product, "UTF-8", true);
            FileUtils.write(dest, "\n========\n\n", "UTF-8", true);

            for(OnapCommandSample sample: cliProductSamples.get(product)) {
                FileUtils.write(dest, "\n\n" + sample.getCommandName(), "UTF-8", true);
                FileUtils.write(dest, "\n" + String.join("", Collections.nCopies(sample.getCommandName().length(), "-"))+ "\n", "UTF-8", true);

                if (!sample.getInput().isEmpty()) {
                    FileUtils.write(dest, "\ninput::\n\n " + sample.getInput() + "\n", "UTF-8", true);
                }

                if (!sample.getOutput().isEmpty()) {
                    FileUtils.write(dest, "\noutput::\n\n " + sample.getOutput().replaceAll("\n", "\n ").trim(), "UTF-8", true);
                }
            }
        }
    }

    public static Map<String, List<OnapCommandSample>> discoverYamls(File path) throws IOException {
        Map<String, List<OnapCommandSample>> cliProductSamples = new HashMap<>();

        Stream<Path> walk = Files.walk(path.toPath());
        walk.filter(p -> (p.toString().contains("src/test/resources/onap-cli-sample")))
                .filter(p -> p.toString().endsWith("sample.yaml"))
                .forEach(p -> {
                    collectSamples(new File(p.toUri()), cliProductSamples);
                });

        return cliProductSamples;
    }

    private static void collectSamples(File file, Map<String, List<OnapCommandSample>> result) {
        List<OnapCommandSample> loadSamples;
        try {
            loadSamples = loadSamples(file);
            loadSamples.stream().forEach(sample -> {
                if (!result.containsKey(sample.getProduct())) {
                    result.put(sample.getProduct(), new ArrayList<>());
                }
                result.get(sample.getProduct()).add(sample);
            });
        } catch (OnapCommandInvalidSample e) {
            LOG.error("Failed to read sample file", e);
        }
    }

    private static List<OnapCommandSample> loadSamples(File file) throws OnapCommandInvalidSample {
        try {
            return loadSamples(new FileInputStream(file), file.getName());
        } catch (FileNotFoundException e) {
            throw new OnapCommandInvalidSample(file.getName(), e);
        }
    }

    private static List<OnapCommandSample> loadSamples(InputStream inputStream, String fileName) throws OnapCommandInvalidSample {
        List<OnapCommandSample> samples = new ArrayList<>();
        Map<String, ?> values = null;
        try {
            values = OnapCommandDiscoveryUtils.loadYaml(inputStream);
        } catch (OnapCommandInvalidSchema e) { //NOSONAR
            LOG.error("Failed to load schema file", e.getMessage());
        }

        OnapCommandSample sample = new OnapCommandSample();

        if (!getValue(values, SAMPLE_VERSION).equals(SAMPLE_VERSION_1_0)) {
            throw new OnapCommandInvalidSample(fileName, "Invalid sample version " + getValue(values, SAMPLE_VERSION));
        }

        sample.setCommandName(getValue(values, SAMPLE_COMMAND_NAME));
        sample.setProduct(getValue(values, SAMPLE_PRODUCT));

        //Retrieve the samples
        values = (Map<String, Map<String, String>>) values.get(SAMPLE_LIST);

        for (String s: values.keySet()) {
            Map<String, ?> sMap = (Map<String, ?>)values.get(s);
            sample.setDescription(getValue(sMap, SAMPLE_DESCRIPTION));
            sample.setInput(getValue(sMap, SAMPLE_INPUT));
            sample.setOutput(getValue(sMap, SAMPLE_OUTPUT));
            sample.setMoco(getValue(sMap, SAMPLE_MOCO));
            samples.add(sample);
        }

        return samples;
    }

    private static String getValue(Map<String, ?> map, String prpName) {
        Object o = map.get(prpName);
        if (o != null) {
            return o.toString();
        }

        return "";
    }

    @Test
    public void testVerify() throws OnapCommandException {
        OnapCommandRegistrar.getRegistrar().setEnabledProductVersion("open-cli");
        OnapCli onapCli = new OnapCli(new String[]{"sample-test-verify", "--verify"});
        onapCli.handle();
        assertEquals(OnapCliConstants.EXIT_SUCCESS, onapCli.getExitCode());
    }
    @Test
    public void testOnapCommandSchemaInfoForUnknownFields(){
        OnapCommandSchemaInfo ocsi = new OnapCommandSchemaInfo();
        String testExp = "{\"schemaName\":\"testSchema\",\"schemaURI\":\"testUri\",\"unknownField\":\"unknown\"}";
        ocsi= gson.fromJson(testExp,OnapCommandSchemaInfo.class);
        assertNotNull(ocsi);
    }
 }