Add some UT codes 95/63795/1
authorGuangrongFu <fu.guangrong@zte.com.cn>
Thu, 30 Aug 2018 11:53:49 +0000 (19:53 +0800)
committerGuangrongFu <fu.guangrong@zte.com.cn>
Thu, 30 Aug 2018 11:53:49 +0000 (19:53 +0800)
Change-Id: I477f23f237dad9efbf473545c80e296ed992ae71
Issue-ID: HOLMES-159
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java
rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java

index 2d63bb6..6c0d732 100644 (file)
@@ -1,11 +1,11 @@
 /**
  * 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
- *
+ * <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. See the License for the specific language governing permissions and limitations under
@@ -21,12 +21,14 @@ import com.google.gson.JsonDeserializer;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonParseException;
 import com.google.gson.reflect.TypeToken;
+
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Type;
 import java.util.HashMap;
 import java.util.List;
 import javax.ws.rs.core.MediaType;
+
 import lombok.extern.slf4j.Slf4j;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpDelete;
@@ -68,8 +70,8 @@ public class DcaeConfigurationPolling implements Runnable {
         try {
             dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname);
             String md5 = Md5Util.md5(dcaeConfigurations);
-            if (prevResult && prevConfigMd5.equals(md5)){
-                log.info("Operation aborted due to identical Configurations.");
+            if (prevResult && prevConfigMd5.equals(md5)) {
+                log.info("Operation aborted due to identical configurations.");
                 return;
             }
             prevConfigMd5 = md5;
@@ -104,7 +106,7 @@ public class DcaeConfigurationPolling implements Runnable {
     }
 
     public RuleQueryListResponse getAllCorrelationRules() throws CorrelationException, IOException {
-              HashMap<String, String> headers = new HashMap<>();
+        HashMap<String, String> headers = new HashMap<>();
         headers.put("Content-Type", MediaType.APPLICATION_JSON);
         CloseableHttpClient httpClient = null;
         HttpGet httpGet = new HttpGet(url);
@@ -112,7 +114,7 @@ public class DcaeConfigurationPolling implements Runnable {
             httpClient = HttpsUtils.getHttpClient(HttpsUtils.DEFUALT_TIMEOUT);
             HttpResponse httpResponse = HttpsUtils.get(httpGet, headers, httpClient);
             String response = HttpsUtils.extractResponseEntity(httpResponse);
-            return JSON.parseObject(response,RuleQueryListResponse.class);
+            return JSON.parseObject(response, RuleQueryListResponse.class);
         } finally {
             httpGet.releaseConnection();
             closeHttpClient(httpClient);
@@ -157,8 +159,8 @@ public class DcaeConfigurationPolling implements Runnable {
         return suc;
     }
 
-    private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs){
-        ruleResult4APIs.forEach(correlationRule ->{
+    private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs) {
+        ruleResult4APIs.forEach(correlationRule -> {
             HashMap<String, String> headers = new HashMap<>();
             headers.put("Content-Type", MediaType.APPLICATION_JSON);
             CloseableHttpClient httpClient = null;
index 1037495..52f1e37 100644 (file)
@@ -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.
  */
 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