2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.guard;
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;
29 import java.util.function.Supplier;
30 import org.drools.core.WorkingMemory;
31 import org.junit.Test;
33 public class CallGuardTaskTest {
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";
41 * Tests that "run" works, and inserts guard response into working memory.
44 public void testRun() {
45 // plain - doesn't need VF module count
46 //doTestRun(Util.INDETERMINATE, "act", "rec", () -> null);
48 // SO actor, but plain recipe - doesn't need VF module count
49 //doTestRun(Util.INDETERMINATE, VF_COUNT_ACTOR, "rec", () -> null);
51 // plain actor, but scale-out recipe - doesn't need VF module count
52 //doTestRun(Util.INDETERMINATE, "act", "VF Module Create", () -> null);
55 //doTestRun(Util.INDETERMINATE, VF_COUNT_ACTOR, INCR_VF_COUNT_RECIPE, () -> 22);
57 // needs VF count, but it's missing ==> DENY
58 doTestRun(Util.DENY, VF_COUNT_ACTOR, INCR_VF_COUNT_RECIPE, () -> null);
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);
67 verify(mockWorkingSession).insert(argThat((Object obj) -> {
68 if (!(obj instanceof PolicyGuardResponse)) {
71 // Check if the inserted response is PolicyGuardResponse, is Indeterminate,
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);