1d3f1ce619798bc6ca488ecee3cb5be8b50fead1
[clamp.git] / src / test / java / org / onap / clamp / clds / client / HolmesPolicyDelegateTest.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.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;
33
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonObject;
36
37 import java.io.IOException;
38
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;
53
54 @RunWith(MockitoJUnitRunner.class)
55 public class HolmesPolicyDelegateTest {
56
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";
81
82     @Mock
83     private Exchange exchange;
84
85     @Mock
86     private PolicyClient policyClient;
87
88     @Mock
89     private ClampProperties clampProperties;
90
91     @Mock
92     private CldsDao cldsDao;
93
94     @InjectMocks
95     private HolmesPolicyDelegate holmesPolicyDelegateTest;
96
97     @Test
98     public void shouldExecuteSuccessfully() throws IOException {
99         // given
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);
105
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));
109
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));
113
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));
117
118         when(policyClient.sendBasePolicyInOther(anyString(), anyString(), any(), anyString()))
119                 .thenReturn(RESPONSE_MESSAGE_VALUE);
120
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);
125
126         // when
127         holmesPolicyDelegateTest.execute(exchange);
128
129         // then
130         verify(exchange).setProperty(eq(HOLMES_POLICY_RESPONSE_MESSAGE_KEY), eq(RESPONSE_MESSAGE_VALUE.getBytes()));
131         verify(cldsDao).setModel(eq(cldsModel), eq(USERID_VALUE));
132     }
133
134     @Test
135     public void shouldExecuteHolmesNotFound() {
136         // given
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);
140
141         // when
142         holmesPolicyDelegateTest.execute(exchange);
143
144         // then
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());
148     }
149
150     @Test(expected = ModelBpmnException.class)
151     public void shouldThrowModelBpmnException() {
152         // given
153         when(exchange.getProperty(eq(MODEL_BPMN_KEY))).thenReturn(NOT_JSON);
154         when(exchange.getProperty(eq(TEST_KEY))).thenReturn(false);
155
156         // when
157         holmesPolicyDelegateTest.execute(exchange);
158     }
159
160     @Test(expected = NullPointerException.class)
161     public void shouldThrowNullPointerException() {
162         // when
163         holmesPolicyDelegateTest.execute(exchange);
164     }
165
166     @Test
167     public void shouldDoFormatHolmesConfigBodySuccessfully() {
168         // given
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);
172
173         // when
174         String result = HolmesPolicyDelegate.formatHolmesConfigBody(prop, holmes);
175
176         // then
177         assertEquals(CONTROL_NAME_VALUE + "$$$" + CORRELATION_LOGIC_JSON, result);
178     }
179
180     private static JsonObject getJsonObject(String jsonText) {
181         return JsonUtils.GSON.fromJson(jsonText, JsonObject.class);
182     }
183 }