05a99b43a75df2738c2bce2dcd88fd89fb3d066b
[dcaegen2/collectors/datafile.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Datafile Collector Service
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA Intellectual Property. 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.dcaegen2.collectors.datafile.tasks;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.spy;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.verifyNoMoreInteractions;
30 import static org.mockito.Mockito.when;
31
32 import java.io.IOException;
33 import java.util.Optional;
34 import org.junit.jupiter.api.Assertions;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.function.Executable;
38 import org.onap.dcaegen2.collectors.datafile.config.AaiClientConfiguration;
39 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
40 import org.onap.dcaegen2.collectors.datafile.exceptions.AaiNotFoundException;
41 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
42 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
43 import org.onap.dcaegen2.collectors.datafile.service.AaiConsumerClient;
44 import org.onap.dcaegen2.collectors.datafile.tasks.AaiConsumerTaskImpl;
45 import org.onap.dcaegen2.collectors.datafile.config.ImmutableAaiClientConfiguration;
46 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
47
48 /**
49  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/17/18
50  */
51 class AaiConsumerTaskImplTest {
52
53     private static ConsumerDmaapModel consumerDmaapModel;
54     private static AaiConsumerTaskImpl aaiConsumerTask;
55
56     private static final String AAI_HOST = "/aai/v12/network/pnfs/pnf/NOKQTFCOC540002E";
57     private static final Integer PORT = 1234;
58     private static final String PROTOCOL = "https";
59     private static final String USER_NAME_PASSWORD = "Datafile";
60     private static final String BASE_PATH = "/aai/v12";
61     private static final String PNF_PATH = "/network/pnfs/pnf";
62
63     private static AaiClientConfiguration aaiClientConfiguration;
64     private static AaiConsumerClient aaiConsumerClient;
65     private static AppConfig appConfig;
66
67     @BeforeAll
68     static void setUp() {
69         aaiClientConfiguration = new ImmutableAaiClientConfiguration.Builder()
70             .aaiHost(AAI_HOST)
71             .aaiPort(PORT)
72             .aaiProtocol(PROTOCOL)
73             .aaiUserName(USER_NAME_PASSWORD)
74             .aaiUserPassword(USER_NAME_PASSWORD)
75             .aaiIgnoreSslCertificateErrors(true)
76             .aaiBasePath(BASE_PATH)
77             .aaiPnfPath(PNF_PATH)
78             .build();
79         consumerDmaapModel = ImmutableConsumerDmaapModel.builder().ipv4("10.16.123.234")
80             .ipv6("0:0:0:0:0:FFFF:0A10:7BEA")
81             .pnfName("NOKQTFCOC540002E").build();
82         appConfig = mock(AppConfig.class);
83
84     }
85
86     @Test
87     void whenPassedObjectDoesntFit_ThrowsDatafileTaskException() {
88         //given/when
89         when(appConfig.getAaiClientConfiguration()).thenReturn(aaiClientConfiguration);
90         aaiConsumerTask = new AaiConsumerTaskImpl(appConfig);
91         Executable executableCode = () -> aaiConsumerTask.execute(null);
92         //then
93         Assertions
94             .assertThrows(DatafileTaskException.class, executableCode, "Passing wrong object type to execute function");
95
96     }
97
98     @Test
99     void whenPassedObjectFits_ReturnsCorrectStatus() throws DatafileTaskException, IOException {
100         //given/when
101         getAaiConsumerTask_WhenMockingHttpResponseCode("200", false);
102         String response = aaiConsumerTask.execute(consumerDmaapModel);
103
104         //then
105         verify(aaiConsumerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class));
106         verifyNoMoreInteractions(aaiConsumerClient);
107         Assertions.assertEquals("200", response);
108     }
109
110     @Test
111     void whenPassedObjectFits_butIncorrectResponseReturns() throws IOException, AaiNotFoundException {
112         //given/when
113         getAaiConsumerTask_WhenMockingHttpResponseCode("400", false);
114         String response = aaiConsumerTask.execute(consumerDmaapModel);
115
116         //then
117         verify(aaiConsumerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class));
118         verifyNoMoreInteractions(aaiConsumerClient);
119         Assertions.assertEquals("400", response);
120     }
121
122     @Test
123     void whenPassedObjectFits_ThrowsIoExceptionAndHandleIt() throws IOException {
124         //given/when
125         getAaiConsumerTask_WhenMockingHttpResponseCode(null, true);
126         Executable executableCode = () -> aaiConsumerTask.execute(any(ConsumerDmaapModel.class));
127         Assertions
128             .assertThrows(DatafileTaskException.class, executableCode, "HttpClient throws IOException");
129
130         //then
131         verifyNoMoreInteractions(aaiConsumerClient);
132     }
133
134
135     private static void getAaiConsumerTask_WhenMockingHttpResponseCode(String httpResponseCode, boolean throwsException)
136         throws IOException {
137         aaiConsumerClient = mock(AaiConsumerClient.class);
138         if (throwsException) {
139             when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenThrow(IOException.class);
140         } else {
141             when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of(httpResponseCode));
142         }
143         when(appConfig.getAaiClientConfiguration()).thenReturn(aaiClientConfiguration);
144         aaiConsumerTask = spy(new AaiConsumerTaskImpl(appConfig));
145         when(aaiConsumerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration);
146         doReturn(aaiConsumerClient).when(aaiConsumerTask).resolveClient();
147     }
148
149 }