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.io.IOException;
28 import java.lang.invoke.MethodHandles;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
34 import org.oransc.ric.a1med.client.api.A1MediatorApi;
35 import org.oransc.ric.a1med.client.invoker.ApiClient;
36 import org.oransc.ric.a1med.client.model.PolicyTypeSchema;
37 import org.oransc.ric.portal.dashboard.TestUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.context.annotation.Bean;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.context.annotation.Profile;
44 import org.springframework.http.HttpStatus;
46 import com.fasterxml.jackson.core.JsonParseException;
47 import com.fasterxml.jackson.databind.JsonMappingException;
48 import com.fasterxml.jackson.databind.ObjectMapper;
51 * Creates a mock implementation of the A1 mediator client builder with mock
52 * methods that answer Admission Control mock data.
56 public class A1MediatorMockConfiguration {
58 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
60 // A "control" is an element in the XApp descriptor
61 public static final String AC_CONTROL_NAME = "admission_control_policy";
62 public static final Integer ADMISSION_CONTROL_POLICY_ID = 21000;
64 // Simulate remote method delay for UI testing
65 private final int delayMs;
68 private final List<Integer> policyTypeIds;
69 private final PolicyTypeSchema rateControlPolicyType;
70 private final Map<String, String> appPolicyMap;
72 public A1MediatorMockConfiguration(@Value("${mock.config.delay:0}") int delayMs)
73 throws IOException, JsonParseException, JsonMappingException {
74 logger.debug("ctor: configured with delay {}", delayMs);
75 this.delayMs = delayMs;
76 policyTypeIds = new ArrayList<>();
77 policyTypeIds.add(ADMISSION_CONTROL_POLICY_ID);
78 ObjectMapper mapper = new ObjectMapper();
79 final String policyType = TestUtils.readDataFromPath("rate-control-policy-type.json");
80 rateControlPolicyType = mapper.readValue(policyType, PolicyTypeSchema.class);
81 final String policyInstance = TestUtils.readDataFromPath("rate-control-policy-instance.json");
82 appPolicyMap = new HashMap<>();
83 appPolicyMap.put(AC_CONTROL_NAME, policyInstance);
86 private void delay() throws InterruptedException {
88 logger.debug("delay: sleeping {}", delayMs);
89 Thread.sleep(delayMs);
93 private ApiClient apiClient() {
94 ApiClient mockClient = mock(ApiClient.class);
95 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
99 private A1MediatorApi a1MediatorApi(String instanceKey) {
100 logger.debug("a1MediatorApi: instance {}", instanceKey);
101 ApiClient apiClient = apiClient();
102 A1MediatorApi mockApi = mock(A1MediatorApi.class);
103 when(mockApi.getApiClient()).thenReturn(apiClient);
106 return policyTypeIds;
107 }).when(mockApi).a1ControllerGetAllPolicyTypes();
110 Integer policyTypeId = inv.<Integer>getArgument(0);
111 if (policyTypeId.compareTo(ADMISSION_CONTROL_POLICY_ID) != 0)
112 throw new IllegalArgumentException("Unexpected policy type: " + policyTypeId);
113 return rateControlPolicyType;
114 }).when(mockApi).a1ControllerGetPolicyType(any(Integer.class));
117 Integer policyTypeId = inv.<Integer>getArgument(0);
118 if (policyTypeId.compareTo(ADMISSION_CONTROL_POLICY_ID) != 0)
119 throw new IllegalArgumentException("Unexpected policy type: " + policyTypeId);
120 String policyInstId = inv.<String>getArgument(1);
121 if (!AC_CONTROL_NAME.equals(policyInstId))
122 throw new IllegalArgumentException("Unexpected policy instance: " + policyInstId);
123 return appPolicyMap.get(policyInstId);
124 }).when(mockApi).a1ControllerGetPolicyInstance(any(Integer.class), any(String.class));
127 Integer policyTypeId = inv.<Integer>getArgument(0);
128 if (policyTypeId.compareTo(ADMISSION_CONTROL_POLICY_ID) != 0)
129 throw new IllegalArgumentException("Unexpected policy type: " + policyTypeId);
130 String policyInstId = inv.<String>getArgument(1);
131 if (!AC_CONTROL_NAME.equals(policyInstId))
132 throw new IllegalArgumentException("Unexpected policy instance: " + policyInstId);
133 String policy = inv.<String>getArgument(2);
134 appPolicyMap.put(policyInstId, policy);
136 }).when(mockApi).a1ControllerCreateOrReplacePolicyInstance(any(Integer.class), any(String.class),
142 // Must use the same name as the non-mock configuration
143 public A1MediatorApiBuilder a1MediatorApiBuilder() {
144 final A1MediatorApiBuilder mockBuilder = mock(A1MediatorApiBuilder.class);
145 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
146 final A1MediatorApi mockApi = a1MediatorApi(key);
147 when(mockBuilder.getA1MediatorApi(key)).thenReturn(mockApi);