aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/rest/MsoRestClientTest.java
blob: ef594af274be8c2960fe1bc10f71470b8488c14d (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
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 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.openecomp.sdc.be.components.distribution.engine.rest;

import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.AnythingPattern;
import com.github.tomakehurst.wiremock.matching.UrlPattern;
import fj.data.Either;
import org.apache.http.HttpHeaders;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.openecomp.sdc.be.components.distribution.engine.DistributionStatusNotificationEnum;
import org.openecomp.sdc.be.components.distribution.engine.DummyDistributionConfigurationManager;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.common.api.ConfigurationSource;
import org.openecomp.sdc.common.http.client.api.HttpResponse;
import org.openecomp.sdc.common.http.config.*;
import org.openecomp.sdc.common.impl.ExternalConfiguration;
import org.openecomp.sdc.common.impl.FSConfigurationSource;
import org.openecomp.sdc.security.SecurityUtil;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

public class MsoRestClientTest {

    private static final String MSO_HOST = "127.0.0.1";
    private static final String MSO_API_URL = "onap/mso/infra/modelDistributions/v1";
    private static final String DISTRIBUTION_ID = "1000";

    private MSORestClient msoRestClient;
    static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
    static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);


    @ClassRule
    public static WireMockRule msoRestServer = new WireMockRule(options()
            .dynamicPort()
            .bindAddress(MSO_HOST));

    @Before
    public void setupMsoServer() throws Exception {
        String encodedPw = "";
        Either<String, String> passkey = SecurityUtil.INSTANCE.decrypt(getPwd());
        if(passkey.isLeft()) {
            encodedPw = passkey.left().value();
        }
        else {
            throw new IllegalArgumentException(passkey.right().value());
        }
        
        msoRestServer.resetToDefaultMappings();
        msoRestServer.stubFor(patch(urlMatching(String.format("/%s%s/(.*)", MSO_API_URL, getDistributionsUrl())))
                .withBasicAuth(getUserName(), encodedPw)
                .withHeader(HttpHeaders.CONTENT_TYPE, containing("application/json"))
                .withRequestBody(matchingJsonPath("$.status"))
                .withRequestBody(matchingJsonPath("$.errorReason", new AnythingPattern()))//error reason is not mandatory
                .willReturn(aResponse().withStatus(200)));
    }

    @Before
    public void setUp() throws Exception {
        DummyDistributionConfigurationManager distributionEngineConfigurationMock = new DummyDistributionConfigurationManager();
        when(distributionEngineConfigurationMock.getConfigurationMock().getMsoConfig()).thenReturn(new MsoDummyConfig(msoRestServer.port()));
        msoRestClient = new MSORestClient();
    }

    @Test
    public void notifyDistributionComplete_emptyErrReason() throws Exception {
        HttpResponse<String> response = msoRestClient.notifyDistributionComplete(DISTRIBUTION_ID, DistributionStatusNotificationEnum.DISTRIBUTION_COMPLETE_OK, "");
        assertThat(response.getStatusCode()).isEqualTo(200);
    }

    private int getNumOfRetries() {
        return ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getMsoConfig().getHttpClientConfig().getNumOfRetries();
    }

    @Test
    public void notifyDistributionComplete_noErrReason() throws Exception {
        HttpResponse<String> response = msoRestClient.notifyDistributionComplete(DISTRIBUTION_ID, DistributionStatusNotificationEnum.DISTRIBUTION_COMPLETE_OK, null);
        assertThat(response.getStatusCode()).isEqualTo(200);
    }

    @Test
    public void notifyDistributionComplete_completeWithError() throws Exception {
        HttpResponse<String> response = msoRestClient.notifyDistributionComplete(DISTRIBUTION_ID, DistributionStatusNotificationEnum.DISTRIBUTION_COMPLETE_ERROR, "my reason");
        assertThat(response.getStatusCode()).isEqualTo(200);
    }

    @Test
    public void testRetries() throws Exception {
        int readTimeout = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getMsoConfig().getHttpClientConfig().getTimeouts().getReadTimeoutMs();
        int expectedNumOfRetries = getNumOfRetries();

        UrlPattern msoReqUrlPattern = urlMatching(String.format("/%s%s/(.*)", MSO_API_URL, getDistributionsUrl()));
        msoRestServer.stubFor(patch(msoReqUrlPattern)
                .willReturn(new ResponseDefinitionBuilder().withFixedDelay(readTimeout + 1)));
        HttpResponse<String> response = msoRestClient.notifyDistributionComplete(DISTRIBUTION_ID, DistributionStatusNotificationEnum.DISTRIBUTION_COMPLETE_ERROR, "my reason");
        verify(expectedNumOfRetries + 1, patchRequestedFor(msoReqUrlPattern));
        assertThat(response.getStatusCode()).isEqualTo(500);
    }

    private static String getDistributionsUrl() {
        return "/distributions";
    }

    private static String getUserName() {
        return "asdc";
    }

    private static String getPwd() {
        return "OTLEp5lfVhYdyw5EAtTUBQ==";
    }

    private static class MsoDummyConfig extends ExternalServiceConfig {
        int port;

        MsoDummyConfig(int port) {
            this.port = port;

            BasicAuthorization basicAuthorization = new BasicAuthorization();
            basicAuthorization.setUserName(MsoRestClientTest.getUserName());
            basicAuthorization.setPassword(getPwd());
            HttpClientConfig httpClientConfig = new HttpClientConfig(new Timeouts(500, 2000), basicAuthorization);
            httpClientConfig.setNumOfRetries(getNumOfRetries());
            super.setHttpClientConfig(httpClientConfig);

            HttpRequestConfig httpRequestConfig = new HttpRequestConfig();
            httpRequestConfig.setServerRootUrl(String.format("http://%s:%s/%s", MSO_HOST, this.port, MSO_API_URL));
            httpRequestConfig.getResourceNamespaces().put(MSORestClient.DISTRIBUTIONS_RESOURCE_CONFIG_PARAM, getDistributionsUrl());
            super.setHttpRequestConfig(httpRequestConfig);
        }

        int getNumOfRetries() {
            return 1;
        }
    }

}