Revise user controller to answer real data
[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.Mockito.doAnswer;
23 import static org.mockito.Mockito.mock;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.lang.invoke.MethodHandles;
29
30 import org.oransc.ric.portal.dashboard.k8sapi.SimpleKubernetesClient;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.context.annotation.Bean;
35 import org.springframework.context.annotation.Configuration;
36 import org.springframework.context.annotation.Profile;
37
38 /**
39  * Creates mock implementations of Kubernetes clients that answer requests with
40  * sample data read from the filesystem.
41  */
42 @Configuration
43 @Profile("test")
44 public class CaasIngressMockConfiguration {
45
46         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
47
48         // Simulate remote method delay for UI testing
49         @Value("${mock.config.delay:0}")
50         private int delayMs;
51
52         private final String auxPods;
53         private final String pltPods;
54
55         public CaasIngressMockConfiguration() throws IOException {
56                 logger.info("Configuring mock CAAS-Ingres clients");
57                 // Files in src/test/resources
58                 auxPods = readDataFromPath("caas-ingress-ricaux-pods.json");
59                 pltPods = readDataFromPath("caas-ingress-ricplt-pods.json");
60         }
61
62         private String readDataFromPath(String path) throws IOException {
63                 InputStream is = MethodHandles.lookup().lookupClass().getClassLoader().getResourceAsStream(path);
64                 if (is == null) {
65                         String msg = "Failed to find resource on classpath: " + path;
66                         logger.error(msg);
67                         throw new RuntimeException(msg);
68                 }
69                 InputStreamReader reader = new InputStreamReader(is, "UTF-8");
70                 StringBuilder sb = new StringBuilder();
71                 char[] buf = new char[8192];
72                 int i;
73                 while ((i = reader.read(buf)) > 0)
74                         sb.append(buf, 0, i);
75                 reader.close();
76                 is.close();
77                 return sb.toString();
78         }
79
80         @Bean
81         // Use the same name as regular configuration
82         public SimpleKubernetesClient ciAuxApi() throws IOException {
83                 SimpleKubernetesClient mockClient = mock(SimpleKubernetesClient.class);
84                 doAnswer(inv -> {
85                         logger.debug("listPods for aux");
86                         return auxPods;
87                 }).when(mockClient).listPods("ricaux");
88                 return mockClient;
89         }
90
91         @Bean
92         // Use the same name as regular configuration
93         public SimpleKubernetesClient ciPltApi() throws IOException {
94                 SimpleKubernetesClient mockClient = mock(SimpleKubernetesClient.class);
95                 doAnswer(inv -> {
96                         logger.debug("listPods for plt");
97                         return pltPods;
98                 }).when(mockClient).listPods("ricplt");
99                 return mockClient;
100         }
101
102 }