Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / netconf-server / src / test / java / org / commscope / tr069adapter / netconf / restapi / NetConfServerManagerRestApiTest.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.netconf.restapi;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22
23 import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;
24 import org.commscope.tr069adapter.netconf.dao.NetConfServerDetailsRepository;
25 import org.commscope.tr069adapter.netconf.entity.NetConfServerDetailsEntity;
26 import org.commscope.tr069adapter.netconf.server.NetConfServerManagerImpl;
27 import org.junit.FixMethodOrder;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.junit.runners.MethodSorters;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.boot.test.mock.mockito.MockBean;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.MediaType;
37 import org.springframework.mock.web.MockHttpServletResponse;
38 import org.springframework.test.context.junit.jupiter.SpringExtension;
39 import org.springframework.test.web.servlet.MockMvc;
40 import org.springframework.test.web.servlet.MvcResult;
41 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
42 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
43
44 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
45 @ExtendWith(SpringExtension.class)
46 @SpringBootTest(classes = {NetConfServiceBooter.class},
47     args = "--schemas-dir test-schemas --debug true --starting-port 17830")
48 @AutoConfigureMockMvc
49 public class NetConfServerManagerRestApiTest {
50
51   @Autowired
52   private MockMvc mockMvc;
53
54   @MockBean
55   NetConfServerDetailsRepository netconfDAO;
56
57   @Autowired
58   NetConfServerManagerImpl manager;
59
60   @Test
61   public void createNetconfServer() throws Exception {
62
63     NetConfServerDetailsEntity entity = new NetConfServerDetailsEntity();
64     entity.setDeviceId("0005B9AB1");
65     entity.setEnodeBName("0005B9AB1");
66     entity.setId(1l);
67     entity.setListenPort("17830");
68
69     MockHttpServletRequestBuilder requestBuilder =
70         MockMvcRequestBuilders.post("/netConfServerManagerService/createServer")
71             .param("deviceId", "0005B9AB1").param("enodeBName", "0005B9AB1")
72             .param("swVersion", "4.4.3").param("hwVersion", "*").accept(MediaType.APPLICATION_JSON);
73     MvcResult result = mockMvc.perform(requestBuilder).andReturn();
74
75     MockHttpServletResponse response = result.getResponse();
76
77     assertEquals(HttpStatus.OK.value(), response.getStatus());
78
79   }
80
81   @Test
82   public void listNetconfServers() throws Exception {
83
84     MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
85         .get("/netConfServerManagerService/listServers").accept(MediaType.APPLICATION_JSON);
86     MvcResult result = mockMvc.perform(requestBuilder).andReturn();
87     MockHttpServletResponse response = result.getResponse();
88
89     assertEquals(HttpStatus.OK.value(), response.getStatus());
90
91   }
92
93   @Test
94   public void restartServersOnStartup() {
95     boolean result = false;
96     try {
97       manager.restartServers();
98       result = true;
99     } catch (Exception e) {
100       assertEquals(false, result); // if no exception
101     }
102     assertEquals(true, result); // if no exception
103
104   }
105 }