dc5e2553cab068a96e73a431feceef5a5ec1c0da
[appc.git] / appc-config / appc-flow-controller / provider / src / test / java / org / onap / appc / flow / controller / ResponseHandlerImpl / DefaultResponseHandlerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia.
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.appc.flow.controller.ResponseHandlerImpl;
21
22 import static org.junit.Assert.assertEquals;
23
24 import com.google.common.collect.Lists;
25 import java.util.Collections;
26 import org.junit.Test;
27 import org.onap.appc.flow.controller.data.Response;
28 import org.onap.appc.flow.controller.data.ResponseAction;
29 import org.onap.appc.flow.controller.data.Transaction;
30
31 public class DefaultResponseHandlerTest {
32
33     @Test
34     public void handlerResponse_shouldReturnEmptyResponseAction_whenTransactionResponsesAreNull() {
35         Transaction transaction = new Transaction();
36         assertExpectedResponseAction(transaction, new ResponseAction());
37     }
38
39     @Test
40     public void handlerResponse_shouldReturnEmptyResponseAction_whenTransactionResponsesAreEmpty() {
41         Transaction transaction = new Transaction();
42         transaction.setResponses(Collections.emptyList());
43         assertExpectedResponseAction(transaction, new ResponseAction());
44     }
45
46     @Test
47     public void handlerResponse_shouldReturnExpectedResponseAction_forMatchingStatusCode() {
48         // GIVEN
49         ResponseAction expectedResponseAction = createExpectedResponseAction();
50         String responseCode = "404";
51
52         Transaction transaction = new Transaction();
53         transaction.setStatusCode(responseCode);
54         transaction.setResponses(Lists.newArrayList(
55             createResponse(null, null),
56             createResponse(null, "500"),
57             createResponse(expectedResponseAction, responseCode)));
58
59         assertExpectedResponseAction(transaction, expectedResponseAction);
60     }
61
62     private ResponseAction createExpectedResponseAction() {
63         ResponseAction expectedResponseAction = new ResponseAction();
64         expectedResponseAction.setIntermediateMessage(true);
65         expectedResponseAction.setJump("value1");
66         expectedResponseAction.setRetry("value2");
67         expectedResponseAction.setWait("value3");
68         expectedResponseAction.setIgnore(true);
69         expectedResponseAction.setStop(true);
70         return expectedResponseAction;
71     }
72
73     private Response createResponse(ResponseAction expectedResponseAction, String responseCode) {
74         Response response = new Response();
75         response.setResponseCode(responseCode);
76         response.setResponseAction(expectedResponseAction);
77         return response;
78     }
79
80     private void assertExpectedResponseAction(Transaction transaction, ResponseAction expectedResponseAction) {
81         // WHEN
82         ResponseAction responseAction = new DefaultResponseHandler().handlerResponse(transaction);
83
84         // THEN
85         assertEquals(expectedResponseAction.isIntermediateMessage(), responseAction.isIntermediateMessage());
86         assertEquals(expectedResponseAction.getJump(), responseAction.getJump());
87         assertEquals(expectedResponseAction.getRetry(), responseAction.getRetry());
88         assertEquals(expectedResponseAction.getWait(), responseAction.getWait());
89         assertEquals(expectedResponseAction.isIgnore(), responseAction.isIgnore());
90         assertEquals(expectedResponseAction.isStop(), responseAction.isStop());
91         assertEquals(expectedResponseAction.toString(), responseAction.toString());
92     }
93 }