Project Created

This commit is contained in:
dDogge 2024-03-26 20:38:55 +01:00
commit ead83c4c5c
38 changed files with 885 additions and 0 deletions

BIN
src/.DS_Store vendored Normal file

Binary file not shown.

BIN
src/main/.DS_Store vendored Normal file

Binary file not shown.

BIN
src/main/java/.DS_Store vendored Normal file

Binary file not shown.

View file

@ -0,0 +1,55 @@
package krusty;
import spark.Request;
import spark.Response;
import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;
import static krusty.Jsonizer.toJson;
public class Database {
/**
* Modify it to fit your environment and then use this string when connecting to your database!
*/
private static final String jdbcString = "jdbc:mysql://localhost/krusty";
// For use with MySQL or PostgreSQL
private static final String jdbcUsername = "<CHANGE ME>";
private static final String jdbcPassword = "<CHANGE ME>";
public void connect() {
// Connect to database here
}
// TODO: Implement and change output in all methods below!
public String getCustomers(Request req, Response res) {
return "{}";
}
public String getRawMaterials(Request req, Response res) {
return "{}";
}
public String getCookies(Request req, Response res) {
return "{\"cookies\":[]}";
}
public String getRecipes(Request req, Response res) {
return "{}";
}
public String getPallets(Request req, Response res) {
return "{\"pallets\":[]}";
}
public String reset(Request req, Response res) {
return "{}";
}
public String createPallet(Request req, Response res) {
return "{}";
}
}

View file

@ -0,0 +1,78 @@
package krusty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOError;
import java.io.IOException;
import java.io.StringWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
/**
* Auxiliary class for automatically translating a ResultSet to JSON
*/
public class Jsonizer {
private static final ObjectMapper mapper = new ObjectMapper();
static {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
mapper.setDateFormat(df);
}
/**
* Convert any POJO or plain Java data to JSON supported by Jackson
*
* @param name name of returned data
* @return {name: jackson serialized json representation}
*/
public static String anythingToJson(Object data, String name) {
try {
Map<String, Object> entries = new HashMap<>();
entries.put(name, data);
return mapper.writeValueAsString(entries);
} catch (JsonProcessingException e) {
throw new IOError(e);
}
}
/**
* Convert JDBC Result to JSON
*
* @param rs open and unused ResultSet
* @param name name of the resultset
* @return JSON object with one entry: {name: result of ResultSet}
* @throws SQLException
*/
public static String toJson(ResultSet rs, String name) throws SQLException {
try {
ResultSetMetaData meta = rs.getMetaData();
StringWriter sw = new StringWriter();
JsonGenerator writer = mapper.getFactory().createGenerator(sw);
writer.writeStartObject();
writer.writeFieldName(name);
writer.writeStartArray();
while (rs.next()) {
writer.writeStartObject();
for (int i = 1; i <= meta.getColumnCount(); i++) {
writer.writeFieldName(meta.getColumnLabel(i));
writer.writeObject(rs.getObject(i));
}
writer.writeEndObject();
}
writer.writeEndArray();
writer.writeEndObject();
writer.flush();
return sw.toString();
} catch (IOException e) {
throw new IOError(e);
}
}
}

View file

