Require RIC instance key in controller methods
[portal/ric-dashboard.git] / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / CaasIngressMockConfiguration.java
index 6b12493..1420086 100644 (file)
  */
 package org.oransc.ric.portal.dashboard.config;
 
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -49,13 +51,11 @@ public class CaasIngressMockConfiguration {
        @Value("${mock.config.delay:0}")
        private int delayMs;
 
-       private final String auxPods;
        private final String pltPods;
 
        public CaasIngressMockConfiguration() throws IOException {
                logger.info("Configuring mock CAAS-Ingres clients");
                // Files in src/test/resources
-               auxPods = readDataFromPath("caas-ingress-ricaux-pods.json");
                pltPods = readDataFromPath("caas-ingress-ricplt-pods.json");
        }
 
@@ -77,26 +77,26 @@ public class CaasIngressMockConfiguration {
                return sb.toString();
        }
 
-       @Bean
-       // Use the same name as regular configuration
-       public SimpleKubernetesClient ciAuxApi() throws IOException {
+       private SimpleKubernetesClient simpleKubernetesClient() {
                SimpleKubernetesClient mockClient = mock(SimpleKubernetesClient.class);
                doAnswer(inv -> {
-                       logger.debug("listPods for aux");
-                       return auxPods;
-               }).when(mockClient).listPods("ricaux");
+                       String ns = inv.<String>getArgument(0);
+                       logger.debug("listPods for namespace {}", ns);
+                       if ("ricplt".equals(ns))
+                               return pltPods;
+                       else
+                               throw new IllegalArgumentException("Fake server failure");
+               }).when(mockClient).listPods(any(String.class));
                return mockClient;
        }
 
        @Bean
-       // Use the same name as regular configuration
-       public SimpleKubernetesClient ciPltApi() throws IOException {
-               SimpleKubernetesClient mockClient = mock(SimpleKubernetesClient.class);
-               doAnswer(inv -> {
-                       logger.debug("listPods for plt");
-                       return pltPods;
-               }).when(mockClient).listPods("ricplt");
-               return mockClient;
+       // The bean (method) name must be globally unique
+       public SimpleKubernetesClientBuilder simpleKubernetesClientBuilder() {
+               final SimpleKubernetesClientBuilder mockBuilder = mock(SimpleKubernetesClientBuilder.class);
+               SimpleKubernetesClient client = simpleKubernetesClient();
+               when(mockBuilder.getSimpleKubernetesClient(any(String.class))).thenReturn(client);
+               return mockBuilder;
        }
 
 }