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;
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;
57 * Creates an implementation of the xApp manager client that answers requests
62 public class AppManagerMockConfiguration {
64 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
66 // Simulate remote method delay for UI testing
69 // Autowire all the properties required by the real class
70 // (even tho not used here) as a test of the properties.
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;
80 * Builds a mock HealthApi object. Does not accept an instance key because this
81 * API answers no text.
83 * @return mock HealthApi
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();
96 * Builds a mock XappApi object.
100 * @return Object that returns instance-specific results
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);
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);
130 logger.debug("getAllXappConfig sleeping {}", delayMs);
131 Thread.sleep(delayMs);
133 return allXappConfigs;
134 }).when(mockApi).getAllXappConfig();
137 logger.debug("modifyXappConfig sleeping {}", delayMs);
138 Thread.sleep(delayMs);
140 return configValErrs;
141 }).when(mockApi).modifyXappConfig(any(XAppConfig.class));
144 logger.debug("deployXapp of {} sleeping {}", inv.getArgument(0), delayMs);
145 Thread.sleep(delayMs);
147 return deployedXapps.get(0);
148 }).when(mockApi).deployXapp(any(XappDescriptor.class));
151 logger.debug("listAllDeployableXapps sleeping {}", delayMs);
152 Thread.sleep(delayMs);
154 return deployableApps;
155 }).when(mockApi).listAllXapps();
158 logger.debug("getAllXapps sleeping {}", delayMs);
159 Thread.sleep(delayMs);
161 return deployedXapps;
162 }).when(mockApi).getAllXapps();
165 logger.debug("getXappByName of {} sleeping {}", inv.getArgument(0), delayMs);
166 Thread.sleep(delayMs);
168 return deployedXapps.get(0);
169 }).when(mockApi).getXappByName(any(String.class));
172 logger.debug("undeployXapp of {} sleeping {}", inv.getArgument(0), delayMs);
173 Thread.sleep(delayMs);
176 }).when(mockApi).undeployXapp(any(String.class));
179 logger.debug("addSubscription sleeping {}", delayMs);
180 Thread.sleep(delayMs);
183 }).when(mockApi).addSubscription(any(SubscriptionRequest.class));
186 logger.debug("deleteSubscription sleeping {}", delayMs);
187 Thread.sleep(delayMs);
190 }).when(mockApi).deleteSubscription(any(String.class));
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);