@ -0,0 +1,83 @@
package krusty;
import java.io.IOError;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static spark.Spark.*;
public class ServerMain {
public static int PORT = 8888;
public static String API_ENTRYPOINT = "/api/v1";
private Database db;
public void startServer() {
staticFiles.location("/public");
db = new Database();
db.connect();
port(PORT);
enableCORS();
initIndex();
initRoutes();
}
private void initIndex() {
try {
byte[] indexData = getClass().getResource("/public/index.html").openStream().readAllBytes();
final String index = new String(indexData, StandardCharsets.UTF_8);
get("/", (req, res) -> index);
} catch (IOException e) {
throw new IOError(e);
}
}
private void initRoutes() {
get(API_ENTRYPOINT + "/customers", (req, res) -> db.getCustomers(req, res));
get(API_ENTRYPOINT + "/raw-materials", (req, res) -> db.getRawMaterials(req, res));
get(API_ENTRYPOINT + "/cookies", (req, res) -> db.getCookies(req, res));
get(API_ENTRYPOINT + "/recipes", (req, res) -> db.getRecipes(req, res));
get(API_ENTRYPOINT + "/pallets", (req, res) -> db.getPallets(req, res));
post(API_ENTRYPOINT + "/reset", (req, res) -> db.reset(req, res));
post(API_ENTRYPOINT + "/pallets", (req, res) -> db.createPallet(req, res));
}
public void stopServer() {
stop();
}
/**
* Setup CORS, see:
* - https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
* - http://sparkjava.com/tutorials/cors
*/
private void enableCORS() {
options("/*", (request, response) -> {
String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers");
if (accessControlRequestHeaders != null) {
response.header("Access-Control-Allow-Headers", accessControlRequestHeaders);
}
String accessControlRequestMethod = request.headers("Access-Control-Request-Method");
if (accessControlRequestMethod != null) {
response.header("Access-Control-Allow-Methods", accessControlRequestMethod);
}
return "OK";
});
before((request, response) -> {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "Content-Type, Accept");
response.type("application/json");
});
}
public static void main(String[] args) throws InterruptedException {
new ServerMain().startServer();
}
}

BIN
src/main/resources/.DS_Store vendored Normal file

Binary file not shown.

BIN
src/main/resources/public/.DS_Store vendored Normal file

Binary file not shown.

View file

@ -0,0 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"><title>Krusty</title><link href="/static/css/main.59f83d58.chunk.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(e){function r(r){for(var n,f,l=r[0],i=r[1],a=r[2],c=0,s=[];c<l.length;c++)f=l[c],Object.prototype.hasOwnProperty.call(o,f)&&o[f]&&s.push(o[f][0]),o[f]=0;for(n in i)Object.prototype.hasOwnProperty.call(i,n)&&(e[n]=i[n]);for(p&&p(r);s.length;)s.shift()();return u.push.apply(u,a||[]),t()}function t(){for(var e,r=0;r<u.length;r++){for(var t=u[r],n=!0,l=1;l<t.length;l++){var i=t[l];0!==o[i]&&(n=!1)}n&&(u.splice(r--,1),e=f(f.s=t[0]))}return e}var n={},o={1:0},u=[];function f(r){if(n[r])return n[r].exports;var t=n[r]={i:r,l:!1,exports:{}};return e[r].call(t.exports,t,t.exports,f),t.l=!0,t.exports}f.m=e,f.c=n,f.d=function(e,r,t){f.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},f.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},f.t=function(e,r){if(1&r&&(e=f(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(f.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)f.d(t,n,function(r){return e[r]}.bind(null,n));return t},f.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return f.d(r,"a",r),r},f.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},f.p="/";var l=this.webpackJsonpfrontend=this.webpackJsonpfrontend||[],i=l.push.bind(l);l.push=r,l=l.slice();for(var a=0;a<l.length;a++)r(l[a]);var p=i;t()}([])</script><script src="/static/js/2.0a7bcc05.chunk.js"></script><script src="/static/js/main.4ee489f9.chunk.js"></script></body></html>

View file

