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.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;
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonObject;
36 import java.io.IOException;
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;
51 @RunWith(MockitoJUnitRunner.class)
52 public class TcaPolicyDelegateTest {
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";
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";
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";
77 private static final String CLDS_MODEL_ID = "id";
78 private static final String CLDS_MODEL_PROP_TEXT = "propText";
81 private Exchange camelExchange;
84 private ClampProperties refProp;
87 private PolicyClient policyClient;
90 private CldsDao cldsDao;
93 private TcaPolicyDelegate tcaPolicyDelegate;
96 public void shouldExecuteSuccessfully() throws IOException {
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);
104 JsonElement jsonTemplate;
105 JsonObject jsonObject;
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);
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);
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);
122 when(policyClient.sendMicroServiceInOther(anyString(), any())).thenReturn(RESPONSE_MESSAGE_VALUE);
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);
130 tcaPolicyDelegate.execute(camelExchange);
133 verify(camelExchange).setProperty(eq(TCA_POLICY_RESPONSE_MESSAGE_KEY), eq(RESPONSE_MESSAGE_VALUE.getBytes()));
134 verify(cldsDao).setModel(eq(cldsModel), eq(USERID_VALUE));
138 public void shouldExecuteTcaNotFound() {
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);
145 tcaPolicyDelegate.execute(camelExchange);
148 verify(policyClient, never()).sendMicroServiceInOther(any(), any());
151 @Test(expected = ModelBpmnException.class)
152 public void shouldThrowModelBpmnException() {
154 when(camelExchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(NOT_JSON);
155 when(camelExchange.getProperty(eq(TEST_KEY))).thenReturn(false);
158 tcaPolicyDelegate.execute(camelExchange);
161 @Test(expected = NullPointerException.class)
162 public void shouldThrowNullPointerException() {
164 tcaPolicyDelegate.execute(camelExchange);