2df2b3d6c02bd00e72cedf8f87fb54e01cf6b84a
[portal/ric-dashboard.git] / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / test / config / AppManagerMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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.test.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.context.annotation.Bean;
46 import org.springframework.context.annotation.Configuration;
47 import org.springframework.context.annotation.Profile;
48 import org.springframework.http.HttpStatus;
49
50 /**
51  * Creates an implementation of the xApp manager client that answers requests
52  * with mock data.
53  */
54 @Profile("test")
55 @Configuration
56 public class AppManagerMockConfiguration {
57
58         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59
60         private final AllDeployableXapps availXapps;
61         private final AllDeployedXapps deployedXapps;
62         private final AllXappConfig allXappConfigs;
63         private final SubscriptionResponse subRes;
64         // Simulate remote method delay for UI testing
65         private final int delayMs = 500;
66
67         public AppManagerMockConfiguration() {
68                 logger.info("Configuring mock xApp Manager");
69                 final String[] appNames = { "AdmissionControl", "Automatic Neighbor Relation", "Dual Connectivity" };
70                 final String configJson = " { \"config\" : \"example\" }";
71                 final String descriptorJson = " { \"descriptor\" : \"example\" }";
72                 allXappConfigs = new AllXappConfig();
73                 availXapps = new AllDeployableXapps();
74                 deployedXapps = new AllDeployedXapps();
75                 for (String n : appNames) {
76                         ConfigMetadata metadata = new ConfigMetadata().configName("config-" + n).name(n).namespace("namespace");
77                         XAppConfig config = new XAppConfig().config(configJson).descriptor(descriptorJson).metadata(metadata);
78                         allXappConfigs.add(config);
79                         availXapps.add(n);
80                         Xapp xapp = new Xapp().name(n).version("version").status(StatusEnum.UNKNOWN);
81                         xapp.addInstancesItem(new XappInstance().name("abcd-1234").ip("127.0.0.1").port(200)
82                                         .status(XappInstance.StatusEnum.RUNNING));
83                         deployedXapps.add(xapp);
84                 }
85                 subRes = new SubscriptionResponse().eventType(SubscriptionResponse.EventTypeEnum.ALL).id("subid").version(1);
86         }
87
88         @Bean
89         // Use the same name as regular configuration
90         public HealthApi xappMgrHealthApi() {
91                 ApiClient mockClient = mock(ApiClient.class);
92                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
93                 HealthApi mockApi = mock(HealthApi.class);
94                 when(mockApi.getApiClient()).thenReturn(mockClient);
95                 doAnswer(i -> null).when(mockApi).getHealthAlive();
96                 doAnswer(i -> null).when(mockApi).getHealthReady();
97                 return mockApi;
98         }
99
100         @Bean
101         // Use the same name as regular configuration
102         public XappApi xappMgrXappApi() {
103                 ApiClient mockClient = mock(ApiClient.class);
104                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
105                 XappApi mockApi = mock(XappApi.class);
106                 when(mockApi.getApiClient()).thenReturn(mockClient);
107                 doAnswer(inv -> {
108                         logger.debug("getAllXappConfig sleeping {}", delayMs);
109                         Thread.sleep(delayMs);
110                         return allXappConfigs;
111                 }).when(mockApi).getAllXappConfig();
112                 doAnswer(inv -> {
113                         logger.debug("createXappConfig sleeping {}", delayMs);
114                         Thread.sleep(delayMs);
115                         return allXappConfigs.get(0);
116                 }).when(mockApi).createXappConfig(any(XAppConfig.class));
117                 doAnswer(inv -> {
118                         logger.debug("modifyXappConfig sleeping {}", delayMs);
119                         Thread.sleep(delayMs);
120                         return allXappConfigs.get(0);
121                 }).when(mockApi).modifyXappConfig(any(XAppConfig.class));
122                 doAnswer(inv -> {
123                         logger.debug("deleteXappConfig sleeping {}", delayMs);
124                         Thread.sleep(delayMs);
125                         return null;
126                 }).when(mockApi).deleteXappConfig(any(ConfigMetadata.class));
127                 doAnswer(inv -> {
128                         logger.debug("deployXapp of {} sleeping {}", inv.getArgument(0), delayMs);
129                         Thread.sleep(delayMs);
130                         return deployedXapps.get(0);
131                 }).when(mockApi).deployXapp(any(XAppInfo.class));
132                 doAnswer(inv -> {
133                         logger.debug("listAllXapps sleeping {}", delayMs);
134                         Thread.sleep(delayMs);
135                         return availXapps;
136                 }).when(mockApi).listAllXapps();
137                 doAnswer(inv -> {
138                         logger.debug("getAllXapps sleeping {}", delayMs);
139                         Thread.sleep(delayMs);
140                         return deployedXapps;
141                 }).when(mockApi).getAllXapps();
142                 doAnswer(inv -> {
143                         logger.debug("getXappByName of {} sleeping {}", inv.getArgument(0), delayMs);
144                         Thread.sleep(delayMs);
145                         return deployedXapps.get(0);
146                 }).when(mockApi).getXappByName(any(String.class));
147                 doAnswer(inv -> {
148                         logger.debug("undeployXapp of {} sleeping {}", inv.getArgument(0), delayMs);
149                         Thread.sleep(delayMs);
150                         return null;
151                 }).when(mockApi).undeployXapp(any(String.class));
152                 doAnswer(inv -> {
153                         logger.debug("addSubscription sleeping {}", delayMs);
154                         Thread.sleep(delayMs);
155                         return subRes;
156                 }).when(mockApi).addSubscription(any(SubscriptionRequest.class));
157                 doAnswer(inv -> {
158                         logger.debug("deleteSubscription sleeping {}", delayMs);
159                         Thread.sleep(delayMs);
160                         return null;
161                 }).when(mockApi).deleteSubscription(any(String.class));
162                 return mockApi;
163         }
164
165 }