9d08d834ce8fc6b19969ccefa951bf143251dea5
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / LCM / impl / WorkerImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24 package org.onap.appc.listener.LCM.impl;
25
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;
31
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;
39
40 public class WorkerImplTest {
41
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\" } }}";
47
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}}}";
51
52     private EventHandler mockEventHandler = mock(EventHandler.class);
53     private ProviderOperations mockProviderOperations = mock(ProviderOperations.class);
54
55
56     @Test(expected = IllegalStateException.class)
57     public void should_throw_when_one_of_worker_fields_is_null() {
58
59         WorkerImpl worker = new WorkerImpl(null, mockEventHandler, mockProviderOperations);
60         worker.run();
61     }
62
63     @Test
64     public void should_post_error_message_to_dmaap_on_exception() throws APPCException {
65
66         when(mockProviderOperations.topologyDG(anyString(), any(JsonNode.class)))
67             .thenThrow(new RuntimeException("test exception"));
68
69         WorkerImpl worker = new WorkerImpl(buildDmaapMessage(), mockEventHandler, mockProviderOperations);
70         worker.run();
71
72         verify(mockEventHandler).postStatus(anyString(), anyString());
73     }
74
75
76     @Test
77     public void should_post_message_to_dmaap_on_successful_run() throws APPCException {
78
79         JsonNode testOutputJsonNode = Mapper.toJsonNodeFromJsonString(jsonOutputBodyStr);
80         when(mockProviderOperations.topologyDG(anyString(), any(JsonNode.class)))
81             .thenReturn(testOutputJsonNode);
82
83         WorkerImpl worker = new WorkerImpl(buildDmaapMessage(), mockEventHandler, mockProviderOperations);
84         worker.run();
85
86         verify(mockEventHandler).postStatus(anyString(), anyString());
87     }
88
89
90     private DmaapMessage buildDmaapMessage() {
91
92         DmaapMessage dmaapMessage = new DmaapMessage();
93         dmaapMessage.setRpcName("test");
94         JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonInputBodyStr);
95         dmaapMessage.setBody(jsonNode);
96         return dmaapMessage;
97     }
98 }