Web version of the canvas standalone game
- The challenge
- Application overview
- Source packages
- Test packages
- Test the application
- Timing
Develop a simple Connect-4 game web service running either as a WAR
in jetty
or
as an embedded-jetty
application, which is able to start a game session, take player
input, keep the state of the game and forward the correct response to the requesting
party.
The program should support the following commands:
- C w h Should create a new canvas of width w and height h.
- L x1 y1 x2 y2 Should create a new line from (x1,y1) to (x2,y2). Currently only horizontal or vertical lines are supported. Horizontal and vertical lines will be drawn using the 'x' character.
- R x1 y1 x2 y2 Should create a new rectangle, whose upper left corner is (x1,y1) and lower right corner is (x2,y2). Horizontal and vertical lines will be drawn using the 'x' character.
- B x y c Should fill the entire area connected to (x,y) with "colour" c. The behaviour of this is the same as that of the "bucket fill" tool in paint programs.
- Q Should quit the program.
See canvas for more details on the game.
From a high level we can see the application subdivided in 4 components:
- Game Implementation
- Classes inside
canvas package
. Same implementation of the original game. So we need to convertInputDTOs
toCommandCanvas
for calling theCanvas
.
- Classes inside
- Business Logic Layer
- Data Access
H2
database,EntityManager
and theDAO package
for accessing the data to theDB
. In this layer we convertEnitity
objects toDTOs
so the application in the higher layers can speak only DTO language.
- Service layer
- The layer which first handles game logic (using canvas package) then handles
the persistence of data in the DB (using dao package). This is the transactional layer. Transactions are configured in the file
spring-context.xml
using AOP.
- The layer which first handles game logic (using canvas package) then handles
the persistence of data in the DB (using dao package). This is the transactional layer. Transactions are configured in the file
- Data Access
- WebService / REST Layer.
- The classes responsible for exposing the code as
RESTFul WebService
. We decided to reply XML message.
- The classes responsible for exposing the code as
- Api
Requests/Responses/DTOs
to reply to the caller.
The application is composed by the following packages:
com.ea.connect4.api
- API of the application. Here we have all the DTOs to communicate with the caller.com.ea.connect4.canvas.cmd
- Commands of the gamecom.ea.connect4.canvas.core
- Implementation fo the gamecom.ea.connect4.canvas.exception
- Exceptions of the gamecom.ea.connect4.canvas.util
- Utilities used for the gamecom.ea.connect4.dao
- Dao interfacecom.ea.connect4.dao.impl
- DAO implementation. If we change DB, this is the only class we have to change.com.ea.connect4.entity
- Entities to persist in DBcom.ea.connect4.service
- Service interfacecom.ea.connect4.service.impl
- Service implementationcom.ea.connect4.ws
- Jetty Servercom.ea.connect4.ws.rest
- Requests handler classes
Enhancements
- Exception
- Enrich Connect4
Application exceptions
and identify the exceptions that application must rollback when occur. In service layer actually all the operations go in commit sinceCanvasExcpetion extends RuntimeException
and is not set torollback
. - Enrich
RESTFullException
in order to reply to the caller all stacktrace
- Enrich Connect4
- CurrentSession
- Actually current session is simulated calling the application with the same user (can be session id or anything to identify a session) In a real application I would use
spring-security
and authentication for a user to log-in and then use the application.
- Actually current session is simulated calling the application with the same user (can be session id or anything to identify a session) In a real application I would use
Used junit for testing Canvas Service. TDD approach was followed for the implementation,
so unit tests were not created at the end but during all the development phase.
For each functionality of the service is present a different file in the package com.ea.connect4.be.service
:
CommandBucketFillTest.java
-> Test cases for BucketFiller command.CommandLineTest.java
-> Test cases for drawing a line command.CommandRectangleTest.java
-> Test cases for drawing a rectangle command.
Unit tests are divided in three categories:
Happy Paths
,testHP_nomeTest
which are the tests that have to succeed. Usually we assert at the end of the method what we are expecting.Case Limit
,testCL_nomeTest
which are the tests that test a Limit Case. Example, x, y are 0 or same as width/height. Usually we assert what we are expecting or pay attention of eventual exceptions.Exceptions
-testEX_nomeTest
which are the tests that test exception cases. In this case we define the exception we are waiting to be thrown.
Inside soapui folder there is a soapui project with the rest calls to test the application.
Run the class JettyServer.java
which creates a server listening in the port 1009
.
The application is ready to be tested using the following requests (for each functionality):
http://localhost:1009/connect4/new/0001/6/4
http://localhost:1009/connect4/line/0001/1/1/5/1
http://localhost:1009/connect4/rectangle/0001/2/3/5/4
http://localhost:1009/connect4/fill/0001/3/2/o
http://localhost:1009/connect4/getCanvas/1
http://localhost:1009/connect4/getAllCanvas
http://localhost:1009/connect4/getCanvasByUser/0001
http://localhost:1009/connect4/deleteCanvasByUser/0001
http://localhost:1est the application009/connect4/ping
If you want to test all the calls with a single click use the class RESTClientTest.java
which calls programmatically the server.
For test scope, all restful methods have changed to @GET
, so in this case the functionalities are testable even from the browser.
In a real application the insertion methods(new, line, rectangle, fill) would be @PUT
(or @POST
in case of form submission) and the deleteCanvasByUser
will be @DELETE
.
Total time spend for the release 20 hours subdivided as follows:
- 8 hours springcontext/h2/dao/service/junit (3+3+2)
- 8 hours JettyServer Spring/RESTFull + test/soapui (2+4+2)
- 4 hours Overview/documentation/repository