@ -0,0 +1,2 @@
div.App{width:800px;margin:0 auto}@media screen and (max-width:800px){div.App{width:100%}}body{margin:10px;padding:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Roboto","Oxygen","Ubuntu","Cantarell","Fira Sans","Droid Sans","Helvetica Neue",sans-serif;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}code{font-family:source-code-pro,Menlo,Monaco,Consolas,"Courier New",monospace}
/*# sourceMappingURL=main.59f83d58.chunk.css.map */

View file

@ -0,0 +1 @@
{"version":3,"sources":["index.css"],"names":[],"mappings":"AAAA,QACC,WAAY,CACZ,aACD,CAEA,oCACE,QACE,UACF,CACF,CAEA,KACE,WAAY,CACZ,SAAU,CACV,mJAEY,CACZ,kCAAmC,CACnC,iCACF,CAEA,KACE,yEAEF","file":"main.59f83d58.chunk.css","sourcesContent":["div.App {\n\twidth: 800px;\n\tmargin: 0 auto;\n}\n\n@media screen and (max-width: 800px) {\n div.App {\n width: 100%;\n }\n}\n\nbody {\n margin: 10px;\n padding: 0;\n font-family: -apple-system, BlinkMacSystemFont, \"Segoe UI\", \"Roboto\", \"Oxygen\",\n \"Ubuntu\", \"Cantarell\", \"Fira Sans\", \"Droid Sans\", \"Helvetica Neue\",\n sans-serif;\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n}\n\ncode {\n font-family: source-code-pro, Menlo, Monaco, Consolas, \"Courier New\",\n monospace;\n}\n"]}

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,49 @@
/*
object-assign
(c) Sindre Sorhus
@license MIT
*/
/**
* A better abstraction over CSS.
*
* @copyright Oleg Isonen (Slobodskoi) / Isonen 2014-present
* @website https://github.com/cssinjs/jss
* @license MIT
*/
/** @license React v0.18.0
* scheduler.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/** @license React v16.12.0
* react-dom.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/** @license React v16.12.0
* react-is.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/** @license React v16.12.0
* react.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,2 @@
!function(e){function r(r){for(var n,f,l=r[0],i=r[1],a=r[2],c=0,s=[];c<l.length;c++)f=l[c],Object.prototype.hasOwnProperty.call(o,f)&&o[f]&&s.push(o[f][0]),o[f]=0;for(n in i)Object.prototype.hasOwnProperty.call(i,n)&&(e[n]=i[n]);for(p&&p(r);s.length;)s.shift()();return u.push.apply(u,a||[]),t()}function t(){for(var e,r=0;r<u.length;r++){for(var t=u[r],n=!0,l=1;l<t.length;l++){var i=t[l];0!==o[i]&&(n=!1)}n&&(u.splice(r--,1),e=f(f.s=t[0]))}return e}var n={},o={1:0},u=[];function f(r){if(n[r])return n[r].exports;var t=n[r]={i:r,l:!1,exports:{}};return e[r].call(t.exports,t,t.exports,f),t.l=!0,t.exports}f.m=e,f.c=n,f.d=function(e,r,t){f.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},f.r=function(e){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},f.t=function(e,r){if(1&r&&(e=f(e)),8&r)return e;if(4&r&&"object"===typeof e&&e&&e.__esModule)return e;var t=Object.create(null);if(f.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var n in e)f.d(t,n,function(r){return e[r]}.bind(null,n));return t},f.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return f.d(r,"a",r),r},f.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},f.p="/";var l=this.webpackJsonpfrontend=this.webpackJsonpfrontend||[],i=l.push.bind(l);l.push=r,l=l.slice();for(var a=0;a<l.length;a++)r(l[a]);var p=i;t()}([]);
//# sourceMappingURL=runtime-main.1c2d59f4.js.map

File diff suppressed because one or more lines are too long

BIN
src/test/.DS_Store vendored Normal file

Binary file not shown.

BIN
src/test/java/.DS_Store vendored Normal file

Binary file not shown.

View file

@ -0,0 +1,195 @@
package krusty;
import static org.junit.Assert.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.json.JSONException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.skyscreamer.jsonassert.JSONAssert;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class KrustyTests {
public static final String BASE_URL = "http://localhost:" + ServerMain.PORT + ServerMain.API_ENTRYPOINT + "/";
/**
*
* Test cases
*
* Note that they are ordered in alphabetical order,
* this because one test case creates pallets that other use.
*
*/
@Test
public void test01Customers() throws JSONException {
String expected = readFile("ExpectedCustomers.json");
String actual = getURL("customers");
JSONAssert.assertEquals(expected, actual, false);
}
@Test
public void test02Cookies() throws JSONException {
String expected = readFile("ExpectedCookies.json");
String actual = getURL("cookies");
JSONAssert.assertEquals(expected, actual, false);
}
@Test
public void test03RawMaterials() throws JSONException {
String expected = readFile("ExpectedRawMaterialsStart.json");
String actual = getURL("raw-materials");
JSONAssert.assertEquals(expected, actual, false);
}
@Test
public void test04CreatePallets() throws JSONException {
createPallet("Nut ring");
createPallet("Nut ring");
createPallet("Tango");
createPallet("Amneris");
createPallet("Amneris");
createPallet("Amneris");
createPallet("Berliner");
String expected = readFile("ExpectedRawMaterialsAfterCreatingPallets.json");
String actual = getURL("raw-materials");
JSONAssert.assertEquals(expected, actual, false);
}
@Test
public void test05Pallets() throws JSONException {
String expected = readFile("ExpectedPallets.json");
String actual = getURL("pallets");
JSONAssert.assertEquals(expected, actual, false);
}
@Test
public void test06PalletsByCookie() throws JSONException, UnirestException {
String expected = readFile("ExpectedPalletsByCookie.json");
String actual = Unirest.get(BASE_URL + "pallets")
.queryString("cookie", "Nut ring")
.asString().getBody();
JSONAssert.assertEquals(expected, actual, false);
}
@Test
public void test07PalletsByCookieAndDate() throws JSONException, UnirestException {
String expected = readFile("ExpectedPalletsByCookie.json");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String today = formatter.format(new Date());
String actual = Unirest.get(BASE_URL + "pallets")
.queryString("cookie", "Nut ring")
.queryString("from", today)
.asString().getBody();
JSONAssert.assertEquals(expected, actual, false);
}
@Test
public void test08PalletsByCookieAndDate2() throws JSONException, UnirestException {
String expected = readFile("ExpectedPalletsEmpty.json");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, 1);
String nextYear = formatter.format(calendar.getTime());
String actual = Unirest.get(BASE_URL + "pallets")
.queryString("cookie", "Nut ring")
.queryString("from", nextYear)
.asString().getBody();
JSONAssert.assertEquals(expected, actual, false);
}
/**
*
* Auxiliary methods
*
*/
protected String readFile(String file) {
try {
String path = "src/test/resources/" + file;
return new String(Files.readAllBytes(Paths.get(path)));
} catch (IOException e) {
e.printStackTrace();
fail(e.getMessage());
}
return "";
}
protected String getURL(String url) {
try {
HttpResponse<String> res = Unirest.get(BASE_URL + url).asString();
return res.getBody();
} catch (UnirestException e) {
fail("Connection failed.\n" + e.getMessage());
}
return "";
}
protected String createPallet(String cookie) {
try {
HttpResponse<String> res = Unirest.post(BASE_URL + "pallets")
.queryString("cookie", cookie)
.asString();
return res.getBody();
} catch (UnirestException e) {
fail("Connection failed.\n" + e.getMessage());
}
return "";
}
/**
*
* Automatically start REST server if it is not running and reset database.
*
*/
private static ServerMain server;
@BeforeClass
public static void startServer() throws InterruptedException {
try {
// Check if rest server is running
Unirest.get(BASE_URL).asString();
} catch (UnirestException e) {
// Start REST server and sleep a bit before start running test cases
server = new ServerMain();
server.startServer();
Thread.sleep(250);
}
// Reset database
try {
Unirest.post(BASE_URL + "reset").asString();
} catch (UnirestException e2) {
fail(e2.getMessage());
}
}
@AfterClass
public static void stopServer() {
if (server != null) {
server.stopServer();
}
}
}

