aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/modelloader/restclient/TestBabelServiceClient.java
blob: 169943c99d2bb1e66993c75e3ef522a66d9b3dfe (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
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 European Software Marketing 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.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.modelloader.restclient;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Properties;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.onap.aai.babel.service.data.BabelArtifact;
import org.onap.aai.modelloader.config.ModelLoaderConfig;
import org.onap.aai.modelloader.service.HttpsBabelServiceClientFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.matching.EqualToPattern;

/**
 * Local testing of the Babel service client.
 *
 */
@SpringBootTest
@AutoConfigureWireMock(port = 0)
public class TestBabelServiceClient {

    @Value("${wiremock.server.port}")
    private int wiremockPort;

    @BeforeAll
    public static void setup() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        List<BabelArtifact> artifacts = List.of(
            new BabelArtifact("art1", null, ""),
            new BabelArtifact("art2", null, ""),
            new BabelArtifact("art3", null, ""));
        WireMock.stubFor(
            WireMock.post(WireMock.urlEqualTo("/generate"))
                .withHeader("X-TransactionId", WireMock.equalTo("Test-Transaction-ID-BabelClient"))
                .withHeader("X-FromAppId", WireMock.equalTo("ModelLoader"))
                .withRequestBody(WireMock.matchingJsonPath("$.artifactName", WireMock.equalTo("service-Vscpass-Test")))
                .withRequestBody(WireMock.matchingJsonPath("$.artifactVersion", WireMock.equalTo("1.0")))
                .withRequestBody(WireMock.matchingJsonPath("$.csar", WireMock.matching(".*")))
                .willReturn(
                    WireMock.aResponse()
                        .withHeader("Content-Type", "application/json")
                        .withBody(objectMapper.writeValueAsString(artifacts))));
    }

    @Test
    public void testRestClient() throws BabelServiceClientException, IOException, URISyntaxException {
        String url = "http://localhost:" + wiremockPort;
        Properties configProperties = new Properties();
        configProperties.put("ml.babel.KEYSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
        configProperties.put("ml.babel.KEYSTORE_FILE", "src/test/resources/auth/aai-client-dummy.p12");
        configProperties.put("ml.babel.TRUSTSTORE_PASSWORD", "OBF:1vn21ugu1saj1v9i1v941sar1ugw1vo0");
        // In a real deployment this would be a different file (to the client keystore)
        configProperties.put("ml.babel.TRUSTSTORE_FILE", "src/test/resources/auth/aai-client-dummy.p12");
        configProperties.put("ml.babel.BASE_URL", url);
        configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "/generate");
        configProperties.put("ml.aai.RESTCLIENT_CONNECT_TIMEOUT", "12000");
        configProperties.put("ml.aai.RESTCLIENT_READ_TIMEOUT", "12000");
        BabelServiceClient client =
                new HttpsBabelServiceClientFactory().create(new ModelLoaderConfig(configProperties, "."));
        List<BabelArtifact> result =
                client.postArtifact(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar"),
                        "service-Vscpass-Test", "1.0", "Test-Transaction-ID-BabelClient");
        assertThat(result.size(), is(equalTo(3)));
    }

    @Test
    public void testRestClientHttp() throws BabelServiceClientException, IOException, URISyntaxException {
        String url = "http://localhost:" + wiremockPort;
        Properties configProperties = new Properties();
        configProperties.put("ml.babel.USE_HTTPS", "false");
        configProperties.put("ml.babel.BASE_URL", url);
        configProperties.put("ml.babel.GENERATE_ARTIFACTS_URL", "/generate");
        configProperties.put("ml.aai.RESTCLIENT_CONNECT_TIMEOUT", "3000");
        configProperties.put("ml.aai.RESTCLIENT_READ_TIMEOUT", "3000");
        BabelServiceClient client =
                new HttpsBabelServiceClientFactory().create(new ModelLoaderConfig(configProperties, "."));
        List<BabelArtifact> result =
                client.postArtifact(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar"),
                        "service-Vscpass-Test", "1.0", "Test-Transaction-ID-BabelClient");
        assertThat(result.size(), is(equalTo(3)));
    }


    private byte[] readBytesFromFile(String resourceFile) throws IOException, URISyntaxException {
        return Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI()));
    }
}