2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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.HashMap;
31 import org.oransc.ric.a1med.client.api.A1MediatorApi;
32 import org.oransc.ric.a1med.client.invoker.ApiClient;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.context.annotation.Bean;
37 import org.springframework.context.annotation.Configuration;
38 import org.springframework.context.annotation.Profile;
39 import org.springframework.http.HttpStatus;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42 import com.fasterxml.jackson.databind.node.ObjectNode;
45 * Creates a mock implementation of the A1 mediator client API.
49 public class A1MediatorMockConfiguration {
51 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53 // A "control" is an element in the XApp descriptor
54 public static final String AC_CONTROL_NAME = "admission_control_policy";
56 // Simulate remote method delay for UI testing
57 @Value("${mock.config.delay:0}")
60 private final Map<String, String> appPolicyMap;
62 public A1MediatorMockConfiguration() {
63 logger.info("Configuring mock A1 Mediator");
64 appPolicyMap = new HashMap<>();
65 // Define a mock AC policy
66 ObjectMapper mapper = new ObjectMapper();
67 ObjectNode node = mapper.createObjectNode();
68 // These fields are defined in the ACAdmissionIntervalControl
69 // Typescript interface, but are otherwise unknown to this backend.
70 node.put("enforce", Boolean.TRUE);
71 node.put("window_length", 0);
72 node.put("blocking_rate", 0);
73 node.put("trigger_threshold", 0);
74 appPolicyMap.put(AC_CONTROL_NAME, node.toString());
77 private ApiClient apiClient() {
78 ApiClient mockClient = mock(ApiClient.class);
79 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
84 // Use the same name as regular configuration
85 public A1MediatorApi a1MediatorApi() {
86 ApiClient apiClient = apiClient();
87 A1MediatorApi mockApi = mock(A1MediatorApi.class);
88 when(mockApi.getApiClient()).thenReturn(apiClient);
91 logger.debug("a1ControllerGetHandler sleeping {}", delayMs);
92 Thread.sleep(delayMs);
94 String appName = inv.<String>getArgument(0);
95 return appPolicyMap.get(appName);
96 }).when(mockApi).a1ControllerGetHandler(any(String.class));
99 logger.debug("a1ControllerPutHandler sleeping {}", delayMs);
100 Thread.sleep(delayMs);
102 String appName = inv.<String>getArgument(0);
103 String policy = inv.<String>getArgument(1);
104 appPolicyMap.put(appName, policy);
106 }).when(mockApi).a1ControllerPutHandler(any(String.class), any(Object.class));