Include response in OperationOutcome
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / test / java / org / onap / policy / controlloop / actorserviceprovider / OperationOutcomeTest.java
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.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28
29 import java.time.Instant;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.controlloop.ControlLoopOperation;
33 import org.onap.policy.controlloop.ControlLoopResponse;
34 import org.onap.policy.controlloop.policy.PolicyResult;
35
36 public class OperationOutcomeTest {
37     private static final String ACTOR = "my-actor";
38     private static final String OPERATION = "my-operation";
39     private static final String TARGET = "my-target";
40     private static final Instant START = Instant.ofEpochMilli(10);
41     private static final Instant END = Instant.ofEpochMilli(20);
42     private static final String SUB_REQ_ID = "my-sub-request-id";
43     private static final PolicyResult RESULT = PolicyResult.FAILURE_GUARD;
44     private static final String MESSAGE = "my-message";
45     private static final String RESPONSE = "my-response";
46
47     private ControlLoopResponse response;
48     private OperationOutcome outcome;
49
50     /**
51      * Sets up.
52      */
53     @Before
54     public void setUp() {
55         response = new ControlLoopResponse();
56
57         outcome = new OperationOutcome();
58     }
59
60     @Test
61     public void testOperationOutcomeOperationOutcome() {
62         setAll();
63
64         OperationOutcome outcome2 = new OperationOutcome(outcome);
65
66         assertEquals(ACTOR, outcome2.getActor());
67         assertEquals(OPERATION, outcome2.getOperation());
68         assertEquals(TARGET, outcome2.getTarget());
69         assertEquals(START, outcome2.getStart());
70         assertEquals(END, outcome2.getEnd());
71         assertEquals(SUB_REQ_ID, outcome2.getSubRequestId());
72         assertEquals(RESULT, outcome2.getResult());
73         assertEquals(MESSAGE, outcome2.getMessage());
74         assertSame(RESPONSE, outcome2.getResponse());
75         assertSame(response, outcome2.getControlLoopResponse());
76     }
77
78     @Test
79     public void testToControlLoopOperation() {
80         setAll();
81
82         ControlLoopOperation outcome2 = outcome.toControlLoopOperation();
83
84         assertEquals(ACTOR, outcome2.getActor());
85         assertEquals(OPERATION, outcome2.getOperation());
86         assertEquals(TARGET, outcome2.getTarget());
87         assertEquals(START, outcome2.getStart());
88         assertEquals(END, outcome2.getEnd());
89         assertEquals(SUB_REQ_ID, outcome2.getSubRequestId());
90         assertEquals(RESULT.toString(), outcome2.getOutcome());
91         assertEquals(MESSAGE, outcome2.getMessage());
92     }
93
94     /**
95      * Tests both isFor() methods, as one invokes the other.
96      */
97     @Test
98     public void testIsFor() {
99         setAll();
100
101         // null case
102         assertFalse(OperationOutcome.isFor(null, ACTOR, OPERATION));
103
104         // actor mismatch
105         assertFalse(OperationOutcome.isFor(outcome, TARGET, OPERATION));
106
107         // operation mismatch
108         assertFalse(OperationOutcome.isFor(outcome, ACTOR, TARGET));
109
110         // null actor in outcome
111         outcome.setActor(null);
112         assertFalse(OperationOutcome.isFor(outcome, ACTOR, OPERATION));
113         outcome.setActor(ACTOR);
114
115         // null operation in outcome
116         outcome.setOperation(null);
117         assertFalse(OperationOutcome.isFor(outcome, ACTOR, OPERATION));
118         outcome.setOperation(OPERATION);
119
120         // null actor argument
121         assertThatThrownBy(() -> outcome.isFor(null, OPERATION));
122
123         // null operation argument
124         assertThatThrownBy(() -> outcome.isFor(ACTOR, null));
125
126         // true case
127         assertTrue(OperationOutcome.isFor(outcome, ACTOR, OPERATION));
128     }
129
130     @Test
131     public void testSetResult() {
132         outcome.setResult(PolicyResult.FAILURE_EXCEPTION);
133         assertEquals(PolicyResult.FAILURE_EXCEPTION, outcome.getResult());
134
135         assertThatThrownBy(() -> outcome.setResult(null));
136     }
137
138     private void setAll() {
139         outcome.setActor(ACTOR);
140         outcome.setEnd(END);
141         outcome.setMessage(MESSAGE);
142         outcome.setOperation(OPERATION);
143         outcome.setResult(RESULT);
144         outcome.setStart(START);
145         outcome.setSubRequestId(SUB_REQ_ID);
146         outcome.setTarget(TARGET);
147         outcome.setResponse(RESPONSE);
148         outcome.setControlLoopResponse(response);
149     }
150 }