2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.config;
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;
27 import java.lang.invoke.MethodHandles;
28 import java.util.Arrays;
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;
54 * Creates an implementation of the xApp manager client that answers requests
59 public class AppManagerMockConfiguration {
61 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
63 // Simulate remote method delay for UI testing
67 public AppManagerMockConfiguration(@Value("${mock.config.delay:0}") int delayMs) {
68 logger.debug("ctor: configured with delay {}", delayMs);
69 this.delayMs = delayMs;
73 * Builds a mock HealthApi object. Does not accept an instance key because this
74 * API answers no text.
76 * @return mock HealthApi
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();
89 * Builds a mock XappApi object.
93 * @return Object that returns instance-specific results
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;
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);
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);
127 logger.debug("getAllXappConfig sleeping {}", delayMs);
128 Thread.sleep(delayMs);
130 return allXappConfigs;
131 }).when(mockApi).getAllXappConfig();
134 logger.debug("createXappConfig sleeping {}", delayMs);
135 Thread.sleep(delayMs);
137 return allXappConfigs.get(0);
138 }).when(mockApi).createXappConfig(any(XAppConfig.class));
141 logger.debug("modifyXappConfig sleeping {}", delayMs);
142 Thread.sleep(delayMs);
144 return allXappConfigs.get(0);
145 }).when(mockApi).modifyXappConfig(any(XAppConfig.class));
148 logger.debug("deleteXappConfig sleeping {}", delayMs);
149 Thread.sleep(delayMs);
152 }).when(mockApi).deleteXappConfig(any(ConfigMetadata.class));
155 logger.debug("deployXapp of {} sleeping {}", inv.getArgument(0), delayMs);
156 Thread.sleep(delayMs);
158 return deployedXapps.get(0);
159 }).when(mockApi).deployXapp(any(XAppInfo.class));
162 logger.debug("listAllDeployableXapps sleeping {}", delayMs);
163 Thread.sleep(delayMs);
165 return deployableApps;
166 }).when(mockApi).listAllDeployableXapps();
169 logger.debug("getAllXapps sleeping {}", delayMs);
170 Thread.sleep(delayMs);
172 return deployedXapps;
173 }).when(mockApi).getAllXapps();
176 logger.debug("getXappByName of {} sleeping {}", inv.getArgument(0), delayMs);
177 Thread.sleep(delayMs);
179 return deployedXapps.get(0);
180 }).when(mockApi).getXappByName(any(String.class));
183 logger.debug("undeployXapp of {} sleeping {}", inv.getArgument(0), delayMs);
184 Thread.sleep(delayMs);
187 }).when(mockApi).undeployXapp(any(String.class));
190 logger.debug("addSubscription sleeping {}", delayMs);
191 Thread.sleep(delayMs);
194 }).when(mockApi).addSubscription(any(SubscriptionRequest.class));
197 logger.debug("deleteSubscription sleeping {}", delayMs);
198 Thread.sleep(delayMs);
201 }).when(mockApi).deleteSubscription(any(String.class));
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);