Revise front-end buildPath support
[portal/ric-dashboard.git] / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / CaasIngressMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * %%
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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===================================
19  */
20 package org.oransc.ric.portal.dashboard.config;
21
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;
26
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.lang.invoke.MethodHandles;
31
32 import org.oransc.ric.portal.dashboard.k8sapi.SimpleKubernetesClient;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.context.annotation.Bean;
38 import org.springframework.context.annotation.Configuration;
39 import org.springframework.context.annotation.Profile;
40
41 /**
42  * Creates mock implementations of Kubernetes clients that answer requests with
43  * sample data read from the filesystem.
44  */
45 @Configuration
46 @Profile("test")
47 public class CaasIngressMockConfiguration {
48
49         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
50
51         // Simulate remote method delay for UI testing
52         private int delayMs;
53
54         @Autowired
55         public CaasIngressMockConfiguration(@Value("${mock.config.delay:0}") int delayMs) {
56                 logger.debug("ctor: configured with delay {}", delayMs);
57                 this.delayMs = delayMs;
58         }
59
60         private String readDataFromPath(String path) throws IOException {
61                 InputStream is = MethodHandles.lookup().lookupClass().getClassLoader().getResourceAsStream(path);
62                 if (is == null) {
63                         String msg = "Failed to find resource on classpath: " + path;
64                         logger.error(msg);
65                         throw new RuntimeException(msg);
66                 }
67                 InputStreamReader reader = new InputStreamReader(is, "UTF-8");
68                 StringBuilder sb = new StringBuilder();
69                 char[] buf = new char[8192];
70                 int i;
71                 while ((i = reader.read(buf)) > 0)
72                         sb.append(buf, 0, i);
73                 reader.close();
74                 is.close();
75                 return sb.toString();
76         }
77
78         private SimpleKubernetesClient simpleKubernetesClient(String instanceKey) throws IOException {
79                 // File in src/test/resources
80                 String pltPods;
81                 if (RICInstanceMockConfiguration.INSTANCE_KEY_1.equals(instanceKey))
82                         pltPods = readDataFromPath("caas-ingress-ricplt-pods-1.json");
83                 else
84                         pltPods = readDataFromPath("caas-ingress-ricplt-pods-2.json");
85                 SimpleKubernetesClient mockClient = mock(SimpleKubernetesClient.class);
86                 doAnswer(inv -> {
87                         String ns = inv.<String>getArgument(0);
88                         logger.debug("listPods for namespace {}", ns);
89                         if (delayMs > 0) {
90                                 logger.debug("listPods sleeping {}", delayMs);
91                                 Thread.sleep(delayMs);
92                         }
93                         if ("ricplt".equals(ns))
94                                 return pltPods;
95                         else
96                                 throw new IllegalArgumentException("Fake server failure");
97                 }).when(mockClient).listPods(any(String.class));
98                 return mockClient;
99         }
100
101         @Bean
102         // The bean (method) name must be globally unique
103         public SimpleKubernetesClientBuilder simpleKubernetesClientBuilder() throws IOException {
104                 final SimpleKubernetesClientBuilder mockBuilder = mock(SimpleKubernetesClientBuilder.class);
105                 for (final String key : RICInstanceMockConfiguration.INSTANCE_KEYS) {
106                         SimpleKubernetesClient client = simpleKubernetesClient(key);
107                         when(mockBuilder.getSimpleKubernetesClient(key)).thenReturn(client);
108                 }
109                 return mockBuilder;
110         }
111
112 }