Added unit tests for OperationalPolicyDelegate
[clamp.git] / src / test / java / org / onap / clamp / clds / client / OperationalPolicyDelegateTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Samsung. 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  */
22
23 package org.onap.clamp.clds.client;
24
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.io.UnsupportedEncodingException;
32
33 import org.apache.camel.Exchange;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.runners.MockitoJUnitRunner;
39 import org.onap.clamp.clds.client.req.policy.PolicyClient;
40 import org.onap.clamp.clds.config.ClampProperties;
41 import org.onap.clamp.clds.exception.ModelBpmnException;
42 import org.onap.policy.controlloop.policy.builder.BuilderException;
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class OperationalPolicyDelegateTest {
46
47     private static final String TEST_KEY = "isTest";
48     private static final String MODEL_BPMN_KEY = "modelBpmnProp";
49     private static final String MODEL_PROP_KEY = "modelProp";
50     private static final String RECIPE_TOPIC_KEY = "op.recipeTopic";
51     private static final String MESSAGE_KEY = "operationalPolicyResponseMessage";
52     private static final String SERVICE_NAME = "service.name";
53     private static final String POLICY_ID_FROM_JSON = "{policy:[{id:Oper12,from:''}]}";
54     private static final String ID_WITH_CHAIN_JSON = "{Oper12:{ab:["
55             + "{name:timeout,value:500},"
56             + "{policyConfigurations:["
57             + "[{name:maxRetries,value:5},"
58             + "{name:retryTimeLimit,value:1000},"
59             + "{name:recipe,value:go},"
60             + "{name:targetResourceId,"
61             + "value:resid234}]]}]},"
62             + "global:[{name:service,value:" + SERVICE_NAME + "}]}";
63     private static final String SIMPLE_JSON = "{}";
64     private static final String NOT_JSON = "not json";
65     private static final String MESSAGE_VALUE = "message";
66     private static final String RECIPE_TOPIC_VALUE = "recipe.topic";
67
68     @Mock
69     private Exchange exchange;
70
71     @Mock
72     private PolicyClient policyClient;
73
74     @Mock
75     private ClampProperties refProp;
76
77     @InjectMocks
78     private OperationalPolicyDelegate operationalPolicyDelegate;
79
80     @Test
81     public void shouldExecuteSuccessfully() throws BuilderException, UnsupportedEncodingException {
82         // given
83         when(exchange.getProperty(eq(TEST_KEY))).thenReturn(true);
84         when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(POLICY_ID_FROM_JSON);
85         when(exchange.getProperty(eq(MODEL_PROP_KEY))).thenReturn(ID_WITH_CHAIN_JSON);
86         when(policyClient.sendBrmsPolicy(any(), any(), any())).thenReturn(MESSAGE_VALUE);
87         when(refProp.getStringValue(eq(RECIPE_TOPIC_KEY), eq(SERVICE_NAME))).thenReturn(RECIPE_TOPIC_VALUE);
88
89         // when
90         operationalPolicyDelegate.execute(exchange);
91
92         // then
93         verify(exchange).setProperty(eq(MESSAGE_KEY), eq(MESSAGE_VALUE.getBytes()));
94     }
95
96     @Test
97     public void shouldExecutePolicyNotFound() throws BuilderException, UnsupportedEncodingException {
98         // given
99         when(exchange.getProperty(eq(TEST_KEY))).thenReturn(false);
100         when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(SIMPLE_JSON);
101         when(exchange.getProperty(eq(MODEL_PROP_KEY))).thenReturn(SIMPLE_JSON);
102
103         // when
104         operationalPolicyDelegate.execute(exchange);
105
106         // then
107         verify(policyClient, never()).sendBrmsPolicy(any(), any(), any());
108     }
109
110     @Test(expected = ModelBpmnException.class)
111     public void shouldThrowModelBpmnException() throws BuilderException, UnsupportedEncodingException {
112         // given
113         when(exchange.getProperty(eq(TEST_KEY))).thenReturn(true);
114         when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(NOT_JSON);
115
116         // when
117         operationalPolicyDelegate.execute(exchange);
118     }
119
120     @Test(expected = NullPointerException.class)
121     public void shouldThrowNullPointerException() throws BuilderException, UnsupportedEncodingException {
122         // when
123         operationalPolicyDelegate.execute(exchange);
124     }
125 }