Move mock configurations to test area
[portal/ric-dashboard.git] / webapp-backend / src / test / java / org / oransc / ric / portal / dashboard / test / config / AnrXappMockConfiguration.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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.test.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.context.annotation.Bean;
41 import org.springframework.context.annotation.Configuration;
42 import org.springframework.context.annotation.Profile;
43 import org.springframework.http.HttpStatus;
44
45 /**
46  * Creates a mock implementation of the ANR xApp client APIs.
47  */
48 @Profile("test")
49 @Configuration
50 public class AnrXappMockConfiguration {
51
52         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53
54         private static final String GNODEB1 = "001EF5:0045FE50";
55         private static final String GNODEB2 = "001EF6:0045FE51";
56         private static final String GNODEB3 = "001EF7:0045FE52";
57         // Sonar wants separate declarations
58         private final NeighborCellRelationTable ncrt;
59         private final NeighborCellRelationTable ncrtNodeB1;
60         private final NeighborCellRelationTable ncrtNodeB2;
61         private final NeighborCellRelationTable ncrtNodeB3;
62         private final GgNodeBTable gNodebTable;
63         // Simulate remote method delay for UI testing
64         private final int delayMs = 500;
65
66         public AnrXappMockConfiguration() {
67                 logger.info("Configuring mock ANR xApp client");
68                 gNodebTable = new GgNodeBTable();
69                 gNodebTable.addGNodeBIdsItem(GNODEB1).addGNodeBIdsItem(GNODEB2).addGNodeBIdsItem(GNODEB3);
70                 ncrtNodeB1 = new NeighborCellRelationTable();
71                 ncrtNodeB2 = new NeighborCellRelationTable();
72                 ncrtNodeB3 = new NeighborCellRelationTable();
73                 ncrt = new NeighborCellRelationTable();
74                 String[] neighbors1 = { "1104", "1105", "1106" };
75                 for (String n : neighbors1)
76                         ncrtNodeB1.addNcrtRelationsItem(
77                                         new NeighborCellRelation().servingCellNrcgi(GNODEB1 + ":1100").neighborCellNrpci(n)
78                                                         .neighborCellNrcgi(GNODEB1 + ":" + n).flagNoHo(true).flagNoXn(true).flagNoRemove(true));
79                 String[] neighbors2 = { "1471", "1472", "1473" };
80                 for (String n : neighbors2)
81                         ncrtNodeB2.addNcrtRelationsItem(
82                                         new NeighborCellRelation().servingCellNrcgi(GNODEB2 + ":1400").neighborCellNrpci(n)
83                                                         .neighborCellNrcgi(GNODEB2 + ":" + n).flagNoHo(false).flagNoXn(false).flagNoRemove(false));
84                 String[] neighbors3 = { "3601", "3601", "3602" };
85                 for (String n : neighbors3)
86                         ncrtNodeB3.addNcrtRelationsItem(
87                                         new NeighborCellRelation().servingCellNrcgi(GNODEB3 + ":3600").neighborCellNrpci(n)
88                                                         .neighborCellNrcgi(GNODEB3 + ":" + n).flagNoHo(true).flagNoXn(true).flagNoRemove(true));
89                 for (NeighborCellRelation ncr : ncrtNodeB1.getNcrtRelations())
90                         ncrt.addNcrtRelationsItem(ncr);
91                 for (NeighborCellRelation ncr : ncrtNodeB2.getNcrtRelations())
92                         ncrt.addNcrtRelationsItem(ncr);
93                 for (NeighborCellRelation ncr : ncrtNodeB3.getNcrtRelations())
94                         ncrt.addNcrtRelationsItem(ncr);
95         }
96
97         private ApiClient apiClient() {
98                 ApiClient mockClient = mock(ApiClient.class);
99                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
100                 return mockClient;
101         }
102
103         @Bean
104         // Use the same name as regular configuration
105         public HealthApi anrHealthApi() {
106                 ApiClient apiClient = apiClient();
107                 HealthApi mockApi = mock(HealthApi.class);
108                 when(mockApi.getApiClient()).thenReturn(apiClient);
109                 doAnswer(i -> null).when(mockApi).getHealthAlive();
110                 doAnswer(i -> null).when(mockApi).getHealthReady();
111                 return mockApi;
112         }
113
114         @Bean
115         // Use the same name as regular configuration
116         public NcrtApi anrNcrtApi() {
117                 ApiClient apiClient = apiClient();
118                 NcrtApi mockApi = mock(NcrtApi.class);
119                 when(mockApi.getApiClient()).thenReturn(apiClient);
120                 doAnswer(inv -> {
121                         logger.debug("getgNodeB sleeping {}", delayMs);
122                         Thread.sleep(delayMs);
123                         return gNodebTable;
124                 }).when(mockApi).getgNodeB();
125                 // Swagger sends nulls; front end sends empty strings
126                 doAnswer(inv -> {
127                         logger.debug("getNcrt (1) sleeping {}", delayMs);
128                         Thread.sleep(delayMs);
129                         return ncrt;
130                 }).when(mockApi).getNcrt((String) isNull(), (String) isNull(), (String) isNull());
131                 doAnswer(inv -> {
132                         logger.debug("getNcrt (2) sleeping {}", delayMs);
133                         Thread.sleep(delayMs);
134                         return ncrt;
135                 }).when(mockApi).getNcrt(eq(""), any(String.class), any(String.class));
136                 doAnswer(inv -> {
137                         logger.debug("getNcrt (3) sleeping {}", delayMs);
138                         Thread.sleep(delayMs);
139                         return ncrtNodeB1;
140                 }).when(mockApi).getNcrt(eq(GNODEB1), any(String.class), any(String.class));
141                 doAnswer(inv -> {
142                         logger.debug("getNcrt (4) sleeping {}", delayMs);
143                         Thread.sleep(delayMs);
144                         return ncrtNodeB2;
145                 }).when(mockApi).getNcrt(eq(GNODEB2), any(String.class), any(String.class));
146                 doAnswer(inv -> {
147                         logger.debug("getNcrt (5) sleeping {}", delayMs);
148                         Thread.sleep(delayMs);
149                         return ncrtNodeB3;
150                 }).when(mockApi).getNcrt(eq(GNODEB3), any(String.class), any(String.class));
151                 doAnswer(inv -> {
152                         logger.debug("deleteNcrt sleeping {}", delayMs);
153                         Thread.sleep(delayMs);
154                         return null;
155                 }).when(mockApi).deleteNcrt(any(String.class), any(String.class));
156                 doAnswer(inv -> {
157                         logger.debug("modifyNcrt sleeping {}", delayMs);
158                         Thread.sleep(delayMs);
159                         return null;
160                 }).when(mockApi).modifyNcrt(any(String.class), any(String.class), any(NeighborCellRelationMod.class));
161                 return mockApi;
162         }
163
164 }