f33fc931728bc0923cb46033205f57272d5432d8
[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 org.junit.jupiter.api.Assertions;
33 import org.junit.jupiter.api.BeforeAll;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.function.Executable;
36 import org.onap.dcaegen2.collectors.datafile.config.AaiClientConfiguration;
37 import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig;
38 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
39 import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel;
40 import org.onap.dcaegen2.collectors.datafile.service.producer.AaiProducerReactiveHttpClient;
41 import org.onap.dcaegen2.collectors.datafile.tasks.AaiProducerTaskImpl;
42 import org.onap.dcaegen2.collectors.datafile.config.ImmutableAaiClientConfiguration;
43 import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel;
44
45 import reactor.core.publisher.Mono;
46 import reactor.test.StepVerifier;
47
48 /**
49  * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 5/14/18
50  */
51 class AaiProducerTaskImplTest {
52
53
54     private static final String AAI_HOST = "/aai/v11/network/pnfs/pnf/NOKQTFCOC540002E";
55     private static final Integer PORT = 1234;
56     private static final String PROTOCOL = "https";
57     private static final String USER_NAME_PASSWORD = "Datafile";
58     private static final String BASE_PATH = "/aai/v11";
59     private static final String PNF_PATH = "/network/pnfs/pnf";
60
61     private static ConsumerDmaapModel consumerDmaapModel;
62     private static AaiProducerTaskImpl aaiProducerTask;
63     private static AaiClientConfiguration aaiClientConfiguration;
64     private static AaiProducerReactiveHttpClient aaiProducerReactiveHttpClient;
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         aaiProducerTask = new AaiProducerTaskImpl(appConfig);
91         Executable executableCode = () -> aaiProducerTask.execute(null);
92
93         //then
94         Assertions
95             .assertThrows(DatafileTaskException.class, executableCode, "Passing wrong object type to execute function");
96     }
97
98     @Test
99     void whenPassedObjectFits_ReturnsCorrectStatus() throws DatafileTaskException {
100         //given/when
101         getAaiProducerTask_whenMockingResponseObject(200);
102         Mono<ConsumerDmaapModel> response = aaiProducerTask.execute(Mono.just(consumerDmaapModel));
103
104         //then
105         verify(aaiProducerReactiveHttpClient, times(1)).getAaiProducerResponse(any());
106         verifyNoMoreInteractions(aaiProducerReactiveHttpClient);
107         Assertions.assertEquals(consumerDmaapModel, response.block());
108
109     }
110
111
112     @Test
113     void whenPassedObjectFits_butIncorrectResponseReturns() throws DatafileTaskException {
114         //given/when
115         getAaiProducerTask_whenMockingResponseObject(400);
116         StepVerifier.create(aaiProducerTask.execute(Mono.just(consumerDmaapModel))).expectSubscription()
117             .expectError(DatafileTaskException.class).verify();
118         //then
119         verify(aaiProducerReactiveHttpClient, times(1)).getAaiProducerResponse(any());
120         verifyNoMoreInteractions(aaiProducerReactiveHttpClient);
121     }
122
123     private static void getAaiProducerTask_whenMockingResponseObject(Integer statusCode) {
124         //given
125         aaiProducerReactiveHttpClient = mock(AaiProducerReactiveHttpClient.class);
126         when(aaiProducerReactiveHttpClient.getAaiProducerResponse(any()))
127             .thenReturn(Mono.just(statusCode));
128         when(appConfig.getAaiClientConfiguration()).thenReturn(aaiClientConfiguration);
129         aaiProducerTask = spy(new AaiProducerTaskImpl(appConfig));
130         when(aaiProducerTask.resolveConfiguration()).thenReturn(aaiClientConfiguration);
131         doReturn(aaiProducerReactiveHttpClient).when(aaiProducerTask).resolveClient();
132     }
133 }