aboutsummaryrefslogtreecommitdiffstats
path: root/validate/validation/src/test/java/org/onap/cli/validation/OnapValidationTest.java
blob: 6a6d38239ce6946913fe30a4d39d31b78dee2e33 (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
/*
 * 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.fail;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.aspectj.lang.annotation.After;
import org.junit.Ignore;
import org.junit.Test;
import org.onap.cli.fw.OnapCommandRegistrar;
import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.utils.ExternalSchema;
import org.onap.cli.main.OnapCli;
import org.onap.cli.moco.OnapCommandHttpMocoServer;
import org.onap.cli.moco.OnapCommandSample;

public class OnapValidationTest {

    OnapCli cli = null;

    /**
     * Clean up.
     */
    @After(value = "")
    public void cleanup() {
        if (this.cli != null) {
            if (cli.getExitCode() != 0) {
                // Fail test case
            }
        }
    }

    private void handle(String[] args) {
        cli = new OnapCli(args);
        cli.handle();
    }

    @Test
    public void validateCommandsResult() throws IOException, OnapCommandException {
        for (String version: OnapCommandRegistrar.getRegistrar().getAvailableProductVersions()) {
            OnapCommandRegistrar.getRegistrar().setEnabledProductVersion(version);
            System.out.println(version);
            System.out.println("==========================\n\n");
            for (ExternalSchema sch : OnapCommandRegistrar.getRegistrar().listCommandInfo()) {
                if (sch.getCmdVersion().equals(version)) {
                    System.out.println(
                    "************************* validate '" + sch.getCmdName() + "' *******************************");
                    OnapCommandRegistrar.getRegistrar().setEnabledProductVersion("cli-1.0");
                    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;
            for (ExternalSchema sch : OnapCommandRegistrar.getRegistrar().listCommandInfo()) {
                if (sch.getCmdVersion().equals(version)) {
                    System.out.println("[" + i++ + "] " + sch.getCmdName());
                    System.out.println("-----------------------------------------------\n\n");
                    this.handle(new String[] { sch.getCmdName(), "-h"});
                    System.out.println("\n");
                }
            }
        }
    }

    @Ignore
    @Test
    public void validateCommands() throws OnapCommandException {
        OnapCommandHttpMocoServer server = new OnapCommandHttpMocoServer();
        server.verifySamples();
    }

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

            FileUtils.deleteQuietly(new File(sampleFileName));
            Map<String, List<OnapCommandSample>> discoveredYamls = OnapCommandHttpMocoServer.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);
                }
            }
        }
    }

 }