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
|
/*
* ============LICENSE_START=======================================================
* BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
* ================================================================================
* Copyright (C) 2019 NOKIA Intellectual Property. 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.bbs.event.processor.tasks;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import java.util.Optional;
import javax.net.ssl.SSLException;
import org.junit.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.onap.bbs.event.processor.config.ApplicationConfiguration;
import org.onap.bbs.event.processor.exceptions.EmptyDmaapResponseException;
import org.onap.bbs.event.processor.model.CpeAuthenticationConsumerDmaapModel;
import org.onap.bbs.event.processor.model.ImmutableCpeAuthenticationConsumerDmaapModel;
import org.onap.bbs.event.processor.utilities.CpeAuthenticationDmaapConsumerJsonParser;
import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapConsumerConfiguration;
import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.ConsumerReactiveHttpClientFactory;
import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.service.consumer.DMaaPConsumerReactiveHttpClient;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
class DmaapCpeAuthenticationConsumerTaskImplTest {
private static final String CPE_AUTHENTICATION_EVENT_TEMPLATE = "{\"event\": {"
+ "\"commonEventHeader\": { \"sourceName\":\"%s\"},"
+ "\"stateChangeFields\": {"
+ " \"oldState\": \"%s\","
+ " \"newState\": \"%s\","
+ " \"stateInterface\": \"%s\","
+ " \"additionalFields\": {"
+ " \"macAddress\": \"%s\","
+ " \"swVersion\": \"%s\""
+ "}}}}";
private static DmaapCpeAuthenticationConsumerTask dmaapConsumerTask;
private static CpeAuthenticationConsumerDmaapModel cpeAuthenticationConsumerDmaapModel;
private static DMaaPConsumerReactiveHttpClient dMaaPConsumerReactiveHttpClient;
private static String eventsArray;
private static Gson gson = new Gson();
@BeforeAll
static void setUp() throws SSLException {
final String sourceName = "PNF-CorrelationId";
final String oldAuthenticationState = "outOfService";
final String newAuthenticationState = "inService";
final String stateInterface = "stateInterface";
final String rgwMacAddress = "00:0a:95:8d:78:16";
final String swVersion = "1.2";
// Mock Re-registration configuration
DmaapConsumerConfiguration dmaapConsumerConfiguration = testVersionOfDmaapConsumerConfiguration();
ApplicationConfiguration configuration = mock(ApplicationConfiguration.class);
when(configuration.getDmaapCpeAuthenticationConsumerConfiguration()).thenReturn(dmaapConsumerConfiguration);
// Mock reactive DMaaP client
ConsumerReactiveHttpClientFactory httpClientFactory = mock(ConsumerReactiveHttpClientFactory.class);
dMaaPConsumerReactiveHttpClient = mock(DMaaPConsumerReactiveHttpClient.class);
doReturn(dMaaPConsumerReactiveHttpClient).when(httpClientFactory).create(dmaapConsumerConfiguration);
dmaapConsumerTask = new DmaapCpeAuthenticationConsumerTaskImpl(configuration,
new CpeAuthenticationDmaapConsumerJsonParser(), httpClientFactory);
cpeAuthenticationConsumerDmaapModel = ImmutableCpeAuthenticationConsumerDmaapModel.builder()
.correlationId(sourceName)
.oldAuthenticationState(oldAuthenticationState)
.newAuthenticationState(newAuthenticationState)
.stateInterface(stateInterface)
.rgwMacAddress(rgwMacAddress)
.swVersion(swVersion)
.build();
String event = String.format(CPE_AUTHENTICATION_EVENT_TEMPLATE, sourceName, oldAuthenticationState,
newAuthenticationState, stateInterface, rgwMacAddress, swVersion);
eventsArray = "[" + event + "]";
}
@AfterEach
void resetMock() {
reset(dMaaPConsumerReactiveHttpClient);
}
@Test
void passingEmptyMessage_NothingHappens() throws Exception {
JsonElement empty = gson.toJsonTree("");
when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse(Optional.empty())).thenReturn(Mono.just(empty));
StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
.expectSubscription()
.expectError(EmptyDmaapResponseException.class);
verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse(Optional.empty());
}
@Test
void passingNormalMessage_ResponseSucceeds() throws Exception {
JsonElement normalEventsArray = gson.toJsonTree(eventsArray);
when(dMaaPConsumerReactiveHttpClient.getDMaaPConsumerResponse(Optional.empty()))
.thenReturn(Mono.just(normalEventsArray));
StepVerifier.create(dmaapConsumerTask.execute("Sample input"))
.expectSubscription()
.consumeNextWith(e -> Assert.assertEquals(e, cpeAuthenticationConsumerDmaapModel));
verify(dMaaPConsumerReactiveHttpClient).getDMaaPConsumerResponse(Optional.empty());
}
private static DmaapConsumerConfiguration testVersionOfDmaapConsumerConfiguration() {
return new ImmutableDmaapConsumerConfiguration.Builder()
.consumerGroup("consumer-group")
.consumerId("consumer-id")
.dmaapContentType("application/json")
.dmaapHostName("message-router.onap.svc.cluster.local")
.dmaapPortNumber(3904)
.dmaapProtocol("http")
.dmaapUserName("admin")
.dmaapUserPassword("admin")
.trustStorePath("change it")
.trustStorePasswordPath("change_it")
.keyStorePath("change it")
.keyStorePasswordPath("change_it")
.enableDmaapCertAuth(false)
.dmaapTopicName("/events/unauthenticated.CPE_AUTHENTICATION")
.timeoutMs(-1)
.messageLimit(-1)
.build();
}
}
|