-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
303 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
buildscript { | ||
ext { | ||
springBootVersion = '2.0.5.RELEASE' | ||
} | ||
repositories { | ||
mavenCentral() | ||
} | ||
dependencies { | ||
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") | ||
} | ||
} | ||
|
||
apply plugin: 'java' | ||
apply plugin: 'eclipse' | ||
apply plugin: 'org.springframework.boot' | ||
apply plugin: 'io.spring.dependency-management' | ||
|
||
group = 'me.vsadokhin.iot' | ||
version = '0.0.1-SNAPSHOT' | ||
sourceCompatibility = 1.8 | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
def powermock_version = "1.7.4" | ||
def mockito_version = "1.10.19" | ||
|
||
dependencies { | ||
compile project(':data') | ||
compile 'org.springframework.boot:spring-boot-starter-web' | ||
|
||
testCompile 'org.springframework.boot:spring-boot-starter-test' | ||
testCompile("org.powermock:powermock-module-junit4:${powermock_version}") { | ||
exclude module: "junit" | ||
exclude module: "mockito-core" | ||
} | ||
testCompile("org.powermock:powermock-api-mockito:${powermock_version}") { | ||
exclude module: "mockito-all" | ||
exclude module: "mockito-core" | ||
} | ||
testCompile "org.mockito:mockito-core:${mockito_version}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
../gradlew clean bootJar | ||
docker rm -f iot-statistics-api 2>/dev/null | ||
docker run --rm -d --name iot-statistics-api \ | ||
-p 8081:8080 \ | ||
--link iot-cassandra:iot-cassandra \ | ||
-v `pwd`/build/libs/statistics-api-0.0.1-SNAPSHOT.jar:/tmp/statistics-api.jar \ | ||
anapsix/alpine-java:8 java -Dcassandra.contact.points=iot-cassandra -jar /tmp/statistics-api.jar |
14 changes: 14 additions & 0 deletions
14
statistics-api/src/main/java/me/vsadokhin/iot/statistics/api/StatisticsApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package me.vsadokhin.iot.statistics.api; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration; | ||
|
||
@SpringBootApplication(exclude = CassandraDataAutoConfiguration.class) | ||
public class StatisticsApplication { | ||
|
||
public static void main(String... args) { | ||
SpringApplication.run(StatisticsApplication.class, args); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
statistics-api/src/main/java/me/vsadokhin/iot/statistics/api/StatisticsConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package me.vsadokhin.iot.statistics.api; | ||
|
||
import me.vsadokhin.iot.data.MetricRepository; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class StatisticsConfig { | ||
|
||
@Bean | ||
public MetricRepository metricRepository() { | ||
return new MetricRepository(); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
statistics-api/src/main/java/me/vsadokhin/iot/statistics/api/StatisticsResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package me.vsadokhin.iot.statistics.api; | ||
|
||
import me.vsadokhin.iot.data.MetricRepository; | ||
import me.vsadokhin.iot.data.domain.GetStatisticsRequest; | ||
import me.vsadokhin.iot.data.exception.GetStatisticsException; | ||
import me.vsadokhin.iot.data.exception.StorageException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class StatisticsResource { | ||
|
||
private final MetricRepository metricRepository; | ||
|
||
@Autowired | ||
public StatisticsResource(MetricRepository metricRepository) { | ||
this.metricRepository = metricRepository; | ||
} | ||
|
||
@GetMapping("/statistics") | ||
ResponseEntity getStatistics(GetStatisticsRequest getStatisticsRequest) { | ||
try { | ||
float result = metricRepository.getStatistics(getStatisticsRequest); | ||
return new ResponseEntity<>(result, HttpStatus.OK); | ||
} catch (StorageException e) { | ||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); | ||
} catch (GetStatisticsException e) { | ||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST); | ||
} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
statistics-api/src/test/java/me/vsadokhin/iot/statistics/api/StatisticsApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package me.vsadokhin.iot.statistics.api; | ||
|
||
|
||
import static org.powermock.api.mockito.PowerMockito.mockStatic; | ||
import static org.powermock.api.mockito.PowerMockito.verifyStatic; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.springframework.boot.SpringApplication; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest(SpringApplication.class) | ||
public class StatisticsApplicationTest { | ||
|
||
@Test | ||
public void testMain_callSpringApplicationRun() { | ||
// setup | ||
mockStatic(SpringApplication.class); | ||
|
||
// act | ||
StatisticsApplication.main("arg", "1", "2"); | ||
|
||
// verify | ||
verifyStatic(SpringApplication.class); | ||
SpringApplication.run(StatisticsApplication.class, "arg", "1", "2"); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
statistics-api/src/test/java/me/vsadokhin/iot/statistics/api/StatisticsConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package me.vsadokhin.iot.statistics.api; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static org.powermock.api.mockito.PowerMockito.mock; | ||
import static org.powermock.api.mockito.PowerMockito.whenNew; | ||
|
||
import me.vsadokhin.iot.data.MetricRepository; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest(StatisticsConfig.class) | ||
public class StatisticsConfigTest { | ||
|
||
private StatisticsConfig statisticsConfig; | ||
|
||
@Before | ||
public void setUp() { | ||
statisticsConfig = new StatisticsConfig(); | ||
} | ||
|
||
@Test | ||
public void getMetricRepository() throws Exception { | ||
// setup | ||
MetricRepository mockMetricRepository = mock(MetricRepository.class); | ||
whenNew(MetricRepository.class).withNoArguments().thenReturn(mockMetricRepository); | ||
|
||
// act | ||
MetricRepository result = statisticsConfig.metricRepository(); | ||
|
||
// verify | ||
assertThat(result, is(mockMetricRepository)); | ||
} | ||
|
||
} |
115 changes: 115 additions & 0 deletions
115
statistics-api/src/test/java/me/vsadokhin/iot/statistics/api/StatisticsResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package me.vsadokhin.iot.statistics.api; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.powermock.api.mockito.PowerMockito.when; | ||
|
||
import me.vsadokhin.iot.data.MetricRepository; | ||
import me.vsadokhin.iot.data.domain.GetStatisticsRequest; | ||
import me.vsadokhin.iot.data.exception.GetStatisticsException; | ||
import me.vsadokhin.iot.data.exception.StorageException; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
public class StatisticsResourceTest { | ||
|
||
private StatisticsResource statisticsResource; | ||
private MetricRepository mockMetricRepository; | ||
|
||
@SuppressWarnings("unchecked") | ||
@Before | ||
public void setUp() { | ||
mockMetricRepository = mock(MetricRepository.class); | ||
statisticsResource = new StatisticsResource(mockMetricRepository); | ||
} | ||
|
||
@Test | ||
public void get_callMetricRepositoryGetStatistics() { | ||
// setup | ||
GetStatisticsRequest mockGetStatisticsRequest = mock(GetStatisticsRequest.class); | ||
|
||
// act | ||
statisticsResource.getStatistics(mockGetStatisticsRequest); | ||
|
||
// verify | ||
verify(mockMetricRepository).getStatistics(mockGetStatisticsRequest); | ||
} | ||
|
||
@Test | ||
public void get_checkResultBody() { | ||
// setup | ||
float value = 1.23F; | ||
when(mockMetricRepository.getStatistics(any())).thenReturn(value); | ||
|
||
// act | ||
ResponseEntity result = statisticsResource.getStatistics(new GetStatisticsRequest()); | ||
|
||
// verify | ||
assertThat(result.getBody(), is(value)); | ||
} | ||
|
||
@Test | ||
public void get_checkResultStatusCode() { | ||
// act | ||
ResponseEntity result = statisticsResource.getStatistics(new GetStatisticsRequest()); | ||
|
||
// verify | ||
assertThat(result.getStatusCode(), is(HttpStatus.OK)); | ||
} | ||
|
||
@Test | ||
public void get_metricRepositoryThrowsStorageException_checkResultBody() { | ||
// setup | ||
String exceptionMessage = "Something bad happen"; | ||
when(mockMetricRepository.getStatistics(any())).thenThrow(new StorageException(exceptionMessage, null)); | ||
|
||
// act | ||
ResponseEntity result = statisticsResource.getStatistics(new GetStatisticsRequest()); | ||
|
||
// verify | ||
assertThat(result.getBody(), is(exceptionMessage)); | ||
} | ||
|
||
@Test | ||
public void get_metricRepositoryThrowsStorageException_checkResultStatusCode() { | ||
// setup | ||
when(mockMetricRepository.getStatistics(any())).thenThrow(new StorageException("message", null)); | ||
|
||
// act | ||
ResponseEntity result = statisticsResource.getStatistics(new GetStatisticsRequest()); | ||
|
||
// verify | ||
assertThat(result.getStatusCode(), is(HttpStatus.INTERNAL_SERVER_ERROR)); | ||
} | ||
|
||
@Test | ||
public void get_metricRepositoryThrowsGetStatisticsException_checkResultBody() { | ||
// setup | ||
String exceptionMessage = "Something bad happen"; | ||
when(mockMetricRepository.getStatistics(any())).thenThrow(new GetStatisticsException(exceptionMessage)); | ||
|
||
// act | ||
ResponseEntity result = statisticsResource.getStatistics(new GetStatisticsRequest()); | ||
|
||
// verify | ||
assertThat(result.getBody(), is(exceptionMessage)); | ||
} | ||
|
||
@Test | ||
public void get_metricRepositoryThrowsGetStatisticsException_checkResultStatusCode() { | ||
// setup | ||
when(mockMetricRepository.getStatistics(any())).thenThrow(new GetStatisticsException("error!")); | ||
|
||
// act | ||
ResponseEntity result = statisticsResource.getStatistics(new GetStatisticsRequest()); | ||
|
||
// verify | ||
assertThat(result.getStatusCode(), is(HttpStatus.BAD_REQUEST)); | ||
} | ||
|
||
} |