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.junit.Assert.assertEquals;
26 import static org.mockito.Mockito.any;
27 import static org.mockito.Mockito.anyString;
28 import static org.mockito.Mockito.eq;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.never;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonObject;
37 import java.io.IOException;
39 import org.apache.camel.Exchange;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.runners.MockitoJUnitRunner;
45 import org.onap.clamp.clds.client.req.policy.PolicyClient;
46 import org.onap.clamp.clds.config.ClampProperties;
47 import org.onap.clamp.clds.dao.CldsDao;
48 import org.onap.clamp.clds.exception.ModelBpmnException;
49 import org.onap.clamp.clds.model.CldsModel;
50 import org.onap.clamp.clds.model.properties.Holmes;
51 import org.onap.clamp.clds.model.properties.ModelProperties;
52 import org.onap.clamp.clds.util.JsonUtils;
54 @RunWith(MockitoJUnitRunner.class)
55 public class HolmesPolicyDelegateTest {
57 private static final String ID_JSON = "{\"id\":{\"r\":[{},{\"serviceConfigurations\":"
58 + "[[\"x\",\"+\",\"2\",\"y\"]]}]}}";
59 private static final String METRICS_JSON = "{\"metricsPerEventName\":[{\"thresholds\":[]}]}";
60 private static final String CONTENT_JSON = "{\"content\":{}}";
61 private static final String NULL_JSON = "{}";
62 private static final String HOLMES_ID_FROM_JSON = "{\"holmes\":[{\"id\":\"id\",\"from\":\"\"}]}";
63 private static final String TCA_ID_FROM_JSON = "{\"tca\":[{\"id\":\"\",\"from\":\"\"}]}";
64 private static final String CORRELATION_LOGIC_JSON = "{\"name\":\"correlationalLogic\"}";
65 private static final String NOT_JSON = "not json";
66 private static final String MODEL_BPMN_KEY = "modelBpmnProp";
67 private static final String MODEL_PROP_KEY = "modelProp";
68 private static final String MODEL_NAME_KEY = "modelName";
69 private static final String TEST_KEY = "isTest";
70 private static final String USERID_KEY = "userid";
71 private static final String TCA_TEMPLATE_KEY = "tca.template";
72 private static final String TCA_POLICY_TEMPLATE_KEY = "tca.policy.template";
73 private static final String TCA_THRESHOLDS_TEMPLATE_KEY = "tca.thresholds.template";
74 private static final String HOLMES_POLICY_RESPONSE_MESSAGE_KEY = "holmesPolicyResponseMessage";
75 private static final String RESPONSE_MESSAGE_VALUE = "responseMessage";
76 private static final String MODEL_NAME_VALUE = "model.name";
77 private static final String CONTROL_NAME_VALUE = "control.name";
78 private static final String USERID_VALUE = "user";
79 private static final String CLDS_MODEL_ID = "id";
80 private static final String CLDS_MODEL_PROP_TEXT = "propText";
83 private Exchange exchange;
86 private PolicyClient policyClient;
89 private ClampProperties clampProperties;
92 private CldsDao cldsDao;
95 private HolmesPolicyDelegate holmesPolicyDelegateTest;
98 public void shouldExecuteSuccessfully() throws IOException {
100 when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(HOLMES_ID_FROM_JSON);
101 when(exchange.getProperty(eq(MODEL_PROP_KEY))).thenReturn(ID_JSON);
102 when(exchange.getProperty(eq(MODEL_NAME_KEY))).thenReturn(MODEL_NAME_VALUE);
103 when(exchange.getProperty(eq(TEST_KEY))).thenReturn(false);
104 when(exchange.getProperty(eq(USERID_KEY))).thenReturn(USERID_VALUE);
106 JsonElement jsonTemplateA = mock(JsonElement.class);
107 when(clampProperties.getJsonTemplate(eq(TCA_TEMPLATE_KEY), anyString())).thenReturn(jsonTemplateA);
108 when(jsonTemplateA.getAsJsonObject()).thenReturn(getJsonObject(METRICS_JSON));
110 JsonElement jsonTemplateB = mock(JsonElement.class);
111 when(clampProperties.getJsonTemplate(eq(TCA_POLICY_TEMPLATE_KEY), anyString())).thenReturn(jsonTemplateB);
112 when(jsonTemplateB.getAsJsonObject()).thenReturn(getJsonObject(CONTENT_JSON));
114 JsonElement jsonTemplateC = mock(JsonElement.class);
115 when(clampProperties.getJsonTemplate(eq(TCA_THRESHOLDS_TEMPLATE_KEY), anyString())).thenReturn(jsonTemplateC);
116 when(jsonTemplateC.getAsJsonObject()).thenReturn(getJsonObject(NULL_JSON));
118 when(policyClient.sendBasePolicyInOther(anyString(), anyString(), any(), anyString()))
119 .thenReturn(RESPONSE_MESSAGE_VALUE);
121 CldsModel cldsModel = new CldsModel();
122 cldsModel.setId(CLDS_MODEL_ID);
123 cldsModel.setPropText(CLDS_MODEL_PROP_TEXT);
124 when(cldsDao.getModelTemplate(eq(MODEL_NAME_VALUE))).thenReturn(cldsModel);
127 holmesPolicyDelegateTest.execute(exchange);
130 verify(exchange).setProperty(eq(HOLMES_POLICY_RESPONSE_MESSAGE_KEY), eq(RESPONSE_MESSAGE_VALUE.getBytes()));
131 verify(cldsDao).setModel(eq(cldsModel), eq(USERID_VALUE));
135 public void shouldExecuteHolmesNotFound() {
137 when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(TCA_ID_FROM_JSON);
138 when(exchange.getProperty(eq(MODEL_PROP_KEY))).thenReturn(ID_JSON);
139 when(exchange.getProperty(eq(TEST_KEY))).thenReturn(false);
142 holmesPolicyDelegateTest.execute(exchange);
145 verify(policyClient, never()).sendBasePolicyInOther(anyString(), anyString(), any(), anyString());
146 verify(exchange, never()).setProperty(eq(HOLMES_POLICY_RESPONSE_MESSAGE_KEY), any());
147 verify(cldsDao, never()).setModel(any(), anyString());
150 @Test(expected = ModelBpmnException.class)
151 public void shouldThrowModelBpmnException() {
153 when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(NOT_JSON);
154 when(exchange.getProperty(eq(TEST_KEY))).thenReturn(false);
157 holmesPolicyDelegateTest.execute(exchange);
160 @Test(expected = NullPointerException.class)
161 public void shouldThrowNullPointerException() {
163 holmesPolicyDelegateTest.execute(exchange);
167 public void shouldDoFormatHolmesConfigBodySuccessfully() {
169 ModelProperties prop = new ModelProperties(null, CONTROL_NAME_VALUE, null, false,
170 HOLMES_ID_FROM_JSON, "{\"id\":" + CORRELATION_LOGIC_JSON + "}");
171 Holmes holmes = prop.getType(Holmes.class);
174 String result = HolmesPolicyDelegate.formatHolmesConfigBody(prop, holmes);
177 assertEquals(CONTROL_NAME_VALUE + "$$$" + CORRELATION_LOGIC_JSON, result);
180 private static JsonObject getJsonObject(String jsonText) {
181 return JsonUtils.GSON.fromJson(jsonText, JsonObject.class);