View file

@ -0,0 +1,10 @@
{
"cookies": [
{"name": "Amneris"},
{"name": "Berliner"},
{"name": "Nut cookie"},
{"name": "Nut ring"},
{"name": "Tango"},
{"name": "Almond delight"}
]
}

View file

@ -0,0 +1,12 @@
{
"customers": [
{"name": "Bjudkakor AB", "address": "Ystad"},
{"name": "Finkakor AB", "address": "Helsingborg"},
{"name": "Gästkakor AB", "address": "Hässleholm"},
{"name": "Kaffebröd AB", "address": "Landskrona"},
{"name": "Kalaskakor AB", "address": "Trelleborg"},
{"name": "Partykakor AB", "address": "Kristianstad"},
{"name": "Skånekakor AB", "address": "Perstorp"},
{"name": "Småbröd AB", "address": "Malmö"}
]
}

View file

@ -0,0 +1,11 @@
{
"pallets": [
{"cookie": "Amneris", "blocked": "no"},
{"cookie": "Amneris", "blocked": "no"},
{"cookie": "Amneris", "blocked": "no"},
{"cookie": "Berliner", "blocked": "no"},
{"cookie": "Nut ring", "blocked": "no"},
{"cookie": "Nut ring", "blocked": "no"},
{"cookie": "Tango", "blocked": "no"}
]
}

