From 1d07c618a9c9cd15a11e2681f8ff7a98fc833cae Mon Sep 17 00:00:00 2001 From: Zellweger Christof Ralf Date: Thu, 30 Jul 2015 12:16:31 +0200 Subject: [PATCH] ATEST-124: - implement the query validator --- build.gradle | 8 ++- .../daq/queryrest/config/QueryRestConfig.java | 7 +++ .../controller/QueryRestController.java | 27 +++++---- .../queryrest/controller/QueryValidator.java | 57 +++++++++++++++++++ .../test/queryrest/AbstractDaqRestTest.java | 7 +-- .../daq/test/queryrest/DaqWebMvcConfig.java | 7 +++ .../controller/DaqRestControllerTest.java | 8 ++- 7 files changed, 98 insertions(+), 23 deletions(-) create mode 100644 src/main/java/ch/psi/daq/queryrest/controller/QueryValidator.java diff --git a/build.gradle b/build.gradle index 1e81a95..c46ec1f 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,11 @@ applicationDefaultJvmArgs = [ dependencies { compile (project(':ch.psi.daq.query')) - compile libraries.spring_boot_starter_web + compile 'org.hibernate:hibernate-validator:5.2.0.Final' + + compile(libraries.spring_boot_starter_web) { + exclude group: 'org.slf4j', module: 'log4j-over-slf4j' + } compile libraries.commons_lang testCompile libraries.spring_boot_starter_test @@ -39,5 +43,5 @@ uploadArchives { } task dropItQueryREST(dependsOn: build) << { - exec{ executable "curl"; args "-X", "POST", "-F", "file=@build/libs/ch.psi.daq.queryrest-" + version + ".jar", "http://dropit.psi.ch:8080"; } + exec{ executable "curl"; args "-X", "POST", "-F", "file=@build/libs/ch.psi.daq.queryrest-" + version + ".jar", "http://dropit.psi.ch:8080/upload"; } } \ No newline at end of file diff --git a/src/main/java/ch/psi/daq/queryrest/config/QueryRestConfig.java b/src/main/java/ch/psi/daq/queryrest/config/QueryRestConfig.java index 1e09fac..0c85ba1 100644 --- a/src/main/java/ch/psi/daq/queryrest/config/QueryRestConfig.java +++ b/src/main/java/ch/psi/daq/queryrest/config/QueryRestConfig.java @@ -15,6 +15,7 @@ import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; +import org.springframework.validation.Validator; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonFactory; @@ -33,6 +34,7 @@ import ch.psi.daq.query.model.AbstractQuery; import ch.psi.daq.query.model.Aggregation; import ch.psi.daq.query.model.Query; import ch.psi.daq.query.model.QueryField; +import ch.psi.daq.queryrest.controller.QueryValidator; import ch.psi.daq.queryrest.model.PropertyFilterMixin; import ch.psi.daq.queryrest.response.JsonStreamSerializer; import ch.psi.daq.queryrest.response.ResponseStreamWriter; @@ -140,6 +142,11 @@ public class QueryRestConfig { return defaultResponseAggregations; } + + @Bean + public Validator queryValidator() { + return new QueryValidator(); + } // ========================================================================================== // TODO: This is simply for initial / rudimentary testing - remove once further evolved diff --git a/src/main/java/ch/psi/daq/queryrest/controller/QueryRestController.java b/src/main/java/ch/psi/daq/queryrest/controller/QueryRestController.java index b243427..d8093f4 100644 --- a/src/main/java/ch/psi/daq/queryrest/controller/QueryRestController.java +++ b/src/main/java/ch/psi/daq/queryrest/controller/QueryRestController.java @@ -2,8 +2,6 @@ package ch.psi.daq.queryrest.controller; import java.io.IOException; import java.util.Collection; -import java.util.LinkedHashSet; -import java.util.LinkedList; import java.util.Map.Entry; import java.util.Set; import java.util.function.Function; @@ -11,6 +9,7 @@ import java.util.stream.Stream; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; +import javax.validation.Valid; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -86,16 +85,16 @@ public class QueryRestController { @RequestMapping( value = QUERY, method = RequestMethod.POST, - consumes = {MediaType.APPLICATION_JSON_VALUE}, - produces = {MediaType.APPLICATION_JSON_VALUE}) - public void executeQuery(@RequestBody AbstractQuery query, HttpServletResponse res) throws IOException { + consumes = {MediaType.APPLICATION_JSON_VALUE}) +// , produces = {MediaType.APPLICATION_JSON_VALUE}) + public void executeQuery(@RequestBody @Valid AbstractQuery query, HttpServletResponse res) throws IOException { try { LOGGER.debug("Execute query '{}'", query.getClass().getSimpleName()); QueryAnalyzer queryAnalizer = queryAnalizerFactory.apply(query); queryAnalizer.validate(); - extendQuery(query); +// extendQuery(query); // all the magic happens here Stream>> channelToDataEvents = queryProcessor.process(queryAnalizer); @@ -111,14 +110,14 @@ public class QueryRestController { } } - private void extendQuery(AbstractQuery query) { - if (query.getFields() == null || query.getFields().isEmpty()) { - query.setFields(new LinkedHashSet<>(defaultResponseFields)); - } - if(query.getAggregations() == null || query.getAggregations().isEmpty()){ - query.setAggregations(new LinkedList<>(defaultResponseAggregations)); - } - } +// private void extendQuery(AbstractQuery query) { +// if (query.getFields() == null || query.getFields().isEmpty()) { +// query.setFields(new LinkedHashSet<>(defaultResponseFields)); +// } +// if(query.getAggregations() == null || query.getAggregations().isEmpty()){ +// query.setAggregations(new LinkedList<>(defaultResponseAggregations)); +// } +// } // ========================================================================================== // TODO: This is simply for initial / rudimentary testing - remove once further evolved diff --git a/src/main/java/ch/psi/daq/queryrest/controller/QueryValidator.java b/src/main/java/ch/psi/daq/queryrest/controller/QueryValidator.java new file mode 100644 index 0000000..8bc1705 --- /dev/null +++ b/src/main/java/ch/psi/daq/queryrest/controller/QueryValidator.java @@ -0,0 +1,57 @@ +/** + * + */ +package ch.psi.daq.queryrest.controller; + +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.Set; + +import javax.annotation.Resource; + +import org.springframework.validation.Errors; +import org.springframework.validation.Validator; + +import ch.psi.daq.query.model.AbstractQuery; +import ch.psi.daq.query.model.Aggregation; +import ch.psi.daq.query.model.QueryField; +import ch.psi.daq.queryrest.config.QueryRestConfig; + +/** + * @author zellweger_c + * + */ +public class QueryValidator implements Validator { + + @Resource(name = QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_FIELDS) + private Set defaultResponseFields; + + @Resource(name = QueryRestConfig.BEAN_NAME_DEFAULT_RESPONSE_AGGREGATIONS) + private Set defaultResponseAggregations; + + /** + * {@inheritDoc} + */ + @Override + public boolean supports(Class clazz) { + return AbstractQuery.class.isAssignableFrom(clazz); + } + + /** + * {@inheritDoc} + */ + @Override + public void validate(Object target, Errors errors) { + + AbstractQuery query = (AbstractQuery) target; + + if (query.getFields() == null || query.getFields().isEmpty()) { + query.setFields(new LinkedHashSet<>(defaultResponseFields)); + } + + if (query.getAggregations() == null || query.getAggregations().isEmpty()) { + query.setAggregations(new LinkedList<>(defaultResponseAggregations)); + } + } + +} diff --git a/src/test/java/ch/psi/daq/test/queryrest/AbstractDaqRestTest.java b/src/test/java/ch/psi/daq/test/queryrest/AbstractDaqRestTest.java index 5013654..563a2f9 100644 --- a/src/test/java/ch/psi/daq/test/queryrest/AbstractDaqRestTest.java +++ b/src/test/java/ch/psi/daq/test/queryrest/AbstractDaqRestTest.java @@ -5,9 +5,9 @@ import javax.annotation.Resource; import org.junit.Before; import org.junit.runner.RunWith; import org.mockito.MockitoAnnotations; +import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext.ClassMode; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; @@ -18,17 +18,16 @@ import org.springframework.web.context.WebApplicationContext; import com.fasterxml.jackson.databind.ObjectMapper; +import ch.psi.daq.queryrest.config.QueryRestConfig; import ch.psi.daq.test.cassandra.CassandraDaqUnitDependencyInjectionTestExecutionListener; @TestExecutionListeners({ CassandraDaqUnitDependencyInjectionTestExecutionListener.class, DependencyInjectionTestExecutionListener.class}) -//@SpringApplicationConfiguration(classes = {QueryRestApplication.class, DaqWebMvcConfig.class}) -//@EmbeddedCassandra +@SpringApplicationConfiguration(classes = {QueryRestConfig.class, DaqWebMvcConfig.class}) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) @WebAppConfiguration -@ContextConfiguration(classes = DaqWebMvcConfig.class) @RunWith(SpringJUnit4ClassRunner.class) public abstract class AbstractDaqRestTest { diff --git a/src/test/java/ch/psi/daq/test/queryrest/DaqWebMvcConfig.java b/src/test/java/ch/psi/daq/test/queryrest/DaqWebMvcConfig.java index d6112b7..f70e67e 100644 --- a/src/test/java/ch/psi/daq/test/queryrest/DaqWebMvcConfig.java +++ b/src/test/java/ch/psi/daq/test/queryrest/DaqWebMvcConfig.java @@ -11,6 +11,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupp import ch.psi.daq.cassandra.reader.DataReader; import ch.psi.daq.query.processor.QueryProcessor; import ch.psi.daq.query.processor.cassandra.CassandraQueryProcessorLocal; +import ch.psi.daq.test.cassandra.admin.CassandraAdmin; +import ch.psi.daq.test.cassandra.admin.CassandraAdminImpl; import ch.psi.daq.test.query.config.LocalQueryTestConfig; import ch.psi.daq.test.queryrest.query.DummyDataReader; @@ -36,4 +38,9 @@ public class DaqWebMvcConfig extends WebMvcConfigurationSupport { public DataReader dataReader() { return new DummyDataReader(); } + + @Bean + public CassandraAdmin cassandraAdmin() { + return new CassandraAdminImpl(); + } } \ No newline at end of file diff --git a/src/test/java/ch/psi/daq/test/queryrest/controller/DaqRestControllerTest.java b/src/test/java/ch/psi/daq/test/queryrest/controller/DaqRestControllerTest.java index ac061eb..4cd6a69 100644 --- a/src/test/java/ch/psi/daq/test/queryrest/controller/DaqRestControllerTest.java +++ b/src/test/java/ch/psi/daq/test/queryrest/controller/DaqRestControllerTest.java @@ -116,12 +116,14 @@ public class DaqRestControllerTest extends AbstractDaqRestTest { TEST_CHANNEL_NAMES); String content = mapper.writeValueAsString(request); + System.out.println(content); this.mockMvc - .perform(MockMvcRequestBuilders.post(QueryRestController.QUERY) + .perform( + MockMvcRequestBuilders.post(QueryRestController.QUERY) .contentType(MediaType.APPLICATION_JSON) - .content(content)) - + .content(content) + ) .andDo(MockMvcResultHandlers.print()) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$").isArray())