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