Initial source code
[oam/tr069-adapter.git] / config-data / src / test / java / org / commscope / tr069adapter / config / ConfugurationDataControllerTests.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.config;\r
20 \r
21 import static org.junit.Assert.assertEquals;\r
22 import static org.junit.Assert.assertThat;\r
23 import static org.junit.jupiter.api.Assertions.fail;\r
24 \r
25 import java.util.Optional;\r
26 \r
27 import org.commscope.tr069adapter.config.repository.ConfigurationDataRepository;\r
28 import org.hamcrest.CoreMatchers;\r
29 import org.junit.Test;\r
30 import org.junit.runner.RunWith;\r
31 import org.mockito.Mockito;\r
32 import org.springframework.beans.factory.annotation.Autowired;\r
33 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;\r
34 import org.springframework.boot.test.context.SpringBootTest;\r
35 import org.springframework.boot.test.mock.mockito.MockBean;\r
36 import org.springframework.http.MediaType;\r
37 import org.springframework.mock.web.MockHttpServletResponse;\r
38 import org.springframework.mock.web.MockMultipartFile;\r
39 import org.springframework.test.context.junit4.SpringRunner;\r
40 import org.springframework.test.web.servlet.MockMvc;\r
41 import org.springframework.test.web.servlet.MvcResult;\r
42 import org.springframework.test.web.servlet.RequestBuilder;\r
43 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;\r
44 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;\r
45 \r
46 @RunWith(SpringRunner.class)\r
47 @SpringBootTest(classes = {ConfigDataServiceApplication.class}) // , args = "--schemas-dir\r
48                                                                 // test-schemas --debug true\r
49                                                                 // --starting-port 17830")\r
50 @AutoConfigureMockMvc\r
51 public class ConfugurationDataControllerTests {\r
52 \r
53   @Autowired\r
54   private MockMvc mockMvc;\r
55 \r
56   @MockBean\r
57   ConfigurationDataRepository configDataRepository;\r
58 \r
59   @Test\r
60   public void getMessageTest() {\r
61     RequestBuilder requestBuilder =\r
62         MockMvcRequestBuilders.get("/isActive").accept(MediaType.APPLICATION_JSON);\r
63 \r
64     MvcResult result = null;\r
65     String resultString = null;\r
66     try {\r
67       result = mockMvc.perform(requestBuilder).andReturn();\r
68       resultString = result.getResponse().getContentAsString();\r
69     } catch (Exception e) {\r
70       fail(e.getMessage());\r
71     }\r
72 \r
73     assertEquals("Application is running", resultString);\r
74   }\r
75 \r
76   @Test\r
77   public void uploadMultipleFilesTest() {\r
78     MockMultipartFile multiFile =\r
79         new MockMultipartFile("files", ConfigDataTestsUtils.CONFIG_FILE_NAME,\r
80             MediaType.APPLICATION_XML_VALUE, ConfigDataTestsUtils.getFileContent().getBytes());\r
81     MockHttpServletRequestBuilder requestBuilder =\r
82         MockMvcRequestBuilders.multipart("/importConfig").file(multiFile);// .contentType(MediaType.MULTIPART_FORM_DATA_VALUE);\r
83 \r
84     MvcResult result = null;\r
85     String resultString = null;\r
86     try {\r
87       result = mockMvc.perform(requestBuilder).andReturn();\r
88       MockHttpServletResponse response = result.getResponse();\r
89       resultString = response.getContentAsString();\r
90     } catch (Exception e) {\r
91       fail(e.getMessage());\r
92     }\r
93 \r
94     String expectedResult =\r
95         "File " + ConfigDataTestsUtils.CONFIG_FILE_NAME + " imported successfully";\r
96     assertEquals(expectedResult, resultString);\r
97   }\r
98 \r
99   @Test\r
100   public void viewFileContentTest() {\r
101     Mockito.when(configDataRepository.findById(ConfigDataTestsUtils.macId))\r
102         .thenReturn(Optional.of(ConfigDataTestsUtils.getConfigFileContent()));\r
103 \r
104     MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders\r
105         .get("/getFileContent/" + ConfigDataTestsUtils.macId).accept(MediaType.APPLICATION_JSON);\r
106 \r
107     MvcResult result = null;\r
108     String resultString = null;\r
109     try {\r
110       result = mockMvc.perform(requestBuilder).andReturn();\r
111       MockHttpServletResponse response = result.getResponse();\r
112       resultString = response.getContentAsString();\r
113     } catch (Exception e) {\r
114       fail(e.getMessage());\r
115     }\r
116 \r
117     String expectedResult =\r
118         "{\"macId\":\"testMacId\",\"fileContent\":\"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\\r\\n<configDataFile>\\r\\n  <fileHeader fileFormatVersion=\\\"32.594 V14.0.0\\\" vendorName=\\\"Commscope\\\"/>\\r\\n  <configData>\\r\\n    <managedElement swVersion=\\\"4.3.00.038\\\" localDn=\\\"0005B95196D0\\\" hwVersion=\\\"750742.00.04\\\" ProductClass=\\\"LTE_Enterprise_C-RANSC_Cntrl\\\" OUI=\\\"0005B9\\\"/>\\r\\n    <Device>\\r\\n      <FAP>\\r\\n        <GPS>\\r\\n          <AGPSServerConfig>\\r\\n            <Enable>1</Enable>\\r\\n            <Password>dmsuser</Password>\\r\\n            <ServerPort>7001</ServerPort>\\r\\n            <ServerURL>NONE</ServerURL>\\r\\n            <Username>dmsuser</Username>\\r\\n          </AGPSServerConfig>\\r\\n          <GPSReset>0</GPSReset>\\r\\n        </GPS>\\r\\n\\t\\t</FAP>\\r\\n    </Device>\\r\\n  </configData>\\r\\n  <fileFooter dateTime=\\\"2019-07-16T17:32:35+05:30\\\"/>\\r\\n</configDataFile>\"}";\r
119     assertEquals(expectedResult, resultString);\r
120   }\r
121 \r
122 \r
123   @Test\r
124   public void viewConfigurationDataTest() {\r
125     Mockito.when(configDataRepository.findById(ConfigDataTestsUtils.macId))\r
126         .thenReturn(Optional.of(ConfigDataTestsUtils.getConfigFileContent()));\r
127 \r
128     MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders\r
129         .get("/getConfig/" + ConfigDataTestsUtils.macId).accept(MediaType.APPLICATION_JSON);\r
130 \r
131     MvcResult result = null;\r
132     String resultString = null;\r
133     try {\r
134       result = mockMvc.perform(requestBuilder).andReturn();\r
135       MockHttpServletResponse response = result.getResponse();\r
136       resultString = response.getContentAsString();\r
137     } catch (Exception e) {\r
138       fail(e.getMessage());\r
139     }\r
140 \r
141     String expectedSubString = "\"localDn\":\"0005B95196D0\"";\r
142     assertThat(resultString, CoreMatchers.containsString(expectedSubString));\r
143   }\r
144 }\r