Merge "Add Rule Management Standalone"
authorGuangrong Fu <fu.guangrong@zte.com.cn>
Tue, 28 Feb 2017 02:56:56 +0000 (02:56 +0000)
committerGerrit Code Review <gerrit@open-o.org>
Tue, 28 Feb 2017 02:56:56 +0000 (02:56 +0000)
rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java
rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java
rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java

index 6267cff..b96303a 100644 (file)
@@ -17,18 +17,19 @@ package org.openo.holmes.rulemgt.bolt.enginebolt;
 \r
 import com.fasterxml.jackson.databind.ObjectMapper;\r
 import java.io.ByteArrayOutputStream;\r
+import java.io.IOException;\r
 import java.io.UnsupportedEncodingException;\r
 import javax.inject.Inject;\r
 import org.apache.commons.lang3.StringUtils;\r
 import org.apache.http.HttpEntity;\r
 import org.apache.http.HttpResponse;\r
-import org.apache.http.client.HttpClient;\r
 import org.apache.http.client.methods.HttpDelete;\r
 import org.apache.http.client.methods.HttpPost;\r
 import org.apache.http.client.methods.HttpPut;\r
 import org.apache.http.client.methods.HttpRequestBase;\r
 import org.apache.http.entity.BufferedHttpEntity;\r
 import org.apache.http.entity.ByteArrayEntity;\r
+import org.apache.http.impl.client.CloseableHttpClient;\r
 import org.apache.http.impl.client.HttpClients;\r
 import org.jvnet.hk2.annotations.Service;\r
 import org.openo.holmes.common.exception.CorrelationException;\r
@@ -44,48 +45,60 @@ public class EngineService {
     @Inject\r
     private RuleAppConfig ruleAppConfig;\r
 \r
-    protected HttpResponse delete(String packageName) throws Exception {\r
+    protected HttpResponse delete(String packageName) throws IOException {\r
         return deleteRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH + "/" + packageName);\r
     }\r
 \r
-    protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine) throws Exception {\r
+    protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine)\r
+            throws IOException {\r
         ObjectMapper mapper = new ObjectMapper();\r
         String content = mapper.writeValueAsString(correlationCheckRule4Engine);\r
         return postRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content);\r
     }\r
 \r
-    protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws Exception {\r
+    protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException {\r
         ObjectMapper mapper = new ObjectMapper();\r
         String content = mapper.writeValueAsString(correlationDeployRule4Engine);\r
         return putRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content);\r
     }\r
 \r
