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