Skip to content

Commit

Permalink
Added module for statistics api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vsadokhin committed Oct 1, 2018
1 parent e5882a7 commit cb8c4b7
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.nullValue;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.fail;

import java.util.ArrayList;
Expand Down Expand Up @@ -105,7 +106,7 @@ public void sendMetric_checkMetricInDB() {
HttpEntity<Metric> entity = new HttpEntity<>(metric, HEADERS);

// act
ResponseEntity<ResponseEntity> result = REST_TEMPLATE.exchange("http://localhost:8080/metric", HttpMethod.POST, entity, ResponseEntity.class);
ResponseEntity<String> result = REST_TEMPLATE.exchange("http://localhost:8080/metric", HttpMethod.POST, entity, String.class);

// verify
assertThat(result.getStatusCode(), is(HttpStatus.ACCEPTED));
Expand Down Expand Up @@ -146,11 +147,12 @@ private void sendMetric_requiredFieldIsEmpty_checkResponseIsBadRequest_checkNoMe
HttpEntity<Metric> entity = new HttpEntity<>(metric, HEADERS);
try {
// act
REST_TEMPLATE.exchange("http://localhost:8080/metric", HttpMethod.POST, entity, ResponseEntity.class);
REST_TEMPLATE.exchange("http://localhost:8080/metric", HttpMethod.POST, entity, String.class);
// verify
fail();
} catch (HttpClientErrorException e) {
assertThat(e.getRawStatusCode(), is(HttpStatus.BAD_REQUEST.value()));
assertThat(e.getResponseBodyAsString(), containsString("NotNull"));
}
waitNoMetricInDB(metric);
}
Expand Down
22 changes: 22 additions & 0 deletions qa/statistics-api/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apply plugin: 'java'

group = 'me.vsadokhin.iot'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}

test.enabled = project.hasProperty("qa-tests")

test {
systemProperties = System.properties as Map<String, ?>
}

