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