2060974d5bc64981807612f5789132fd821aa608
[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 =\r
70         MockMvcRequestBuilders.post("/netConfServerManagerService/createServer")\r
71             .param("deviceId", "0005B9AB1").param("enodeBName", "0005B9AB1")\r
72             .param("swVersion", "4.4.3").param("hwVersion", "*").accept(MediaType.APPLICATION_JSON);\r
73     MvcResult result = mockMvc.perform(requestBuilder).andReturn();\r
74 \r
75     MockHttpServletResponse response = result.getResponse();\r
76 \r
77     assertEquals(HttpStatus.OK.value(), response.getStatus());\r
78 \r
79   }\r
80 \r
81   @Test\r
82   public void listNetconfServers() throws Exception {\r
83 \r
84     MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders\r
85         .get("/netConfServerManagerService/listServers").accept(MediaType.APPLICATION_JSON);\r
86     MvcResult result = mockMvc.perform(requestBuilder).andReturn();\r
87     MockHttpServletResponse response = result.getResponse();\r
88 \r
89     assertEquals(HttpStatus.OK.value(), response.getStatus());\r
90 \r
91   }\r
92 \r
93   @Test\r
94   public void restartServersOnStartup() {\r
95     boolean result = false;\r
96     try {\r
97       manager.restartServers();\r
98       result = true;\r
99     } catch (Exception e) {\r
100       assertEquals(false, result); // if no exception\r
101     }\r
102     assertEquals(true, result); // if no exception\r
103 \r
104   }\r
105 }\r