dependencies {
testCompile project(':data')
testCompile 'junit:junit:4.12'
testCompile 'org.springframework:spring-web:5.1.0.RELEASE'
testCompile 'com.fasterxml.jackson.core:jackson-databind:2.9.6'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
package me.vsadokhin.iot.qa.statistics.api;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.datastax.driver.core.Session;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import me.vsadokhin.iot.data.MetricRepository;
import me.vsadokhin.iot.data.MetricTable;
import me.vsadokhin.iot.data.domain.Metric;
import me.vsadokhin.iot.data.domain.MetricBuilder;
import me.vsadokhin.iot.data.utility.CassandraClusterUtility;
import me.vsadokhin.iot.data.utility.CassandraSessionUtility;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

public class GetStatisticsTest {

private static final String ENDPOINT = "http://localhost:8081/statistics";

private static RestTemplate REST_TEMPLATE = new RestTemplate();

private static Session SESSION = CassandraSessionUtility.getSession();

private static final HttpHeaders HEADERS = new HttpHeaders();

private static final MetricRepository METRIC_REPOSITORY = new MetricRepository();

private HttpEntity<Metric> entity;

@BeforeClass
public static void setUpClass() {
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
messageConverters.add(converter);

REST_TEMPLATE.setMessageConverters(messageConverters);
HEADERS.setContentType(MediaType.APPLICATION_JSON);
HEADERS.setBasicAuth("getStatisticsUser", "statistics123");
}

@AfterClass
public static void afterClass() {
CassandraClusterUtility.closeCluster();
}

@Before
public void setUp() {
entity = new HttpEntity<>(HEADERS);
for (MetricTable metricTable : MetricTable.values()) {
SESSION.execute(QueryBuilder.truncate(metricTable.getTable()));
}
}

@Test
public void getStatistics_min_bySensor() {
// setup
Metric metric = new MetricBuilder("sensor1", "type1")
.setValue(Float.MIN_VALUE).setWhen(0L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

metric = new MetricBuilder("sensor1", "type1")
.setValue(1.0F).setWhen(1L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

metric = new MetricBuilder("sensor2", "type1")
.setValue(Float.MIN_VALUE).setWhen(1L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

metric = new MetricBuilder("sensor1", "type1")
.setValue(2.0F).setWhen(2L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

metric = new MetricBuilder("sensor1", "type1")
.setValue(Float.MIN_VALUE).setWhen(3L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

// act
UriComponentsBuilder uirBuilder = UriComponentsBuilder.fromHttpUrl("http://localhost:8081/statistics")
.queryParam("sensorId", "sensor1")
.queryParam("aggregator", "min")
.queryParam("from", 1)
.queryParam("to", 2);
ResponseEntity<Float> result = REST_TEMPLATE.exchange(uirBuilder.toUriString(),
HttpMethod.GET, entity, Float.class);

// verify
assertThat(result.getBody(), is(1.0F));
}

@Test
public void getStatistics_max_bySensor() {
// setup
Metric metric = new MetricBuilder("sensor1", "type1")
.setValue(Float.MAX_VALUE).setWhen(0L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

metric = new MetricBuilder("sensor1", "type1")
.setValue(1.0F).setWhen(1L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

metric = new MetricBuilder("sensor2", "type1")
.setValue(Float.MAX_VALUE).setWhen(1L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

metric = new MetricBuilder("sensor1", "type1")
.setValue(2.0F).setWhen(2L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

metric = new MetricBuilder("sensor1", "type1")
.setValue(Float.MAX_VALUE).setWhen(3L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

// act
UriComponentsBuilder uirBuilder = UriComponentsBuilder.fromHttpUrl(ENDPOINT)
.queryParam("sensorId", "sensor1")
.queryParam("aggregator", "max")
.queryParam("from", 1)
.queryParam("to", 2);
ResponseEntity<Float> result = REST_TEMPLATE.exchange(uirBuilder.toUriString(),
HttpMethod.GET, entity, Float.class);

// verify
assertThat(result.getBody(), is(2.0F));
}

@Test
public void getStatistics_avg_bySensor() {
// setup
Metric metric = new MetricBuilder("sensor1", "type1")
.setValue(123F).setWhen(0L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

metric = new MetricBuilder("sensor1", "type1")
.setValue(1.0F).setWhen(1L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

metric = new MetricBuilder("sensor2", "type1")
.setValue(123F).setWhen(1L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

metric = new MetricBuilder("sensor1", "type1")
.setValue(2.0F).setWhen(2L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

metric = new MetricBuilder("sensor1", "type1")
.setValue(123F).setWhen(3L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_SENSOR);

// act
UriComponentsBuilder uirBuilder = UriComponentsBuilder.fromHttpUrl(ENDPOINT)
.queryParam("sensorId", "sensor1")
.queryParam("aggregator", "avg")
.queryParam("from", 1)
.queryParam("to", 2);
ResponseEntity<Float> result = REST_TEMPLATE.exchange(uirBuilder.toUriString(),
HttpMethod.GET, entity, Float.class);

// verify
assertThat(result.getBody(), is(1.5F));
}

@Test
public void getStatistics_min_byType() {
// setup
Metric metric = new MetricBuilder("sensor1", "type1")
.setValue(Float.MIN_VALUE).setWhen(0L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

metric = new MetricBuilder("sensor2", "type1")
.setValue(1.0F).setWhen(1L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

metric = new MetricBuilder("sensor3", "type2")
.setValue(Float.MIN_VALUE).setWhen(1L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

metric = new MetricBuilder("sensor1", "type1")
.setValue(2.0F).setWhen(2L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

metric = new MetricBuilder("sensor1", "type1")
.setValue(Float.MIN_VALUE).setWhen(3L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

// act
UriComponentsBuilder uirBuilder = UriComponentsBuilder.fromHttpUrl("http://localhost:8081/statistics")
.queryParam("type", "type1")
.queryParam("aggregator", "min")
.queryParam("from", 1)
.queryParam("to", 2);
ResponseEntity<Float> result = REST_TEMPLATE.exchange(uirBuilder.toUriString(),
HttpMethod.GET, entity, Float.class);

// verify
assertThat(result.getBody(), is(1.0F));
}

@Test
public void getStatistics_max_byType() {
// setup
Metric metric = new MetricBuilder("sensor1", "type1")
.setValue(Float.MAX_VALUE).setWhen(0L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

metric = new MetricBuilder("sensor2", "type1")
.setValue(1.0F).setWhen(1L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

metric = new MetricBuilder("sensor3", "type2")
.setValue(Float.MAX_VALUE).setWhen(1L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

metric = new MetricBuilder("sensor1", "type1")
.setValue(2.0F).setWhen(2L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

metric = new MetricBuilder("sensor1", "type1")
.setValue(Float.MAX_VALUE).setWhen(3L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

// act
UriComponentsBuilder uirBuilder = UriComponentsBuilder.fromHttpUrl(ENDPOINT)
.queryParam("type", "type1")
.queryParam("aggregator", "max")
.queryParam("from", 1)
.queryParam("to", 2);
ResponseEntity<Float> result = REST_TEMPLATE.exchange(uirBuilder.toUriString(),
HttpMethod.GET, entity, Float.class);

// verify
assertThat(result.getBody(), is(2.0F));
}

@Test
public void getStatistics_avg_byType() {
// setup
Metric metric = new MetricBuilder("sensor1", "type1")
.setValue(123F).setWhen(0L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

metric = new MetricBuilder("sensor2", "type1")
.setValue(1.0F).setWhen(1L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

metric = new MetricBuilder("sensor3", "type2")
.setValue(123F).setWhen(1L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

metric = new MetricBuilder("sensor1", "type1")
.setValue(2.0F).setWhen(2L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

metric = new MetricBuilder("sensor1", "type1")
.setValue(123F).setWhen(3L).build();
METRIC_REPOSITORY.insert(metric, MetricTable.METRIC_BY_TYPE);

// act
UriComponentsBuilder uirBuilder = UriComponentsBuilder.fromHttpUrl(ENDPOINT)
.queryParam("type", "type1")
.queryParam("aggregator", "avg")
.queryParam("from", 1)
.queryParam("to", 2);
ResponseEntity<Float> result = REST_TEMPLATE.exchange(uirBuilder.toUriString(),
HttpMethod.GET, entity, Float.class);

// verify
assertThat(result.getBody(), is(1.5F));
}

}
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ include ':data',
':stream-consumer',
':statistics-api',
':qa:load',
':qa:receive-api'
':qa:receive-api',
':qa:statistics-api'

0 comments on commit cb8c4b7

Please sign in to comment.