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