Revise user controller to answer real data
[portal/ric-dashboard.git] / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / config / AnrXappMockConfiguration.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.ArgumentMatchers.eq;
24 import static org.mockito.ArgumentMatchers.isNull;
25 import static org.mockito.Mockito.doAnswer;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.lang.invoke.MethodHandles;
30
31 import org.oransc.ric.anrxapp.client.api.HealthApi;
32 import org.oransc.ric.anrxapp.client.api.NcrtApi;
33 import org.oransc.ric.anrxapp.client.invoker.ApiClient;
34 import org.oransc.ric.anrxapp.client.model.GgNodeBTable;
35 import org.oransc.ric.anrxapp.client.model.NeighborCellRelation;
36 import org.oransc.ric.anrxapp.client.model.NeighborCellRelationMod;
37 import org.oransc.ric.anrxapp.client.model.NeighborCellRelationTable;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.context.annotation.Bean;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.context.annotation.Profile;
44 import org.springframework.http.HttpStatus;
45
46 /**
47  * Creates a mock implementation of the ANR xApp client APIs.
48  */
49 @Configuration
50 @Profile("test")
51 public class AnrXappMockConfiguration {
52
53         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54
55         // Simulate remote method delay for UI testing
56         @Value("${mock.config.delay:0}")
57         private int delayMs;
58
59         private static final String GNODEB1 = "001EF5:0045FE50";
60         private static final String GNODEB2 = "001EF6:0045FE51";
61         private static final String GNODEB3 = "001EF7:0045FE52";
62
63         // Sonar wants separate declarations
64         private final NeighborCellRelationTable ncrt;
65         private final NeighborCellRelationTable ncrtNodeB1;
66         private final NeighborCellRelationTable ncrtNodeB2;
67         private final NeighborCellRelationTable ncrtNodeB3;
68         private final GgNodeBTable gNodebTable;
69
70         public AnrXappMockConfiguration() {
71                 logger.info("Configuring mock ANR xApp client");
72                 gNodebTable = new GgNodeBTable();
73                 gNodebTable.addGNodeBIdsItem(GNODEB1).addGNodeBIdsItem(GNODEB2).addGNodeBIdsItem(GNODEB3);
74                 ncrtNodeB1 = new NeighborCellRelationTable();
75                 ncrtNodeB2 = new NeighborCellRelationTable();
76                 ncrtNodeB3 = new NeighborCellRelationTable();
77                 ncrt = new NeighborCellRelationTable();
78                 String[] neighbors1 = { "1104", "1105", "1106" };
79                 for (String n : neighbors1)
80                         ncrtNodeB1.addNcrtRelationsItem(
81                                         new NeighborCellRelation().servingCellNrcgi(GNODEB1 + ":1100").neighborCellNrpci(n)
82                                                         .neighborCellNrcgi(GNODEB1 + ":" + n).flagNoHo(true).flagNoXn(true).flagNoRemove(true));
83                 String[] neighbors2 = { "1471", "1472", "1473" };
84                 for (String n : neighbors2)
85                         ncrtNodeB2.addNcrtRelationsItem(
86                                         new NeighborCellRelation().servingCellNrcgi(GNODEB2 + ":1400").neighborCellNrpci(n)
87                                                         .neighborCellNrcgi(GNODEB2 + ":" + n).flagNoHo(false).flagNoXn(false).flagNoRemove(false));
88                 String[] neighbors3 = { "3601", "3601", "3602" };
89                 for (String n : neighbors3)
90                         ncrtNodeB3.addNcrtRelationsItem(
91                                         new NeighborCellRelation().servingCellNrcgi(GNODEB3 + ":3600").neighborCellNrpci(n)
92                                                         .neighborCellNrcgi(GNODEB3 + ":" + n).flagNoHo(true).flagNoXn(true).flagNoRemove(true));
93                 for (NeighborCellRelation ncr : ncrtNodeB1.getNcrtRelations())
94                         ncrt.addNcrtRelationsItem(ncr);
95                 for (NeighborCellRelation ncr : ncrtNodeB2.getNcrtRelations())
96                         ncrt.addNcrtRelationsItem(ncr);
97                 for (NeighborCellRelation ncr : ncrtNodeB3.getNcrtRelations())
98                         ncrt.addNcrtRelationsItem(ncr);
99         }
100
101         private ApiClient apiClient() {
102                 ApiClient mockClient = mock(ApiClient.class);
103                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
104                 return mockClient;
105         }
106
107         @Bean
108         // Use the same name as regular configuration
109         public HealthApi anrHealthApi() {
110                 ApiClient apiClient = apiClient();
111                 HealthApi mockApi = mock(HealthApi.class);
112                 when(mockApi.getApiClient()).thenReturn(apiClient);
113                 doAnswer(i -> null).when(mockApi).getHealthAlive();
114                 doAnswer(i -> null).when(mockApi).getHealthReady();
115                 return mockApi;
116         }
117
118         @Bean
119         // Use the same name as regular configuration
120         public NcrtApi anrNcrtApi() {
121                 ApiClient apiClient = apiClient();
122                 NcrtApi mockApi = mock(NcrtApi.class);
123                 when(mockApi.getApiClient()).thenReturn(apiClient);
124                 doAnswer(inv -> {
125                         if (delayMs > 0) {
126                                 logger.debug("getgNodeB sleeping {}", delayMs);
127                                 Thread.sleep(delayMs);
128                         }
129                         return gNodebTable;
130                 }).when(mockApi).getgNodeB();
131                 // Swagger sends nulls; front end sends empty strings
132                 doAnswer(inv -> {
133                         if (delayMs > 0) {
134                                 logger.debug("getNcrt (1) sleeping {}", delayMs);
135                                 Thread.sleep(delayMs);
136                         }
137                         return ncrt;
138                 }).when(mockApi).getNcrt((String) isNull(), (String) isNull(), (String) isNull());
139                 doAnswer(inv -> {
140                         if (delayMs > 0) {
141                                 logger.debug("getNcrt (2) sleeping {}", delayMs);
142                                 Thread.sleep(delayMs);
143                         }
144                         return ncrt;
145                 }).when(mockApi).getNcrt(eq(""), any(String.class), any(String.class));
146                 doAnswer(inv -> {
147                         if (delayMs > 0) {
148                                 logger.debug("getNcrt (3) sleeping {}", delayMs);
149                                 Thread.sleep(delayMs);
150                         }
151                         return ncrtNodeB1;
152                 }).when(mockApi).getNcrt(eq(GNODEB1), any(String.class), any(String.class));
153                 doAnswer(inv -> {
154                         if (delayMs > 0) {
155                                 logger.debug("getNcrt (4) sleeping {}", delayMs);
156                                 Thread.sleep(delayMs);
157                         }
158                         return ncrtNodeB2;
159                 }).when(mockApi).getNcrt(eq(GNODEB2), any(String.class), any(String.class));
160                 doAnswer(inv -> {
161                         if (delayMs > 0) {
162                                 logger.debug("getNcrt (5) sleeping {}", delayMs);
163                                 Thread.sleep(delayMs);
164                         }
165                         return ncrtNodeB3;
166                 }).when(mockApi).getNcrt(eq(GNODEB3), any(String.class), any(String.class));
167                 doAnswer(inv -> {
168                         if (delayMs > 0) {
169                                 logger.debug("deleteNcrt sleeping {}", delayMs);
170                                 Thread.sleep(delayMs);
171                         }
172                         String servCellNrcgi = inv.<String>getArgument(0);
173                         String neighCellNrpci = inv.<String>getArgument(1);
174                         for (NeighborCellRelation ncr : ncrt.getNcrtRelations()) {
175                                 if (servCellNrcgi.equals(ncr.getServingCellNrcgi())
176                                                 && neighCellNrpci.equals(ncr.getNeighborCellNrpci())) {
177                                         logger.debug("deleteNcrt: removing {}", ncr);
178                                         ncrt.getNcrtRelations().remove(ncr);
179                                         break;
180                                 }
181                         }
182                         return null;
183                 }).when(mockApi).deleteNcrt(any(String.class), any(String.class));
184                 doAnswer(inv -> {
185                         if (delayMs > 0) {
186                                 logger.debug("modifyNcrt sleeping {}", delayMs);
187                                 Thread.sleep(delayMs);
188                         }
189                         String servCellNrcgi = inv.<String>getArgument(0);
190                         String neighCellNrpci = inv.<String>getArgument(1);
191                         NeighborCellRelationMod mod = inv.<NeighborCellRelationMod>getArgument(2);
192                         for (NeighborCellRelation ncr : ncrt.getNcrtRelations()) {
193                                 if (servCellNrcgi.equals(ncr.getServingCellNrcgi())
194                                                 && neighCellNrpci.equals(ncr.getNeighborCellNrpci())) {
195                                         logger.debug("modifyNcrt: modifying {} to {}", ncr, mod);
196                                         ncr.setFlagNoHo(mod.isFlagNoHo());
197                                         ncr.setFlagNoRemove(mod.isFlagNoRemove());
198                                         ncr.setFlagNoXn(mod.isFlagNoXn());
199                                         break;
200                                 }
201                         }
202                         return null;
203                 }).when(mockApi).modifyNcrt(any(String.class), any(String.class), any(NeighborCellRelationMod.class));
204                 return mockApi;
205         }
206
207 }