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 org.apache.camel.Exchange;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.onap.clamp.clds.client.req.policy.PolicyClient;
38 import org.onap.clamp.clds.exception.ModelBpmnException;
40 @RunWith(MockitoJUnitRunner.class)
41 public class GuardPolicyDelegateTest {
43 private static final String TEST_KEY = "isTest";
44 private static final String MODEL_BPMN_KEY = "modelBpmnProp";
45 private static final String MODEL_PROP_KEY = "modelProp";
46 private static final String POLICY_ID_FROM_JSON = "{policy:[{id:guard,from:''}]}";
47 private static final String ID_WITH_CHAIN_JSON = "{guard:{q:["
48 + "{name:timeout,value:200},"
49 + "{policyConfigurations:["
50 + "[{name:maxRetries,value:3},"
51 + "{name:retryTimeLimit,value:800},"
52 + "{name:enableGuardPolicy,value:on}]]}]}}";
53 private static final String SIMPLE_JSON = "{}";
54 private static final String NOT_JSON = "not json";
57 private Exchange exchange;
60 private PolicyClient policyClient;
63 private GuardPolicyDelegate guardPolicyDelegate;
66 public void shouldExecuteSuccessfully() {
68 when(exchange.getProperty(eq(TEST_KEY))).thenReturn(false);
69 when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(POLICY_ID_FROM_JSON);
70 when(exchange.getProperty(eq(MODEL_PROP_KEY))).thenReturn(ID_WITH_CHAIN_JSON);
73 guardPolicyDelegate.execute(exchange);
76 verify(policyClient).sendGuardPolicy(any(), any(), any(), any());
80 public void shouldExecutePolicyNotFound() {
82 when(exchange.getProperty(eq(TEST_KEY))).thenReturn(false);
83 when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(SIMPLE_JSON);
84 when(exchange.getProperty(eq(MODEL_PROP_KEY))).thenReturn(SIMPLE_JSON);
87 guardPolicyDelegate.execute(exchange);
90 verify(policyClient, never()).sendGuardPolicy(any(), any(), any(), any());
93 @Test(expected = ModelBpmnException.class)
94 public void shouldThrowModelBpmnException() {
96 when(exchange.getProperty(eq(TEST_KEY))).thenReturn(true);
97 when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(NOT_JSON);
100 guardPolicyDelegate.execute(exchange);
103 @Test(expected = NullPointerException.class)
104 public void shouldThrowNullPointerException() {
106 guardPolicyDelegate.execute(exchange);