2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.config;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.doAnswer;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
27 import java.io.IOException;
28 import java.lang.invoke.MethodHandles;
30 import org.oransc.itdev.xapponboarder.client.api.ChartsApi;
31 import org.oransc.itdev.xapponboarder.client.api.HealthApi;
32 import org.oransc.itdev.xapponboarder.client.api.OnboardApi;
33 import org.oransc.itdev.xapponboarder.client.invoker.ApiClient;
34 import org.oransc.itdev.xapponboarder.client.model.Descriptor;
35 import org.oransc.itdev.xapponboarder.client.model.DescriptorRemote;
36 import org.oransc.itdev.xapponboarder.client.model.Status;
37 import org.oransc.ric.portal.dashboard.TestUtils;
38 import org.oransc.ric.portal.dashboard.model.RicRegionList;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.beans.factory.annotation.Value;
43 import org.springframework.context.annotation.Bean;
44 import org.springframework.context.annotation.Configuration;
45 import org.springframework.context.annotation.Profile;
46 import org.springframework.http.HttpStatus;
49 * Creates a mock implementation of the Xapp Onboarder client API.
53 public class XappOnboarderMockConfiguration {
55 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
57 // Simulate remote method delay for UI testing
60 // Autowire all the properties required by the real class
61 // (even tho not used here) as a test of the properties.
63 public XappOnboarderMockConfiguration(@Value("${xappobrd.url.suffix}") final String urlSuffix, //
64 final RicRegionList instanceConfig, //
65 @Value("${mock.config.delay:0}") int delayMs) {
66 logger.info("ctor: configured with suffix {}, instances {}, delay {}", urlSuffix, instanceConfig, delayMs);
67 this.delayMs = delayMs;
70 private ApiClient apiClient() {
71 ApiClient mockClient = mock(ApiClient.class);
72 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
76 private HealthApi healthApi() {
77 ApiClient apiClient = apiClient();
78 HealthApi mockApi = mock(HealthApi.class);
79 when(mockApi.getApiClient()).thenReturn(apiClient);
81 return new Status().status("OK");
82 }).when(mockApi).getHealthCheck();
86 private ChartsApi chartsApi() throws IOException {
87 final String sampleChartListJson = TestUtils.readDataFromPath("sample-chart-list.json");
88 final String sampleChartYaml = TestUtils.readDataFromPath("sample-chart.yaml");
89 final String sampleValuesYaml = TestUtils.readDataFromPath("sample-values.yaml");
90 ApiClient apiClient = apiClient();
91 ChartsApi mockApi = mock(ChartsApi.class);
92 when(mockApi.getApiClient()).thenReturn(apiClient);
95 logger.debug("getChartsList sleeping {}", delayMs);
96 Thread.sleep(delayMs);
98 return sampleChartListJson;
99 }).when(mockApi).getChartsList();
102 logger.debug("getChartsFetcher sleeping {}", delayMs);
103 Thread.sleep(delayMs);
105 return sampleChartYaml;
106 }).when(mockApi).getChartsFetcher(any(String.class), any(String.class));
109 logger.debug("getValuesYamlFetcher sleeping {}", delayMs);
110 Thread.sleep(delayMs);
112 return sampleValuesYaml;
113 }).when(mockApi).getValuesYamlFetcher(any(String.class), any(String.class));
117 private OnboardApi onboardApi() {
118 ApiClient apiClient = apiClient();
119 OnboardApi mockApi = mock(OnboardApi.class);
120 when(mockApi.getApiClient()).thenReturn(apiClient);
123 logger.debug("postOnboardxApps sleeping {}", delayMs);
124 Thread.sleep(delayMs);
126 return new Status().status("OK");
127 }).when(mockApi).postOnboardxApps(any(Descriptor.class));
130 logger.debug("postOnboardxAppsDownload sleeping {}", delayMs);
131 Thread.sleep(delayMs);
133 return new Status().status("OK");
134 }).when(mockApi).postOnboardxAppsDownload(any(DescriptorRemote.class));
139 // Must use the same name as the non-mock configuration
140 public XappOnboarderApiBuilder xappOnboarderApiBuilder() throws IOException {
141 final XappOnboarderApiBuilder mockBuilder = mock(XappOnboarderApiBuilder.class);
142 final HealthApi mockHealthApi = healthApi();
143 when(mockBuilder.getHealthApi(any(String.class))).thenReturn(mockHealthApi);
144 final ChartsApi mockChartsApi = chartsApi();
145 when(mockBuilder.getChartsApi(any(String.class))).thenReturn(mockChartsApi);
146 final OnboardApi mockOnboardApi = onboardApi();
147 when(mockBuilder.getOnboardApi(any(String.class))).thenReturn(mockOnboardApi);