Merge "Added more unit tests and fix import"
[clamp.git] / src / test / java / org / onap / clamp / clds / client / OperationalPolicyDeleteDelegateTest.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.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import org.apache.camel.Exchange;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.onap.clamp.clds.client.req.policy.PolicyClient;
39 import org.onap.clamp.clds.exception.ModelBpmnException;
40 import org.onap.clamp.clds.model.properties.ModelProperties;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class OperationalPolicyDeleteDelegateTest {
44
45     private static final String TEST_KEY = "isTest";
46     private static final String MODEL_BPMN_KEY = "modelBpmnProp";
47     private static final String MODEL_PROP_KEY = "modelProp";
48     private static final String EVENT_ACTION_KEY = "eventAction";
49     private static final String POLICY_ID_FROM_JSON = "{policy:[{id:Poli2,from:''}]}";
50     private static final String ID_WITH_CHAIN_JSON = "{Poli2:{ab:c,xy:z}}";
51     private static final String ID_NO_CHAIN_JSON = "{Poli2:{}}";
52     private static final String EVENT_ACTION_VALUE = "still";
53     private static final String NOT_JSON = "23e";
54
55     @Mock
56     private Exchange exchange;
57
58     @Mock
59     private PolicyClient policyClient;
60
61     @InjectMocks
62     private OperationalPolicyDeleteDelegate operationalPolicyDeleteDelegate;
63
64     @Test
65     public void shouldExecuteSuccessfully() {
66         // given
67         when(exchange.getProperty(eq(TEST_KEY))).thenReturn(false);
68         when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(POLICY_ID_FROM_JSON);
69         when(exchange.getProperty(eq(MODEL_PROP_KEY))).thenReturn(ID_WITH_CHAIN_JSON);
70         when(exchange.getProperty(eq(EVENT_ACTION_KEY))).thenReturn(EVENT_ACTION_VALUE);
71
72         // when
73         operationalPolicyDeleteDelegate.execute(exchange);
74
75         // then
76         verify(policyClient, times(2)).deleteBrms(any(ModelProperties.class));
77     }
78
79     @Test
80     public void shouldExecuteTcaNotFound() {
81         // given
82         when(exchange.getProperty(eq(TEST_KEY))).thenReturn(true);
83         when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(POLICY_ID_FROM_JSON);
84         when(exchange.getProperty(eq(MODEL_PROP_KEY))).thenReturn(ID_NO_CHAIN_JSON);
85
86         // when
87         operationalPolicyDeleteDelegate.execute(exchange);
88
89         // then
90         verify(policyClient, never()).deleteBrms(any(ModelProperties.class));
91     }
92
93     @Test(expected = ModelBpmnException.class)
94     public void shouldThrowModelBpmnException() {
95         // given
96         when(exchange.getProperty(eq(TEST_KEY))).thenReturn(false);
97         when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(NOT_JSON);
98
99         // when
100         operationalPolicyDeleteDelegate.execute(exchange);
101     }
102
103     @Test(expected = NullPointerException.class)
104     public void shouldThrowNullPointerException() {
105         // when
106         operationalPolicyDeleteDelegate.execute(exchange);
107     }
108 }