a10876b98822e25ba1fee14676888457b30146db
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.netconfsimulator.netconfcore.model;
22
23
24 import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import org.apache.http.HttpEntity;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.StatusLine;
36 import org.apache.http.client.HttpClient;
37 import org.apache.http.client.methods.HttpDelete;
38 import org.apache.http.client.methods.HttpPost;
39 import org.apache.http.client.methods.HttpRequestBase;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.mockito.ArgumentCaptor;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.springframework.web.multipart.MultipartFile;
46
47 class NetconfModelLoaderServiceTest {
48
49     @Mock
50     private HttpClient httpClient;
51
52     private NetconfModelLoaderService modelLoaderService;
53
54     @BeforeEach
55     void setUp() {
56         MockitoAnnotations.initMocks(this);
57         modelLoaderService = new NetconfModelLoaderService(httpClient);
58     }
59
60
61     @Test
62     void shouldSendMultipartToServer() throws IOException {
63         //given
64         String loadModelAddress = modelLoaderService.getBackendAddress();
65         makeMockClientReturnStatusOk(httpClient, HttpPost.class);
66         ArgumentCaptor<HttpPost> postArgumentCaptor = ArgumentCaptor.forClass(HttpPost.class);
67         MultipartFile yangMmodel = mock(MultipartFile.class);
68         MultipartFile initialConfig = mock(MultipartFile.class);
69         String moduleName = "moduleName";
70         when(yangMmodel.getInputStream()).thenReturn(getEmptyImputStream());
71         when(initialConfig.getInputStream()).thenReturn(getEmptyImputStream());
72
73         //when
74         LoadModelResponse response = modelLoaderService.loadYangModel(yangMmodel, initialConfig, moduleName);
75
76         //then
77         verify(httpClient).execute(postArgumentCaptor.capture());
78         HttpPost sentPost = postArgumentCaptor.getValue();
79         assertThat(response.getStatusCode()).isEqualTo(200);
80         assertThat(response.getMessage()).isEqualTo("");
81         assertThat(sentPost.getURI().toString()).isEqualTo(loadModelAddress);
82         assertThat(sentPost.getEntity().getContentType().getElements()[0].getName()).isEqualTo("multipart/form-data");
83     }
84
85     @Test
86     void shouldSendDeleteRequestToServer() throws IOException {
87         //given
88         String yangModelName = "sampleModel";
89         String deleteModelAddress = modelLoaderService.getDeleteAddress(yangModelName);
90         makeMockClientReturnStatusOk(httpClient, HttpDelete.class);
91         ArgumentCaptor<HttpDelete> deleteArgumentCaptor = ArgumentCaptor.forClass(HttpDelete.class);
92
93         //when
94         LoadModelResponse response = modelLoaderService.deleteYangModel(yangModelName);
95
96         //then
97         verify(httpClient).execute(deleteArgumentCaptor.capture());
98         HttpDelete sendDelete = deleteArgumentCaptor.getValue();
99         assertThat(response.getStatusCode()).isEqualTo(200);
100         assertThat(response.getMessage()).isEqualTo("");
101         assertThat(sendDelete.getURI().toString()).isEqualTo(deleteModelAddress);
102     }
103
104     private void makeMockClientReturnStatusOk(HttpClient client,
105             Class<? extends HttpRequestBase> httpMethodClass) throws IOException {
106         HttpResponse httpResponse = mock(HttpResponse.class);
107         StatusLine mockStatus = mock(StatusLine.class);
108         HttpEntity mockEntity = mock(HttpEntity.class);
109
110         when(client.execute(any(httpMethodClass))).thenReturn(httpResponse);
111         when(httpResponse.getStatusLine()).thenReturn(mockStatus);
112         when(mockStatus.getStatusCode()).thenReturn(200);
113         when(httpResponse.getEntity()).thenReturn(mockEntity);
114         when(mockEntity.getContent()).thenReturn(getEmptyImputStream());
115     }
116
117     private InputStream getEmptyImputStream() {
118         return new ByteArrayInputStream("".getBytes());
119     }
120
121 }