Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / config-data / src / test / java / org / commscope / tr069adapter / config / ConfugurationDataControllerTests.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : tr-069-adapter
4  * =================================================================================================
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.
6  * =================================================================================================
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9  * may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ===============LICENSE_END=======================================================================
17  */
18
19 package org.commscope.tr069adapter.config;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.jupiter.api.Assertions.fail;
23
24 import org.commscope.tr069adapter.config.repository.ConfigurationDataRepository;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mockito;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
30 import org.springframework.boot.test.context.SpringBootTest;
31 import org.springframework.boot.test.mock.mockito.MockBean;
32 import org.springframework.http.MediaType;
33 import org.springframework.mock.web.MockHttpServletResponse;
34 import org.springframework.mock.web.MockMultipartFile;
35 import org.springframework.test.context.junit4.SpringRunner;
36 import org.springframework.test.web.servlet.MockMvc;
37 import org.springframework.test.web.servlet.MvcResult;
38 import org.springframework.test.web.servlet.RequestBuilder;
39 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
40 import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
41
42 @RunWith(SpringRunner.class)
43 @SpringBootTest(classes = {ConfigDataServiceApplication.class}) // , args = "--schemas-dir
44                                                                 // test-schemas --debug true
45                                                                 // --starting-port 17830")
46 @AutoConfigureMockMvc
47 public class ConfugurationDataControllerTests {
48
49   @Autowired
50   private MockMvc mockMvc;
51
52   @MockBean
53   ConfigurationDataRepository configDataRepository;
54
55   @Test
56   public void getMessageTest() {
57     RequestBuilder requestBuilder =
58         MockMvcRequestBuilders.get("/isActive").accept(MediaType.APPLICATION_JSON);
59
60     MvcResult result = null;
61     String resultString = null;
62     try {
63       result = mockMvc.perform(requestBuilder).andReturn();
64       resultString = result.getResponse().getContentAsString();
65     } catch (Exception e) {
66       fail(e.getMessage());
67     }
68
69     assertEquals("Application is running", resultString);
70   }
71
72   @Test
73   public void uploadMultipleFilesTest() {
74     MockMultipartFile multiFile =
75         new MockMultipartFile("files", ConfigDataTestsUtils.CONFIG_FILE_NAME,
76             MediaType.APPLICATION_XML_VALUE, ConfigDataTestsUtils.getFileContent().getBytes());
77     MockHttpServletRequestBuilder requestBuilder =
78         MockMvcRequestBuilders.multipart("/importConfig").file(multiFile);// .contentType(MediaType.MULTIPART_FORM_DATA_VALUE);
79
80     MvcResult result = null;
81     String resultString = null;
82     try {
83       result = mockMvc.perform(requestBuilder).andReturn();
84       MockHttpServletResponse response = result.getResponse();
85       resultString = response.getContentAsString();
86     } catch (Exception e) {
87       fail(e.getMessage());
88     }
89
90     String expectedResult =
91         "File " + ConfigDataTestsUtils.CONFIG_FILE_NAME + " imported successfully";
92     assertEquals(expectedResult, resultString);
93   }
94
95   @Test
96   public void viewConfigurationDataTest() {
97     Mockito.when(configDataRepository.findByMacId(ConfigDataTestsUtils.macId))
98         .thenReturn(ConfigDataTestsUtils.getConfigFileContent());
99
100     MockHttpServletRequestBuilder requestBuilder =
101         MockMvcRequestBuilders.post("/getConfig").param("macId", "0005B95196D0")
102             .param("swVersion", "4.5").param("hwVersion", "1.1").accept(MediaType.APPLICATION_JSON);
103
104     MvcResult result = null;
105     try {
106       result = mockMvc.perform(requestBuilder).andReturn();
107       MockHttpServletResponse response = result.getResponse();
108       assertEquals(200, response.getStatus());
109     } catch (Exception e) {
110       fail(e.getMessage());
111     }
112   }
113 }