summaryrefslogtreecommitdiffstats
path: root/rulemgt
diff options
context:
space:
mode:
Diffstat (limited to 'rulemgt')
-rw-r--r--rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java63
-rw-r--r--rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java5
-rw-r--r--rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java37
3 files changed, 73 insertions, 32 deletions
diff --git a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java
index 6267cff..b96303a 100644
--- a/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java
+++ b/rulemgt/src/main/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineService.java
@@ -17,18 +17,19 @@ package org.openo.holmes.rulemgt.bolt.enginebolt;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.inject.Inject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
-import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jvnet.hk2.annotations.Service;
import org.openo.holmes.common.exception.CorrelationException;
@@ -44,48 +45,60 @@ public class EngineService {
@Inject
private RuleAppConfig ruleAppConfig;
- protected HttpResponse delete(String packageName) throws Exception {
+ protected HttpResponse delete(String packageName) throws IOException {
return deleteRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH + "/" + packageName);
}
- protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine) throws Exception {
+ protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine)
+ throws IOException {
ObjectMapper mapper = new ObjectMapper();
String content = mapper.writeValueAsString(correlationCheckRule4Engine);
return postRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content);
}
- protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws Exception {
+ protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String content = mapper.writeValueAsString(correlationDeployRule4Engine);
return putRequest(ruleAppConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH, content);
}
- private HttpResponse postRequest(String url, String content) throws Exception {
- HttpClient httpClient = HttpClients.createDefault();
- HttpPost httpPost = new HttpPost(url);
- setHeader(httpPost);
- if (StringUtils.isNotEmpty(content)) {
- httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
+ private HttpResponse postRequest(String url, String content) throws IOException {
+ CloseableHttpClient httpClient = HttpClients.createDefault();
+ try {
+ HttpPost httpPost = new HttpPost(url);
+ setHeader(httpPost);
+ if (StringUtils.isNotEmpty(content)) {
+ httpPost.setEntity(new ByteArrayEntity(content.getBytes()));
+ }
+ return httpClient.execute(httpPost);
+ } finally {
+ httpClient.close();
}
- return httpClient.execute(httpPost);
}
- private HttpResponse putRequest(String url, String content)
- throws Exception {
- HttpClient httpClient = HttpClients.createDefault();
- HttpPut httpPut = new HttpPut(url);
- setHeader(httpPut);
- if (StringUtils.isNotEmpty(content)) {
- httpPut.setEntity(new ByteArrayEntity(content.getBytes()));
+ private HttpResponse putRequest(String url, String content) throws IOException {
+ CloseableHttpClient httpClient = HttpClients.createDefault();
+ try {
+ HttpPut httpPut = new HttpPut(url);
+ setHeader(httpPut);
+ if (StringUtils.isNotEmpty(content)) {
+ httpPut.setEntity(new ByteArrayEntity(content.getBytes()));
+ }
+ return httpClient.execute(httpPut);
+ } finally {
+ httpClient.close();
}
- return httpClient.execute(httpPut);
}
- private HttpResponse deleteRequest(String url) throws Exception {
- HttpClient httpClient = HttpClients.createDefault();
- HttpDelete httpDelete = new HttpDelete(url);
- setHeader(httpDelete);
- return httpClient.execute(httpDelete);
+ private HttpResponse deleteRequest(String url) throws IOException {
+ CloseableHttpClient httpClient = HttpClients.createDefault();
+ try {
+ HttpDelete httpDelete = new HttpDelete(url);
+ setHeader(httpDelete);
+ return httpClient.execute(httpDelete);
+ } finally {
+ httpClient.close();
+ }
}
private void setHeader(HttpRequestBase httpRequestBase) {
@@ -94,7 +107,7 @@ public class EngineService {
httpRequestBase.setHeader("Content-Type", "application/json");
}
- public byte[] getData(HttpEntity httpEntity) throws Exception {
+ public byte[] getData(HttpEntity httpEntity) throws IOException {
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bufferedHttpEntity.writeTo(byteArrayOutputStream);
diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java
index 0c2f3c1..4f35b94 100644
--- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java
+++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/RuleActiveAppTest.java
@@ -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
diff --git a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java
index d911f9c..596a408 100644
--- a/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java
+++ b/rulemgt/src/test/java/org/openo/holmes/rulemgt/bolt/enginebolt/EngineServiceTest.java
@@ -18,15 +18,15 @@
package org.openo.holmes.rulemgt.bolt.enginebolt;
-import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
-import org.apache.http.client.HttpClient;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.openo.holmes.rulemgt.RuleAppConfig;
+import org.openo.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;
+import org.openo.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;
import org.powermock.api.easymock.PowerMock;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.powermock.reflect.Whitebox;
@@ -38,18 +38,18 @@ public class EngineServiceTest {
@Rule
public PowerMockRule powerMockRule = new PowerMockRule();
private EngineService engineService;
- private HttpEntity httpEntityMock;
private HttpResponse httpResponseMock;
- private HttpClient httpClient;
private RuleAppConfig ruleAppConfig = new RuleAppConfig();
+ private CorrelationDeployRule4Engine correlationDeployRule4Engine;
@Before
public void setUp() {
engineService = new EngineService();
- httpEntityMock = PowerMock.createMock(HttpEntity.class);
httpResponseMock = PowerMock.createMock(HttpResponse.class);
- httpClient = PowerMock.createMock(HttpClient.class);
Whitebox.setInternalState(engineService, "ruleAppConfig", ruleAppConfig);
+ correlationDeployRule4Engine = new CorrelationDeployRule4Engine();
+ correlationDeployRule4Engine.setContent("{\"package\":\"test\"}");
+ correlationDeployRule4Engine.setEngineId("engine_id");
}
@Test
@@ -61,4 +61,29 @@ public class EngineServiceTest {
PowerMock.verifyAll();
}
+
+ @Test
+ public void delete_exception() throws Exception {
+ thrown.expect(Exception.class);
+
+ engineService.delete("test");
+
+ }
+
+ @Test
+ public void deploy_exception() throws Exception {
+
+ thrown.expect(Exception.class);
+
+ engineService.deploy(correlationDeployRule4Engine);
+
+ }
+
+ @Test
+ public void check_normal() throws Exception {
+ thrown.expect(Exception.class);
+
+ engineService.check(new CorrelationCheckRule4Engine());
+
+ }
} \ No newline at end of file