* ========================LICENSE_START=================================
* O-RAN-SC
* %%
- * Copyright (C) 2019 AT&T Intellectual Property and Nokia
+ * Copyright (C) 2019 AT&T Intellectual Property
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import java.io.IOException;
import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import org.oransc.ric.a1med.client.api.A1MediatorApi;
import org.oransc.ric.a1med.client.invoker.ApiClient;
+import org.oransc.ric.a1med.client.model.PolicyTypeSchema;
+import org.oransc.ric.portal.dashboard.TestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.ObjectNode;
/**
- * Creates a mock implementation of the A1 mediator client API.
+ * Creates a mock implementation of the A1 mediator client builder with mock
+ * methods that answer Admission Control mock data.
*/
-@Profile("test")
@Configuration
+@Profile("test")
public class A1MediatorMockConfiguration {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
// A "control" is an element in the XApp descriptor
public static final String AC_CONTROL_NAME = "admission_control_policy";
+ public static final Integer ADMISSION_CONTROL_POLICY_ID = 21000;
// Simulate remote method delay for UI testing
- @Value("${mock.config.delay:0}")
- private int delayMs;
+ private final int delayMs;
+ // Mock values
+ private final List<Integer> policyTypeIds;
+ private final PolicyTypeSchema rateControlPolicyType;
private final Map<String, String> appPolicyMap;
- public A1MediatorMockConfiguration() {
- logger.info("Configuring mock A1 Mediator");
- appPolicyMap = new HashMap<>();
- // Define a mock AC policy
+ public A1MediatorMockConfiguration(@Value("${mock.config.delay:0}") int delayMs)
+ throws IOException, JsonParseException, JsonMappingException {
+ logger.debug("ctor: configured with delay {}", delayMs);
+ this.delayMs = delayMs;
+ policyTypeIds = new ArrayList<>();
+ policyTypeIds.add(ADMISSION_CONTROL_POLICY_ID);
ObjectMapper mapper = new ObjectMapper();
- ObjectNode node = mapper.createObjectNode();
- // These fields are defined in the ACAdmissionIntervalControl
- // Typescript interface, but are otherwise unknown to this backend.
- node.put("enforce", Boolean.TRUE);
- node.put("window_length", 0);
- node.put("blocking_rate", 0);
- node.put("trigger_threshold", 0);
- appPolicyMap.put(AC_CONTROL_NAME, node.toString());
+ final String policyType = TestUtils.readDataFromPath("rate-control-policy-type.json");
+ rateControlPolicyType = mapper.readValue(policyType, PolicyTypeSchema.class);
+ final String policyInstance = TestUtils.readDataFromPath("rate-control-policy-instance.json");
+ appPolicyMap = new HashMap<>();
+ appPolicyMap.put(AC_CONTROL_NAME, policyInstance);
+ }
+
+ private void delay() throws InterruptedException {
+ if (delayMs > 0) {
+ logger.debug("delay: sleeping {}", delayMs);
+ Thread.sleep(delayMs);
+ }
}
private ApiClient apiClient() {
return mockClient;
}
- @Bean
- // Use the same name as regular configuration
- public A1MediatorApi a1MediatorApi() {
+ private A1MediatorApi a1MediatorApi(String instanceKey) {
+ logger.debug("a1MediatorApi: instance {}", instanceKey);
ApiClient apiClient = apiClient();
A1MediatorApi mockApi = mock(A1MediatorApi.class);
when(mockApi.getApiClient()).thenReturn(apiClient);
doAnswer(inv -> {
- if (delayMs > 0) {
- logger.debug("a1ControllerGetHandler sleeping {}", delayMs);
- Thread.sleep(delayMs);
- }
- String appName = inv.<String>getArgument(0);
- return appPolicyMap.get(appName);
- }).when(mockApi).a1ControllerGetHandler(any(String.class));
+ delay();
+ return policyTypeIds;
+ }).when(mockApi).a1ControllerGetAllPolicyTypes();
+ doAnswer(inv -> {
+ delay();
+ Integer policyTypeId = inv.<Integer>getArgument(0);
+ if (policyTypeId.compareTo(ADMISSION_CONTROL_POLICY_ID) != 0)
+ throw new IllegalArgumentException("Unexpected policy type: " + policyTypeId);
+ return rateControlPolicyType;
+ }).when(mockApi).a1ControllerGetPolicyType(any(Integer.class));
+ doAnswer(inv -> {
+ delay();
+ Integer policyTypeId = inv.<Integer>getArgument(0);
+ if (policyTypeId.compareTo(ADMISSION_CONTROL_POLICY_ID) != 0)
+ throw new IllegalArgumentException("Unexpected policy type: " + policyTypeId);
+ String policyInstId = inv.<String>getArgument(1);
+ if (!AC_CONTROL_NAME.equals(policyInstId))
+ throw new IllegalArgumentException("Unexpected policy instance: " + policyInstId);
+ return appPolicyMap.get(policyInstId);
+ }).when(mockApi).a1ControllerGetPolicyInstance(any(Integer.class), any(String.class));
doAnswer(inv -> {
- if (delayMs > 0) {
- logger.debug("a1ControllerPutHandler sleeping {}", delayMs);
- Thread.sleep(delayMs);
- }
- String appName = inv.<String>getArgument(0);
- String policy = inv.<String>getArgument(1);
- appPolicyMap.put(appName, policy);
+ delay();
+ Integer policyTypeId = inv.<Integer>getArgument(0);
+ if (policyTypeId.compareTo(ADMISSION_CONTROL_POLICY_ID) != 0)
+ throw new IllegalArgumentException("Unexpected policy type: " + policyTypeId);
+ String policyInstId = inv.<String>getArgument(1);
+ if (!AC_CONTROL_NAME.equals(policyInstId))
+ throw new IllegalArgumentException("Unexpected policy instance: " + policyInstId);
+ String policy = inv.<String>getArgument(2);
+ appPolicyMap.put(policyInstId, policy);
return null;
- }).when(mockApi).a1ControllerPutHandler(any(String.class), any(Object.class));
+ }).when(mockApi).a1ControllerCreateOrReplacePolicyInstance(any(Integer.class), any(String.class),
+ any(Object.class));
return mockApi;
}
+ @Bean
+ // Must use the same name as the non-mock configuration
+ public A1MediatorApiBuilder a1MediatorApiBuilder() {
+ final A1MediatorApiBuilder mockBuilder = mock(A1MediatorApiBuilder.class);
+ for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
+ final A1MediatorApi mockApi = a1MediatorApi(key);
+ when(mockBuilder.getA1MediatorApi(key)).thenReturn(mockApi);
+ }
+ return mockBuilder;
+ }
+
}