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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.collectors.datafile.tasks;
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;
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;
49 * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/17/18
51 class AaiConsumerTaskImplTest {
53 private static ConsumerDmaapModel consumerDmaapModel;
54 private static AaiConsumerTaskImpl aaiConsumerTask;
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";
63 private static AaiClientConfiguration aaiClientConfiguration;
64 private static AaiConsumerClient aaiConsumerClient;
65 private static AppConfig appConfig;
69 aaiClientConfiguration = new ImmutableAaiClientConfiguration.Builder()
72 .aaiProtocol(PROTOCOL)
73 .aaiUserName(USER_NAME_PASSWORD)
74 .aaiUserPassword(USER_NAME_PASSWORD)
75 .aaiIgnoreSslCertificateErrors(true)
76 .aaiBasePath(BASE_PATH)
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);
87 void whenPassedObjectDoesntFit_ThrowsDatafileTaskException() {
89 when(appConfig.getAaiClientConfiguration()).thenReturn(aaiClientConfiguration);
90 aaiConsumerTask = new AaiConsumerTaskImpl(appConfig);
91 Executable executableCode = () -> aaiConsumerTask.execute(null);
94 .assertThrows(DatafileTaskException.class, executableCode, "Passing wrong object type to execute function");
99 void whenPassedObjectFits_ReturnsCorrectStatus() throws DatafileTaskException, IOException {
101 getAaiConsumerTask_WhenMockingHttpResponseCode("200", false);
102 String response = aaiConsumerTask.execute(consumerDmaapModel);
105 verify(aaiConsumerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class));
106 verifyNoMoreInteractions(aaiConsumerClient);
107 Assertions.assertEquals("200", response);
111 void whenPassedObjectFits_butIncorrectResponseReturns() throws IOException, AaiNotFoundException {
113 getAaiConsumerTask_WhenMockingHttpResponseCode("400", false);
114 String response = aaiConsumerTask.execute(consumerDmaapModel);
117 verify(aaiConsumerClient, times(1)).getHttpResponse(any(ConsumerDmaapModel.class));
118 verifyNoMoreInteractions(aaiConsumerClient);
119 Assertions.assertEquals("400", response);
123 void whenPassedObjectFits_ThrowsIoExceptionAndHandleIt() throws IOException {
125 getAaiConsumerTask_WhenMockingHttpResponseCode(null, true);
126 Executable executableCode = () -> aaiConsumerTask.execute(any(ConsumerDmaapModel.class));
128 .assertThrows(DatafileTaskException.class, executableCode, "HttpClient throws IOException");
131 verifyNoMoreInteractions(aaiConsumerClient);
135 private static void getAaiConsumerTask_WhenMockingHttpResponseCode(String httpResponseCode, boolean throwsException)
137 aaiConsumerClient = mock(AaiConsumerClient.class);
138 if (throwsException) {
139 when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenThrow(IOException.class);
141 when(aaiConsumerClient.getHttpResponse(consumerDmaapModel)).thenReturn(Optional.of(httpResponseCode));
143 when(appConfig.getAaiClientConfiguration()).thenReturn(aaiClientConfiguration);
144 aaiConsumerTask = spy(new AaiConsumerTaskImpl(appConfig));
145 when(aaiConsumerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration);
146 doReturn(aaiConsumerClient).when(aaiConsumerTask).resolveClient();