Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
Changes:

CakeEntity (amended)
- rename as simply Cake
- remove references to Employee
- add JsonProperty annotation for description field deserialization

CakeFactory (added)
- class added for use when creating cakes

CakeServlet (amended)
- fix servlet mappings so servlet runs on "/" and "/cakes" and set so it is initialised on startup
- replace System out with Logger (using Logback)
- add package private constructor which accepts class dependencies, and no-arg constructor creating class with default version of dependencies
- refactor init method so initial cakes are deserialized using jackson, and stored in database using CakeStore class (new)
- amend doGet method to check accept header and return JSON / HTML depending on presence of "application/json" value
- add doPost method, which uses the CakeFactory to make a cake and the CakeStore to persist it, then simply runs the doGet method to render suitable output to the user
- add destroy method to close the Hibernate session on closure

CakeStore (added)
- added method for retrieving all cakes from the database
- added method for storing individual cakes to the database
- rather than living with lots of exceptions when duplicate titles are inserted, check for these in advance and if they are found then update the cake

HibernateUtil
- make class final
- add private constructor

src/main/resources/initial-cakes.json (added)
- copy of the json file previously pulled from github. Held locally to speed up testing, and allow webapp to function without web connection

src/main/resources/logback.xml (added)
- added to provide some basic configuration of the log output

src/main/webapp/css/narrow-jumbortron.css (added)
- copied from bootstrap.com, used for presentation

src/main/webapp/WEB-INF/web.xml (amended)
- fixed web-app tag

src/main/webapp/index.jsp (amended)
- changed to present the output in a user friendly manner. Not entirely happy with the look/responsiveness of this but couldnt afford to spend more time on it

CakeFactoryTest (added)
- junit tests for the CakeFactory

CakeServletTest (added)
- junit tests for the CakeServlet

CakeStoreTest (new)
- junit tests for the CakeStore

pom.xml (amended)
- changed jackson-core to jackson-databind, and updated version
- add slf4j-api and logback-classic
- change junit version to 4.12
- add mockito and hamcrest

Areas where further work might be done if time allowed
- add cucumber acceptance test / some kind of UI test
- use some means of object creation / dependency injection such as spring
- use a NoSQL DB such as MongoDB rather than HSQL
- personal dislike for the HibernateUtil singleton as it makes junit testing difficult
- allow json to be written / retrieved direct from database
- more work on presentation
- potentially rewrite using Node / Mongoose / React / Mongodb
31 changes: 27 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<!-- JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.0</version>
<artifactId>jackson-databind</artifactId>
<version>2.9.2</version>
</dependency>

<!-- JPA -->
Expand All @@ -36,12 +36,34 @@
<artifactId>hsqldb</artifactId>
<version>2.3.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>

<!-- Test dependencies. -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1</version>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>

Expand All @@ -62,8 +84,9 @@
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.7.v20170914</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<scanIntervalSeconds>5</scanIntervalSeconds>
<stopKey>STOP</stopKey>
<stopPort>8005</stopPort>
<httpConnector>
Expand Down
106 changes: 0 additions & 106 deletions src/main/java/com.waracle.cakemgr/CakeServlet.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
package com.waracle.cakemgr;

import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(name = "Employee", uniqueConstraints = {@UniqueConstraint(columnNames = "ID"), @UniqueConstraint(columnNames = "EMAIL")})
public class CakeEntity implements Serializable {

private static final long serialVersionUID = -1798070786993154676L;
@DynamicUpdate
@Table(name = "Cake", uniqueConstraints = {@UniqueConstraint(columnNames = "ID"), @UniqueConstraint(columnNames = "TITLE")})
public class Cake implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
private Integer employeeId;
private Integer cakeId;

@Column(name = "EMAIL", unique = true, nullable = false, length = 100)
@Column(name = "TITLE", unique = true, nullable = false, length = 100)
private String title;

@Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)
@JsonProperty("desc")
@Column(name = "DESCRIPTION", nullable = false, length = 100)
private String description;

@Column(name = "LAST_NAME", unique = false, nullable = false, length = 300)
@Column(name = "IMAGE", nullable = false, length = 300)
private String image;

public Integer getCakeId() {
return cakeId;
}
public String getTitle() {
return title;
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/waracle/cakemgr/CakeFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.waracle.cakemgr;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

class CakeFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(CakeFactory.class);
private static final String TITLE = "title";
private static final String DESCRIPTION = "description";
private static final String IMAGE = "image";

Cake makeCake(final HttpServletRequest req) throws IOException {
Cake cake = new Cake();
String title = req.getParameter(TITLE);
LOGGER.debug("title: {}", title);
cake.setTitle(title);
String description = req.getParameter(DESCRIPTION);
LOGGER.debug("description: {}", description);
cake.setDescription(description);
String image = req.getParameter(IMAGE);
LOGGER.debug("image: {}", image);
cake.setImage(image);
return cake;
}
}
Loading