Initial source code
[oam/tr069-adapter.git] / acs / application-booter / src / test / java / org / commscope / tr069adapter / acs / cpe / test / inform / BootstrapInformTest.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.acs.cpe.test.inform;\r
20 \r
21 import static org.junit.Assert.assertEquals;\r
22 import static org.junit.Assert.fail;\r
23 \r
24 import javax.servlet.http.Cookie;\r
25 \r
26 import org.apache.activemq.broker.BrokerService;\r
27 import org.apache.commons.httpclient.HttpStatus;\r
28 import org.commscope.tr069adapter.acs.booter.ACSServiceBooter;\r
29 import org.commscope.tr069adapter.acs.common.DeviceInform;\r
30 import org.commscope.tr069adapter.acs.common.utils.AcsConstants;\r
31 import org.commscope.tr069adapter.acs.cpe.handler.DeviceValidator;\r
32 import org.commscope.tr069adapter.acs.cpe.rpc.Inform;\r
33 import org.commscope.tr069adapter.acs.requestprocessor.dao.DeviceRPCRequestRepository;\r
34 import org.commscope.tr069adapter.acs.requestprocessor.impl.TR069EventNotificationService;\r
35 import org.junit.After;\r
36 import org.junit.Test;\r
37 import org.junit.runner.RunWith;\r
38 import org.mockito.Mockito;\r
39 import org.springframework.beans.factory.annotation.Autowired;\r
40 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;\r
41 import org.springframework.boot.test.context.SpringBootTest;\r
42 import org.springframework.boot.test.mock.mockito.MockBean;\r
43 import org.springframework.http.MediaType;\r
44 import org.springframework.mock.web.MockHttpServletResponse;\r
45 import org.springframework.test.context.ContextConfiguration;\r
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;\r
47 import org.springframework.test.web.servlet.MockMvc;\r
48 import org.springframework.test.web.servlet.MvcResult;\r
49 import org.springframework.test.web.servlet.RequestBuilder;\r
50 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;\r
51 \r
52 @SpringBootTest(classes = {ACSServiceBooter.class})\r
53 @RunWith(SpringJUnit4ClassRunner.class)\r
54 @AutoConfigureMockMvc\r
55 @ContextConfiguration\r
56 public class BootstrapInformTest {\r
57 \r
58   @Autowired\r
59   private MockMvc mockMvc;\r
60 \r
61   @MockBean\r
62   private DeviceValidator deviceValidator;\r
63 \r
64   @MockBean\r
65   protected DeviceRPCRequestRepository deviceRPCRequestRepository;\r
66 \r
67   @MockBean\r
68   private TR069EventNotificationService tr069EventNotificationService;\r
69 \r
70   @Autowired\r
71   BrokerService broker;\r
72 \r
73   @Test\r
74   public void processInformPnPTest() throws Exception {\r
75 \r
76     String exampleInform =\r
77         "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:Envelope xmlns:ns2=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns4=\"urn:dslforum-org:cwmp-1-0\"><ns2:Header><ns4:ID ns2:mustUnderstand=\"1\">1</ns4:ID></ns2:Header><ns2:Body><ns4:Inform><ns4:DeviceId><Manufacturer></Manufacturer><OUI>0005B9</OUI><ProductClass>LTE_Enterprise_C-RANSC_Cntrl</ProductClass><SerialNumber>0005B9423910</SerialNumber></ns4:DeviceId><Event ns1:arrayType=\"EventStruct[1]\"><EventStruct><EventCode>0 BOOTSTRAP</EventCode><CommandKey></CommandKey></EventStruct></Event><MaxEnvelopes>1</MaxEnvelopes><CurrentTime>2020-06-10T11:27:26.054Z</CurrentTime><RetryCount>0</RetryCount><ParameterList ns1:arrayType=\"ParameterValueStruct[7]\"><ParameterValueStruct><Name>Device.DeviceInfo.HardwareVersion</Name><Value>750742.00.13</Value></ParameterValueStruct><ParameterValueStruct><Name>Device.DeviceInfo.SoftwareVersion</Name><Value>4.3.00.229</Value></ParameterValueStruct><ParameterValueStruct><Name>Device.DeviceInfo.ProvisioningCode</Name><Value></Value></ParameterValueStruct><ParameterValueStruct><Name>Device.ManagementServer.ConnectionRequestURL</Name><Value>http://172.20.0.9:30150/ConnectionRequest?command=cr&amp;sn=0005B9423910</Value></ParameterValueStruct><ParameterValueStruct><Name>Device.ManagementServer.ParameterKey</Name><Value>zzzz</Value></ParameterValueStruct><ParameterValueStruct><Name>Device.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.ExternalIPAddress</Name><Value>172.17.19.193</Value></ParameterValueStruct><ParameterValueStruct><Name>Device.Services.FAPService.1.FAPControl.LTE.AdminState</Name><Value>0</Value></ParameterValueStruct></ParameterList></ns4:Inform></ns2:Body></ns2:Envelope>";\r
78     try {\r
79 \r
80       Mockito.doNothing().when(tr069EventNotificationService)\r
81           .sendDeviceInformToNBI(Mockito.any(DeviceInform.class));\r
82 \r
83       Mockito\r
84           .when(deviceValidator.isDeviceAuthorized(Mockito.any(Inform.class), Mockito.anyString()))\r
85           .thenReturn(new Boolean(true));\r
86 \r
87       Mockito.when(deviceValidator.validateDevice(Mockito.anyString(), Mockito.anyString(),\r
88           Mockito.anyString())).thenReturn(new Boolean(true));\r
89 \r
90       Mockito.when(deviceRPCRequestRepository.findByDeviceIdAndIsProcessed(Mockito.anyString(),\r
91           Mockito.anyInt())).thenReturn(null);\r
92 \r
93       RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/CPEMgmt/acs")\r
94           .accept(MediaType.TEXT_PLAIN).content(exampleInform).header("Authorization", "basic")\r
95           .contentType(MediaType.TEXT_PLAIN);\r
96 \r
97       MvcResult result = mockMvc.perform(requestBuilder).andReturn();\r
98 \r
99       MockHttpServletResponse response = result.getResponse();\r
100       assertEquals(HttpStatus.SC_OK, response.getStatus());\r
101 \r
102       Cookie cookie = response.getCookie(AcsConstants.ACS_SESSIONID);\r
103       requestBuilder =\r
104           MockMvcRequestBuilders.post("/CPEMgmt/acs").accept(MediaType.TEXT_PLAIN).content("")\r
105               .header("Authorization", "basic").cookie(cookie).contentType(MediaType.TEXT_PLAIN);\r
106 \r
107       result = mockMvc.perform(requestBuilder).andReturn();\r
108 \r
109       response = result.getResponse();\r
110       assertEquals(HttpStatus.SC_NO_CONTENT, response.getStatus());\r
111     } catch (Exception e) {\r
112       fail(e.getMessage());\r
113     }\r
114   }\r
115 \r
116   @After\r
117   public void stopBroker() throws Exception {\r
118     try {\r
119       System.out.println("Tearing down the broker");\r
120       broker.stop();\r
121       broker.waitUntilStopped();\r
122       broker = null;\r
123     } catch (Exception e) {\r
124       e.printStackTrace();\r
125     }\r
126   }\r
127 \r
128 }\r