5eb35e9d082c66dc4e8adda080f15fb9014dbddd
[policy/models.git] / models-interactions / model-actors / actor.test / src / test / java / org / onap / policy / controlloop / actor / test / BasicOperationTest.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.actor.test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.Map;
30 import java.util.concurrent.CompletableFuture;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.aai.AaiCqResponse;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
37 import org.onap.policy.controlloop.actorserviceprovider.Util;
38 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationPartial;
39 import org.onap.policy.controlloop.policy.PolicyResult;
40
41 public class BasicOperationTest {
42     private static final String ACTOR = "my-actor";
43     private static final String OPERATION = "my-operation";
44
45     private BasicOperation oper;
46
47
48     @Before
49     public void setUp() throws Exception {
50         oper = new BasicHttpOperation<>(ACTOR, OPERATION);
51         oper.setUpBasic();
52     }
53
54     @Test
55     public void testBasicHttpOperation() {
56         oper = new BasicHttpOperation<>();
57         assertEquals(BasicHttpOperation.DEFAULT_ACTOR, oper.actorName);
58         assertEquals(BasicHttpOperation.DEFAULT_OPERATION, oper.operationName);
59     }
60
61     @Test
62     public void testBasicHttpOperationStringString() {
63         assertEquals(ACTOR, oper.actorName);
64         assertEquals(OPERATION, oper.operationName);
65     }
66
67     @Test
68     public void testSetUp() throws Exception {
69         assertNotNull(oper.future);
70         assertNotNull(oper.context);
71         assertNotNull(oper.outcome);
72         assertNotNull(oper.executor);
73         assertNotNull(oper.guardOperation);
74
75         CompletableFuture<OperationOutcome> future = oper.service.getActor(OperationPartial.GUARD_ACTOR_NAME)
76                         .getOperator(OperationPartial.GUARD_OPERATION_NAME).buildOperation(null).start();
77         assertTrue(future.isDone());
78         assertEquals(PolicyResult.SUCCESS, future.get().getResult());
79     }
80
81     @Test
82     public void testMakeContext() {
83         oper.makeContext();
84
85         assertTrue(oper.enrichment.isEmpty());
86
87         assertSame(BasicHttpOperation.REQ_ID, oper.event.getRequestId());
88         assertSame(oper.enrichment, oper.event.getAai());
89
90         assertSame(oper.event, oper.context.getEvent());
91
92         assertSame(oper.context, oper.params.getContext());
93         assertSame(oper.service, oper.params.getActorService());
94         assertSame(oper.executor, oper.params.getExecutor());
95         assertEquals(ACTOR, oper.params.getActor());
96         assertEquals(OPERATION, oper.params.getOperation());
97         assertEquals(BasicHttpOperation.TARGET_ENTITY, oper.params.getTargetEntity());
98     }
99
100     @Test
101     public void testMakeEnrichment_testMakePayload() {
102         assertTrue(oper.makeEnrichment().isEmpty());
103         assertNull(oper.makePayload());
104     }
105
106     @Test
107     public void testVerifyRequest() throws CoderException {
108         Map<String, Object> map = Util.translateToMap("", ResourceUtils.getResourceAsString("actual.json"));
109         oper.verifyRequest("expected.json", map, "svc-request-id", "vnf-id");
110     }
111
112     @Test
113     public void testProvideCqResponse() throws Exception {
114         AaiCqResponse cq = new AaiCqResponse("{}");
115         oper.provideCqResponse(cq);
116
117         assertSame(cq, oper.context.getProperty(AaiCqResponse.CONTEXT_KEY));
118         assertTrue(oper.cqFuture.isDone());
119         assertEquals(PolicyResult.SUCCESS, oper.cqFuture.get().getResult());
120     }
121 }