Revise front-end buildPath support
[portal/ric-dashboard.git] / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / AppManagerMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 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.lang.invoke.MethodHandles;
28 import java.util.Arrays;
29
30 import org.oransc.ric.plt.appmgr.client.api.HealthApi;
31 import org.oransc.ric.plt.appmgr.client.api.XappApi;
32 import org.oransc.ric.plt.appmgr.client.invoker.ApiClient;
33 import org.oransc.ric.plt.appmgr.client.model.AllDeployableXapps;
34 import org.oransc.ric.plt.appmgr.client.model.AllDeployedXapps;
35 import org.oransc.ric.plt.appmgr.client.model.AllXappConfig;
36 import org.oransc.ric.plt.appmgr.client.model.ConfigMetadata;
37 import org.oransc.ric.plt.appmgr.client.model.SubscriptionRequest;
38 import org.oransc.ric.plt.appmgr.client.model.SubscriptionResponse;
39 import org.oransc.ric.plt.appmgr.client.model.XAppConfig;
40 import org.oransc.ric.plt.appmgr.client.model.XAppInfo;
41 import org.oransc.ric.plt.appmgr.client.model.Xapp;
42 import org.oransc.ric.plt.appmgr.client.model.Xapp.StatusEnum;
43 import org.oransc.ric.plt.appmgr.client.model.XappInstance;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.beans.factory.annotation.Value;
48 import org.springframework.context.annotation.Bean;
49 import org.springframework.context.annotation.Configuration;
50 import org.springframework.context.annotation.Profile;
51 import org.springframework.http.HttpStatus;
52
53 /**
54  * Creates an implementation of the xApp manager client that answers requests
55  * with mock data.
56  */
57 @Configuration
58 @Profile("test")
59 public class AppManagerMockConfiguration {
60
61         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
62
63         // Simulate remote method delay for UI testing
64         private int delayMs;
65
66         @Autowired
67         public AppManagerMockConfiguration(@Value("${mock.config.delay:0}") int delayMs) {
68                 logger.debug("ctor: configured with delay {}", delayMs);
69                 this.delayMs = delayMs;
70         }
71
72         /**
73          * Builds a mock HealthApi object. Does not accept an instance key because this
74          * API answers no text.
75          * 
76          * @return mock HealthApi
77          */
78         private HealthApi healthApi() {
79                 ApiClient mockClient = mock(ApiClient.class);
80                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
81                 HealthApi mockApi = mock(HealthApi.class);
82                 when(mockApi.getApiClient()).thenReturn(mockClient);
83                 doAnswer(i -> null).when(mockApi).getHealthAlive();
84                 doAnswer(i -> null).when(mockApi).getHealthReady();
85                 return mockApi;
86         }
87
88         /**
89          * Builds a mock XappApi object.
90          * 
91          * @param instanceKey
92          *                        RIC instance
93          * @return Object that returns instance-specific results
94          */
95         private XappApi xappApi(String instanceKey) {
96                 logger.debug("Creating XappApi for instance {}", instanceKey);
97                 // Create instance-specific objects
98                 String[] appNames = { "AdmissionControl " + instanceKey, "UE Event Collector " + instanceKey };
99                 if (RICInstanceMockConfiguration.INSTANCE_KEY_1.equals(instanceKey)) {
100                         appNames = Arrays.copyOf(appNames, appNames.length + 1);
101                         appNames[appNames.length - 1] = "ANR " + instanceKey;
102                 }
103                 final String configJson = " { \"config\" : \"example-" + instanceKey + "\"}";
104                 final String descriptorJson = " { \"descriptor\" : \"example-" + instanceKey + "\"}";
105                 final AllXappConfig allXappConfigs = new AllXappConfig();
106                 final AllDeployableXapps deployableApps = new AllDeployableXapps();
107                 final AllDeployedXapps deployedXapps = new AllDeployedXapps();
108                 for (String n : appNames) {
109                         ConfigMetadata metadata = new ConfigMetadata().configName("config-" + n).name(n).namespace("namespace");
110                         XAppConfig config = new XAppConfig().config(configJson).descriptor(descriptorJson).metadata(metadata);
111                         allXappConfigs.add(config);
112                         deployableApps.add(n);
113                         Xapp xapp = new Xapp().name(n).version("version").status(StatusEnum.UNKNOWN);
114                         xapp.addInstancesItem(new XappInstance().name("abcd-1234").ip("127.0.0.1").port(200)
115                                         .status(XappInstance.StatusEnum.RUNNING));
116                         deployedXapps.add(xapp);
117                 }
118                 final SubscriptionResponse subRes = new SubscriptionResponse().eventType(SubscriptionResponse.EventTypeEnum.ALL)
119                                 .id("subid").version(1);
120                 // Mock the methods to return the instance-specific objects
121                 ApiClient mockClient = mock(ApiClient.class);
122                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
123                 XappApi mockApi = mock(XappApi.class);
124                 when(mockApi.getApiClient()).thenReturn(mockClient);
125                 doAnswer(inv -> {
126                         if (delayMs > 0) {
127                                 logger.debug("getAllXappConfig sleeping {}", delayMs);
128                                 Thread.sleep(delayMs);
129                         }
130                         return allXappConfigs;
131                 }).when(mockApi).getAllXappConfig();
132                 doAnswer(inv -> {
133                         if (delayMs > 0) {
134                                 logger.debug("createXappConfig sleeping {}", delayMs);
135                                 Thread.sleep(delayMs);
136                         }
137                         return allXappConfigs.get(0);
138                 }).when(mockApi).createXappConfig(any(XAppConfig.class));
139                 doAnswer(inv -> {
140                         if (delayMs > 0) {
141                                 logger.debug("modifyXappConfig sleeping {}", delayMs);
142                                 Thread.sleep(delayMs);
143                         }
144                         return allXappConfigs.get(0);
145                 }).when(mockApi).modifyXappConfig(any(XAppConfig.class));
146                 doAnswer(inv -> {
147                         if (delayMs > 0) {
148                                 logger.debug("deleteXappConfig sleeping {}", delayMs);
149                                 Thread.sleep(delayMs);
150                         }
151                         return null;
152                 }).when(mockApi).deleteXappConfig(any(ConfigMetadata.class));
153                 doAnswer(inv -> {
154                         if (delayMs > 0) {
155                                 logger.debug("deployXapp of {} sleeping {}", inv.getArgument(0), delayMs);
156                                 Thread.sleep(delayMs);
157                         }
158                         return deployedXapps.get(0);
159                 }).when(mockApi).deployXapp(any(XAppInfo.class));
160                 doAnswer(inv -> {
161                         if (delayMs > 0) {
162                                 logger.debug("listAllDeployableXapps sleeping {}", delayMs);
163                                 Thread.sleep(delayMs);
164                         }
165                         return deployableApps;
166                 }).when(mockApi).listAllDeployableXapps();
167                 doAnswer(inv -> {
168                         if (delayMs > 0) {
169                                 logger.debug("getAllXapps sleeping {}", delayMs);
170                                 Thread.sleep(delayMs);
171                         }
172                         return deployedXapps;
173                 }).when(mockApi).getAllXapps();
174                 doAnswer(inv -> {
175                         if (delayMs > 0) {
176                                 logger.debug("getXappByName of {} sleeping {}", inv.getArgument(0), delayMs);
177                                 Thread.sleep(delayMs);
178                         }
179                         return deployedXapps.get(0);
180                 }).when(mockApi).getXappByName(any(String.class));
181                 doAnswer(inv -> {
182                         if (delayMs > 0) {
183                                 logger.debug("undeployXapp of {} sleeping {}", inv.getArgument(0), delayMs);
184                                 Thread.sleep(delayMs);
185                         }
186                         return null;
187                 }).when(mockApi).undeployXapp(any(String.class));
188                 doAnswer(inv -> {
189                         if (delayMs > 0) {
190                                 logger.debug("addSubscription sleeping {}", delayMs);
191                                 Thread.sleep(delayMs);
192                         }
193                         return subRes;
194                 }).when(mockApi).addSubscription(any(SubscriptionRequest.class));
195                 doAnswer(inv -> {
196                         if (delayMs > 0) {
197                                 logger.debug("deleteSubscription sleeping {}", delayMs);
198                                 Thread.sleep(delayMs);
199                         }
200                         return null;
201                 }).when(mockApi).deleteSubscription(any(String.class));
202                 return mockApi;
203         }
204
205         @Bean
206         // Must use the same name as the non-mock configuration
207         public AppManagerApiBuilder appManagerApiBuilder() {
208                 final AppManagerApiBuilder mockBuilder = mock(AppManagerApiBuilder.class);
209                 final HealthApi mockHealthApi = healthApi();
210                 when(mockBuilder.getHealthApi(any(String.class))).thenReturn(mockHealthApi);
211                 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
212                         final XappApi mockXappApi = xappApi(key);
213                         when(mockBuilder.getXappApi(key)).thenReturn(mockXappApi);
214                 }
215                 return mockBuilder;
216         }
217
218 }