GenericProviderOperationsRequestFormatter junits
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / LCM / operation / GenericProviderOperationsRequestFormatterTest.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.operation;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNull;
28
29 import com.fasterxml.jackson.databind.JsonNode;
30 import java.net.MalformedURLException;
31 import java.net.URL;
32 import org.json.JSONObject;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.appc.exceptions.APPCException;
36 import org.onap.appc.listener.LCM.model.InputBody;
37 import org.onap.appc.listener.LCM.model.ResponseStatus;
38 import org.onap.appc.listener.util.Mapper;
39
40 public class GenericProviderOperationsRequestFormatterTest {
41
42     private static final String jsonOutputBodyStr =
43         "{\"output\":{\"common-header\":{\"timestamp\":\"2016-08-03T08:50:18.97Z\","
44             + "\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},\"sub-request-id\":\"1\","
45             + "\"request-id\":\"123\",\"originator-id\":\"1\"},\"locked\": \"test-locked\","
46             + "\"status\":{\"message\":\"test message\",\"code\":200}}}";
47
48     private static final String invalidJsonOutputBodyStr =
49         "{\"output\":{\"common-header\":{\"timestamp\":\"2016-08-03T08:50:18.97Z\","
50             + "\"api-ver\":\"1\",\"flags\":{\"force\":\"TRUE\",\"ttl\":\"9900\"},\"sub-request-id\":\"1\","
51             + "\"request-id\":\"123\",\"originator-id\":\"1\"}}}";
52
53     private GenericProviderOperationRequestFormatter requestFormatter;
54
55
56     @Before
57     public void setup() {
58         requestFormatter = new GenericProviderOperationRequestFormatter();
59     }
60
61     @Test
62     public void should_build_path() throws MalformedURLException {
63         String result = requestFormatter.buildPath(new URL("http://127.0.0.1/abc/def"), "test");
64         assertEquals("/abc/def:test", result);
65     }
66
67     @Test
68     public void should_build_request_json() {
69         InputBody inputBody = new InputBody();
70         inputBody.setPayload("\"key1\": \"value1\", \"key2\": \"value2\"");
71
72         assertEquals("{\"input\": {\"payload\":\"\\\"key1\\\": \\\"value1\\\", \\\"key2\\\": \\\"value2\\\"\"}}",
73             requestFormatter.buildRequest(inputBody));
74     }
75
76     @Test(expected = APPCException.class)
77     public void should_throw_when_invalid_json() throws APPCException {
78
79         JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(invalidJsonOutputBodyStr);
80         requestFormatter.getResponseStatus(jsonNode);
81     }
82
83     @Test
84     public void should_extract_response_status() throws APPCException {
85
86         JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonOutputBodyStr);
87         ResponseStatus status = requestFormatter.getResponseStatus(jsonNode);
88
89         assertEquals("test message", status.getValue());
90         assertEquals(Integer.valueOf(200), status.getCode());
91     }
92
93     @Test
94     public void should_return_extract_locked_field() throws APPCException {
95
96         assertNull(requestFormatter.getLocked(new JSONObject(invalidJsonOutputBodyStr)));
97         assertEquals("test-locked", requestFormatter.getLocked(new JSONObject(jsonOutputBodyStr)));
98     }
99 }