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