31c6d20772d3b3ccd54f9e98bf6ed4088e9694b1
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
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
21 package org.onap.policy.controlloop.actorserviceprovider;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertSame;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.UUID;
28 import java.util.concurrent.CompletableFuture;
29 import java.util.concurrent.atomic.AtomicReference;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.controlloop.VirtualControlLoopEvent;
33 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
35 import org.onap.policy.controlloop.policy.PolicyResult;
36
37 public class AsyncResponseHandlerTest {
38
39     private static final String ACTOR = "my-actor";
40     private static final String OPERATION = "my-operation";
41     private static final UUID REQ_ID = UUID.randomUUID();
42     private static final String TEXT = "some text";
43
44     private VirtualControlLoopEvent event;
45     private ControlLoopEventContext context;
46     private ControlLoopOperationParams params;
47     private OperationOutcome outcome;
48     private MyHandler handler;
49
50     /**
51      * Initializes all fields, including {@link #handler}.
52      */
53     @Before
54     public void setUp() {
55         event = new VirtualControlLoopEvent();
56         event.setRequestId(REQ_ID);
57
58         context = new ControlLoopEventContext(event);
59         params = ControlLoopOperationParams.builder().actor(ACTOR).operation(OPERATION).context(context).build();
60         outcome = params.makeOutcome();
61
62         handler = new MyHandler(params, outcome);
63     }
64
65     @Test
66     public void testAsyncResponseHandler_testGetParams_testGetOutcome() {
67         assertSame(params, handler.getParams());
68         assertSame(outcome, handler.getOutcome());
69     }
70
71     @Test
72     public void testHandle() {
73         CompletableFuture<String> future = new CompletableFuture<>();
74         handler.handle(future).complete(outcome);
75
76         assertTrue(future.isCancelled());
77     }
78
79     @Test
80     public void testCompleted() throws Exception {
81         CompletableFuture<OperationOutcome> result = handler.handle(new CompletableFuture<>());
82         handler.completed(TEXT);
83         assertTrue(result.isDone());
84         assertSame(outcome, result.get());
85         assertEquals(PolicyResult.FAILURE_RETRIES, outcome.getResult());
86         assertEquals(TEXT, outcome.getMessage());
87     }
88
89     /**
90      * Tests completed() when doCompleted() throws an exception.
91      */
92     @Test
93     public void testCompletedException() throws Exception {
94         IllegalStateException except = new IllegalStateException();
95
96         outcome = params.makeOutcome();
97         handler = new MyHandler(params, outcome) {
98             @Override
99             protected OperationOutcome doComplete(String rawResponse) {
100                 throw except;
101             }
102         };
103
104         CompletableFuture<OperationOutcome> result = handler.handle(new CompletableFuture<>());
105         handler.completed(TEXT);
106         assertTrue(result.isCompletedExceptionally());
107
108         AtomicReference<Throwable> thrown = new AtomicReference<>();
109         result.whenComplete((unused, thrown2) -> thrown.set(thrown2));
110
111         assertSame(except, thrown.get());
112     }
113
114     @Test
115     public void testFailed() throws Exception {
116         IllegalStateException except = new IllegalStateException();
117
118         CompletableFuture<OperationOutcome> result = handler.handle(new CompletableFuture<>());
119         handler.failed(except);
120
121         assertTrue(result.isDone());
122         assertSame(outcome, result.get());
123         assertEquals(PolicyResult.FAILURE_GUARD, outcome.getResult());
124     }
125
126     /**
127      * Tests failed() when doFailed() throws an exception.
128      */
129     @Test
130     public void testFailedException() throws Exception {
131         IllegalStateException except = new IllegalStateException();
132
133         outcome = params.makeOutcome();
134         handler = new MyHandler(params, outcome) {
135             @Override
136             protected OperationOutcome doFailed(Throwable thrown) {
137                 throw except;
138             }
139         };
140
141         CompletableFuture<OperationOutcome> result = handler.handle(new CompletableFuture<>());
142         handler.failed(except);
143         assertTrue(result.isCompletedExceptionally());
144
145         AtomicReference<Throwable> thrown = new AtomicReference<>();
146         result.whenComplete((unused, thrown2) -> thrown.set(thrown2));
147
148         assertSame(except, thrown.get());
149     }
150
151     private class MyHandler extends AsyncResponseHandler<String> {
152
153         public MyHandler(ControlLoopOperationParams params, OperationOutcome outcome) {
154             super(params, outcome);
155         }
156
157         @Override
158         protected OperationOutcome doComplete(String rawResponse) {
159             OperationOutcome outcome = getOutcome();
160             outcome.setResult(PolicyResult.FAILURE_RETRIES);
161             outcome.setMessage(rawResponse);
162             return outcome;
163         }
164
165         @Override
166         protected OperationOutcome doFailed(Throwable thrown) {
167             OperationOutcome outcome = getOutcome();
168             outcome.setResult(PolicyResult.FAILURE_GUARD);
169             return outcome;
170         }
171     }
172 }