2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
23 package org.onap.clamp.clds.client;
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;
31 import java.io.UnsupportedEncodingException;
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;
44 @RunWith(MockitoJUnitRunner.class)
45 public class OperationalPolicyDelegateTest {
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";
69 private Exchange exchange;
72 private PolicyClient policyClient;
75 private ClampProperties refProp;
78 private OperationalPolicyDelegate operationalPolicyDelegate;
81 public void shouldExecuteSuccessfully() throws BuilderException, UnsupportedEncodingException {
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);
90 operationalPolicyDelegate.execute(exchange);
93 verify(exchange).setProperty(eq(MESSAGE_KEY), eq(MESSAGE_VALUE.getBytes()));
97 public void shouldExecutePolicyNotFound() throws BuilderException, UnsupportedEncodingException {
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);
104 operationalPolicyDelegate.execute(exchange);
107 verify(policyClient, never()).sendBrmsPolicy(any(), any(), any());
110 @Test(expected = ModelBpmnException.class)
111 public void shouldThrowModelBpmnException() throws BuilderException, UnsupportedEncodingException {
113 when(exchange.getProperty(eq(TEST_KEY))).thenReturn(true);
114 when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(NOT_JSON);
117 operationalPolicyDelegate.execute(exchange);
120 @Test(expected = NullPointerException.class)
121 public void shouldThrowNullPointerException() throws BuilderException, UnsupportedEncodingException {
123 operationalPolicyDelegate.execute(exchange);