4e1b9b05d6038ce54db4086cd4e27e12d205bb63
[policy/models.git] / models-interactions / model-actors / actor.xacml / src / test / java / org / onap / policy / controlloop / actor / xacml / XacmlActorParamsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 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.xacml;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.Map;
29 import java.util.TreeMap;
30 import java.util.function.Consumer;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.common.parameters.ValidationResult;
34 import org.onap.policy.controlloop.actor.xacml.XacmlActorParams;
35 import org.onap.policy.controlloop.actorserviceprovider.Util;
36 import org.onap.policy.controlloop.actorserviceprovider.parameters.ActorParams;
37
38 public class XacmlActorParamsTest {
39     private static final String CONTAINER = "my-container";
40     private static final String CLIENT = "my-client";
41     private static final int TIMEOUT = 10;
42     private static final String ONAP_NAME = "onap-nap";
43     private static final String ONAP_COMP = "onap-component";
44     private static final String ONAP_INST = "onap-instance";
45     private static final String MY_ACTION = "my-action";
46
47     private static final String PATH1 = "path #1";
48     private static final String PATH2 = "path #2";
49     private static final String URI1 = "uri #1";
50     private static final String URI2 = "uri #2";
51
52     private Map<String, Map<String, Object>> operations;
53     private XacmlActorParams params;
54
55     /**
56      * Initializes {@link #operations} with two items and {@link params} with a fully
57      * populated object.
58      */
59     @Before
60     public void setUp() {
61         operations = new TreeMap<>();
62         operations.put(PATH1, Map.of("path", URI1));
63         operations.put(PATH2, Map.of("path", URI2));
64
65         params = makeXacmlActorParams();
66     }
67
68     @Test
69     public void testIsDisabled() {
70         // disabled by default
71         assertFalse(params.isDisabled());
72     }
73
74     @Test
75     public void testValidate() {
76         assertTrue(params.validate(CONTAINER).isValid());
77
78         // only a few fields are required
79         XacmlActorParams sparse = Util.translate(CONTAINER, Map.of(ActorParams.OPERATIONS_FIELD, operations),
80                         XacmlActorParams.class);
81         assertTrue(sparse.validate(CONTAINER).isValid());
82
83         assertEquals(XacmlActorParams.DEFAULT_ACTION, sparse.getAction());
84
85         // check fields from superclass
86         testValidateField(ActorParams.OPERATIONS_FIELD, "null", params2 -> params2.setOperations(null));
87         testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1));
88     }
89
90     private void testValidateField(String fieldName, String expected, Consumer<XacmlActorParams> makeInvalid) {
91
92         // original params should be valid
93         ValidationResult result = params.validate(CONTAINER);
94         assertTrue(fieldName, result.isValid());
95
96         // make invalid params
97         XacmlActorParams params2 = makeXacmlActorParams();
98         makeInvalid.accept(params2);
99         result = params2.validate(CONTAINER);
100         assertFalse(fieldName, result.isValid());
101         assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
102     }
103
104     private XacmlActorParams makeXacmlActorParams() {
105         XacmlActorParams params2 = new XacmlActorParams();
106         params2.setClientName(CLIENT);
107         params2.setTimeoutSec(TIMEOUT);
108         params2.setOperations(operations);
109
110         params2.setOnapName(ONAP_NAME);
111         params2.setOnapComponent(ONAP_COMP);
112         params2.setOnapInstance(ONAP_INST);
113         params2.setAction(MY_ACTION);
114
115         return params2;
116     }
117 }