Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / mapper / src / test / java / org / commscope / tr069adapter / mapper / NetConfServerManagerTest.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : tr-069-adapter
4  * =================================================================================================
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.
6  * =================================================================================================
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9  * may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ===============LICENSE_END=======================================================================
17  */
18
19 package org.commscope.tr069adapter.mapper;
20
21 import java.util.ArrayList;
22
23 import org.commscope.tr069adapter.acs.common.DeviceDetails;
24 import org.commscope.tr069adapter.acs.common.DeviceInform;
25 import org.commscope.tr069adapter.acs.common.InformType;
26 import org.commscope.tr069adapter.acs.common.ParameterDTO;
27 import org.commscope.tr069adapter.acs.common.dto.TR069InformType;
28 import org.commscope.tr069adapter.acs.common.inform.BootstrapInform;
29 import org.commscope.tr069adapter.mapper.boot.MapperServiceBooter;
30 import org.commscope.tr069adapter.mapper.model.NetConfServerDetails;
31 import org.commscope.tr069adapter.mapper.model.VESNotificationResponse;
32 import org.commscope.tr069adapter.mapper.netconf.NetConfNotificationSender;
33 import org.commscope.tr069adapter.mapper.netconf.NetConfServerManager;
34 import org.commscope.tr069adapter.mapper.ves.VESNotificationSender;
35 import org.junit.Assert;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mockito;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.boot.test.mock.mockito.MockBean;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.test.context.junit4.SpringRunner;
45 import org.springframework.web.client.RestTemplate;
46
47 @SpringBootTest(classes = {MapperServiceBooter.class})
48 @RunWith(SpringRunner.class)
49 public class NetConfServerManagerTest {
50
51   @Autowired
52   NetConfServerManager server;
53
54   @Autowired
55   NetConfNotificationSender nfSender;
56
57   @Autowired
58   VESNotificationSender vesSender;
59
60   @MockBean
61   RestTemplate restTemplate;
62
63   @Test
64   public void createServerTest() {
65     ResponseEntity<Object> res = new ResponseEntity<Object>(
66         new NetConfServerDetails("0005B9A1", "10.221.55.14", "17830"), HttpStatus.ACCEPTED);
67     Mockito.when(
68         restTemplate.postForEntity(Mockito.anyString(), Mockito.anyObject(), Mockito.anyObject()))
69         .thenReturn(res);
70     NetConfServerDetails nfDetails =
71         server.createNetconfServer("0005B9A1", "EnodB1", "4.3.0.0", "*");
72
73     Assert.assertNotNull(nfDetails);
74     Assert.assertEquals("17830", nfDetails.getListenPort());
75   }
76
77   @Test
78   public void createServerFailureWithRestCallTest() {
79     NetConfServerDetails nfDetails =
80         server.createNetconfServer("0005B9A1", "EnodB1", "4.3.0.0", "*");
81     Assert.assertNull(nfDetails);
82   }
83
84   @Test
85   public void sendNotificaionTest() {
86     ResponseEntity res = new ResponseEntity<>(HttpStatus.ACCEPTED);
87     Mockito.when(
88         restTemplate.postForObject(Mockito.anyString(), Mockito.anyObject(), Mockito.anyObject()))
89         .thenReturn(res);
90     DeviceInform inform = new BootstrapInform();
91     inform.setDeviceDetails(getDeviceDetails());
92     ArrayList<InformType> list = new ArrayList<>();
93     list.add(TR069InformType.BOOTSTRAP);
94     inform.setInformTypeList(list);
95     inform.setParameters(getGeneralParams());
96     ResponseEntity response = nfSender.sendNotification(inform);
97     Assert.assertNotNull(response);
98     Assert.assertEquals(HttpStatus.ACCEPTED, response.getStatusCode());
99   }
100
101   @Test
102   public void sendNotificaionInformNullTest() {
103     ResponseEntity res = new ResponseEntity<>(HttpStatus.ACCEPTED);
104     Mockito.when(
105         restTemplate.postForObject(Mockito.anyString(), Mockito.anyObject(), Mockito.anyObject()))
106         .thenReturn(res);
107     ResponseEntity response = nfSender.sendNotification(null);
108     Assert.assertNull(response);
109   }
110
111   @Test
112   public void sendVesEventTest() {
113
114     Mockito.when(
115         restTemplate.postForObject(Mockito.anyString(), Mockito.anyObject(), Mockito.anyObject()))
116         .thenReturn(new VESNotificationResponse(202, "sucess"));
117
118     DeviceInform inform = new BootstrapInform();
119     inform.setDeviceDetails(getDeviceDetails());
120     ArrayList<InformType> list = new ArrayList<InformType>();
121     list.add(TR069InformType.BOOTSTRAP);
122     inform.setInformTypeList(list);
123     inform.setParameters(getGeneralParams());
124     VESNotificationResponse response = vesSender.sendNotification(inform,
125         new NetConfServerDetails("0005B9A1", "10.221.55.14", "17830"));
126     Assert.assertNotNull(response);
127     Assert.assertEquals(202, response.getStatusCode());
128   }
129
130   private ArrayList<ParameterDTO> getGeneralParams() {
131     ArrayList<ParameterDTO> params = new ArrayList<>();
132     params.add(new ParameterDTO("Device.DeviceInfo.ManufacturerOUI", "0005B9"));
133     params.add(new ParameterDTO("Device.DeviceInfo.ProductClass", "LTE_Enterprise_C-RANSC_Cntrl"));
134     params.add(new ParameterDTO("Device.Services.FAPService.1.CellConfig.LTE.EPC.PLMNList.1.PLMNID",
135         "30324"));
136     params.add(new ParameterDTO(
137         "Device.Services.FAPService.1.CellConfig.LTE.EPC.PLMNList.1.IsPrimary", "1"));
138     params.add(
139         new ParameterDTO("Device.Services.FAPService.1.CellConfig.LTE.EPC.PLMNList.1.Enable", "0"));
140     return params;
141   }
142
143   private DeviceDetails getDeviceDetails() {
144     DeviceDetails nf = new DeviceDetails();
145     nf.setDeviceId("00005B9A1");
146     nf.setDeviceTypeId(50);
147     nf.setOui("0005B9");
148     nf.setProductClass("LTE_Enterprise_C-RANSC_Cntrl");
149     return nf;
150   }
151 }