Non-functional changes to silence Sonar
[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         // Sonar wants separate declarations
55         private final NeighborCellRelationTable ncrt;
56         private final NeighborCellRelationTable ncrtNodeB1;
57         private final NeighborCellRelationTable ncrtNodeB2;
58         private final NeighborCellRelationTable ncrtNodeB3;
59
60         private final GgNodeBTable gNodebTable;
61
62         private static final String GNODEB1 = "001EF5:0045FE50";
63         private static final String GNODEB2 = "001EF6:0045FE51";
64         private static final String GNODEB3 = "001EF7:0045FE52";
65
66         public AnrXappMockConfiguration() {
67
68                 logger.info("Configuring mock ANR xApp client");
69                 gNodebTable = new GgNodeBTable();
70                 gNodebTable.addGNodeBIdsItem(GNODEB1).addGNodeBIdsItem(GNODEB2).addGNodeBIdsItem(GNODEB3);
71                 ncrtNodeB1 = new NeighborCellRelationTable();
72                 ncrtNodeB2 = new NeighborCellRelationTable();
73                 ncrtNodeB3 = new NeighborCellRelationTable();
74                 ncrt = new NeighborCellRelationTable();
75                 String[] neighbors1 = { "1104", "1105", "1106" };
76                 for (String n : neighbors1)
77                         ncrtNodeB1.addNcrtRelationsItem(
78                                         new NeighborCellRelation().servingCellNrcgi(GNODEB1 + ":1100").neighborCellNrpci(n)
79                                                         .neighborCellNrcgi(GNODEB1 + ":" + n).flagNoHo(true).flagNoXn(true).flagNoRemove(true));
80                 String[] neighbors2 = { "1471", "1472", "1473" };
81                 for (String n : neighbors2)
82                         ncrtNodeB2.addNcrtRelationsItem(
83                                         new NeighborCellRelation().servingCellNrcgi(GNODEB2 + ":1400").neighborCellNrpci(n)
84                                                         .neighborCellNrcgi(GNODEB2 + ":" + n).flagNoHo(false).flagNoXn(false).flagNoRemove(false));
85                 String[] neighbors3 = { "3601", "3601", "3602" };
86                 for (String n : neighbors3)
87                         ncrtNodeB3.addNcrtRelationsItem(
88                                         new NeighborCellRelation().servingCellNrcgi(GNODEB3 + ":3600").neighborCellNrpci(n)
89                                                         .neighborCellNrcgi(GNODEB3 + ":" + n).flagNoHo(true).flagNoXn(true).flagNoRemove(true));
90                 for (NeighborCellRelation ncr : ncrtNodeB1.getNcrtRelations())
91                         ncrt.addNcrtRelationsItem(ncr);
92                 for (NeighborCellRelation ncr : ncrtNodeB2.getNcrtRelations())
93                         ncrt.addNcrtRelationsItem(ncr);
94                 for (NeighborCellRelation ncr : ncrtNodeB3.getNcrtRelations())
95                         ncrt.addNcrtRelationsItem(ncr);
96         }
97
98         private ApiClient apiClient() {
99                 ApiClient mockClient = mock(ApiClient.class);
100                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
101                 return mockClient;
102         }
103
104         @Bean
105         // Use the same name as regular configuration
106         public HealthApi anrHealthApi() {
107                 ApiClient apiClient = apiClient();
108                 HealthApi mockApi = mock(HealthApi.class);
109                 when(mockApi.getApiClient()).thenReturn(apiClient);
110                 doAnswer(i -> null).when(mockApi).getHealthAlive();
111                 doAnswer(i -> null).when(mockApi).getHealthReady();
112                 return mockApi;
113         }
114
115         @Bean
116         // Use the same name as regular configuration
117         public NcrtApi anrNcrtApi() {
118                 ApiClient apiClient = apiClient();
119                 NcrtApi mockApi = mock(NcrtApi.class);
120                 when(mockApi.getApiClient()).thenReturn(apiClient);
121                 when(mockApi.getgNodeB()).thenReturn(gNodebTable);
122                 // Swagger sends nulls; front end sends empty strings
123                 when(mockApi.getNcrt((String) isNull(), (String) isNull(), (String) isNull())).thenReturn(ncrt);
124                 when(mockApi.getNcrt(eq(""), any(String.class), any(String.class))).thenReturn(ncrt);
125                 when(mockApi.getNcrt(eq(GNODEB1), any(String.class), any(String.class))).thenReturn(ncrtNodeB1);
126                 when(mockApi.getNcrt(eq(GNODEB2), any(String.class), any(String.class))).thenReturn(ncrtNodeB2);
127                 when(mockApi.getNcrt(eq(GNODEB3), any(String.class), any(String.class))).thenReturn(ncrtNodeB3);
128                 doAnswer(i -> null).when(mockApi).deleteNcrt(any(String.class), any(String.class));
129                 doAnswer(i -> null).when(mockApi).modifyNcrt(any(String.class), any(String.class),
130                                 any(NeighborCellRelationMod.class));
131                 return mockApi;
132         }
133
134 }