View file

@ -0,0 +1,6 @@
{
"pallets": [
{"cookie": "Nut ring", "blocked": "no"},
{"cookie": "Nut ring", "blocked": "no"}
]
}

View file

@ -0,0 +1,4 @@
{
"pallets": [
]
}

View file

@ -0,0 +1,23 @@
{
"raw-materials": [
{"name": "Bread crumbs", "amount": 500000, "unit": "g"},
{"name": "Butter", "amount": 386600, "unit": "g"},
{"name": "Chocolate", "amount": 497300, "unit": "g"},
{"name": "Chopped almonds", "amount": 500000, "unit": "g"},
{"name": "Cinnamon", "amount": 500000, "unit": "g"},
{"name": "Egg whites", "amount": 500000, "unit": "ml"},
{"name": "Eggs", "amount": 456800, "unit": "g"},
{"name": "Fine-ground nuts", "amount": 500000, "unit": "g"},
{"name": "Flour", "amount": 416300, "unit": "g"},
{"name": "Ground, roasted nuts", "amount": 500000, "unit": "g"},
{"name": "Icing sugar", "amount": 474080, "unit": "g"},
{"name": "Marzipan", "amount": 378500, "unit": "g"},
{"name": "Potato starch", "amount": 495950, "unit": "g"},
{"name": "Roasted, chopped nuts", "amount": 475700, "unit": "g"},
{"name": "Sodium bicarbonate", "amount": 499784, "unit": "g"},
{"name": "Sugar", "amount": 486500, "unit": "g"},
{"name": "Vanilla", "amount": 499892, "unit": "g"},
{"name": "Vanilla sugar", "amount": 499730, "unit": "g"},
{"name": "Wheat flour", "amount": 495950, "unit": "g"}
]
}

View file

@ -0,0 +1,23 @@
{
"raw-materials": [
{"name": "Bread crumbs", "amount": 500000, "unit": "g"},
{"name": "Butter", "amount": 500000, "unit": "g"},
{"name": "Chocolate", "amount": 500000, "unit": "g"},
{"name": "Chopped almonds", "amount": 500000, "unit": "g"},
{"name": "Cinnamon", "amount": 500000, "unit": "g"},
{"name": "Egg whites", "amount": 500000, "unit": "ml"},
{"name": "Eggs", "amount": 500000, "unit": "g"},
{"name": "Fine-ground nuts", "amount": 500000, "unit": "g"},
{"name": "Flour", "amount": 500000, "unit": "g"},
{"name": "Ground, roasted nuts", "amount": 500000, "unit": "g"},
{"name": "Icing sugar", "amount": 500000, "unit": "g"},
{"name": "Marzipan", "amount": 500000, "unit": "g"},
{"name": "Potato starch", "amount": 500000, "unit": "g"},
{"name": "Roasted, chopped nuts", "amount": 500000, "unit": "g"},
{"name": "Sodium bicarbonate", "amount": 500000, "unit": "g"},
{"name": "Sugar", "amount": 500000, "unit": "g"},
{"name": "Vanilla", "amount": 500000, "unit": "g"},
{"name": "Vanilla sugar", "amount": 500000, "unit": "g"},
{"name": "Wheat flour", "amount": 500000, "unit": "g"}
]
}