summaryrefslogtreecommitdiffstats
path: root/rulemgt/src/test/java/org
diff options
context:
space:
mode:
authorGuangrongFu <fu.guangrong@zte.com.cn>2018-08-30 19:53:49 +0800
committerGuangrongFu <fu.guangrong@zte.com.cn>2018-08-30 19:53:49 +0800
commita9e01a4e720ff10684a9c46e9e8ab85848d3b3a7 (patch)
tree6b510c5185f755915da572d0a88512da1b578a42 /rulemgt/src/test/java/org
parentba8c5a4a8963f1f6601469110b4cb088cb420a98 (diff)
Add some UT codes
Change-Id: I477f23f237dad9efbf473545c80e296ed992ae71 Issue-ID: HOLMES-159 Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
Diffstat (limited to 'rulemgt/src/test/java/org')
-rw-r--r--rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java223
1 files changed, 148 insertions, 75 deletions
diff --git a/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java b/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java
index 1037495..52f1e37 100644
--- a/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java
+++ b/rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java
@@ -1,12 +1,12 @@
/**
* Copyright 2017 ZTE Corporation.
- *
+ * <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,103 +15,176 @@
*/
package org.onap.holmes.rulemgt.dcae;
-import static org.hamcrest.CoreMatchers.equalTo;
-import static org.junit.Assert.assertThat;
-import static org.powermock.api.mockito.PowerMockito.when;
+import com.alibaba.fastjson.JSONObject;
+import org.apache.http.HttpResponse;
+import org.apache.http.StatusLine;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
-import org.onap.holmes.common.config.MicroServiceConfig;
+import org.onap.holmes.common.dcae.DcaeConfigurationQuery;
import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
import org.onap.holmes.common.dcae.entity.Rule;
-import org.onap.holmes.common.dcae.utils.DcaeConfigurationParser;
-import org.onap.holmes.common.exception.CorrelationException;
-import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
-import org.powermock.api.easymock.PowerMock;
-import org.powermock.api.mockito.PowerMockito;
+import org.onap.holmes.common.utils.HttpsUtils;
+import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
+import org.onap.holmes.rulemgt.bean.response.RuleResult4API;
import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
-@PrepareForTest({DcaeConfigurationPolling.class, MicroServiceConfig.class})
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.expect;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.powermock.api.easymock.PowerMock.*;
+
+@PrepareForTest({HttpsUtils.class, DcaeConfigurationQuery.class, DcaeConfigurationPolling.class})
+@SuppressStaticInitializationFor("org.onap.holmes.common.utils.HttpsUtils")
@RunWith(PowerMockRunner.class)
public class DcaeConfigurationPollingTest {
@org.junit.Rule
public ExpectedException thrown = ExpectedException.none();
- private DcaeConfigurationPolling daceConfigurationPolling;
+ @Test
+ public void run() throws Exception {
+ DcaeConfigurations dcaeConfigurations = new DcaeConfigurations();
+ dcaeConfigurations.addDefaultRule(new Rule("test", "clName", "contents", 1));
+ mockStatic(DcaeConfigurationQuery.class);
+ expect(DcaeConfigurationQuery.getDcaeConfigurations(anyObject(String.class))).andReturn(dcaeConfigurations);
+ DcaeConfigurationPolling dcaeConfigurationPolling = createPartialMock(DcaeConfigurationPolling.class,
+ "getAllCorrelationRules");
+ Whitebox.setInternalState(dcaeConfigurationPolling, "url", "http://127.0.0.1");
- @Before
- public void setUp() {
- daceConfigurationPolling = new DcaeConfigurationPolling("holmes-rule-mgmt");
- }
+ RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse();
+ List<RuleResult4API> ruleResult4APIList = new ArrayList<RuleResult4API>(){
+ {
+ add(new RuleResult4API());
+ }
+ };
+ ruleQueryListResponse.setCorrelationRules(ruleResult4APIList);
+ ruleQueryListResponse.setTotalCount(ruleResult4APIList.size());
+ expect(dcaeConfigurationPolling.getAllCorrelationRules()).andReturn(ruleQueryListResponse);
- @Test
- public void testDaceConfigurationPolling_getDcaeConfigurations_exception() throws Exception {
- PowerMock.resetAll();
- thrown.expect(CorrelationException.class);
- thrown.expectMessage("syntax error, pos 1");
- PowerMockito.mockStatic(MicroServiceConfig.class);
- when(MicroServiceConfig.getServiceConfigInfoFromCBS("holmes-rule-mgmt"))
- .thenReturn("host");
- PowerMock.createMock(DcaeConfigurationParser.class);
- PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
- .andThrow(new CorrelationException("tests")).anyTimes();
-
- PowerMock.replayAll();
- Whitebox.invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
- PowerMock.verifyAll();
- }
+ CloseableHttpClient clientMock = createMock(CloseableHttpClient.class);
+ HttpResponse httpResponseMock = createMock(HttpResponse.class);
+ expect(HttpsUtils.getHttpClient(30000)).andReturn(clientMock);
+ expect(HttpsUtils.delete(anyObject(HttpDelete.class), anyObject(HashMap.class), anyObject(CloseableHttpClient.class)))
+ .andReturn(httpResponseMock);
+ clientMock.close();
+ expectLastCall();
- @Test
- public void testDaceConfigurationPolling_getDcaeConfigurations_null() throws Exception {
- PowerMock.resetAll();
- thrown.expect(CorrelationException.class);
- PowerMockito.mockStatic(MicroServiceConfig.class);
- when(MicroServiceConfig.getServiceConfigInfoFromCBS("holmes-rule-mgmt"))
- .thenReturn("host");
- PowerMock.createMock(DcaeConfigurationParser.class);
- PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
- .andReturn(null).anyTimes();
-
- PowerMock.replayAll();
- DcaeConfigurations dcaeConfigurations = Whitebox
- .invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
- PowerMock.verifyAll();
-
- assertThat(dcaeConfigurations == null, equalTo(true));
+ expect(HttpsUtils.getHttpClient(30000)).andReturn(clientMock);
+ expect(HttpsUtils.put(anyObject(HttpPut.class), anyObject(HashMap.class), anyObject(HashMap.class),
+ anyObject(StringEntity.class), anyObject(CloseableHttpClient.class)))
+ .andReturn(httpResponseMock);
+ clientMock.close();
+ expectLastCall();
+
+ StatusLine sl = createMock(StatusLine.class);
+ expect(httpResponseMock.getStatusLine()).andReturn(sl);
+ expect(sl.getStatusCode()).andReturn(200);
+
+ replayAll();
+
+ dcaeConfigurationPolling.run();
+
+ verifyAll();
}
@Test
- public void testDaceConfigurationPolling_addAllCorrelationRules_connection_exception()
- throws Exception {
- PowerMock.resetAll();
- thrown.expect(CorrelationException.class);
+ public void run_identical_contents() throws Exception {
DcaeConfigurations dcaeConfigurations = new DcaeConfigurations();
- Rule rule = new Rule("test", "test", "tset",1);
- dcaeConfigurations.getDefaultRules().add(rule);
+ dcaeConfigurations.addDefaultRule(new Rule("test", "clName", "contents", 1));
+ mockStatic(DcaeConfigurationQuery.class);
+ expect(DcaeConfigurationQuery.getDcaeConfigurations(anyObject(String.class))).andReturn(dcaeConfigurations).times(2);
+ DcaeConfigurationPolling dcaeConfigurationPolling = createPartialMock(DcaeConfigurationPolling.class,
+ "getAllCorrelationRules");
+ Whitebox.setInternalState(dcaeConfigurationPolling, "url", "http://127.0.0.1");
- PowerMock.replayAll();
- Whitebox.invokeMethod(daceConfigurationPolling, "addAllCorrelationRules",
- dcaeConfigurations);
- PowerMock.verifyAll();
+ RuleQueryListResponse ruleQueryListResponse = new RuleQueryListResponse();
+ List<RuleResult4API> ruleResult4APIList = new ArrayList<RuleResult4API>(){
+ {
+ add(new RuleResult4API());
+ }
+ };
+ ruleQueryListResponse.setCorrelationRules(ruleResult4APIList);
+ ruleQueryListResponse.setTotalCount(ruleResult4APIList.size());
+ expect(dcaeConfigurationPolling.getAllCorrelationRules()).andReturn(ruleQueryListResponse);
+
+ CloseableHttpClient clientMock = createMock(CloseableHttpClient.class);
+ HttpResponse httpResponseMock = createMock(HttpResponse.class);
+ expect(HttpsUtils.getHttpClient(30000)).andReturn(clientMock);
+ expect(HttpsUtils.delete(anyObject(HttpDelete.class), anyObject(HashMap.class), anyObject(CloseableHttpClient.class)))
+ .andReturn(httpResponseMock);
+ clientMock.close();
+ expectLastCall();
+
+ expect(HttpsUtils.getHttpClient(30000)).andReturn(clientMock);
+ expect(HttpsUtils.put(anyObject(HttpPut.class), anyObject(HashMap.class), anyObject(HashMap.class),
+ anyObject(StringEntity.class), anyObject(CloseableHttpClient.class)))
+ .andReturn(httpResponseMock);
+ clientMock.close();
+ expectLastCall();
+
+ StatusLine sl = createMock(StatusLine.class);
+ expect(httpResponseMock.getStatusLine()).andReturn(sl);
+ expect(sl.getStatusCode()).andReturn(200);
+
+ replayAll();
+
+ dcaeConfigurationPolling.run();
+ dcaeConfigurationPolling.run();
+
+ verifyAll();
}
+
+
@Test
- public void testDaceConfigurationPolling_getRuleCreateRequest() throws Exception {
- PowerMock.resetAll();
- Rule rule = new Rule("test", "test1", "stest",1);
- PowerMock.replayAll();
- RuleCreateRequest actual = Whitebox
- .invokeMethod(daceConfigurationPolling, "getRuleCreateRequest", rule);
- PowerMock.verifyAll();
-
- assertThat(actual.getRuleName(), equalTo("test"));
- assertThat(actual.getLoopControlName(), equalTo("test1"));
- assertThat(actual.getContent(), equalTo("stest"));
- assertThat(actual.getDescription(), equalTo(""));
- assertThat(actual.getEnabled(), equalTo(1));
+ public void getAllCorrelationRules() throws Exception {
+ Map<String, Object> responseObj = new HashMap(){
+ {
+ put("correlationRules", new ArrayList<String>());
+ put("totalCount", 0);
+ }
+ };
+
+ CloseableHttpClient clientMock = createMock(CloseableHttpClient.class);
+ HttpResponse httpResponseMock = createMock(HttpResponse.class);
+ expect(HttpsUtils.getHttpClient(30000)).andReturn(clientMock);
+ expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(HashMap.class), anyObject(CloseableHttpClient.class)))
+ .andReturn(httpResponseMock);
+ expect(HttpsUtils.extractResponseEntity(httpResponseMock)).andReturn(JSONObject.toJSONString(responseObj));
+ clientMock.close();
+ expectLastCall();
+
+ replayAll();
+ DcaeConfigurationPolling daceConfigurationPolling = new DcaeConfigurationPolling("holmes-rule-mgmt");
+ RuleQueryListResponse response = daceConfigurationPolling.getAllCorrelationRules();
+ assertThat(response.getTotalCount(), is(0));
+ verifyAll();
+ }
+
+ @Before
+ public void setUp() {
+ mockStatic(HttpsUtils.class);
+ }
+
+ @After
+ public void tearDown() {
+ resetAll();
}
} \ No newline at end of file