Created
June 12, 2015 19:03
-
-
Save joshuacalloway/3bd629fcff5158c8ef0c to your computer and use it in GitHub Desktop.
first spock test with embedded mongo and spring boot
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
package com.shoppertrak.ssclogging | |
import com.mongodb.MongoClient | |
import de.flapdoodle.embed.mongo.MongodExecutable | |
import de.flapdoodle.embed.mongo.MongodProcess | |
import de.flapdoodle.embed.mongo.MongodStarter | |
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder | |
import de.flapdoodle.embed.mongo.config.Net | |
import de.flapdoodle.embed.mongo.distribution.Version | |
import de.flapdoodle.embed.process.runtime.Network | |
import groovyx.net.http.RESTClient | |
import org.springframework.boot.SpringApplication | |
import org.springframework.context.ConfigurableApplicationContext | |
import spock.lang.Shared | |
import java.util.concurrent.Callable | |
import java.util.concurrent.Executors | |
import java.util.concurrent.Future | |
import java.util.concurrent.TimeUnit | |
/** | |
* Created by jc on 5/12/15. | |
*/ | |
class CheckinControllerSpec extends spock.lang.Specification { | |
@Shared MongoClient mongoClient // = new MongoClient("localhost", 27017); | |
private static final MongodStarter starter = MongodStarter.getDefaultInstance() | |
@Shared private MongodExecutable mongodExe; | |
@Shared private MongodProcess mongod; | |
@Shared private MongoClient _mongo; | |
def startMongoViaDocker() { | |
'docker run -d --name mongotest -p 27017:27017 mongo'.execute() | |
sleep(1000*90) // wait for mongo to comeup | |
} | |
def startMongoEmbeddedMemory() { | |
mongodExe = starter.prepare(new MongodConfigBuilder() | |
.version(Version.Main.PRODUCTION) | |
.net(new Net(27017, Network.localhostIsIPv6())) | |
.build()) | |
mongod = mongodExe.start() | |
} | |
def setupSpec() { | |
System.setProperty("spring.profile.active", "test") | |
Future future = Executors | |
.newSingleThreadExecutor().submit( | |
new Callable() { | |
@Override | |
public ConfigurableApplicationContext call() throws Exception { | |
return (ConfigurableApplicationContext) SpringApplication | |
.run(com.shoppertrak.ssclogging.Application.class) | |
} | |
}) | |
def context = future.get(60, TimeUnit.SECONDS) | |
mongoClient = new MongoClient("localhost", 27017) | |
} | |
def stopMongoEmbedded() { | |
mongod.stop(); | |
mongodExe.stop(); | |
} | |
def stopMongoViaDocker() { | |
'docker kill mongotest'.execute() | |
'docker rm -v mongotest'.execute() | |
} | |
def cleanupSpec() { | |
stopMongoEmbedded() | |
} | |
def "Only one checkin per day is saved"() { | |
setup: | |
mongoClient.getDB("ssc").getCollection("checkins").drop() | |
def checkinEndpoint = new RESTClient( 'http://localhost:8080/checkin') | |
def queryEndpoint = new RESTClient( 'http://localhost:8080/query') | |
when: | |
checkinEndpoint.post( requestContentType: 'application/json', | |
body: [ mac: "abc", | |
cameraTime: System.currentTimeMillis(), | |
ip: "1.2.3", | |
conveyorIp: "4.5.6", | |
conveyorServer: "letsgo.com", | |
version: "beta", | |
sscVersion: "alpha"]) | |
checkinEndpoint.post( requestContentType: 'application/json', | |
body: [ mac: "abc", | |
cameraTime: System.currentTimeMillis(), | |
ip: "1.2.3", | |
conveyorIp: "4.5.6", | |
conveyorServer: "overwrite.com", | |
version: "beta", | |
sscVersion: "alpha"]) | |
then: | |
def response = queryEndpoint.get(query: ['mac':'abc', 'daysago':1]) | |
println response.data | |
response.data.size() == 2 | |
} | |
def "Checkin yesterday, Checkin today we should have 2 checkins"() { | |
setup: | |
mongoClient.getDB("ssc").getCollection("checkins").drop() | |
def checkinEndpoint = new RESTClient( 'http://localhost:8080/checkin') | |
def queryEndpoint = new RESTClient( 'http://localhost:8080/query') | |
def yesterday = System.currentTimeMillis() - System.currentTimeMillis() * 1000 * 60 * 60 * 60 * 24 | |
when: | |
checkinEndpoint.post( requestContentType: 'application/json', | |
body: [ mac: "abc", | |
cameraTime: System.currentTimeMillis(), | |
timestamp: yesterday, | |
ip: "1.2.3", | |
conveyorIp: "4.5.6", | |
conveyorServer: "letsgo.com", | |
version: "beta", | |
sscVersion: "alpha"]) | |
checkinEndpoint.post( requestContentType: 'application/json', | |
body: [ mac: "abc", | |
cameraTime: System.currentTimeMillis(), | |
ip: "1.2.3", | |
conveyorIp: "4.5.6", | |
conveyorServer: "overwrite.com", | |
version: "beta", | |
sscVersion: "alpha"]) | |
then: | |
def response = queryEndpoint.get(query: ['mac':'abc']) | |
println response.data | |
response.data.size() == 2 | |
} | |
def "No checkins exists if nothing has checked in"() { | |
setup: | |
mongoClient.getDB("ssc").getCollection("checkins").drop() | |
def queryEndpoint = new RESTClient( 'http://localhost:8080/query') | |
expect: | |
queryEndpoint.get(query: ['mac':'abc']).data == [] | |
} | |
def "Count is zero when no checkins happened"() { | |
setup: | |
mongoClient.getDB("ssc").getCollection("checkins").drop() | |
def endpoint = new RESTClient( 'http://localhost:8080/count') | |
expect: | |
def response = endpoint.get( [:]) | |
println response.data | |
response.data == 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment