SDNR Actor enhancements
[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
46     private ControlLoopResponse response;
47     private OperationOutcome outcome;
48
49     /**
50      * Sets up.
51      */
52     @Before
53     public void setUp() {
54         response = new ControlLoopResponse();
55
56         outcome = new OperationOutcome();
57     }
58
59     @Test
60     public void testOperationOutcomeOperationOutcome() {
61         setAll();
62
63         OperationOutcome outcome2 = new OperationOutcome(outcome);
64
65         assertEquals(ACTOR, outcome2.getActor());
66         assertEquals(OPERATION, outcome2.getOperation());
67         assertEquals(TARGET, outcome2.getTarget());
68         assertEquals(START, outcome2.getStart());
69         assertEquals(END, outcome2.getEnd());
70         assertEquals(SUB_REQ_ID, outcome2.getSubRequestId());
71         assertEquals(RESULT, outcome2.getResult());
72         assertEquals(MESSAGE, outcome2.getMessage());
73         assertSame(response, outcome2.getControlLoopResponse());
74     }
75
76     @Test
77     public void testToControlLoopOperation() {
78         setAll();
79
80         ControlLoopOperation outcome2 = outcome.toControlLoopOperation();
81
82         assertEquals(ACTOR, outcome2.getActor());
83         assertEquals(OPERATION, outcome2.getOperation());
84         assertEquals(TARGET, outcome2.getTarget());
85         assertEquals(START, outcome2.getStart());
86         assertEquals(END, outcome2.getEnd());
87         assertEquals(SUB_REQ_ID, outcome2.getSubRequestId());
88         assertEquals(RESULT.toString(), outcome2.getOutcome());
89         assertEquals(MESSAGE, outcome2.getMessage());
90     }
91
92     /**
93      * Tests both isFor() methods, as one invokes the other.
94      */
95     @Test
96     public void testIsFor() {
97         setAll();
98
99         // null case
100         assertFalse(OperationOutcome.isFor(null, ACTOR, OPERATION));
101
102         // actor mismatch
103         assertFalse(OperationOutcome.isFor(outcome, TARGET, OPERATION));
104
105         // operation mismatch
106         assertFalse(OperationOutcome.isFor(outcome, ACTOR, TARGET));
107
108         // null actor in outcome
109         outcome.setActor(null);
110         assertFalse(OperationOutcome.isFor(outcome, ACTOR, OPERATION));
111         outcome.setActor(ACTOR);
112
113         // null operation in outcome
114         outcome.setOperation(null);
115         assertFalse(OperationOutcome.isFor(outcome, ACTOR, OPERATION));
116         outcome.setOperation(OPERATION);
117
118         // null actor argument
119         assertThatThrownBy(() -> outcome.isFor(null, OPERATION));
120
121         // null operation argument
122         assertThatThrownBy(() -> outcome.isFor(ACTOR, null));
123
124         // true case
125         assertTrue(OperationOutcome.isFor(outcome, ACTOR, OPERATION));
126     }
127
128     @Test
129     public void testSetResult() {
130         outcome.setResult(PolicyResult.FAILURE_EXCEPTION);
131         assertEquals(PolicyResult.FAILURE_EXCEPTION, outcome.getResult());
132
133         assertThatThrownBy(() -> outcome.setResult(null));
134     }
135
136     private void setAll() {
137         outcome.setActor(ACTOR);
138         outcome.setEnd(END);
139         outcome.setMessage(MESSAGE);
140         outcome.setOperation(OPERATION);
141         outcome.setResult(RESULT);
142         outcome.setStart(START);
143         outcome.setSubRequestId(SUB_REQ_ID);
144         outcome.setTarget(TARGET);
145         outcome.setControlLoopResponse(response);
146     }
147 }