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