Split URL properties into prefix/suffix
[portal/ric-dashboard.git] / webapp-backend / src / main / 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 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.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("mock")
49 @Configuration
50 public class AnrXappMockConfiguration {
51
52         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53
54         private final NeighborCellRelationTable ncrt, ncrtNodeB1, ncrtNodeB2, ncrtNodeB3;
55         private final GgNodeBTable gNodebTable;
56
57         private static final String GNODEB1 = "001EF5:0045FE50";
58         private static final String GNODEB2 = "001EF6:0045FE51";
59         private static final String GNODEB3 = "001EF7:0045FE52";
60
61         public AnrXappMockConfiguration() {
62
63                 logger.info("Configuring mock ANR xApp client");
64                 gNodebTable = new GgNodeBTable();
65                 gNodebTable.addGNodeBIdsItem(GNODEB1).addGNodeBIdsItem(GNODEB2).addGNodeBIdsItem(GNODEB3);
66                 ncrtNodeB1 = new NeighborCellRelationTable();
67                 ncrtNodeB2 = new NeighborCellRelationTable();
68                 ncrtNodeB3 = new NeighborCellRelationTable();
69                 ncrt = new NeighborCellRelationTable();
70                 String[] neighbors1 = { "1104", "1105", "1106" };
71                 for (String n : neighbors1)
72                         ncrtNodeB1.addNcrtRelationsItem(
73                                         new NeighborCellRelation().servingCellNrcgi(GNODEB1 + ":1100").neighborCellNrpci(n)
74                                                         .neighborCellNrcgi(GNODEB1 + ":" + n).flagNoHo(true).flagNoXn(true).flagNoRemove(true));
75                 String[] neighbors2 = { "1471", "1472", "1473" };
76                 for (String n : neighbors2)
77                         ncrtNodeB2.addNcrtRelationsItem(
78                                         new NeighborCellRelation().servingCellNrcgi(GNODEB2 + ":1400").neighborCellNrpci(n)
79                                                         .neighborCellNrcgi(GNODEB2 + ":" + n).flagNoHo(false).flagNoXn(false).flagNoRemove(false));
80                 String[] neighbors3 = { "3601", "3601", "3602" };
81                 for (String n : neighbors3)
82                         ncrtNodeB3.addNcrtRelationsItem(
83                                         new NeighborCellRelation().servingCellNrcgi(GNODEB3 + ":3600").neighborCellNrpci(n)
84                                                         .neighborCellNrcgi(GNODEB3 + ":" + n).flagNoHo(true).flagNoXn(true).flagNoRemove(true));
85                 for (NeighborCellRelation ncr : ncrtNodeB1.getNcrtRelations())
86                         ncrt.addNcrtRelationsItem(ncr);
87                 for (NeighborCellRelation ncr : ncrtNodeB2.getNcrtRelations())
88                         ncrt.addNcrtRelationsItem(ncr);
89                 for (NeighborCellRelation ncr : ncrtNodeB3.getNcrtRelations())
90                         ncrt.addNcrtRelationsItem(ncr);
91         }
92
93         private ApiClient apiClient() {
94                 ApiClient mockClient = mock(ApiClient.class);
95                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
96                 return mockClient;
97         }
98
99         @Bean
100         // Use the same name as regular configuration
101         public HealthApi anrHealthApi() {
102                 ApiClient apiClient = apiClient();
103                 HealthApi mockApi = mock(HealthApi.class);
104                 when(mockApi.getApiClient()).thenReturn(apiClient);
105                 doAnswer(i -> {
106                         return null;
107                 }).when(mockApi).getHealthAlive();
108                 doAnswer(i -> {
109                         return null;
110                 }).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                 when(mockApi.getgNodeB()).thenReturn(gNodebTable);
121                 // Swagger sends nulls; front end sends empty strings
122                 when(mockApi.getNcrt((String) isNull(), (String) isNull(), (String) isNull())).thenReturn(ncrt);
123                 when(mockApi.getNcrt(eq(""), any(String.class), any(String.class))).thenReturn(ncrt);
124                 when(mockApi.getNcrt(eq(GNODEB1), any(String.class), any(String.class))).thenReturn(ncrtNodeB1);
125                 when(mockApi.getNcrt(eq(GNODEB2), any(String.class), any(String.class))).thenReturn(ncrtNodeB2);
126                 when(mockApi.getNcrt(eq(GNODEB3), any(String.class), any(String.class))).thenReturn(ncrtNodeB3);
127                 doAnswer(i -> {
128                         return null;
129                 }).when(mockApi).deleteNcrt(any(String.class), any(String.class));
130                 doAnswer(i -> {
131                         return null;
132                 }).when(mockApi).modifyNcrt(any(String.class), any(String.class), any(NeighborCellRelationMod.class));
133                 return mockApi;
134         }
135
136 }