Add Xapp Onboarder client to backend
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / XappOnboarderMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 AT&T Intellectual Property
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 package org.oransc.ric.portal.dashboard.config;
21
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;
26
27 import java.io.IOException;
28 import java.lang.invoke.MethodHandles;
29
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;
47
48 /**
49  * Creates a mock implementation of the Xapp Onboarder client API.
50  */
51 @Configuration
52 @Profile("test")
53 public class XappOnboarderMockConfiguration {
54
55         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56
57         // Simulate remote method delay for UI testing
58         private int delayMs;
59
60         // Autowire all the properties required by the real class
61         // (even tho not used here) as a test of the properties.
62         @Autowired
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;
68         }
69
70         private ApiClient apiClient() {
71                 ApiClient mockClient = mock(ApiClient.class);
72                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
73                 return mockClient;
74         }
75
76         private HealthApi healthApi() {
77                 ApiClient apiClient = apiClient();
78                 HealthApi mockApi = mock(HealthApi.class);
79                 when(mockApi.getApiClient()).thenReturn(apiClient);
80                 doAnswer(i -> {
81                         return new Status().status("OK");
82                 }).when(mockApi).getHealthCheck();
83                 return mockApi;
84         }
85
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);
93                 doAnswer(inv -> {
94                         if (delayMs > 0) {
95                                 logger.debug("getChartsList sleeping {}", delayMs);
96                                 Thread.sleep(delayMs);
97                         }
98                         return sampleChartListJson;
99                 }).when(mockApi).getChartsList();
100                 doAnswer(inv -> {
101                         if (delayMs > 0) {
102                                 logger.debug("getChartsFetcher sleeping {}", delayMs);
103                                 Thread.sleep(delayMs);
104                         }
105                         return sampleChartYaml;
106                 }).when(mockApi).getChartsFetcher(any(String.class), any(String.class));
107                 doAnswer(inv -> {
108                         if (delayMs > 0) {
109                                 logger.debug("getValuesYamlFetcher sleeping {}", delayMs);
110                                 Thread.sleep(delayMs);
111                         }
112                         return sampleValuesYaml;
113                 }).when(mockApi).getValuesYamlFetcher(any(String.class), any(String.class));
114                 return mockApi;
115         }
116
117         private OnboardApi onboardApi() {
118                 ApiClient apiClient = apiClient();
119                 OnboardApi mockApi = mock(OnboardApi.class);
120                 when(mockApi.getApiClient()).thenReturn(apiClient);
121                 doAnswer(inv -> {
122                         if (delayMs > 0) {
123                                 logger.debug("postOnboardxApps sleeping {}", delayMs);
124                                 Thread.sleep(delayMs);
125                         }
126                         return new Status().status("OK");
127                 }).when(mockApi).postOnboardxApps(any(Descriptor.class));
128                 doAnswer(inv -> {
129                         if (delayMs > 0) {
130                                 logger.debug("postOnboardxAppsDownload sleeping {}", delayMs);
131                                 Thread.sleep(delayMs);
132                         }
133                         return new Status().status("OK");
134                 }).when(mockApi).postOnboardxAppsDownload(any(DescriptorRemote.class));
135                 return mockApi;
136         }
137
138         @Bean
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);
148                 return mockBuilder;
149         }
150
151 }