38634d03461aec8c2d8e9bb0d2dbd7d0fb01ecba
[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 import org.commscope.tr069adapter.config.repository.ConfigurationDataRepository;\r
25 import org.hamcrest.CoreMatchers;\r
26 import org.junit.Test;\r
27 import org.junit.runner.RunWith;\r
28 import org.mockito.Mockito;\r
29 import org.springframework.beans.factory.annotation.Autowired;\r
30 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;\r
31 import org.springframework.boot.test.context.SpringBootTest;\r
32 import org.springframework.boot.test.mock.mockito.MockBean;\r
33 import org.springframework.http.HttpStatus;\r
34 import org.springframework.http.MediaType;\r
35 import org.springframework.mock.web.MockHttpServletResponse;\r
36 import org.springframework.mock.web.MockMultipartFile;\r
37 import org.springframework.test.context.junit4.SpringRunner;\r
38 import org.springframework.test.web.servlet.MockMvc;\r
39 import org.springframework.test.web.servlet.MvcResult;\r
40 import org.springframework.test.web.servlet.RequestBuilder;\r
41 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;\r
42 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;\r
43 \r
44 @RunWith(SpringRunner.class)\r
45 @SpringBootTest(classes = {ConfigDataServiceApplication.class}) // , args = "--schemas-dir\r
46                                                                 // test-schemas --debug true\r
47                                                                 // --starting-port 17830")\r
48 @AutoConfigureMockMvc\r
49 public class ConfugurationDataControllerTests {\r
50 \r
51   @Autowired\r
52   private MockMvc mockMvc;\r
53 \r
54   @MockBean\r
55   ConfigurationDataRepository configDataRepository;\r
56 \r
57   @Test\r
58   public void getMessageTest() {\r
59     RequestBuilder requestBuilder =\r
60         MockMvcRequestBuilders.get("/isActive").accept(MediaType.APPLICATION_JSON);\r
61 \r
62     MvcResult result = null;\r
63     String resultString = null;\r
64     try {\r
65       result = mockMvc.perform(requestBuilder).andReturn();\r
66       resultString = result.getResponse().getContentAsString();\r
67     } catch (Exception e) {\r
68       fail(e.getMessage());\r
69     }\r
70 \r
71     assertEquals("Application is running", resultString);\r
72   }\r
73 \r
74   @Test\r
75   public void uploadMultipleFilesTest() {\r
76     MockMultipartFile multiFile =\r
77         new MockMultipartFile("files", ConfigDataTestsUtils.CONFIG_FILE_NAME,\r
78             MediaType.APPLICATION_XML_VALUE, ConfigDataTestsUtils.getFileContent().getBytes());\r
79     MockHttpServletRequestBuilder requestBuilder =\r
80         MockMvcRequestBuilders.multipart("/importConfig").file(multiFile);// .contentType(MediaType.MULTIPART_FORM_DATA_VALUE);\r
81 \r
82     MvcResult result = null;\r
83     String resultString = null;\r
84     try {\r
85       result = mockMvc.perform(requestBuilder).andReturn();\r
86       MockHttpServletResponse response = result.getResponse();\r
87       resultString = response.getContentAsString();\r
88     } catch (Exception e) {\r
89       fail(e.getMessage());\r
90     }\r
91 \r
92     String expectedResult =\r
93         "File " + ConfigDataTestsUtils.CONFIG_FILE_NAME + " imported successfully";\r
94     assertEquals(expectedResult, resultString);\r
95   }\r
96 \r
97   @Test\r
98   public void viewConfigurationDataTest() {\r
99     Mockito.when(configDataRepository.findByMacId(ConfigDataTestsUtils.macId))\r
100         .thenReturn(ConfigDataTestsUtils.getConfigFileContent());\r
101 \r
102     MockHttpServletRequestBuilder requestBuilder =\r
103         MockMvcRequestBuilders.post("/getConfig").param("macId", "0005B95196D0")\r
104             .param("swVersion", "4.5").param("hwVersion", "1.1").accept(MediaType.APPLICATION_JSON);\r
105 \r
106     MvcResult result = null;\r
107     try {\r
108       result = mockMvc.perform(requestBuilder).andReturn();\r
109       MockHttpServletResponse response = result.getResponse();\r
110       assertEquals(200, response.getStatus());\r
111     } catch (Exception e) {\r
112       fail(e.getMessage());\r
113     }\r
114   }\r
115 }\r