CSIT Fix for SDC-2585
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / distribution / engine / rest / MsoRestClientTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.distribution.engine.rest;
22
23 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
24 import com.github.tomakehurst.wiremock.junit.WireMockRule;
25 import com.github.tomakehurst.wiremock.matching.AnythingPattern;
26 import com.github.tomakehurst.wiremock.matching.UrlPattern;
27 import fj.data.Either;
28 import org.apache.http.HttpHeaders;
29 import org.junit.Before;
30 import org.junit.ClassRule;
31 import org.junit.Test;
32 import org.openecomp.sdc.be.components.distribution.engine.DistributionStatusNotificationEnum;
33 import org.openecomp.sdc.be.components.distribution.engine.DummyDistributionConfigurationManager;
34 import org.openecomp.sdc.be.config.ConfigurationManager;
35 import org.openecomp.sdc.common.api.ConfigurationSource;
36 import org.openecomp.sdc.common.http.client.api.HttpResponse;
37 import org.openecomp.sdc.common.http.config.*;
38 import org.openecomp.sdc.common.impl.ExternalConfiguration;
39 import org.openecomp.sdc.common.impl.FSConfigurationSource;
40 import org.openecomp.sdc.security.SecurityUtil;
41
42 import static com.github.tomakehurst.wiremock.client.WireMock.*;
43 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.mockito.Mockito.when;
46
47 public class MsoRestClientTest {
48
49     private static final String MSO_HOST = "127.0.0.1";
50     private static final String MSO_API_URL = "onap/mso/infra/modelDistributions/v1";
51     private static final String DISTRIBUTION_ID = "1000";
52
53     private MSORestClient msoRestClient;
54     static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
55     static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
56
57
58     @ClassRule
59     public static WireMockRule msoRestServer = new WireMockRule(options()
60             .dynamicPort()
61             .bindAddress(MSO_HOST));
62
63     @Before
64     public void setupMsoServer() throws Exception {
65         String encodedPw = "";
66         Either<String, String> passkey = SecurityUtil.INSTANCE.decrypt(getPwd());
67         if(passkey.isLeft()) {
68             encodedPw = passkey.left().value();
69         }
70         else {
71             throw new IllegalArgumentException(passkey.right().value());
72         }
73         
74         msoRestServer.resetToDefaultMappings();
75         msoRestServer.stubFor(patch(urlMatching(String.format("/%s%s/(.*)", MSO_API_URL, getDistributionsUrl())))
76                 .withBasicAuth(getUserName(), encodedPw)
77                 .withHeader(HttpHeaders.CONTENT_TYPE, containing("application/json"))
78                 .withRequestBody(matchingJsonPath("$.status"))
79                 .withRequestBody(matchingJsonPath("$.errorReason", new AnythingPattern()))//error reason is not mandatory
80                 .willReturn(aResponse().withStatus(200)));
81     }
82
83     @Before
84     public void setUp() throws Exception {
85         DummyDistributionConfigurationManager distributionEngineConfigurationMock = new DummyDistributionConfigurationManager();
86         when(distributionEngineConfigurationMock.getConfigurationMock().getMsoConfig()).thenReturn(new MsoDummyConfig(msoRestServer.port()));
87         msoRestClient = new MSORestClient();
88     }
89
90     @Test
91     public void notifyDistributionComplete_emptyErrReason() throws Exception {
92         HttpResponse<String> response = msoRestClient.notifyDistributionComplete(DISTRIBUTION_ID, DistributionStatusNotificationEnum.DISTRIBUTION_COMPLETE_OK, "");
93         assertThat(response.getStatusCode()).isEqualTo(200);
94     }
95
96     private int getNumOfRetries() {
97         return ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getMsoConfig().getHttpClientConfig().getNumOfRetries();
98     }
99
100     @Test
101     public void notifyDistributionComplete_noErrReason() throws Exception {
102         HttpResponse<String> response = msoRestClient.notifyDistributionComplete(DISTRIBUTION_ID, DistributionStatusNotificationEnum.DISTRIBUTION_COMPLETE_OK, null);
103         assertThat(response.getStatusCode()).isEqualTo(200);
104     }
105
106     @Test
107     public void notifyDistributionComplete_completeWithError() throws Exception {
108         HttpResponse<String> response = msoRestClient.notifyDistributionComplete(DISTRIBUTION_ID, DistributionStatusNotificationEnum.DISTRIBUTION_COMPLETE_ERROR, "my reason");
109         assertThat(response.getStatusCode()).isEqualTo(200);
110     }
111
112     @Test
113     public void testRetries() throws Exception {
114         int readTimeout = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getMsoConfig().getHttpClientConfig().getTimeouts().getReadTimeoutMs();
115         int expectedNumOfRetries = getNumOfRetries();
116
117         UrlPattern msoReqUrlPattern = urlMatching(String.format("/%s%s/(.*)", MSO_API_URL, getDistributionsUrl()));
118         msoRestServer.stubFor(patch(msoReqUrlPattern)
119                 .willReturn(new ResponseDefinitionBuilder().withFixedDelay(readTimeout + 1)));
120         HttpResponse<String> response = msoRestClient.notifyDistributionComplete(DISTRIBUTION_ID, DistributionStatusNotificationEnum.DISTRIBUTION_COMPLETE_ERROR, "my reason");
121         verify(expectedNumOfRetries + 1, patchRequestedFor(msoReqUrlPattern));
122         assertThat(response.getStatusCode()).isEqualTo(500);
123     }
124
125     private static String getDistributionsUrl() {
126         return "/distributions";
127     }
128
129     private static String getUserName() {
130         return "asdc";
131     }
132
133     private static String getPwd() {
134         return "OTLEp5lfVhYdyw5EAtTUBQ==";
135     }
136
137     private static class MsoDummyConfig extends ExternalServiceConfig {
138         int port;
139
140         MsoDummyConfig(int port) {
141             this.port = port;
142
143             BasicAuthorization basicAuthorization = new BasicAuthorization();
144             basicAuthorization.setUserName(MsoRestClientTest.getUserName());
145             basicAuthorization.setPassword(getPwd());
146             HttpClientConfig httpClientConfig = new HttpClientConfig(new Timeouts(500, 2000), basicAuthorization);
147             httpClientConfig.setNumOfRetries(getNumOfRetries());
148             super.setHttpClientConfig(httpClientConfig);
149
150             HttpRequestConfig httpRequestConfig = new HttpRequestConfig();
151             httpRequestConfig.setServerRootUrl(String.format("http://%s:%s/%s", MSO_HOST, this.port, MSO_API_URL));
152             httpRequestConfig.getResourceNamespaces().put(MSORestClient.DISTRIBUTIONS_RESOURCE_CONFIG_PARAM, getDistributionsUrl());
153             super.setHttpRequestConfig(httpRequestConfig);
154         }
155
156         int getNumOfRetries() {
157             return 1;
158         }
159     }
160
161 }