aboutsummaryrefslogtreecommitdiffstats
path: root/netconfsimulator/src/test/java/org/onap/netconfsimulator/netconfcore/NetconfControllerTest.java
blob: 73fb627ea27cba625bcb2e2bdcca7421474d4770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * ============LICENSE_START=======================================================
 * PNF-REGISTRATION-HANDLER
 * ================================================================================
 * Copyright (C) 2018 Nokia. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *                        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.netconfsimulator.netconfcore;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.tailf.jnc.JNCException;
import java.io.IOException;
import java.nio.file.Files;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.onap.netconfsimulator.netconfcore.configuration.NetconfConfigurationService;
import org.onap.netconfsimulator.netconfcore.model.LoadModelResponse;
import org.onap.netconfsimulator.netconfcore.model.NetconfModelLoaderService;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

class NetconfControllerTest {

    private MockMvc mockMvc;

    @Mock
    private NetconfConfigurationService netconfService;

    @Mock
    private NetconfModelLoaderService netconfModelLoaderService;

    @InjectMocks
    private NetconfController controller;

    private static final String SAMPLE_CONFIGURATION = "<config xmlns=\"http://onap.org/pnf-simulator\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><itemValue1>11</itemValue1><itemValue2>22</itemValue2></config>";

    @BeforeEach
    void setUp() {
        initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
    }

    @Test
    void testShouldDigestMultipartFile() throws Exception {
        byte[] bytes =
            Files.readAllBytes(ResourceUtils.getFile("classpath:updatedConfig.xml").toPath());
        MockMultipartFile file = new MockMultipartFile("editConfigXml", bytes);

        mockMvc
            .perform(MockMvcRequestBuilders.multipart("/netconf/edit-config").file(file))
            .andExpect(status().isAccepted());

        verify(netconfService).editCurrentConfiguration(any(MultipartFile.class));
    }

    @Test
    void testShouldThrowExceptionWhenEditConfigFileWithIncorrectNameProvided() throws Exception {
        MockMultipartFile file = new MockMultipartFile("wrongName", new byte[0]);

        mockMvc
            .perform(MockMvcRequestBuilders.multipart("/netconf/edit-config").file(file))
            .andExpect(status().isBadRequest());

        verify(netconfService, never()).editCurrentConfiguration(any(MultipartFile.class));
    }

    @Test
    void testShouldReturnCurrentConfiguration() throws Exception {
        when(netconfService.getCurrentConfiguration()).thenReturn(SAMPLE_CONFIGURATION);

        String contentAsString =
            mockMvc
                .perform(get("/netconf/get"))
                .andExpect(status().isOk())
                .andReturn()
                .getResponse()
                .getContentAsString();

        verify(netconfService).getCurrentConfiguration();
        assertThat(contentAsString).isEqualTo(SAMPLE_CONFIGURATION);
    }

    @Test
    void testShouldReturnConfigurationForGivenPath() throws Exception {
        when(netconfService.getCurrentConfiguration("sampleModel", "sampleContainer"))
            .thenReturn(SAMPLE_CONFIGURATION);

        String contentAsString =
            mockMvc
                .perform(get("/netconf/get/sampleModel/sampleContainer"))
                .andExpect(status().isOk())
                .andReturn()
                .getResponse()
                .getContentAsString();

        verify(netconfService).getCurrentConfiguration("sampleModel", "sampleContainer");
        assertThat(contentAsString).isEqualTo(SAMPLE_CONFIGURATION);
    }

    @Test
    void testShouldRaiseBadRequestWhenConfigurationIsNotPresent() throws Exception {
        when(netconfService.getCurrentConfiguration("sampleModel", "sampleContainer2"))
            .thenThrow(new JNCException(JNCException.ELEMENT_MISSING, "/sampleModel:sampleContainer2"));

        String contentAsString =
            mockMvc
            .perform(get("/netconf/get/sampleModel/sampleContainer2"))
            .andExpect(status().isBadRequest())
            .andReturn()
            .getResponse()
            .getContentAsString();

        assertThat(contentAsString).isEqualTo("Element does not exists: /sampleModel:sampleContainer2");
    }

    @Test
    void shouldThrowExceptionWhenNoConfigurationPresent() throws IOException, JNCException {
        when(netconfService.getCurrentConfiguration()).thenThrow(JNCException.class);

        assertThatThrownBy(() -> mockMvc.perform(get("/netconf/get")))
            .hasRootCauseExactlyInstanceOf(JNCException.class);
    }

    @Test
    void testShouldDeleteYangModel() throws Exception {
        String responseOkString = "Alles klar";
        String yangModelName = "someModel";
        LoadModelResponse loadModelResponse = new LoadModelResponse(200, responseOkString);
        String uri = String.format("/netconf/model/%s", yangModelName);
        when(netconfModelLoaderService.deleteYangModel(yangModelName)).thenReturn(loadModelResponse);

        String contentAsString =
            mockMvc
                .perform(delete(uri))
                .andExpect(status().isOk())
                .andReturn()
                .getResponse()
                .getContentAsString();

        verify(netconfModelLoaderService).deleteYangModel(yangModelName);
        assertThat(contentAsString).isEqualTo(responseOkString);
    }
}