550963b33ed7c2d2f53e775c28e468e7472180f3
[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.ArgumentMatchers.startsWith;
26 import static org.mockito.Mockito.doAnswer;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29
30 import java.lang.invoke.MethodHandles;
31
32 import org.oransc.ric.anrxapp.client.api.HealthApi;
33 import org.oransc.ric.anrxapp.client.api.NcrtApi;
34 import org.oransc.ric.anrxapp.client.invoker.ApiClient;
35 import org.oransc.ric.anrxapp.client.model.GgNodeBTable;
36 import org.oransc.ric.anrxapp.client.model.NeighborCellRelation;
37 import org.oransc.ric.anrxapp.client.model.NeighborCellRelationMod;
38 import org.oransc.ric.anrxapp.client.model.NeighborCellRelationTable;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
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 @Profile("mock")
50 @Configuration
51 public class AnrXappMockConfiguration {
52
53         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54
55         private final NeighborCellRelationTable ncrt, ncrtNodeB1, ncrtNodeB2;
56         private final GgNodeBTable gNodebTable;
57
58         public AnrXappMockConfiguration() {
59                 logger.info("Configuring mock ANR xApp client");
60                 gNodebTable = new GgNodeBTable();
61                 gNodebTable.addGNodeBIdsItem("A").addGNodeBIdsItem("B");
62                 ncrtNodeB1 = new NeighborCellRelationTable();
63                 ncrtNodeB2 = new NeighborCellRelationTable();
64                 ncrt = new NeighborCellRelationTable();
65                 String[] cells1 = { "A", "B", "C", "D" };
66                 for (String s : cells1)
67                         ncrtNodeB1.addNcrtRelationsItem(
68                                         new NeighborCellRelation().servingCellNrcgi(s + "12345").neighborCellNrpci(s + "12346")
69                                                         .neighborCellNrcgi(s + "12347").flagNoHo(true).flagNoXn(true).flagNoRemove(true));
70                 String[] cells2 = { "E", "F", "G", "H" };
71                 for (String s : cells2)
72                         ncrtNodeB2.addNcrtRelationsItem(
73                                         new NeighborCellRelation().servingCellNrcgi(s + "12345").neighborCellNrpci(s + "12346")
74                                                         .neighborCellNrcgi(s + "12347").flagNoHo(true).flagNoXn(true).flagNoRemove(true));
75                 for (NeighborCellRelation ncr : ncrtNodeB1.getNcrtRelations())
76                         ncrt.addNcrtRelationsItem(ncr);
77                 for (NeighborCellRelation ncr : ncrtNodeB2.getNcrtRelations())
78                         ncrt.addNcrtRelationsItem(ncr);
79         }
80
81         private ApiClient apiClient() {
82                 ApiClient mockClient = mock(ApiClient.class);
83                 when(mockClient.getStatusCode()).thenReturn(HttpStatus.OK);
84                 return mockClient;
85         }
86
87         @Bean
88         public HealthApi anrHealthApi() {
89                 ApiClient apiClient = apiClient();
90                 HealthApi mockApi = mock(HealthApi.class);
91                 when(mockApi.getApiClient()).thenReturn(apiClient);
92                 doAnswer(i -> {
93                         return null;
94                 }).when(mockApi).getHealthAlive();
95                 doAnswer(i -> {
96                         return null;
97                 }).when(mockApi).getHealthReady();
98                 return mockApi;
99         }
100
101         @Bean
102         public NcrtApi ncrtMockApi() {
103                 ApiClient apiClient = apiClient();
104                 NcrtApi mockApi = mock(NcrtApi.class);
105                 when(mockApi.getApiClient()).thenReturn(apiClient);
106                 when(mockApi.getgNodeB()).thenReturn(gNodebTable);
107                 // Swagger sends nulls; front end sends empty strings
108                 when(mockApi.getNcrt((String) isNull(), (String) isNull(), (String) isNull())).thenReturn(ncrt);
109                 when(mockApi.getNcrt(eq(""), any(String.class), any(String.class))).thenReturn(ncrt);
110                 when(mockApi.getNcrt(startsWith("A"), any(String.class), any(String.class))).thenReturn(ncrtNodeB1);
111                 when(mockApi.getNcrt(startsWith("B"), any(String.class), any(String.class))).thenReturn(ncrtNodeB2);
112                 doAnswer(i -> {
113                         return null;
114                 }).when(mockApi).deleteNcrt(any(String.class), any(String.class));
115                 doAnswer(i -> {
116                         return null;
117                 }).when(mockApi).modifyNcrt(any(String.class), any(String.class), any(NeighborCellRelationMod.class));
118                 return mockApi;
119         }
120
121 }