From 7548eb9fafc5aab6f2c780bc870f69d5cc1e55de Mon Sep 17 00:00:00 2001 From: fanfuxiaoran <495538672@qq.com> Date: Tue, 11 Mar 2014 17:25:26 +0800 Subject: [PATCH] finish plugin api --- .../bench4q/master/api/BaseController.java | 4 +- .../master/domain/entity/plugin/Plugin.java | 11 ++-- .../master/domain/factory/PluginFactory.java | 53 +++++++++++++++++-- .../domain/repository/PluginRepository.java | 3 ++ .../master/domain/service/PluginService.java | 32 +++++++---- .../test/service/Test_PluginService.java | 51 +++++++++++++----- 6 files changed, 120 insertions(+), 34 deletions(-) diff --git a/src/main/java/org/bench4q/master/api/BaseController.java b/src/main/java/org/bench4q/master/api/BaseController.java index 9e3c07aa..c361628d 100644 --- a/src/main/java/org/bench4q/master/api/BaseController.java +++ b/src/main/java/org/bench4q/master/api/BaseController.java @@ -6,7 +6,6 @@ import org.apache.log4j.Logger; import org.bench4q.master.domain.entity.User; import org.bench4q.master.domain.service.auth.AuthenticationManager; import org.bench4q.master.exception.Bench4QException; -import org.bench4q.master.exception.ExceptionLog; import org.bench4q.share.models.ErrorResponseModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -44,6 +43,7 @@ public abstract class BaseController { @ResponseStatus(value = HttpStatus.BAD_REQUEST) @ResponseBody public ErrorResponseModel handleException(Bench4QException e) { + logger.error(e,e.fillInStackTrace()); return ErrorResponseModel.buildErrorResponse(e.getCode(), e.getMessage(), e.getResource()); } @@ -52,7 +52,7 @@ public abstract class BaseController { @ResponseStatus(value = HttpStatus.BAD_REQUEST) @ResponseBody public ErrorResponseModel handleException(RuntimeException e) { - logger.info(ExceptionLog.getStackTrace(e)); + logger.error(e,e.fillInStackTrace()); return ErrorResponseModel.buildErrorResponse("RUNTIME_EXCEPTION", e.getMessage(), ""); } diff --git a/src/main/java/org/bench4q/master/domain/entity/plugin/Plugin.java b/src/main/java/org/bench4q/master/domain/entity/plugin/Plugin.java index c7d467d7..437e0916 100644 --- a/src/main/java/org/bench4q/master/domain/entity/plugin/Plugin.java +++ b/src/main/java/org/bench4q/master/domain/entity/plugin/Plugin.java @@ -17,7 +17,7 @@ import javax.persistence.Table; public class Plugin { private int id; private String name; - private Set methods; + private Set pluginMethods; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -39,13 +39,14 @@ public class Plugin { this.name = name; } + @OneToMany(mappedBy = "plugin", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER) - public Set getMethods() { - return methods; + public Set getPluginMethods() { + return pluginMethods; } - public void setMethods(Set methods) { - this.methods = methods; + public void setPluginMethods(Set pluginMethods) { + this.pluginMethods = pluginMethods; } } diff --git a/src/main/java/org/bench4q/master/domain/factory/PluginFactory.java b/src/main/java/org/bench4q/master/domain/factory/PluginFactory.java index d1aeaaff..fc6b7ad8 100644 --- a/src/main/java/org/bench4q/master/domain/factory/PluginFactory.java +++ b/src/main/java/org/bench4q/master/domain/factory/PluginFactory.java @@ -1,5 +1,8 @@ package org.bench4q.master.domain.factory; +import java.util.ArrayList; +import java.util.HashSet; + import org.bench4q.master.domain.entity.plugin.Method; import org.bench4q.master.domain.entity.plugin.MethodParam; import org.bench4q.master.domain.entity.plugin.ParamProperty; @@ -13,10 +16,11 @@ public class PluginFactory { public static Plugin createPluginEntity(PluginModel pluginModel) { Plugin plugin = new Plugin(); - if (plugin.getMethods() != null) { + plugin.setPluginMethods(new HashSet()); + if (pluginModel.getMethods() != null) { for (MethodModel methodModel : pluginModel.getMethods()) { - plugin.getMethods() - .add(createMethodEntity(methodModel, plugin)); + plugin.getPluginMethods().add( + createMethodEntity(methodModel, plugin)); } } @@ -28,6 +32,7 @@ public class PluginFactory { private static Method createMethodEntity(MethodModel methodModel, Plugin plugin) { Method method = new Method(); + method.setMethodParams(new HashSet()); method.setName(methodModel.getName()); if (methodModel.getMethodParams() != null) { for (MethodParamModel methodParamModel : methodModel @@ -48,6 +53,7 @@ public class PluginFactory { methodParam.setLable(methodParamModel.getLable()); methodParam.setName(methodParamModel.getName()); methodParam.setSeperator(methodParamModel.getSeperator()); + methodParam.setParamProperties(new ArrayList()); if (methodParamModel.getParamProperties() != null) { for (ParamPropertyModel paramPropertyModel : methodParamModel .getParamProperties()) { @@ -69,4 +75,45 @@ public class PluginFactory { paramProperty.setMethodParam(methodParam); return paramProperty; } + + public static MethodModel extractMethodModel(Method method) { + MethodModel methodModel = new MethodModel(); + methodModel.setName(method.getName()); + if (method.getMethodParams() != null) { + methodModel.setMethodParams(new HashSet()); + for (MethodParam methodParam : method.getMethodParams()) { + methodModel.getMethodParams().add( + extractMethodParamModel(methodParam)); + } + } + return methodModel; + } + + private static MethodParamModel extractMethodParamModel( + MethodParam methodParam) { + MethodParamModel methodParamModel = new MethodParamModel(); + methodParamModel.setEditorType(methodParam.getEditorType()); + methodParamModel.setLable(methodParam.getLable()); + methodParamModel.setName(methodParam.getName()); + methodParamModel.setSeperator(methodParam.getSeperator()); + if (methodParam.getParamProperties() != null) { + methodParamModel + .setParamProperties(new ArrayList()); + for (ParamProperty paramProperty : methodParam.getParamProperties()) { + methodParamModel.getParamProperties().add( + extractParamPropertyModel(paramProperty)); + } + } + return methodParamModel; + } + + private static ParamPropertyModel extractParamPropertyModel( + ParamProperty paramProperty) { + ParamPropertyModel paramPropertyModel = new ParamPropertyModel(); + paramPropertyModel.setKey(paramProperty.getPropertyName()); + paramPropertyModel.setValue(paramProperty.getPropertyValue()); + paramPropertyModel.setValueSeperator(paramProperty.getValueSeperator()); + return paramPropertyModel; + } + } diff --git a/src/main/java/org/bench4q/master/domain/repository/PluginRepository.java b/src/main/java/org/bench4q/master/domain/repository/PluginRepository.java index a51c60e1..6d32fdac 100644 --- a/src/main/java/org/bench4q/master/domain/repository/PluginRepository.java +++ b/src/main/java/org/bench4q/master/domain/repository/PluginRepository.java @@ -13,6 +13,7 @@ import org.springframework.stereotype.Component; @Component public class PluginRepository extends AbstractRepositoty { public boolean attatch(Plugin plugin) throws Bench4QException { + Session session = this.getSessionHelper().openSession(); Transaction transaction = session.beginTransaction(); Plugin pluginExist = null; @@ -23,6 +24,7 @@ public class PluginRepository extends AbstractRepositoty { ; if (pluginExist != null) { logger.info("the plugin exist"); + return false; } session.merge(plugin); @@ -55,6 +57,7 @@ public class PluginRepository extends AbstractRepositoty { .add(Restrictions.eq("name", pluginName)).uniqueResult(); if (pluginExist == null) { logger.info("plugin not exist"); + System.out.println("not exist"); return false; } session.delete(pluginExist); diff --git a/src/main/java/org/bench4q/master/domain/service/PluginService.java b/src/main/java/org/bench4q/master/domain/service/PluginService.java index 6de376cf..442c93bb 100644 --- a/src/main/java/org/bench4q/master/domain/service/PluginService.java +++ b/src/main/java/org/bench4q/master/domain/service/PluginService.java @@ -9,6 +9,9 @@ import org.bench4q.master.domain.entity.plugin.Plugin; import org.bench4q.master.domain.repository.PluginRepository; import org.bench4q.master.exception.Bench4QException; import org.bench4q.master.domain.entity.plugin.Method; +import org.bench4q.master.domain.factory.PluginFactory; +import org.bench4q.share.models.master.plugin.MethodParamModel; +import org.bench4q.share.models.master.plugin.PluginModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -33,8 +36,9 @@ public class PluginService { return this.plugins; } - public boolean addPlugin(Plugin plugin) throws Bench4QException { - return this.getPluginRepository().attatch(plugin); + public boolean addPlugin(PluginModel pluginModel) throws Bench4QException { + return this.getPluginRepository().attatch( + PluginFactory.createPluginEntity(pluginModel)); } public boolean deletePlugin(String pluginName) throws Bench4QException { @@ -58,7 +62,8 @@ public class PluginService { public List getMethodNameInPlugin(String pluginName) { List methodNameList = new ArrayList(); - Set methods = this.getPluginByName(pluginName).getMethods(); + Set methods = this.getPluginByName(pluginName) + .getPluginMethods(); if (methods != null) { for (Method method : methods) { methodNameList.add(method.getName()); @@ -82,15 +87,22 @@ public class PluginService { } - private Set getMethodInPlugin(String pluginName) { - - return this.getPluginByName(pluginName).getMethods(); + public Set getMethodParamModelsInPlugin( + String pluginName, String methodName) throws Bench4QException { + return PluginFactory.extractMethodModel( + this.getMethodInPlugin(pluginName, methodName)) + .getMethodParams(); } - public Set getMethodParams(String pluginName, - String methodName) throws Bench4QException { + private Set getMethodInPlugin(String pluginName) { + + return this.getPluginByName(pluginName).getPluginMethods(); + } + + public Set getMethodParams(String pluginName, String methodName) + throws Bench4QException { Method method = this.getMethodInPlugin(pluginName, methodName); return method.getMethodParams(); } - -} + +} diff --git a/src/test/java/org/bench4q/master/test/service/Test_PluginService.java b/src/test/java/org/bench4q/master/test/service/Test_PluginService.java index 2ba5bce9..60c39cc3 100644 --- a/src/test/java/org/bench4q/master/test/service/Test_PluginService.java +++ b/src/test/java/org/bench4q/master/test/service/Test_PluginService.java @@ -8,12 +8,11 @@ import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.UUID; import javax.xml.bind.JAXBException; import org.apache.commons.io.FileUtils; -import org.bench4q.master.domain.entity.plugin.Plugin; -import org.bench4q.master.domain.factory.PluginFactory; import org.bench4q.master.domain.service.PluginService; import org.bench4q.master.exception.Bench4QException; import org.bench4q.share.helper.MarshalHelper; @@ -21,7 +20,6 @@ import org.bench4q.share.models.master.plugin.MethodModel; import org.bench4q.share.models.master.plugin.MethodParamModel; import org.bench4q.share.models.master.plugin.ParamPropertyModel; import org.bench4q.share.models.master.plugin.PluginModel; -import org.bench4q.share.models.master.plugin.PluginGUI; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -65,9 +63,41 @@ public class Test_PluginService { @Test public void testAddPlugin() throws JAXBException, Bench4QException { - Plugin plugin=PluginFactory.createPluginEntity(createOnePlugin()); + Assert.assertTrue(this.getPluginService().addPlugin(createOnePlugin())); + } + @Test + public void testDeletePlugin() throws Bench4QException { + PluginModel plugin = createOnePlugin(); + plugin.setName("ppp"); Assert.assertTrue(this.getPluginService().addPlugin(plugin)); + Assert.assertTrue(this.getPluginService() + .deletePlugin(plugin.getName())); + + } + + @Test + public void testLoadPluginNameList() throws Bench4QException { + PluginModel plugin = createOnePlugin(); + plugin.setName("ppp"); + Assert.assertTrue(this.getPluginService().addPlugin(plugin)); + List pluginNameList = this.getPluginService() + .getPluginNameList(); + Assert.assertTrue(pluginNameList.size() >0); + Assert.assertTrue(this.getPluginService() + .deletePlugin(plugin.getName())); + } + + @Test + public void testGetMethodNameList() throws Bench4QException { + PluginModel plugin = createOnePlugin(); + plugin.setName("ppp"); + Assert.assertTrue(this.getPluginService().addPlugin(plugin)); + List methodNameList = this.getPluginService() + .getMethodNameInPlugin(plugin.getName()); + Assert.assertTrue(methodNameList.size() == 1); + Assert.assertTrue(this.getPluginService() + .deletePlugin(plugin.getName())); } private PluginModel createOnePlugin() { @@ -85,28 +115,21 @@ public class Test_PluginService { methodParamParam.setSeperator("&"); methodParamParam.setParamProperties(generateParamProperties()); - List methodParams = new ArrayList(); + Set methodParams = new HashSet(); methodParams.add(methodParamParam); methodParams.add(methodParamParam); method.setName("get"); - method.setParams(methodParams); + method.setMethodParams(methodParams); Set methods = new HashSet(); methods.add(method); plugin.setMethods(methods); - plugin.setName("http"); + plugin.setName("http" + UUID.randomUUID()); return plugin; } - private PluginGUI generateOnePluginGUI() { - PluginGUI pluginGUI = new PluginGUI(); - pluginGUI.setPlugin(createOnePlugin()); - return pluginGUI; - - } - private List generateUrlProperties() { ParamPropertyModel paramPropertyUrl = new ParamPropertyModel(); paramPropertyUrl.setKey("size");