Bump drools-apps to 1.7.1-SNAPSHOT
[policy/drools-applications.git] / controlloop / m2 / guard / src / test / java / org / onap / policy / guard / CallGuardTaskTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * guard
4  * ================================================================================
5  * Copyright (C) 2017-2019 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.guard;
22
23 import static org.mockito.ArgumentMatchers.argThat;
24 import static org.mockito.ArgumentMatchers.isNotNull;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import java.util.function.Supplier;
30 import org.drools.core.WorkingMemory;
31 import org.junit.Test;
32
33 public class CallGuardTaskTest {
34
35     private static final String REQ_ID = "1-2-3-4-5";
36     private static final String REQ_MATCHER = "0+1-0+2-0+3-0+4-0+5";
37     private static final String VF_COUNT_ACTOR = "SO";
38     private static final String INCR_VF_COUNT_RECIPE = "VF Module Create";
39
40     /**
41      * Tests that "run" works, and inserts guard response into working memory.
42      */
43     @Test
44     public void testRun() {
45         // plain - doesn't need VF module count
46         //doTestRun(Util.INDETERMINATE, "act", "rec", () -> null);
47
48         // SO actor, but plain recipe - doesn't need VF module count
49         //doTestRun(Util.INDETERMINATE, VF_COUNT_ACTOR, "rec", () -> null);
50
51         // plain actor, but scale-out recipe - doesn't need VF module count
52         //doTestRun(Util.INDETERMINATE, "act", "VF Module Create", () -> null);
53
54         // needs VF count
55         //doTestRun(Util.INDETERMINATE, VF_COUNT_ACTOR, INCR_VF_COUNT_RECIPE, () -> 22);
56
57         // needs VF count, but it's missing ==> DENY
58         doTestRun(Util.DENY, VF_COUNT_ACTOR, INCR_VF_COUNT_RECIPE, () -> null);
59     }
60
61     private void doTestRun(String status, String actor, String recipe, Supplier<Integer> vfCount) {
62         WorkingMemory mockWorkingSession = mock(WorkingMemory.class);
63         when(mockWorkingSession.insert(isNotNull())).thenReturn(null);
64         // Create CallGuardTask and run
65         CallGuardTask cgt = new CallGuardTask(mockWorkingSession, "cl", actor, recipe, "tar", REQ_ID, vfCount);
66         cgt.run();
67         verify(mockWorkingSession).insert(argThat((Object obj) -> {
68             if (!(obj instanceof PolicyGuardResponse)) {
69                 return false;
70             }
71             // Check if the inserted response is PolicyGuardResponse, is Indeterminate,
72             // and has same reqID
73             PolicyGuardResponse response = (PolicyGuardResponse) obj;
74             // req ID has form 00000001-0002-0003-0004-000000000005
75             return status.equals(response.getResult()) && response.getRequestId().toString().matches(REQ_MATCHER);
76         }));
77     }
78 }