-    private HttpResponse postRequest(String url, String content) throws Exception {\r
-        HttpClient httpClient = HttpClients.createDefault();\r
-        HttpPost httpPost = new HttpPost(url);\r
-        setHeader(httpPost);\r
-        if (StringUtils.isNotEmpty(content)) {\r
-            httpPost.setEntity(new ByteArrayEntity(content.getBytes()));\r
+    private HttpResponse postRequest(String url, String content) throws IOException {\r
+        CloseableHttpClient httpClient = HttpClients.createDefault();\r
+        try {\r
+            HttpPost httpPost = new HttpPost(url);\r
+            setHeader(httpPost);\r
+            if (StringUtils.isNotEmpty(content)) {\r
+                httpPost.setEntity(new ByteArrayEntity(content.getBytes()));\r
+            }\r
+            return httpClient.execute(httpPost);\r
+        } finally {\r
+            httpClient.close();\r
         }\r
-        return httpClient.execute(httpPost);\r
     }\r
 \r
-    private HttpResponse putRequest(String url, String content)\r
-            throws Exception {\r
-        HttpClient httpClient = HttpClients.createDefault();\r
-        HttpPut httpPut = new HttpPut(url);\r
-        setHeader(httpPut);\r
-        if (StringUtils.isNotEmpty(content)) {\r
-            httpPut.setEntity(new ByteArrayEntity(content.getBytes()));\r
+    private HttpResponse putRequest(String url, String content) throws IOException {\r
+        CloseableHttpClient httpClient = HttpClients.createDefault();\r
+        try {\r
+            HttpPut httpPut = new HttpPut(url);\r
+            setHeader(httpPut);\r
+            if (StringUtils.isNotEmpty(content)) {\r
+                httpPut.setEntity(new ByteArrayEntity(content.getBytes()));\r
+            }\r
+            return httpClient.execute(httpPut);\r
+        } finally {\r
+            httpClient.close();\r
         }\r
-        return httpClient.execute(httpPut);\r
     }\r
 \r
-    private HttpResponse deleteRequest(String url) throws Exception {\r
-        HttpClient httpClient = HttpClients.createDefault();\r
-        HttpDelete httpDelete = new HttpDelete(url);\r
-        setHeader(httpDelete);\r
-        return httpClient.execute(httpDelete);\r
+    private HttpResponse deleteRequest(String url) throws IOException {\r
+        CloseableHttpClient httpClient = HttpClients.createDefault();\r
+        try {\r
+            HttpDelete httpDelete = new HttpDelete(url);\r
+            setHeader(httpDelete);\r
+            return httpClient.execute(httpDelete);\r
+        } finally {\r
+            httpClient.close();\r
+        }\r
     }\r
 \r
     private void setHeader(HttpRequestBase httpRequestBase) {\r
@@ -94,7 +107,7 @@ public class EngineService {
         httpRequestBase.setHeader("Content-Type", "application/json");\r
     }\r
 \r
-    public byte[] getData(HttpEntity httpEntity) throws Exception {\r
+    public byte[] getData(HttpEntity httpEntity) throws IOException {\r
         BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);\r
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();\r
         bufferedHttpEntity.writeTo(byteArrayOutputStream);\r
index 0c2f3c1..4f35b94 100644 (file)
@@ -19,15 +19,18 @@ package org.openo.holmes.rulemgt;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.core.IsEqual.equalTo;
 
+import org.junit.Test;
+
 public class RuleActiveAppTest {
 
+    @Test
     public void getName() throws Exception {
         RuleActiveApp app = new RuleActiveApp();
         assertThat(app.getName(), equalTo("Holmes Rule Management ActiveApp APP "));
     }
 
     public static void main(String[] args) throws Exception {
-        String filePath = "E:\\code\\OES_Analytics_FM_Relation\\correlation-mgt\\rulemgt-standalone\\src\\assembly\\resource\\conf\\correlation-rule.yml";
+        String filePath = "C:\\correlation-rule.yml";
         new RuleActiveApp().run(new String[]{"server", filePath});
     }
 }
\ No newline at end of file
index d911f9c..596a408 100644 (file)
 package org.openo.holmes.rulemgt.bolt.enginebolt;\r
 \r
 \r
-import org.apache.http.HttpEntity;\r
 import org.apache.http.HttpResponse;\r
-import org.apache.http.client.HttpClient;\r
 import org.easymock.EasyMock;\r
 import org.junit.Before;\r
 import org.junit.Rule;\r
 import org.junit.Test;\r
 import org.junit.rules.ExpectedException;\r
 import org.openo.holmes.rulemgt.RuleAppConfig;\r
+import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
+import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
 import org.powermock.api.easymock.PowerMock;\r
 import org.powermock.modules.junit4.rule.PowerMockRule;\r
 import org.powermock.reflect.Whitebox;\r
@@ -38,18 +38,18 @@ public class EngineServiceTest {
     @Rule\r
     public PowerMockRule powerMockRule = new PowerMockRule();\r
     private EngineService engineService;\r
-    private HttpEntity httpEntityMock;\r
     private HttpResponse httpResponseMock;\r
-    private HttpClient httpClient;\r
     private RuleAppConfig ruleAppConfig = new RuleAppConfig();\r
+    private CorrelationDeployRule4Engine correlationDeployRule4Engine;\r
 \r
     @Before\r
     public void setUp() {\r
         engineService = new EngineService();\r
-        httpEntityMock = PowerMock.createMock(HttpEntity.class);\r
         httpResponseMock = PowerMock.createMock(HttpResponse.class);\r
-        httpClient = PowerMock.createMock(HttpClient.class);\r
         Whitebox.setInternalState(engineService, "ruleAppConfig", ruleAppConfig);\r
+        correlationDeployRule4Engine = new CorrelationDeployRule4Engine();\r
+        correlationDeployRule4Engine.setContent("{\"package\":\"test\"}");\r
+        correlationDeployRule4Engine.setEngineId("engine_id");\r
     }\r
 \r
     @Test\r
@@ -61,4 +61,29 @@ public class EngineServiceTest {
 \r
         PowerMock.verifyAll();\r
     }\r
+\r
+    @Test\r
+    public void delete_exception() throws Exception {\r
+        thrown.expect(Exception.class);\r
+\r
+        engineService.delete("test");\r
+\r
+    }\r
+\r
+    @Test\r
+    public void deploy_exception() throws Exception {\r
+\r
+        thrown.expect(Exception.class);\r
+\r
+        engineService.deploy(correlationDeployRule4Engine);\r
+\r
+    }\r
+\r
+    @Test\r
+    public void check_normal() throws Exception {\r
+        thrown.expect(Exception.class);\r
+\r
+        engineService.check(new CorrelationCheckRule4Engine());\r
+\r
+    }\r
 }
\ No newline at end of file