2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.services.pmmapper.datarouter;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
26 import static org.mockito.Mockito.any;
27 import static org.mockito.Mockito.anyInt;
28 import static org.mockito.Mockito.doAnswer;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
35 import ch.qos.logback.classic.spi.ILoggingEvent;
36 import ch.qos.logback.core.read.ListAppender;
37 import com.google.gson.JsonObject;
38 import com.google.gson.JsonParser;
39 import io.undertow.io.Receiver;
40 import io.undertow.server.HttpServerExchange;
41 import io.undertow.util.HeaderMap;
42 import io.undertow.util.StatusCodes;
44 import java.nio.file.Files;
45 import java.nio.file.Path;
46 import java.nio.file.Paths;
47 import org.junit.jupiter.api.BeforeEach;
48 import org.junit.jupiter.api.Test;
49 import org.junit.jupiter.api.extension.ExtendWith;
50 import org.mockito.Mock;
51 import org.mockito.junit.jupiter.MockitoExtension;
52 import org.mockito.stubbing.Answer;
53 import org.onap.dcaegen2.services.pmmapper.model.Event;
54 import utils.LoggingUtils;
56 @ExtendWith(MockitoExtension.class)
57 class DeliveryHandlerTest {
59 private Path VALID_METADATA_PATH = Paths.get("src/test/resources/valid_metadata.json");
60 private Path INVALID_METADATA_PATH = Paths.get("src/test/resources/invalid_metadata.json");
63 private EventReceiver eventReceiver;
65 private DeliveryHandler objUnderTest;
69 objUnderTest = new DeliveryHandler(eventReceiver);
73 void testRequestInboundInvalidMetadata() throws Exception {
74 HttpServerExchange httpServerExchange = mock(HttpServerExchange.class, RETURNS_DEEP_STUBS);
75 JsonObject metadata = new JsonParser().parse(new String(Files
76 .readAllBytes(INVALID_METADATA_PATH))).getAsJsonObject();
77 when(httpServerExchange.getRequestHeaders().get(any(String.class)).get(anyInt()))
78 .thenReturn(metadata.toString());
79 when(httpServerExchange.setStatusCode(anyInt())).thenReturn(httpServerExchange);
80 objUnderTest.handleRequest(httpServerExchange);
81 verify(httpServerExchange, times(1)).setStatusCode(StatusCodes.BAD_REQUEST);
82 verify(httpServerExchange.getResponseSender(), times(1)).send("Malformed Metadata.");
87 void testRequestInboundNoMetadata() throws Exception {
88 HttpServerExchange httpServerExchange = mock(HttpServerExchange.class, RETURNS_DEEP_STUBS);
89 Receiver receiver = mock(Receiver.class);
90 HeaderMap headers = mock(HeaderMap.class);
91 when(httpServerExchange.getRequestReceiver()).thenReturn(receiver);
92 when(httpServerExchange.setStatusCode(anyInt())).thenReturn(httpServerExchange);
93 when(httpServerExchange.getRequestHeaders()).thenReturn(headers);
94 when(headers.get(any(String.class))).thenReturn(null);
95 objUnderTest.handleRequest(httpServerExchange);
96 verify(httpServerExchange, times(1)).setStatusCode(StatusCodes.BAD_REQUEST);
97 verify(httpServerExchange.getResponseSender(), times(1)).send("Missing Metadata.");
102 void testRequestInboundSuccess() throws Exception {
103 ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(DeliveryHandler.class);
104 HttpServerExchange httpServerExchange = mock(HttpServerExchange.class, RETURNS_DEEP_STUBS);
105 Receiver receiver = mock(Receiver.class);
106 when(httpServerExchange.getRequestReceiver()).thenReturn(receiver);
107 String testString = "MESSAGE BODY";
108 JsonObject metadata = new JsonParser().parse(
109 new String(Files.readAllBytes(VALID_METADATA_PATH))).getAsJsonObject();
110 when(httpServerExchange.getRequestHeaders().get(DeliveryHandler.METADATA_HEADER).get(anyInt()))
111 .thenReturn(metadata.toString());
112 when(httpServerExchange.getRequestHeaders().get(DeliveryHandler.PUB_ID_HEADER).getFirst()).thenReturn("");
113 doAnswer((Answer<Void>) invocationOnMock -> {
114 Receiver.FullStringCallback callback = invocationOnMock.getArgument(0);
115 callback.handle(httpServerExchange, testString);
117 }).when(receiver).receiveFullString(any());
119 doAnswer((Answer<Void>) invocationOnMock -> {
120 Runnable runnable = invocationOnMock.getArgument(0);
123 }).when(httpServerExchange).dispatch(any(Runnable.class));
125 objUnderTest.handleRequest(httpServerExchange);
126 verify(eventReceiver, times(1)).receive(any(Event.class));
128 assertEquals(logAppender.list.get(0).getMarker().getName(), "ENTRY");
129 assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("InvocationID"));
130 assertNotNull(logAppender.list.get(0).getMDCPropertyMap().get("RequestID"));
131 assertEquals(logAppender.list.get(1).getMarker().getName(), "EXIT");