01c7d5899fa91f5d4c6698e86977ffc7be4292bf
[policy/models.git] / models-interactions / model-actors / actor.xacml / src / test / java / org / onap / policy / controlloop / actor / xacml / DecisionParamsTest.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.function.Function;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.common.parameters.ValidationResult;
32 import org.onap.policy.controlloop.actor.xacml.DecisionParams;
33 import org.onap.policy.controlloop.actor.xacml.DecisionParams.DecisionParamsBuilder;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpParams.HttpParamsBuilder;
35
36 public class DecisionParamsTest {
37     private static final String CONTAINER = "my-container";
38     private static final String CLIENT = "my-client";
39     private static final String PATH = "my-path";
40     private static final int TIMEOUT = 10;
41     private static final String ONAP_NAME = "onap-nap";
42     private static final String ONAP_COMP = "onap-component";
43     private static final String ONAP_INST = "onap-instance";
44     private static final String MY_ACTION = "my-action";
45
46     private DecisionParams params;
47
48     @Before
49     public void setUp() {
50         params = DecisionParams.builder().onapName(ONAP_NAME).onapComponent(ONAP_COMP).onapInstance(ONAP_INST)
51                         .action(MY_ACTION).clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
52     }
53
54     @Test
55     public void testIsDisabled() {
56         // disabled by default
57         assertFalse(params.isDisabled());
58     }
59
60     @Test
61     public void testValidate() {
62         assertTrue(params.validate(CONTAINER).isValid());
63
64         testValidateField("onapName", "null", bldr -> bldr.onapName(null));
65         testValidateField("onapComponent", "null", bldr -> bldr.onapComponent(null));
66         testValidateField("onapInstance", "null", bldr -> bldr.onapInstance(null));
67         testValidateField("action", "null", bldr -> bldr.action(null));
68
69         // validate one of the superclass fields
70         testValidateField("clientName", "null", bldr -> bldr.clientName(null));
71     }
72
73     @Test
74     public void testBuilder_testToBuilder() {
75         assertEquals(CLIENT, params.getClientName());
76
77         assertEquals(ONAP_NAME, params.getOnapName());
78         assertEquals(ONAP_COMP, params.getOnapComponent());
79         assertEquals(ONAP_INST, params.getOnapInstance());
80         assertEquals(MY_ACTION, params.getAction());
81
82         assertEquals(params, params.toBuilder().build());
83     }
84
85     private void testValidateField(String fieldName, String expected,
86                     @SuppressWarnings("rawtypes") Function<DecisionParamsBuilder, HttpParamsBuilder> makeInvalid) {
87
88         // original params should be valid
89         ValidationResult result = params.validate(CONTAINER);
90         assertTrue(fieldName, result.isValid());
91
92         // make invalid params
93         result = makeInvalid.apply(params.toBuilder()).build().validate(CONTAINER);
94         assertFalse(fieldName, result.isValid());
95         assertThat(result.getResult()).contains(fieldName).contains(expected);
96     }
97 }