2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Ericsson
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.
19 * ============LICENSE_END=========================================================
22 package org.onap.appc.listener.demo.model;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
26 import java.io.IOException;
27 import java.net.InetAddress;
28 import java.net.UnknownHostException;
29 import org.apache.commons.io.IOUtils;
30 import org.hamcrest.CoreMatchers;
31 import org.json.JSONObject;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mockito;
36 import org.onap.appc.listener.util.Mapper;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
41 @RunWith(PowerMockRunner.class)
42 @PrepareForTest(Mapper.class)
43 public class OutgoingMessageTest {
45 private IncomingMessage incomingMessage;
48 public void setup() throws IOException {
49 String incomingStr = IOUtils.toString(getClass().getResourceAsStream("/IncomingMessagedemo.txt"), "UTF-8");
50 incomingMessage = Mapper.mapOne(incomingStr, IncomingMessage.class);
54 public void testOutgoingMessage() throws UnknownHostException {
55 InetAddress mockInetAddress = Mockito.mock(InetAddress.class);
56 Mockito.when(mockInetAddress.getCanonicalHostName()).thenReturn("TEST_CANONICAL_HOSTNAME");
57 OutgoingMessage outgoingMessage = Mockito.spy(new OutgoingMessage(incomingMessage));
58 PowerMockito.when(outgoingMessage.getLocalHost()).thenReturn(mockInetAddress);
59 outgoingMessage.updateResponseTime();
60 assertEquals("appc@TEST_CANONICAL_HOSTNAME", outgoingMessage.generateFrom());
64 public void testOutgoingMessageUnknowHost() throws UnknownHostException {
65 OutgoingMessage outgoingMessage = Mockito.spy(new OutgoingMessage(incomingMessage));
66 PowerMockito.when(outgoingMessage.getLocalHost()).thenThrow(new UnknownHostException());
67 assertEquals("appc@UnknownHost", outgoingMessage.generateFrom());
71 public void testJson() {
72 PowerMockito.mockStatic(Mapper.class);
73 JSONObject mockObject = Mockito.mock(JSONObject.class);
74 PowerMockito.when(Mapper.toJsonObject(Mockito.any())).thenReturn(mockObject);
75 OutgoingMessage outgoingMessage = Mockito.spy(new OutgoingMessage(incomingMessage));
76 assertEquals(mockObject, outgoingMessage.toResponse());
80 public void testSetResponse() {
81 OutgoingMessage outgoingMessage = new OutgoingMessage(incomingMessage);
82 outgoingMessage.setResponse(null);
83 assertEquals(new OutgoingMessage.OutStatus().getValue(), outgoingMessage.getStatus().getValue());
84 outgoingMessage.setResponse(Status.ACCEPTED);
85 assertEquals("100", outgoingMessage.getStatus().getCode());
86 outgoingMessage.setResponse(Status.FAILURE);
87 assertEquals("500", outgoingMessage.getStatus().getCode());
88 outgoingMessage.setResponse(Status.SUCCESS);
89 assertEquals("400", outgoingMessage.getStatus().getCode());