ATEST-122
This commit is contained in:
@@ -8,9 +8,11 @@ import org.springframework.context.annotation.PropertySources;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
|
||||
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.query.config.LocalQueryTestConfig;
|
||||
import ch.psi.daq.test.queryrest.query.DummyQueryProcessor;
|
||||
import ch.psi.daq.test.queryrest.query.DummyDataReader;
|
||||
|
||||
@Configuration
|
||||
@PropertySources(value = {
|
||||
@@ -27,6 +29,11 @@ public class DaqWebMvcConfig extends WebMvcConfigurationSupport {
|
||||
|
||||
@Bean
|
||||
public QueryProcessor queryProcessor() {
|
||||
return new DummyQueryProcessor();
|
||||
return new CassandraQueryProcessorLocal();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataReader dataReader() {
|
||||
return new DummyDataReader();
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,64 @@
|
||||
package ch.psi.daq.test.queryrest.controller;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
import ch.psi.daq.cassandra.util.test.CassandraDataGen;
|
||||
import ch.psi.daq.common.ordering.Ordering;
|
||||
import ch.psi.daq.query.model.AggregationType;
|
||||
import ch.psi.daq.query.model.PulseRangeQuery;
|
||||
import ch.psi.daq.query.model.TimeRangeQuery;
|
||||
import ch.psi.daq.query.model.TimeRangeQueryDate;
|
||||
import ch.psi.daq.queryrest.controller.QueryRestController;
|
||||
import ch.psi.daq.test.cassandra.admin.CassandraAdmin;
|
||||
import ch.psi.daq.test.queryrest.AbstractDaqRestTest;
|
||||
import ch.psi.daq.test.queryrest.query.DummyQueryProcessor;
|
||||
|
||||
/**
|
||||
* Tests the {@link DaqController} implementation.
|
||||
*/
|
||||
public class DaqRestControllerTest extends AbstractDaqRestTest {
|
||||
|
||||
@Resource
|
||||
private CassandraAdmin cassandraAdmin;
|
||||
|
||||
@Resource
|
||||
private CassandraDataGen dataGen;
|
||||
|
||||
private static final boolean initialized = false;
|
||||
|
||||
private static final int DATA_REPLICATION = 1;
|
||||
public static final String[] TEST_CHANNEL_NAMES = new String[]{"testChannel1", "testChannel2"};
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
if (!initialized) {
|
||||
cassandraAdmin.truncateAll();
|
||||
|
||||
dataGen.writeData(DATA_REPLICATION, 100, 2, TEST_CHANNEL_NAMES);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {}
|
||||
|
||||
@Test
|
||||
public void testPulseRangeQuery() throws Exception {
|
||||
PulseRangeQuery request = new PulseRangeQuery(
|
||||
100,
|
||||
101,
|
||||
DummyQueryProcessor.TEST_CHANNEL_NAMES);
|
||||
TEST_CHANNEL_NAMES);
|
||||
|
||||
String content = mapper.writeValueAsString(request);
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.post("/query")
|
||||
.perform(MockMvcRequestBuilders.post(QueryRestController.QUERY)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(content))
|
||||
|
||||
@@ -51,12 +82,12 @@ public class DaqRestControllerTest extends AbstractDaqRestTest {
|
||||
TimeRangeQuery request = new TimeRangeQuery(
|
||||
100,
|
||||
101,
|
||||
DummyQueryProcessor.TEST_CHANNEL_NAMES);
|
||||
TEST_CHANNEL_NAMES);
|
||||
|
||||
String content = mapper.writeValueAsString(request);
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.post("/query")
|
||||
.perform(MockMvcRequestBuilders.post(QueryRestController.QUERY)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(content))
|
||||
|
||||
@@ -76,18 +107,18 @@ public class DaqRestControllerTest extends AbstractDaqRestTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapper() throws Exception {
|
||||
public void testDateRangeQuery() throws Exception {
|
||||
String startDate = TimeRangeQueryDate.format(100);
|
||||
String endDate = TimeRangeQueryDate.format(101);
|
||||
TimeRangeQueryDate request = new TimeRangeQueryDate(
|
||||
startDate,
|
||||
endDate,
|
||||
DummyQueryProcessor.TEST_CHANNEL_NAMES);
|
||||
TEST_CHANNEL_NAMES);
|
||||
|
||||
String content = mapper.writeValueAsString(request);
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.post("/query")
|
||||
.perform(MockMvcRequestBuilders.post(QueryRestController.QUERY)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(content))
|
||||
|
||||
@@ -105,4 +136,61 @@ public class DaqRestControllerTest extends AbstractDaqRestTest {
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$[1].data[0].pulseId").value(100))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$[1].data[1].pulseId").value(101));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtremaAggregation() throws Exception {
|
||||
PulseRangeQuery request = new PulseRangeQuery(
|
||||
100,
|
||||
101,
|
||||
false,
|
||||
Ordering.asc,
|
||||
AggregationType.extrema,
|
||||
TEST_CHANNEL_NAMES[0]);
|
||||
|
||||
String content = mapper.writeValueAsString(request);
|
||||
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.post(QueryRestController.QUERY)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(content))
|
||||
|
||||
.andDo(MockMvcResultHandlers.print())
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.min").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.min.value").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.min.value").value(100))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.min.event").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.min.event.channel").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.min.event.channel").value(TEST_CHANNEL_NAMES[0]))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.min.event.pulseId").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.min.event.pulseId").value(100))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.maxima").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.max").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.max.value").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.max.value").value(101))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.max.event").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.max.event.channel").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.max.event.channel").value(TEST_CHANNEL_NAMES[0]))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.max.event.pulseId").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$.minima.max.event.pulseId").value(101));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChannelNameQuery() throws Exception {
|
||||
this.mockMvc
|
||||
.perform(MockMvcRequestBuilders.get(QueryRestController.CHANNELS)
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
|
||||
.andDo(MockMvcResultHandlers.print())
|
||||
.andExpect(MockMvcResultMatchers.status().isOk())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$").isArray())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$[0]").value("testChannel1"))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$[1]").exists())
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$[1]").value("testChannel2"))
|
||||
.andExpect(MockMvcResultMatchers.jsonPath("$[2]").doesNotExist());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package ch.psi.daq.test.queryrest.query;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import ch.psi.daq.cassandra.reader.DataReader;
|
||||
import ch.psi.daq.common.ordering.Ordering;
|
||||
import ch.psi.daq.domain.cassandra.ChannelEvent;
|
||||
import ch.psi.daq.domain.cassandra.DataEvent;
|
||||
|
||||
public class DummyDataReader implements DataReader {
|
||||
|
||||
public static final String TEST_CHANNEL_1 = "testChannel1";
|
||||
public static final String TEST_CHANNEL_2 = "testChannel2";
|
||||
public static final List<String> TEST_CHANNEL_NAMES = Lists.newArrayList(TEST_CHANNEL_1, TEST_CHANNEL_2);
|
||||
|
||||
@Override
|
||||
public Stream<String> getChannelStream(String regex) {
|
||||
Stream<String> channelStream = TEST_CHANNEL_NAMES.stream();
|
||||
|
||||
if (regex != null) {
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
channelStream = channelStream.filter(channel -> pattern.matcher(channel).find());
|
||||
}
|
||||
|
||||
return channelStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<? extends DataEvent> getEventStream(String channel, long startPulseId, long endPulseId,
|
||||
Ordering ordering, String... columns) {
|
||||
return getElements(channel, startPulseId, endPulseId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<? extends DataEvent> getEventStream(String channel, long startMillis, long startNanos, long endMillis,
|
||||
long endNanos, Ordering ordering, String... columns) {
|
||||
return getElements(channel, startMillis, endMillis);
|
||||
}
|
||||
|
||||
protected Stream<? extends DataEvent> getElements(String channel, long start, long end) {
|
||||
return LongStream.rangeClosed(start, end).mapToObj(i -> {
|
||||
return new ChannelEvent(
|
||||
channel,
|
||||
i,
|
||||
0,
|
||||
i,
|
||||
i,
|
||||
0,
|
||||
"data_" + UUID.randomUUID().toString());
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package ch.psi.daq.test.queryrest.query;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import ch.psi.daq.domain.cassandra.ChannelEvent;
|
||||
import ch.psi.daq.domain.cassandra.DataEvent;
|
||||
import ch.psi.daq.query.model.Query;
|
||||
import ch.psi.daq.query.processor.QueryProcessor;
|
||||
|
||||
/**
|
||||
* @author zellweger_c
|
||||
*
|
||||
*/
|
||||
public class DummyQueryProcessor implements QueryProcessor {
|
||||
|
||||
public static final List<String> TEST_CHANNEL_NAMES = Lists.newArrayList("testChannel1", "testChannel2");
|
||||
|
||||
@Override
|
||||
public Collection<String> getChannels() {
|
||||
return TEST_CHANNEL_NAMES;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> getChannels(String regex) {
|
||||
if (regex != null) {
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
return TEST_CHANNEL_NAMES.stream().filter(channel -> pattern.matcher(channel).find())
|
||||
.collect(Collectors.toList());
|
||||
} else {
|
||||
return getChannels();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Stream<Entry<String, Stream<? extends DataEvent>>> process(Query query) {
|
||||
|
||||
int nrOfEntries = 2;
|
||||
int startingPulseId = 100;
|
||||
|
||||
Map<String, Stream<? extends DataEvent>> result = Maps.newHashMap();
|
||||
|
||||
TEST_CHANNEL_NAMES.forEach(chName -> {
|
||||
|
||||
List<DataEvent> entities = IntStream.rangeClosed(1, nrOfEntries).boxed().map(i -> {
|
||||
long pulseId = startingPulseId + i - 1;
|
||||
return new ChannelEvent(
|
||||
chName,
|
||||
pulseId,
|
||||
0,
|
||||
pulseId,
|
||||
pulseId,
|
||||
0,
|
||||
"data_" + UUID.randomUUID().toString());
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
result.put(chName, entities.stream());
|
||||
});
|
||||
|
||||
return result.entrySet().stream();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user