Last active
March 18, 2019 04:29
-
-
Save dschinkel/5d97fbe457208cb3d7e615eb8b93de99 to your computer and use it in GitHub Desktop.
FizzBuzz API Kata
This file contains hidden or 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
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 }"; | |
}); | |
} | |
} |
This file contains hidden or 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
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 |
This file contains hidden or 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.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); | |
}); | |
} | |
} |
This file contains hidden or 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
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; | |
} | |
} |
This file contains hidden or 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
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); | |
} | |
} |
This file contains hidden or 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
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