X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=webapp-backend%2Fsrc%2Ftest%2Fjava%2Forg%2Foransc%2Fric%2Fportal%2Fdashboard%2Fconfig%2FA1MediatorMockConfiguration.java;h=21b2face37116d99a7463ef79927ec2be6509966;hb=9cf5b95a5bc9810d739d14d324dd7d80cbdb4a7d;hp=aef5bc4ccace08f46379c982902557856a34744e;hpb=3f812ea25d352ec33d07f5ffa4c2aa2a77e8e793;p=portal%2Fric-dashboard.git diff --git a/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/A1MediatorMockConfiguration.java b/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/A1MediatorMockConfiguration.java index aef5bc4c..21b2face 100644 --- a/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/A1MediatorMockConfiguration.java +++ b/webapp-backend/src/test/java/org/oransc/ric/portal/dashboard/config/A1MediatorMockConfiguration.java @@ -2,7 +2,7 @@ * ========================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. @@ -24,10 +24,16 @@ import static org.mockito.Mockito.doAnswer; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; @@ -36,21 +42,51 @@ import org.springframework.context.annotation.Configuration; 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; + /** - * 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 -public class A1MediatorMockConfiguration { +@Profile("test") +public class A1MediatorMockConfiguration extends AbstractMockConfiguration { 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 policyTypeIds; + private final PolicyTypeSchema rateControlPolicyType; + private final Map appPolicyMap; + + 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(); + final String policyType = readDataFromPath("rate-control-policy-type.json"); + rateControlPolicyType = mapper.readValue(policyType, PolicyTypeSchema.class); + final String policyInstance = readDataFromPath("rate-control-policy-instance.json"); + appPolicyMap = new HashMap<>(); + appPolicyMap.put(AC_CONTROL_NAME, policyInstance); + } - public A1MediatorMockConfiguration() { - logger.info("Configuring mock A1 Mediator"); + private void delay() throws InterruptedException { + if (delayMs > 0) { + logger.debug("delay: sleeping {}", delayMs); + Thread.sleep(delayMs); + } } private ApiClient apiClient() { @@ -59,27 +95,57 @@ public class A1MediatorMockConfiguration { 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); - } - return null; - }).when(mockApi).a1ControllerGetHandler(any(String.class)); + delay(); + return policyTypeIds; + }).when(mockApi).a1ControllerGetAllPolicyTypes(); + doAnswer(inv -> { + delay(); + Integer policyTypeId = inv.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.getArgument(0); + if (policyTypeId.compareTo(ADMISSION_CONTROL_POLICY_ID) != 0) + throw new IllegalArgumentException("Unexpected policy type: " + policyTypeId); + String policyInstId = inv.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); - } + delay(); + Integer policyTypeId = inv.getArgument(0); + if (policyTypeId.compareTo(ADMISSION_CONTROL_POLICY_ID) != 0) + throw new IllegalArgumentException("Unexpected policy type: " + policyTypeId); + String policyInstId = inv.getArgument(1); + if (!AC_CONTROL_NAME.equals(policyInstId)) + throw new IllegalArgumentException("Unexpected policy instance: " + policyInstId); + String policy = inv.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; + } + }