summaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/scripts/AdministrationScriptsTest.java
blob: ddd28fd8256645a698b8837a6429c8d677e7a008 (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
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. 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.vid.scripts;


import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp;
import static com.xebialabs.restito.semantics.Action.ok;
import static com.xebialabs.restito.semantics.Action.stringContent;
import static com.xebialabs.restito.semantics.Condition.post;
import static com.xebialabs.restito.semantics.Condition.withHeader;
import static com.xebialabs.restito.semantics.Condition.withPostBodyContaining;
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.text.MessageFormat.format;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import com.google.common.collect.ImmutableList;
import com.xebialabs.restito.semantics.Applicable;
import com.xebialabs.restito.semantics.Condition;
import com.xebialabs.restito.server.StubServer;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.testng.annotations.Test;

public class AdministrationScriptsTest {

    @Test
    public void categoryParameterSh_addReleaseCategory_PostAsExpected() throws Exception {
        final StubServer stubServer = new StubServer();
        final URI scriptSource = AdministrationScriptsTest.class
            .getResource("/scripts/category_parameter.sh").toURI();

        stubServer.start();

        try {

            // given
            Condition[] expectedRequest = {
                post("/vid/maintenance/category_parameter/release"),
                withHeader("Content-Type", "application/json"),
                withPostBodyContaining(asSpacedRegex("{ \"options\" : [ \"2019.08\" ] }"))
            };

            String responseBody = "stubbed response body";
            Applicable[] stubbedResponse = {ok(), stringContent(responseBody)};

            whenHttp(stubServer).match(expectedRequest).then(stubbedResponse);

            // when
            Path script = copyToTempFile(scriptSource);
            Path input = createTempFile("2019.08");

            Pair<Integer, String> result = exec(
                format("sed -i s/8080/{0}/g {1}", String.valueOf(stubServer.getPort()), path(script)),
                format("chmod +x {0}", path(script)),
                format("{0} -o ADD -p {1} -c release", path(script), path(input))
            );

            // then
            assertThat("output is descriptive", result.getRight(), allOf(
                    containsString("Sending request:"),
                    containsString("wget output:"),
                    containsString(responseBody)
                ));
            assertThat("error code is 0",  result.getLeft(), is(0));
            verifyHttp(stubServer).once(expectedRequest);

        } finally {
            stubServer.stop();
        }
    }

    private Path createTempFile(String text) throws IOException {
        Path input = Files.createTempFile("input", ".txt");
        return Files.write(input, ImmutableList.of(text));
    }

    private Path copyToTempFile(URI scriptSource) throws IOException {
        Path script = Files.createTempFile("script", ".sh");
        Files.copy(Paths.get(scriptSource), script, COPY_ATTRIBUTES, REPLACE_EXISTING);
        return script;
    }

    private String path(Path filePath) {
        return filePath.toUri().getPath();
    }

    private Pattern asSpacedRegex(String string) {
        final String spaces = "\\s*";
        return Pattern.compile(""
            + spaces
            + string
                .replaceAll("([\\[\\]{}\\(\\).])", "\\\\$0") // add an escaping-slash before any bracket or dot
                .replace(" ", spaces) // allow 0 or more spaces anywhere
            + spaces
        );
    }

    private Pair<Integer, String> exec(String... cmds) throws IOException, InterruptedException {
        String cmd = "" + String.join(" && ", cmds) + "";
        Process process = Runtime.getRuntime().exec(new String[]{"sh", "-c", cmd});

        int exitCode = process.waitFor();
        String output = IOUtils.toString(
            exitCode == 0 ? process.getInputStream() : process.getErrorStream(), Charset.defaultCharset());

        return ImmutablePair.of(exitCode, cmd + ": " + output);
    }

}