ab8535ae46423d356c677c5914847031ccc5ff25
[policy/models.git] /
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.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.when;
27
28 import java.util.Map;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.onap.policy.common.endpoints.http.client.HttpClient;
35 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
36 import org.onap.policy.controlloop.actorserviceprovider.Util;
37 import org.onap.policy.controlloop.actorserviceprovider.parameters.ParameterValidationRuntimeException;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class DecisionOperatorTest {
41     private static final String ACTOR = "my-actor";
42     private static final String OPERATION = "my-name";
43     private static final String CLIENT = "my-client";
44     private static final String PATH = "my-path";
45     private static final int TIMEOUT = 10;
46     private static final String ONAP_NAME = "onap-nap";
47     private static final String ONAP_COMP = "onap-component";
48     private static final String ONAP_INST = "onap-instance";
49     private static final String MY_ACTION = "my-action";
50
51     @Mock
52     private HttpClient client;
53
54     @Mock
55     private HttpClientFactory factory;
56
57
58     private DecisionOperator oper;
59
60     /**
61      * Initializes fields, including {@link #oper}, and resets the static fields used by
62      * the REST server.
63      */
64     @Before
65     public void setUp() {
66         when(factory.get(CLIENT)).thenReturn(client);
67
68         oper = new MyOperator();
69
70         DecisionParams params =
71                         DecisionParams.builder().onapName(ONAP_NAME).onapComponent(ONAP_COMP).onapInstance(ONAP_INST)
72                                         .action(MY_ACTION).clientName(CLIENT).path(PATH).timeoutSec(TIMEOUT).build();
73         Map<String, Object> paramMap = Util.translateToMap(OPERATION, params);
74         oper.configure(paramMap);
75
76         assertTrue(oper.makeConfiguration(paramMap) instanceof DecisionConfig);
77     }
78
79     @Test
80     public void testConstructor() {
81         assertEquals(ACTOR, oper.getActorName());
82         assertEquals(OPERATION, oper.getName());
83         assertEquals(ACTOR + "." + OPERATION, oper.getFullName());
84     }
85
86     @Test
87     public void testDoConfigure_testGetters() {
88         assertTrue(oper.getCurrentConfig() instanceof DecisionConfig);
89
90         // test invalid parameters
91         Map<String, Object> paramMap2 = Util.translateToMap(OPERATION, DecisionParams.builder().build());
92         assertThatThrownBy(() -> oper.configure(paramMap2)).isInstanceOf(ParameterValidationRuntimeException.class);
93     }
94
95
96     private class MyOperator extends DecisionOperator {
97         public MyOperator() {
98             super(ACTOR, OPERATION, null);
99         }
100
101         @Override
102         protected HttpClientFactory getClientFactory() {
103             return factory;
104         }
105     }
106 }