Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels Dommerholt committed Mar 31, 2017
0 parents commit 982f9e7
Show file tree
Hide file tree
Showing 14 changed files with 439 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target/
.idea/
*.iml

125 changes: 125 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.nibado.example</groupId>
<artifactId>cassandra-it</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

<spring.boot.version>1.5.2.RELEASE</spring.boot.version>
<datastax.version>3.1.2</datastax.version>

<lombok.version>1.16.10</lombok.version>

<log4j.version>1.2.17</log4j.version>
<slf4j.version>1.7.13</slf4j.version>
<logback.version>1.1.3</logback.version>

<junit.version>4.12</junit.version>
<mockito.version>1.9.5</mockito.version>
<assertj.version>3.3.0</assertj.version>
<achilles.version>5.2.1</achilles.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>${datastax.version}</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>${datastax.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>


<dependency>
<groupId>info.archinnov</groupId>
<artifactId>achilles-junit</artifactId>
<version>${achilles.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<configuration>
<finalName>${artifactId}</finalName>
</configuration>
<executions>
<execution>
<goals>
<goal>build-info</goal>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.nibado.example.cassandrait;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class ApplicationConfiguration {
@Bean
public Session createSession() {
log.info("ApplicationConfiguration.createSession");
Cluster cluster;

cluster = Cluster.builder()
.addContactPoint("127.0.0.1")
.build();

Session session = cluster.connect();

session.execute("CREATE KEYSPACE IF NOT EXISTS cassandrait WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }");
session.execute("DROP TABLE IF EXISTS cassandrait.counter");
session.execute("CREATE TABLE cassandrait.counter (key text, value counter, PRIMARY key(key));");

return session;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nibado.example.cassandrait;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/nibado/example/cassandrait/base/Counter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.nibado.example.cassandrait.base;

import com.datastax.driver.mapping.annotations.Column;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;
import lombok.Data;

@Data
@Table(keyspace = "cassandrait", name = "counter")
public class Counter {
@PartitionKey
@Column(name = "key")
private String key;
@Column(name = "value")
private long value;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.nibado.example.cassandrait.base;

import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.Session;
import com.datastax.driver.mapping.Mapper;
import com.datastax.driver.mapping.MappingManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.Optional;

@Repository
public class CounterRepository {
private final Session session;
private final PreparedStatement incrementStatement;
private final Mapper<Counter> mapper;

@Autowired
public CounterRepository(final Session session) {
this.session = session;
this.mapper = new MappingManager(session).mapper(Counter.class);
this.incrementStatement = session.prepare("UPDATE cassandrait.counter SET value = value + 1 WHERE key=?");
}

public void increment(final String key) {
session.execute(incrementStatement.bind(key));
}

public Optional<Counter> get(final String key) {
return Optional.ofNullable(mapper.get(key));
}

@PostConstruct
public void initTable() {



}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.nibado.example.cassandrait.controller;

import com.nibado.example.cassandrait.base.CounterRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/counter")
public class CounterController {
private final CounterRepository repository;

@Autowired
public CounterController(final CounterRepository repository) {
this.repository = repository;
}

@RequestMapping(value = "/{key}", method = RequestMethod.POST)
public void increment(@PathVariable final String key) {
repository.increment(key);
}

@RequestMapping(value = "/{key}", method = RequestMethod.GET)
public ResponseEntity<?> get(@PathVariable final String key) {
return repository.get(key)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.nibado.example.cassandrait.base;

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.mapping.Mapper;
import com.datastax.driver.mapping.MappingManager;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public abstract class CounterIntegrationTest {
@Autowired
private WebApplicationContext wac;

@Autowired
private Session session;

private Mapper<Counter> mapper;
private MockMvc mockMvc;

@Before
public void setup() throws Exception {
mockMvc = MockMvcBuilders
.webAppContextSetup(wac)
.build();

mapper = new MappingManager(session).mapper(Counter.class);
}

@Test
public void getAndIncrement() throws Exception {
assertThat(countRows(session)).isZero();

increment("foo");
increment("bar");
increment("foo");

assertThat(countRows(session)).isEqualTo(2);

get("foo", 2);
get("bar",1);
}

private void increment(String key) throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/counter/" + key))
.andDo(print())
.andExpect(status().isOk());
}

private void get(String key, int expected) throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/counter/" + key))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.value", is(expected)));
}

public static long countRows(Session session) {
ResultSet set = session.execute("SELECT COUNT(*) from cassandrait.counter");

return set.one().getLong(0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.nibado.example.cassandrait.base;

import com.datastax.driver.core.Session;
import org.junit.Before;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public abstract class CounterRepositoryTest {
private Session session;

protected CounterRepository repository;

public abstract Session getSession();

@Before
public void setup() {
repository = new CounterRepository(getSession());
}

@Test
public void getAndIncrement() {
assertThat(repository.get("foo")).isEmpty();

repository.increment("foo");
repository.increment("foo");
repository.increment("bar");

assertThat(repository.get("foo").get().getValue()).isEqualTo(2);
assertThat(repository.get("bar").get().getValue()).isEqualTo(1L);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.nibado.example.cassandrait.embedded;

import com.nibado.example.cassandrait.ExampleApplication;
import com.nibado.example.cassandrait.base.CounterIntegrationTest;
import org.junit.runner.RunWith;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = {ExampleApplication.class, TestConfigurationEmbedded.class})
@EnableConfigurationProperties
public class CounterIntegrationTestEmbedded extends CounterIntegrationTest {

}
Loading

0 comments on commit 982f9e7

Please sign in to comment.