2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2018 Nokia Solutions and Networks
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
24 package org.onap.appc.listener.LCM.impl;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Matchers.anyString;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import com.fasterxml.jackson.databind.JsonNode;
33 import org.junit.Test;
34 import org.onap.appc.exceptions.APPCException;
35 import org.onap.appc.listener.EventHandler;
36 import org.onap.appc.listener.LCM.model.DmaapMessage;
37 import org.onap.appc.listener.LCM.operation.ProviderOperations;
38 import org.onap.appc.listener.util.Mapper;
40 public class WorkerImplTest {
42 private static final String jsonInputBodyStr =
43 "{\"input\":{ \"common-header\": { \"timestamp\": \"2016-08-03T08:50:18.97Z\", "
44 + "\"api-ver\": \"1\", \"originator-id\": \"1\", \"request-id\": \"123\", \"sub-request-id\": \"1\", "
45 + "\"flags\": { \"force\":\"TRUE\", \"ttl\":\"9900\" } }, \"action\": \"Stop\", "
46 + "\"action-identifiers\": { \"vnf-id\": \"TEST\" } }}";
48 private static final String jsonOutputBodyStr = "{\"output\":{\"common-header\":{\"timestamp\":\"2016-08-03T08:50:18.97Z\","
49 + "\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},\"sub-request-id\":\"1\","
50 + "\"request-id\":\"123\",\"originator-id\":\"1\"},\"status\":{\"value\":\"TestException\",\"code\":200}}}";
52 private EventHandler mockEventHandler = mock(EventHandler.class);
53 private ProviderOperations mockProviderOperations = mock(ProviderOperations.class);
56 @Test(expected = IllegalStateException.class)
57 public void should_throw_when_one_of_worker_fields_is_null() {
59 WorkerImpl worker = new WorkerImpl(null, mockEventHandler, mockProviderOperations);
64 public void should_post_error_message_to_dmaap_on_exception() throws APPCException {
66 when(mockProviderOperations.topologyDG(anyString(), any(JsonNode.class)))
67 .thenThrow(new RuntimeException("test exception"));
69 WorkerImpl worker = new WorkerImpl(buildDmaapMessage(), mockEventHandler, mockProviderOperations);
72 verify(mockEventHandler).postStatus(anyString(), anyString());
77 public void should_post_message_to_dmaap_on_successful_run() throws APPCException {
79 JsonNode testOutputJsonNode = Mapper.toJsonNodeFromJsonString(jsonOutputBodyStr);
80 when(mockProviderOperations.topologyDG(anyString(), any(JsonNode.class)))
81 .thenReturn(testOutputJsonNode);
83 WorkerImpl worker = new WorkerImpl(buildDmaapMessage(), mockEventHandler, mockProviderOperations);
86 verify(mockEventHandler).postStatus(anyString(), anyString());
90 private DmaapMessage buildDmaapMessage() {
92 DmaapMessage dmaapMessage = new DmaapMessage();
93 dmaapMessage.setRpcName("test");
94 JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonInputBodyStr);
95 dmaapMessage.setBody(jsonNode);