More actor clean-up
[policy/models.git] / models-interactions / model-actors / actor.guard / src / test / java / org / onap / policy / controlloop / actor / guard / GuardActorParamsTest.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.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.actorserviceprovider.Util;
35
36 public class GuardActorParamsTest {
37     private static final String CONTAINER = "my-container";
38     private static final String CLIENT = "my-client";
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 static final String PATH1 = "path #1";
46     private static final String PATH2 = "path #2";
47     private static final String URI1 = "uri #1";
48     private static final String URI2 = "uri #2";
49
50     private Map<String, Map<String, Object>> operations;
51     private GuardActorParams params;
52
53     /**
54      * Initializes {@link #operations} with two items and {@link params} with a fully
55      * populated object.
56      */
57     @Before
58     public void setUp() {
59         operations = new TreeMap<>();
60         operations.put(PATH1, Map.of("path", URI1));
61         operations.put(PATH2, Map.of("path", URI2));
62
63         params = makeGuardActorParams();
64     }
65
66     @Test
67     public void testValidate() {
68         assertTrue(params.validate(CONTAINER).isValid());
69
70         // only a few fields are required
71         GuardActorParams sparse = Util.translate(CONTAINER, Map.of("operation", operations), GuardActorParams.class);
72         assertTrue(sparse.validate(CONTAINER).isValid());
73
74         assertEquals(GuardActorParams.DEFAULT_ACTION, sparse.getAction());
75
76         // check fields from superclass
77         testValidateField("operation", "null", params2 -> params2.setOperation(null));
78         testValidateField("timeoutSec", "minimum", params2 -> params2.setTimeoutSec(-1));
79     }
80
81     private void testValidateField(String fieldName, String expected, Consumer<GuardActorParams> makeInvalid) {
82
83         // original params should be valid
84         ValidationResult result = params.validate(CONTAINER);
85         assertTrue(fieldName, result.isValid());
86
87         // make invalid params
88         GuardActorParams params2 = makeGuardActorParams();
89         makeInvalid.accept(params2);
90         result = params2.validate(CONTAINER);
91         assertFalse(fieldName, result.isValid());
92         assertThat(result.getResult()).contains(CONTAINER).contains(fieldName).contains(expected);
93     }
94
95     private GuardActorParams makeGuardActorParams() {
96         GuardActorParams params2 = new GuardActorParams();
97         params2.setClientName(CLIENT);
98         params2.setTimeoutSec(TIMEOUT);
99         params2.setOperation(operations);
100
101         params2.setOnapName(ONAP_NAME);
102         params2.setOnapComponent(ONAP_COMP);
103         params2.setOnapInstance(ONAP_INST);
104         params2.setAction(MY_ACTION);
105
106         return params2;
107     }
108 }