Skip to content

Instantly share code, notes, and snippets.

@dschinkel
Last active March 18, 2019 04:29
Show Gist options
  • Save dschinkel/5d97fbe457208cb3d7e615eb8b93de99 to your computer and use it in GitHub Desktop.
Save dschinkel/5d97fbe457208cb3d7e615eb8b93de99 to your computer and use it in GitHub Desktop.
FizzBuzz API Kata
import com.fasterxml.jackson.jr.ob.JSON;
import java.util.Map;
import static spark.Spark.get;
import static spark.Spark.port;
public class FakeNumberServer {
public static void main(String[] args) {
port(2000);
get("/number", (req, res) -> {
return "{ \"number\": 7 }";
});
}
}
Feature: FizzBuzz API
As a user, I want to be able to play fizzbuzz
Scenario: Receives Same Number Sent In
Given a FizzBuzz server is running
And the number service is running
When the number service returns a number not divisible by 5 or 3
Then I receive the same number sent in
package com.atdd.fizzbuzz;
import com.fasterxml.jackson.jr.ob.JSON;
import cucumber.api.java8.En;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class fizzbuzzApiSpecs implements En {
String number;
public fizzbuzzApiSpecs() {
Given("^the number service is running$", () -> {
});
When("^the number service returns a number not divisible by (\\d+) or (\\d+)$", (Integer arg0, Integer arg1) -> {
try (java.util.Scanner s = new java.util.Scanner(new java.net.URL("http://localhost:4567/fizzbuzz").openStream()).useDelimiter("\\A")) {
String response = s.next() ;
Map<String,Object> map = JSON.std.mapFrom(response);
number = map.get("number").toString();
}
});
Then("^I receive the same number sent in$", () -> {
assertEquals(number, 7);
});
}
}
import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.jr.ob.*;
public class FizzBuzzParser {
public int parseNumberFromJson(String responseJSON) throws IOException {
Map<String,Object> data = JSON.std.mapFrom(responseJSON);
int number = (Integer)Integer.parseInt(data.get("number").toString());
return number;
}
}
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
public class FizzBuzzParserTest {
@Test
public void parsesNumberFromString() throws IOException {
FizzBuzzParser api = new FizzBuzzParser();
int number = api.parseNumberFromJson("{ \"number\": 1 }");
assertEquals(number, 1);
}
}
import com.fasterxml.jackson.jr.ob.*;
import java.util.Map;
import static spark.Spark.*;
public class FizzBuzzServer {
port(4567);
public static void main(String[] args) {
get("/hello", (req, res) -> {
String response;
try (java.util.Scanner s = new java.util.Scanner(new java.net.URL("http://localhost:7654/todos").openStream()).useDelimiter("\\A")) {
response = s.hasNext() ? s.next() : "";
System.out.println(response);
Map<String,Object> map = JSON.std.mapFrom(response);
response = map.get("title").toString();
}
return response;
});
get("/fizzbuzz", (req, res) -> {
String response;
Integer number;
try (java.util.Scanner s = new java.util.Scanner(new java.net.URL("http://localhost:2000/number").openStream()).useDelimiter("\\A")) {
response = s.hasNext() ? s.next() : "";
System.out.println(response);
FizzBuzzParser parser = new FizzBuzzParser();
number = parser.parseNumberFromJson(response);
}
return number;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment