Merge "Validate ids"
[clamp.git] / src / test / java / org / onap / clamp / clds / client / TcaPolicyDelegateTest.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.Mockito.any;
26 import static org.mockito.Mockito.anyString;
27 import static org.mockito.Mockito.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonObject;
35
36 import java.io.IOException;
37
38 import org.apache.camel.Exchange;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.InjectMocks;
42 import org.mockito.Mock;
43 import org.mockito.runners.MockitoJUnitRunner;
44 import org.onap.clamp.clds.client.req.policy.PolicyClient;
45 import org.onap.clamp.clds.config.ClampProperties;
46 import org.onap.clamp.clds.dao.CldsDao;
47 import org.onap.clamp.clds.exception.ModelBpmnException;
48 import org.onap.clamp.clds.model.CldsModel;
49 import org.onap.clamp.clds.util.JsonUtils;
50
51 @RunWith(MockitoJUnitRunner.class)
52 public class TcaPolicyDelegateTest {
53
54     private static final String MODEL_BPMN_KEY = "modelBpmnProp";
55     private static final String MODEL_PROP_KEY = "modelProp";
56     private static final String MODEL_NAME_KEY = "modelName";
57     private static final String TEST_KEY = "isTest";
58     private static final String USERID_KEY = "userid";
59     private static final String TCA_TEMPLATE_KEY = "tca.template";
60     private static final String TCA_POLICY_TEMPLATE_KEY = "tca.policy.template";
61     private static final String TCA_THRESHOLDS_TEMPLATE_KEY = "tca.thresholds.template";
62     private static final String TCA_POLICY_RESPONSE_MESSAGE_KEY = "tcaPolicyResponseMessage";
63
64     private static final String TCA_ID_FROM_JSON = "{\"tca\":[{\"id\":\"id\",\"from\":\"\"}]}";
65     private static final String ID_JSON = "{\"id\":{\"r\":[{},{\"serviceConfigurations\":"
66             + "[[\"x\",\"+\",\"2\",\"y\"]]}]}}";
67     private static final String TCA_TEMPLATE_JSON = "{\"metricsPerEventName\":[{\"thresholds\":[]}]}";
68     private static final String TCA_POLICY_TEMPLATE_JSON = "{\"content\":{}}";
69     private static final String TCA_THRESHOLDS_TEMPLATE_JSON = "{}";
70     private static final String HOLMES_ID_FROM_JSON = "{\"holmes\":[{\"id\":\"\",\"from\":\"\"}]}";
71     private static final String NOT_JSON = "not json";
72
73     private static final String RESPONSE_MESSAGE_VALUE = "responseMessage";
74     private static final String MODEL_NAME_VALUE = "ModelName";
75     private static final String USERID_VALUE = "user";
76
77     private static final String CLDS_MODEL_ID = "id";
78     private static final String CLDS_MODEL_PROP_TEXT = "propText";
79
80     @Mock
81     private Exchange camelExchange;
82
83     @Mock
84     private ClampProperties refProp;
85
86     @Mock
87     private PolicyClient policyClient;
88
89     @Mock
90     private CldsDao cldsDao;
91
92     @InjectMocks
93     private TcaPolicyDelegate tcaPolicyDelegate;
94
95     @Test
96     public void shouldExecuteSuccessfully() throws IOException {
97         //given
98         when(camelExchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(TCA_ID_FROM_JSON);
99         when(camelExchange.getProperty(eq(MODEL_PROP_KEY))).thenReturn(ID_JSON);
100         when(camelExchange.getProperty(eq(MODEL_NAME_KEY))).thenReturn(MODEL_NAME_VALUE);
101         when(camelExchange.getProperty(eq(TEST_KEY))).thenReturn(false);
102         when(camelExchange.getProperty(eq(USERID_KEY))).thenReturn(USERID_VALUE);
103
104         JsonElement jsonTemplate;
105         JsonObject jsonObject;
106
107         jsonTemplate = mock(JsonElement.class);
108         when(refProp.getJsonTemplate(eq(TCA_TEMPLATE_KEY), anyString())).thenReturn(jsonTemplate);
109         jsonObject = JsonUtils.GSON.fromJson(TCA_TEMPLATE_JSON, JsonObject.class);
110         when(jsonTemplate.getAsJsonObject()).thenReturn(jsonObject);
111
112         jsonTemplate = mock(JsonElement.class);
113         when(refProp.getJsonTemplate(eq(TCA_POLICY_TEMPLATE_KEY), anyString())).thenReturn(jsonTemplate);
114         jsonObject = JsonUtils.GSON.fromJson(TCA_POLICY_TEMPLATE_JSON, JsonObject.class);
115         when(jsonTemplate.getAsJsonObject()).thenReturn(jsonObject);
116
117         jsonTemplate = mock(JsonElement.class);
118         when(refProp.getJsonTemplate(eq(TCA_THRESHOLDS_TEMPLATE_KEY), anyString())).thenReturn(jsonTemplate);
119         jsonObject = JsonUtils.GSON.fromJson(TCA_THRESHOLDS_TEMPLATE_JSON, JsonObject.class);
120         when(jsonTemplate.getAsJsonObject()).thenReturn(jsonObject);
121
122         when(policyClient.sendMicroServiceInOther(anyString(), any())).thenReturn(RESPONSE_MESSAGE_VALUE);
123
124         CldsModel cldsModel = new CldsModel();
125         cldsModel.setId(CLDS_MODEL_ID);
126         cldsModel.setPropText(CLDS_MODEL_PROP_TEXT);
127         when(cldsDao.getModelTemplate(eq(MODEL_NAME_VALUE))).thenReturn(cldsModel);
128
129         //when
130         tcaPolicyDelegate.execute(camelExchange);
131
132         //then
133         verify(camelExchange).setProperty(eq(TCA_POLICY_RESPONSE_MESSAGE_KEY), eq(RESPONSE_MESSAGE_VALUE.getBytes()));
134         verify(cldsDao).setModel(eq(cldsModel), eq(USERID_VALUE));
135     }
136
137     @Test
138     public void shouldExecuteTcaNotFound() {
139         //given
140         when(camelExchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(HOLMES_ID_FROM_JSON);
141         when(camelExchange.getProperty(eq(MODEL_PROP_KEY))).thenReturn(ID_JSON);
142         when(camelExchange.getProperty(eq(TEST_KEY))).thenReturn(false);
143
144         //when
145         tcaPolicyDelegate.execute(camelExchange);
146
147         //then
148         verify(policyClient, never()).sendMicroServiceInOther(any(), any());
149     }
150
151     @Test(expected = ModelBpmnException.class)
152     public void shouldThrowModelBpmnException() {
153         //given
154         when(camelExchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(NOT_JSON);
155         when(camelExchange.getProperty(eq(TEST_KEY))).thenReturn(false);
156
157         //when
158         tcaPolicyDelegate.execute(camelExchange);
159     }
160
161     @Test(expected = NullPointerException.class)
162     public void shouldThrowNullPointerException() {
163         //when
164         tcaPolicyDelegate.execute(camelExchange);
165     }
166 }