Initial source code
[oam/tr069-adapter.git] / netconf-server / src / test / java / org / commscope / tr069adapter / netconf / restapi / NetConfServerManagerRestApiTest.java
1 /*\r
2  * ============LICENSE_START========================================================================\r
3  * ONAP : tr-069-adapter\r
4  * =================================================================================================\r
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
6  * =================================================================================================\r
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
9  * may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
14  * either express or implied. See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ===============LICENSE_END=======================================================================\r
17  */\r
18 \r
19 package org.commscope.tr069adapter.netconf.restapi;\r
20 \r
21 import static org.junit.jupiter.api.Assertions.assertEquals;\r
22 \r
23 import org.commscope.tr069adapter.netconf.boot.NetConfServiceBooter;\r
24 import org.commscope.tr069adapter.netconf.dao.NetConfServerDetailsRepository;\r
25 import org.commscope.tr069adapter.netconf.entity.NetConfServerDetailsEntity;\r
26 import org.commscope.tr069adapter.netconf.server.NetConfServerManagerImpl;\r
27 import org.junit.FixMethodOrder;\r
28 import org.junit.jupiter.api.Test;\r
29 import org.junit.jupiter.api.extension.ExtendWith;\r
30 import org.junit.runners.MethodSorters;\r
31 import org.springframework.beans.factory.annotation.Autowired;\r
32 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;\r
33 import org.springframework.boot.test.context.SpringBootTest;\r
34 import org.springframework.boot.test.mock.mockito.MockBean;\r
35 import org.springframework.http.HttpStatus;\r
36 import org.springframework.http.MediaType;\r
37 import org.springframework.mock.web.MockHttpServletResponse;\r
38 import org.springframework.test.context.junit.jupiter.SpringExtension;\r
39 import org.springframework.test.web.servlet.MockMvc;\r
40 import org.springframework.test.web.servlet.MvcResult;\r
41 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;\r
42 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;\r
43 \r
44 @FixMethodOrder(MethodSorters.NAME_ASCENDING)\r
45 @ExtendWith(SpringExtension.class)\r
46 @SpringBootTest(classes = {NetConfServiceBooter.class},\r
47     args = "--schemas-dir test-schemas --debug true --starting-port 17830")\r
48 @AutoConfigureMockMvc\r
49 public class NetConfServerManagerRestApiTest {\r
50 \r
51   @Autowired\r
52   private MockMvc mockMvc;\r
53 \r
54   @MockBean\r
55   NetConfServerDetailsRepository netconfDAO;\r
56 \r
57   @Autowired\r
58   NetConfServerManagerImpl manager;\r
59 \r
60   @Test\r
61   public void createNetconfServer() throws Exception {\r
62 \r
63     NetConfServerDetailsEntity entity = new NetConfServerDetailsEntity();\r
64     entity.setDeviceId("0005B9AB1");\r
65     entity.setEnodeBName("0005B9AB1");\r
66     entity.setId(1l);\r
67     entity.setListenPort("17830");\r
68 \r
69     MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders\r
70         .post("/netConfServerManagerService/createServer").param("deviceId", "0005B9AB1")\r
71         .param("enodeBName", "0005B9AB1").accept(MediaType.APPLICATION_JSON);\r
72     MvcResult result = mockMvc.perform(requestBuilder).andReturn();\r
73 \r
74     MockHttpServletResponse response = result.getResponse();\r
75 \r
76     assertEquals(HttpStatus.OK.value(), response.getStatus());\r
77 \r
78   }\r
79 \r
80   @Test\r
81   public void listNetconfServers() throws Exception {\r
82 \r
83     MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders\r
84         .get("/netConfServerManagerService/listServers").accept(MediaType.APPLICATION_JSON);\r
85     MvcResult result = mockMvc.perform(requestBuilder).andReturn();\r
86     MockHttpServletResponse response = result.getResponse();\r
87 \r
88     assertEquals(HttpStatus.OK.value(), response.getStatus());\r
89 \r
90   }\r
91 \r
92   @Test\r
93   public void restartServersOnStartup() {\r
94     boolean result = false;\r
95     try {\r
96       manager.restartServers();\r
97       result = true;\r
98     } catch (Exception e) {\r
99       assertEquals(false, result); // if no exception\r
100     }\r
101     assertEquals(true, result); // if no exception\r
102 \r
103   }\r
104 }\r