From 66b66c876a10cd55dffb4357fe5a4f1f712344fb Mon Sep 17 00:00:00 2001 From: PatrykMandrak Date: Tue, 14 May 2019 09:48:00 +0200 Subject: [PATCH 01/11] Added mockito dependency --- pom.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pom.xml b/pom.xml index 8db3f0d..f1dadeb 100644 --- a/pom.xml +++ b/pom.xml @@ -31,9 +31,22 @@ 20180813 + + junit + junit + 4.12 + test + + + org.mockito + mockito-all + 1.10.19 + test + + 1.8 1.8 From cae825a1024157c48cbfe63c1f572107e43ce213 Mon Sep 17 00:00:00 2001 From: PatrykMandrak Date: Tue, 14 May 2019 14:20:08 +0200 Subject: [PATCH 02/11] Added parsing cookie test --- .../StudentArtifactTransaction.java | 1 + .../com/queststore/DAO/DBCPDataSource.java | 6 +++--- .../queststore/Services/ItemCardUpdate.java | 2 -- .../com/queststore/helpers/CookieHelper.java | 4 ++-- .../queststore/helpers/CookieHelperTest.java | 21 +++++++++++++++++++ 5 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 src/test/java/com/queststore/helpers/CookieHelperTest.java diff --git a/src/main/java/com/queststore/Controller/StudentArtifactTransaction.java b/src/main/java/com/queststore/Controller/StudentArtifactTransaction.java index 0f04fb5..31bedba 100644 --- a/src/main/java/com/queststore/Controller/StudentArtifactTransaction.java +++ b/src/main/java/com/queststore/Controller/StudentArtifactTransaction.java @@ -12,6 +12,7 @@ public class StudentArtifactTransaction extends StudentTransaction { private UserService userService = new UserService(new UserDAOSql(), new ClassDAOSql(), new TransactionDAOSql()); + //To test boolean canBeBought(Card card, User user) throws DaoException { int balance = userService.getCoinBalance(user.getId()); return balance >= card.getValue(); diff --git a/src/main/java/com/queststore/DAO/DBCPDataSource.java b/src/main/java/com/queststore/DAO/DBCPDataSource.java index b868bc7..7a914d4 100644 --- a/src/main/java/com/queststore/DAO/DBCPDataSource.java +++ b/src/main/java/com/queststore/DAO/DBCPDataSource.java @@ -7,9 +7,9 @@ class DBCPDataSource { - private static final String URL = "jdbc:postgresql://localhost:5432/queststore"; - private static final String USER = "postgres"; - private static final String PASS = "12345"; + private static final String URL = "jdbc:postgresql://localhost:5432/public"; + private static final String USER = "gosteek"; + private static final String PASS = "Patryk342351"; private static BasicDataSource ds = new BasicDataSource(); static { diff --git a/src/main/java/com/queststore/Services/ItemCardUpdate.java b/src/main/java/com/queststore/Services/ItemCardUpdate.java index c97213d..7cd7599 100644 --- a/src/main/java/com/queststore/Services/ItemCardUpdate.java +++ b/src/main/java/com/queststore/Services/ItemCardUpdate.java @@ -61,7 +61,5 @@ private List updateCardInDB(List items) throws DaoException { cardDAO.update(createCart); cardList.addAll(cardDAO.getCardsOfType(cardDAO.getCardTypeById(artifactTypeId))); return cardList; - } - } diff --git a/src/main/java/com/queststore/helpers/CookieHelper.java b/src/main/java/com/queststore/helpers/CookieHelper.java index ab56760..ea6ff35 100644 --- a/src/main/java/com/queststore/helpers/CookieHelper.java +++ b/src/main/java/com/queststore/helpers/CookieHelper.java @@ -12,7 +12,7 @@ public class CookieHelper { private static final String SESSION_COOKIE_NAME = "session_id"; - private List parseCookies(String cookieStr) { + public List parseCookies(String cookieStr) { List cookies = new ArrayList<>(); if (cookieStr == null || cookieStr.isEmpty()) { @@ -23,7 +23,6 @@ private List parseCookies(String cookieStr) { String[] pair = cookie.split("="); cookies.add(new HttpCookie(pair[0].trim(), pair[1].trim().replaceAll("\"", ""))); } - return cookies; } @@ -43,6 +42,7 @@ public HttpCookie generateNewSessionIdCookie() { public Optional getSessionIdCookie(HttpExchange exchange) { String cookieStr = exchange.getRequestHeaders().getFirst("Cookie"); + System.out.println(cookieStr); List cookies = parseCookies(cookieStr); return getCookieByName(cookies); } diff --git a/src/test/java/com/queststore/helpers/CookieHelperTest.java b/src/test/java/com/queststore/helpers/CookieHelperTest.java new file mode 100644 index 0000000..f43e29d --- /dev/null +++ b/src/test/java/com/queststore/helpers/CookieHelperTest.java @@ -0,0 +1,21 @@ +package com.queststore.helpers; + +import org.junit.Test; + +import java.net.HttpCookie; +import java.util.List; + +import static org.junit.Assert.*; + +public class CookieHelperTest { + + @Test + public void parseCookies() { + CookieHelper cookieHelper = new CookieHelper(); + List cookies = cookieHelper.parseCookies("session_id=\"c212a7ae-f42b-4b3d-ace5-3b76711867fe\";test_id=\"asdasdasdasdasdasdsadasdasdas\""); + + assertEquals("c212a7ae-f42b-4b3d-ace5-3b76711867fe", cookies.get(0).getValue()); + assertEquals("asdasdasdasdasdasdsadasdasdas", cookies.get(1).getValue()); + + } +} \ No newline at end of file From 6715b4d3cdfe00fa6bbb2b8c665953bd4ac75f26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ziemowit=20B=C5=82a=C5=BCk=C3=B3w?= Date: Wed, 15 May 2019 15:06:25 +0200 Subject: [PATCH 03/11] Added UserCardAdd createStudent Test --- .../queststore/Services/UserCardAddTest.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/test/java/com/queststore/Services/UserCardAddTest.java diff --git a/src/test/java/com/queststore/Services/UserCardAddTest.java b/src/test/java/com/queststore/Services/UserCardAddTest.java new file mode 100644 index 0000000..920bfce --- /dev/null +++ b/src/test/java/com/queststore/Services/UserCardAddTest.java @@ -0,0 +1,44 @@ +package com.queststore.Services; + +import com.queststore.DAO.ClassDAO; +import com.queststore.DAO.DaoException; +import com.queststore.Model.Class; +import com.queststore.Model.User; +import com.queststore.Model.UserType; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +public class UserCardAddTest { + + @Test + public void createStudentTest2() throws DaoException { + ClassDAO classDAOMock = mock(ClassDAO.class); + when(classDAOMock.getClassId(any(String.class))).thenReturn(java.util.Optional.of(1)); + UserCardAdd userCardAdd = new UserCardAdd(classDAOMock); + //verify(classDAOMock.getClassId(any(String.class)), times(1)); + List items = new ArrayList<>(); + items.add("someEmailName"); + items.add("someFirstName"); + items.add("someLastName"); + + User user = userCardAdd.createStudent(items); + User expectedUser = new User( + 1, + "someFirstName", + "someLastName", + "someEmailName", + null, + null, + new UserType(1, "student") + ); + + assertEquals(expectedUser.getFirstName(), user.getFirstName()); + assertEquals(expectedUser.getLastName(), user.getLastName()); + } +} \ No newline at end of file From 4b9f9d226ea75cfcc6ba1e2aeb4f0baf2659cc89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ziemowit=20B=C5=82a=C5=BCk=C3=B3w?= Date: Wed, 15 May 2019 17:49:52 +0200 Subject: [PATCH 04/11] Added CardDAO injection to createUser --- pom.xml | 6 ++++++ .../java/com/queststore/Services/UserCardAdd.java | 12 ++++++++++-- .../java/com/queststore/helpers/CookieHelper.java | 2 +- .../Controller/StudentArtifactTransactionTest.java | 10 ++++++++++ .../com/queststore/helpers/CookieHelperTest.java | 2 +- 5 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/queststore/Controller/StudentArtifactTransactionTest.java diff --git a/pom.xml b/pom.xml index f1dadeb..051868c 100644 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,12 @@ 1.10.19 test + + org.junit.jupiter + junit-jupiter-api + RELEASE + test + diff --git a/src/main/java/com/queststore/Services/UserCardAdd.java b/src/main/java/com/queststore/Services/UserCardAdd.java index ae404e7..39fc761 100644 --- a/src/main/java/com/queststore/Services/UserCardAdd.java +++ b/src/main/java/com/queststore/Services/UserCardAdd.java @@ -19,6 +19,15 @@ import java.util.Random; public class UserCardAdd implements HttpHandler { + private ClassDAO classDAO; + + public UserCardAdd(ClassDAO classDAO) { + this.classDAO = classDAO; + } + + public UserCardAdd() { + this.classDAO = new ClassDAOSql(); + } @Override public void handle(HttpExchange httpExchange) throws IOException { @@ -70,8 +79,7 @@ private List addUser(List items) throws DaoException { return userList; } - private User createStudent (List items) throws DaoException { - ClassDAO classDAO = new ClassDAOSql(); + public User createStudent (List items) throws DaoException { User newUser = new User.UserBuilder() .firstName(items.get(items.size()-2)) diff --git a/src/main/java/com/queststore/helpers/CookieHelper.java b/src/main/java/com/queststore/helpers/CookieHelper.java index ea6ff35..3ec3612 100644 --- a/src/main/java/com/queststore/helpers/CookieHelper.java +++ b/src/main/java/com/queststore/helpers/CookieHelper.java @@ -26,7 +26,7 @@ public List parseCookies(String cookieStr) { return cookies; } - private Optional getCookieByName(List cookies) { + public Optional getCookieByName(List cookies) { for (HttpCookie cookie : cookies) { if (cookie.getName().equals(SESSION_COOKIE_NAME)) { return Optional.of(cookie); diff --git a/src/test/java/com/queststore/Controller/StudentArtifactTransactionTest.java b/src/test/java/com/queststore/Controller/StudentArtifactTransactionTest.java new file mode 100644 index 0000000..d170ca2 --- /dev/null +++ b/src/test/java/com/queststore/Controller/StudentArtifactTransactionTest.java @@ -0,0 +1,10 @@ +package com.queststore.Controller; + +import static org.junit.Assert.*; + +public class StudentArtifactTransactionTest { + + public void canBeBought() { + + } +} \ No newline at end of file diff --git a/src/test/java/com/queststore/helpers/CookieHelperTest.java b/src/test/java/com/queststore/helpers/CookieHelperTest.java index f43e29d..cdc6679 100644 --- a/src/test/java/com/queststore/helpers/CookieHelperTest.java +++ b/src/test/java/com/queststore/helpers/CookieHelperTest.java @@ -1,6 +1,6 @@ package com.queststore.helpers; -import org.junit.Test; +import org.junit.*; import java.net.HttpCookie; import java.util.List; From b88bc31c0a491268c82fd8e0e048d8b81d95f19a Mon Sep 17 00:00:00 2001 From: PatrykMandrak Date: Wed, 15 May 2019 18:26:19 +0200 Subject: [PATCH 05/11] Added getCoolcoinBalance test --- .../com/queststore/Services/UserService.java | 3 +- .../queststore/Services/UserCardAddTest.java | 6 ++-- .../queststore/Services/UserServiceTest.java | 36 +++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/queststore/Services/UserServiceTest.java diff --git a/src/main/java/com/queststore/Services/UserService.java b/src/main/java/com/queststore/Services/UserService.java index eba2070..ff54b67 100644 --- a/src/main/java/com/queststore/Services/UserService.java +++ b/src/main/java/com/queststore/Services/UserService.java @@ -17,6 +17,7 @@ public class UserService { private ClassDAO classDAO; private TransactionDAO transactionDAO; private ConfigurationDAO configurationDAOsql = new ConfigurationDAOSql(); + public UserService(UserDAO userDAO, ClassDAO classDAO, TransactionDAO transactionDAO) { this.userDAO = userDAO; this.classDAO = classDAO; @@ -58,7 +59,7 @@ public int getCoinBalance(int userId) throws DaoException { return coinBalance; } - public String calculateUserLvl(int userId) throws DaoException{ + public String calculateUserLvl(int userId) throws DaoException { List questsList = new ArrayList<>(); int questId = 1; questsList.addAll(transactionDAO.getTransactions(userId, questId)); diff --git a/src/test/java/com/queststore/Services/UserCardAddTest.java b/src/test/java/com/queststore/Services/UserCardAddTest.java index 920bfce..1dd988a 100644 --- a/src/test/java/com/queststore/Services/UserCardAddTest.java +++ b/src/test/java/com/queststore/Services/UserCardAddTest.java @@ -2,7 +2,6 @@ import com.queststore.DAO.ClassDAO; import com.queststore.DAO.DaoException; -import com.queststore.Model.Class; import com.queststore.Model.User; import com.queststore.Model.UserType; import org.junit.jupiter.api.Test; @@ -14,14 +13,13 @@ import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; -public class UserCardAddTest { +class UserCardAddTest { @Test - public void createStudentTest2() throws DaoException { + void createStudentTest2() throws DaoException { ClassDAO classDAOMock = mock(ClassDAO.class); when(classDAOMock.getClassId(any(String.class))).thenReturn(java.util.Optional.of(1)); UserCardAdd userCardAdd = new UserCardAdd(classDAOMock); - //verify(classDAOMock.getClassId(any(String.class)), times(1)); List items = new ArrayList<>(); items.add("someEmailName"); items.add("someFirstName"); diff --git a/src/test/java/com/queststore/Services/UserServiceTest.java b/src/test/java/com/queststore/Services/UserServiceTest.java new file mode 100644 index 0000000..135f561 --- /dev/null +++ b/src/test/java/com/queststore/Services/UserServiceTest.java @@ -0,0 +1,36 @@ +package com.queststore.Services; + +import com.queststore.DAO.ClassDAOSql; +import com.queststore.DAO.DaoException; +import com.queststore.DAO.TransactionDAO; +import com.queststore.DAO.UserDAOSql; +import com.queststore.Model.Transaction; +import com.queststore.Model.TransactionStatus; +import com.queststore.Model.User; +import com.queststore.Model.UserType; +import org.junit.Test; + +import java.util.Collections; + +import static org.junit.Assert.*; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class UserServiceTest { + + + @Test + public void getCoinBalance() throws DaoException { + User user = new User(100, "kuba", "buba", "kubabuba@kupa.pl", null, null, new UserType(0, "student")); + TransactionDAO transactionDAOMock = mock(TransactionDAO.class); + + UserService userService = new UserService(new UserDAOSql(), new ClassDAOSql(), transactionDAOMock); + + TransactionStatus transactionStatus = new TransactionStatus(1, "accepted"); + when(transactionDAOMock.getTransactions(100, 1)).thenReturn(Collections.singletonList(new Transaction(1, null, null, null, transactionStatus, 40))); + when(transactionDAOMock.getTransactions(100, 2)).thenReturn(Collections.singletonList(new Transaction(1, null, null, null, transactionStatus, 30))); + + assertEquals(10, userService.getCoinBalance(user.getId())); + } +} \ No newline at end of file From d4f411b1bc06825faa173748deba656c0518405c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ziemowit=20B=C5=82a=C5=BCk=C3=B3w?= Date: Thu, 16 May 2019 11:40:02 +0200 Subject: [PATCH 06/11] Added calculateUserLvl test --- .../com/queststore/Services/UserService.java | 8 ++- .../queststore/Services/UserServiceTest.java | 61 ++++++++++++++++--- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/queststore/Services/UserService.java b/src/main/java/com/queststore/Services/UserService.java index ff54b67..4a2612c 100644 --- a/src/main/java/com/queststore/Services/UserService.java +++ b/src/main/java/com/queststore/Services/UserService.java @@ -16,12 +16,18 @@ public class UserService { private UserDAO userDAO; private ClassDAO classDAO; private TransactionDAO transactionDAO; - private ConfigurationDAO configurationDAOsql = new ConfigurationDAOSql(); + private ConfigurationDAO configurationDAOsql; public UserService(UserDAO userDAO, ClassDAO classDAO, TransactionDAO transactionDAO) { this.userDAO = userDAO; this.classDAO = classDAO; this.transactionDAO = transactionDAO; + if(configurationDAOsql==null) this.configurationDAOsql = new ConfigurationDAOSql(); + } + + public UserService(UserDAO userDAO, ClassDAO classDAO, TransactionDAO transactionDAO, ConfigurationDAO configurationDAO) { + this(userDAO, classDAO, transactionDAO); + this.configurationDAOsql = configurationDAO; } List getAllStudentsInMentorClass(int mentorId) throws DaoException { diff --git a/src/test/java/com/queststore/Services/UserServiceTest.java b/src/test/java/com/queststore/Services/UserServiceTest.java index 135f561..7d11488 100644 --- a/src/test/java/com/queststore/Services/UserServiceTest.java +++ b/src/test/java/com/queststore/Services/UserServiceTest.java @@ -1,16 +1,12 @@ package com.queststore.Services; -import com.queststore.DAO.ClassDAOSql; -import com.queststore.DAO.DaoException; -import com.queststore.DAO.TransactionDAO; -import com.queststore.DAO.UserDAOSql; -import com.queststore.Model.Transaction; -import com.queststore.Model.TransactionStatus; -import com.queststore.Model.User; -import com.queststore.Model.UserType; +import com.queststore.DAO.*; +import com.queststore.Model.*; import org.junit.Test; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; import static org.junit.Assert.*; import static org.mockito.Matchers.any; @@ -33,4 +29,53 @@ public void getCoinBalance() throws DaoException { assertEquals(10, userService.getCoinBalance(user.getId())); } +//Integer id, Date date, User user, Card card, TransactionStatus transactionStatus, int cost) { + @Test + public void calculateUserLvl() throws DaoException { + User user = new User(100, "kuba", "buba", "kubabuba@kupa.pl", null, null, new UserType(0, "student")); + TransactionDAO transactionDAOMock = getTransactionDAOMock(); + ConfigurationDAO configurationDAOMock = getConfigurationDAOMock(); + + UserService userService = new UserService( + null, + null, + transactionDAOMock, + configurationDAOMock + ); + + assertEquals("pro", userService.calculateUserLvl(100)); + } + + private TransactionDAO getTransactionDAOMock() throws DaoException { + TransactionDAO transactionDAOMock = mock(TransactionDAOSql.class); + UserType userType = new UserType(1, "accepted"); + List transactionList = new ArrayList<>(); + transactionList.add(new Transaction( + 0, + null, + null, + null, + new TransactionStatus(1, "accepted"), + 120 + )); + when(transactionDAOMock.getTransactions(100, 1)).thenReturn(transactionList); + return transactionDAOMock; + } + + private ConfigurationDAO getConfigurationDAOMock() throws DaoException { + ConfigurationDAO configurationDAOMock = mock(ConfigurationDAOSql.class); + List experienceLevels = getSampleExperienceLevelsList(); + when(configurationDAOMock.getAllLevels()).thenReturn(experienceLevels); + return configurationDAOMock; + } + + private List getSampleExperienceLevelsList() { + List experienceLevels = new ArrayList<>(); + experienceLevels.add(new ExperienceLevel(1, "noob0", 0)); + experienceLevels.add(new ExperienceLevel(2, "noob2", 30)); + experienceLevels.add(new ExperienceLevel(3, "noob3", 50)); + experienceLevels.add(new ExperienceLevel(4, "good", 80)); + experienceLevels.add(new ExperienceLevel(5, "pro", 119)); + return experienceLevels; + } } \ No newline at end of file From c697b270dcdf77a98d8ee39d918aaceb69212e29 Mon Sep 17 00:00:00 2001 From: PatrykMandrak Date: Thu, 16 May 2019 11:40:36 +0200 Subject: [PATCH 07/11] Added updateCardInDB test --- pom.xml | 6 +++ .../queststore/Services/ItemCardUpdate.java | 12 +++++- .../Services/ItemCardUpdateTest.java | 41 +++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/queststore/Services/ItemCardUpdateTest.java diff --git a/pom.xml b/pom.xml index 051868c..a782247 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,12 @@ RELEASE test + + junit + junit + 4.12 + test + diff --git a/src/main/java/com/queststore/Services/ItemCardUpdate.java b/src/main/java/com/queststore/Services/ItemCardUpdate.java index 7cd7599..0123548 100644 --- a/src/main/java/com/queststore/Services/ItemCardUpdate.java +++ b/src/main/java/com/queststore/Services/ItemCardUpdate.java @@ -17,6 +17,15 @@ import java.util.List; public class ItemCardUpdate implements HttpHandler { + private CardDAO cardDAO; + + ItemCardUpdate(CardDAO cardDAO) { + this.cardDAO = cardDAO; + } + + public ItemCardUpdate() { + this.cardDAO = new CardDAOSql(); + } @Override public void handle(HttpExchange httpExchange) throws IOException { @@ -51,8 +60,7 @@ public void handle(HttpExchange httpExchange) throws IOException { } - private List updateCardInDB(List items) throws DaoException { - CardDAO cardDAO = new CardDAOSql(); + List updateCardInDB(List items) throws DaoException { List cardList = new ArrayList<>(); int artifactTypeId = 2; diff --git a/src/test/java/com/queststore/Services/ItemCardUpdateTest.java b/src/test/java/com/queststore/Services/ItemCardUpdateTest.java new file mode 100644 index 0000000..7336861 --- /dev/null +++ b/src/test/java/com/queststore/Services/ItemCardUpdateTest.java @@ -0,0 +1,41 @@ +package com.queststore.Services; + +import com.queststore.DAO.CardDAO; +import com.queststore.DAO.DaoException; +import com.queststore.Model.Card; +import com.queststore.Model.CardTypes; +import com.queststore.Model.Categories; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ItemCardUpdateTest { + + @Test + public void updateCardInDB() throws DaoException { + CardDAO cardDAOMock = mock(CardDAO.class); + + ItemCardUpdate itemCardUpdate = new ItemCardUpdate(cardDAOMock); + + List items = new ArrayList<>(); + items.add("1"); + items.add("dupa"); + items.add("1"); + items.add("dupa"); + + Card card = new Card(Integer.parseInt(items.get(0)), items.get(1), items.get(3), new Categories(1, "easy"), null, + Integer.parseInt(items.get(2)), new CardTypes(2, "artifact"), true); + + List listOfCards = new ArrayList<>(); + listOfCards.add(card); + + when(cardDAOMock.getCardsOfType(cardDAOMock.getCardTypeById(2))).thenReturn(listOfCards); + + assertEquals(listOfCards, itemCardUpdate.updateCardInDB(items)); + } +} \ No newline at end of file From e34be48a77c05f22963703b1f301fcb399df83cd Mon Sep 17 00:00:00 2001 From: PatrykMandrak Date: Thu, 16 May 2019 13:27:33 +0200 Subject: [PATCH 08/11] Added addCardToDB test --- .../com/queststore/Services/ItemCardAdd.java | 13 ++++-- .../queststore/Services/ItemCardAddTest.java | 42 +++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/queststore/Services/ItemCardAddTest.java diff --git a/src/main/java/com/queststore/Services/ItemCardAdd.java b/src/main/java/com/queststore/Services/ItemCardAdd.java index 81d0d82..62da38e 100644 --- a/src/main/java/com/queststore/Services/ItemCardAdd.java +++ b/src/main/java/com/queststore/Services/ItemCardAdd.java @@ -17,6 +17,15 @@ import java.util.List; public class ItemCardAdd implements HttpHandler { + private CardDAO cardDAO; + + ItemCardAdd(CardDAO cardDAO) { + this.cardDAO = cardDAO; + } + + public ItemCardAdd() { + this.cardDAO = new CardDAOSql(); + } @Override public void handle(HttpExchange httpExchange) throws IOException { @@ -53,14 +62,12 @@ public void handle(HttpExchange httpExchange) throws IOException { } public List addCardToDB(List items) throws DaoException { - - CardDAO cardDAO = new CardDAOSql(); List cardList = new ArrayList<>(); int artifactTypeId = 2; Card newCard = new Card(4, items.get(0), items.get(2), new Categories(1, "easy"), null, Integer.parseInt((String) items.get(1)), new CardTypes(artifactTypeId, "artifact"), true); cardDAO.add(newCard); - cardList.addAll(cardDAO.getCardsOfType(cardDAO.getCardTypeById(artifactTypeId))); + cardList.add(newCard); return cardList; } diff --git a/src/test/java/com/queststore/Services/ItemCardAddTest.java b/src/test/java/com/queststore/Services/ItemCardAddTest.java new file mode 100644 index 0000000..ee6fb6f --- /dev/null +++ b/src/test/java/com/queststore/Services/ItemCardAddTest.java @@ -0,0 +1,42 @@ +package com.queststore.Services; + +import com.queststore.DAO.CardDAO; +import com.queststore.DAO.DaoException; +import com.queststore.Model.Card; +import com.queststore.Model.CardTypes; +import com.queststore.Model.Categories; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ItemCardAddTest{ + + @Test + public void addCardToDB() throws DaoException { + CardDAO cardDAOMock = mock(CardDAO.class); + ItemCardAdd itemCardAdd = new ItemCardAdd(cardDAOMock); + + List items = new ArrayList<>(); + items.add("1"); + items.add("2"); + items.add("3"); + items.add("4"); + + Card card = new Card(4, items.get(0), items.get(2), new Categories(1, "easy"), null, + Integer.parseInt(items.get(1)), new CardTypes(2, "artifact"), true); + + List listOfCards = new ArrayList<>(); + listOfCards.add(card); + + assertEquals(itemCardAdd.addCardToDB(items).get(0).getValue(), listOfCards.get(0).getValue()); + assertEquals(itemCardAdd.addCardToDB(items).get(0).getId(), listOfCards.get(0).getId()); + assertEquals(itemCardAdd.addCardToDB(items).get(0).getDescription(), listOfCards.get(0).getDescription()); + assertEquals(itemCardAdd.addCardToDB(items).get(0).getName(), listOfCards.get(0).getName()); + } + +} \ No newline at end of file From ad13eff149a6af99d2f1908cbfa67cb68ba984ab Mon Sep 17 00:00:00 2001 From: PatrykMandrak Date: Thu, 16 May 2019 13:53:34 +0200 Subject: [PATCH 09/11] Added parseJSONListToArray test and code refactor --- .../Services/ConfigurationService.java | 4 ---- .../com/queststore/Services/UserCardAdd.java | 2 +- .../com/queststore/Services/UserService.java | 6 +++--- .../StudentArtifactTransactionTest.java | 10 ---------- .../queststore/Services/ItemCardAddTest.java | 3 +-- .../queststore/Services/JSONparserTest.java | 20 +++++++++++++++++++ .../queststore/Services/UserServiceTest.java | 3 ++- 7 files changed, 27 insertions(+), 21 deletions(-) delete mode 100644 src/main/java/com/queststore/Services/ConfigurationService.java delete mode 100644 src/test/java/com/queststore/Controller/StudentArtifactTransactionTest.java create mode 100644 src/test/java/com/queststore/Services/JSONparserTest.java diff --git a/src/main/java/com/queststore/Services/ConfigurationService.java b/src/main/java/com/queststore/Services/ConfigurationService.java deleted file mode 100644 index b3f84c6..0000000 --- a/src/main/java/com/queststore/Services/ConfigurationService.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.queststore.Services; - -public class ConfigurationService { -} diff --git a/src/main/java/com/queststore/Services/UserCardAdd.java b/src/main/java/com/queststore/Services/UserCardAdd.java index 39fc761..a4a9a15 100644 --- a/src/main/java/com/queststore/Services/UserCardAdd.java +++ b/src/main/java/com/queststore/Services/UserCardAdd.java @@ -79,7 +79,7 @@ private List addUser(List items) throws DaoException { return userList; } - public User createStudent (List items) throws DaoException { + User createStudent (List items) throws DaoException { User newUser = new User.UserBuilder() .firstName(items.get(items.size()-2)) diff --git a/src/main/java/com/queststore/Services/UserService.java b/src/main/java/com/queststore/Services/UserService.java index 4a2612c..2775878 100644 --- a/src/main/java/com/queststore/Services/UserService.java +++ b/src/main/java/com/queststore/Services/UserService.java @@ -25,7 +25,7 @@ public UserService(UserDAO userDAO, ClassDAO classDAO, TransactionDAO transactio if(configurationDAOsql==null) this.configurationDAOsql = new ConfigurationDAOSql(); } - public UserService(UserDAO userDAO, ClassDAO classDAO, TransactionDAO transactionDAO, ConfigurationDAO configurationDAO) { + UserService(UserDAO userDAO, ClassDAO classDAO, TransactionDAO transactionDAO, ConfigurationDAO configurationDAO) { this(userDAO, classDAO, transactionDAO); this.configurationDAOsql = configurationDAO; } @@ -49,7 +49,7 @@ public int getCoinBalance(int userId) throws DaoException { questsList.addAll(transactionDAO.getTransactions(userId, questId)); artifactsList.addAll(transactionDAO.getTransactions(userId, artifactId)); - Integer coinBalance = 0; + int coinBalance = 0; for (Transaction transaction : questsList) { if (transaction.getTransactionStatus().getName().equals("accepted")) { coinBalance += transaction.getCost(); @@ -70,7 +70,7 @@ public String calculateUserLvl(int userId) throws DaoException { int questId = 1; questsList.addAll(transactionDAO.getTransactions(userId, questId)); - Integer coinBalance = 0; + int coinBalance = 0; for (Transaction transaction : questsList) { if (transaction.getTransactionStatus().getName().equals("accepted")) { coinBalance += transaction.getCost(); diff --git a/src/test/java/com/queststore/Controller/StudentArtifactTransactionTest.java b/src/test/java/com/queststore/Controller/StudentArtifactTransactionTest.java deleted file mode 100644 index d170ca2..0000000 --- a/src/test/java/com/queststore/Controller/StudentArtifactTransactionTest.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.queststore.Controller; - -import static org.junit.Assert.*; - -public class StudentArtifactTransactionTest { - - public void canBeBought() { - - } -} \ No newline at end of file diff --git a/src/test/java/com/queststore/Services/ItemCardAddTest.java b/src/test/java/com/queststore/Services/ItemCardAddTest.java index ee6fb6f..0524a0e 100644 --- a/src/test/java/com/queststore/Services/ItemCardAddTest.java +++ b/src/test/java/com/queststore/Services/ItemCardAddTest.java @@ -12,9 +12,8 @@ import static org.junit.Assert.*; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -public class ItemCardAddTest{ +public class ItemCardAddTest { @Test public void addCardToDB() throws DaoException { diff --git a/src/test/java/com/queststore/Services/JSONparserTest.java b/src/test/java/com/queststore/Services/JSONparserTest.java new file mode 100644 index 0000000..59689d6 --- /dev/null +++ b/src/test/java/com/queststore/Services/JSONparserTest.java @@ -0,0 +1,20 @@ +package com.queststore.Services; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class JSONparserTest { + + @Test + public void parseJSONlistToArray() throws java.io.IOException { + JSONparser jsoNparser = new JSONparser(); + String json = "[\n" + + " \"my/path/old\",\n" + + " \"my/path/new\"\n" + + " ]"; + System.out.println(jsoNparser.parseJSONlistToArray(json)); + assertEquals("my/path/old", jsoNparser.parseJSONlistToArray(json).get(0)); + assertEquals("my/path/new", jsoNparser.parseJSONlistToArray(json).get(1)); + } +} \ No newline at end of file diff --git a/src/test/java/com/queststore/Services/UserServiceTest.java b/src/test/java/com/queststore/Services/UserServiceTest.java index 7d11488..87c8bf8 100644 --- a/src/test/java/com/queststore/Services/UserServiceTest.java +++ b/src/test/java/com/queststore/Services/UserServiceTest.java @@ -29,7 +29,8 @@ public void getCoinBalance() throws DaoException { assertEquals(10, userService.getCoinBalance(user.getId())); } -//Integer id, Date date, User user, Card card, TransactionStatus transactionStatus, int cost) { + + //Integer id, Date date, User user, Card card, TransactionStatus transactionStatus, int cost) { @Test public void calculateUserLvl() throws DaoException { User user = new User(100, "kuba", "buba", "kubabuba@kupa.pl", null, null, new UserType(0, "student")); From 296bee5b071f004a30d6d0a48b90517b245f78a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ziemowit=20B=C5=82a=C5=BCk=C3=B3w?= Date: Thu, 16 May 2019 15:54:18 +0200 Subject: [PATCH 10/11] Added convertJSONtoString test --- .../queststore/Services/JSONparserTest.java | 40 ++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/queststore/Services/JSONparserTest.java b/src/test/java/com/queststore/Services/JSONparserTest.java index 59689d6..7f3c05e 100644 --- a/src/test/java/com/queststore/Services/JSONparserTest.java +++ b/src/test/java/com/queststore/Services/JSONparserTest.java @@ -1,20 +1,50 @@ package com.queststore.Services; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +import com.sun.net.httpserver.HttpExchange; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class JSONparserTest { + private JSONparser jsonParser; + + @BeforeEach + void setup() { + jsonParser = new JSONparser(); + } + + @AfterEach + void tearDown() { + jsonParser = null; + } + @Test public void parseJSONlistToArray() throws java.io.IOException { - JSONparser jsoNparser = new JSONparser(); String json = "[\n" + " \"my/path/old\",\n" + " \"my/path/new\"\n" + " ]"; - System.out.println(jsoNparser.parseJSONlistToArray(json)); - assertEquals("my/path/old", jsoNparser.parseJSONlistToArray(json).get(0)); - assertEquals("my/path/new", jsoNparser.parseJSONlistToArray(json).get(1)); + System.out.println(jsonParser.parseJSONlistToArray(json)); + assertEquals("my/path/old", jsonParser.parseJSONlistToArray(json).get(0)); + assertEquals("my/path/new", jsonParser.parseJSONlistToArray(json).get(1)); + } + + @Test + public void convertJSONtoString() throws IOException { + HttpExchange httpExchangeMock = mock(HttpExchange.class); + String bodyString = "sampleRequestBody"; + InputStream inputStream = new ByteArrayInputStream(bodyString.getBytes()); + when(httpExchangeMock.getRequestBody()).thenReturn(inputStream); + assertEquals(bodyString, jsonParser.convertJSONtoString(httpExchangeMock)); } } \ No newline at end of file From f6081bda673fcc7ebd12ab8cdad129e096cbb98f Mon Sep 17 00:00:00 2001 From: PatrykMandrak Date: Fri, 17 May 2019 10:52:27 +0200 Subject: [PATCH 11/11] Code refactor --- src/test/java/com/queststore/Services/JSONparserTest.java | 4 ++-- src/test/java/com/queststore/Services/UserServiceTest.java | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/queststore/Services/JSONparserTest.java b/src/test/java/com/queststore/Services/JSONparserTest.java index 7f3c05e..57bda0f 100644 --- a/src/test/java/com/queststore/Services/JSONparserTest.java +++ b/src/test/java/com/queststore/Services/JSONparserTest.java @@ -29,7 +29,7 @@ void tearDown() { } @Test - public void parseJSONlistToArray() throws java.io.IOException { + void parseJSONlistToArray() throws java.io.IOException { String json = "[\n" + " \"my/path/old\",\n" + " \"my/path/new\"\n" + @@ -40,7 +40,7 @@ public void parseJSONlistToArray() throws java.io.IOException { } @Test - public void convertJSONtoString() throws IOException { + void convertJSONtoString() throws IOException { HttpExchange httpExchangeMock = mock(HttpExchange.class); String bodyString = "sampleRequestBody"; InputStream inputStream = new ByteArrayInputStream(bodyString.getBytes()); diff --git a/src/test/java/com/queststore/Services/UserServiceTest.java b/src/test/java/com/queststore/Services/UserServiceTest.java index 87c8bf8..777956c 100644 --- a/src/test/java/com/queststore/Services/UserServiceTest.java +++ b/src/test/java/com/queststore/Services/UserServiceTest.java @@ -9,7 +9,6 @@ import java.util.List; import static org.junit.Assert.*; -import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -30,10 +29,8 @@ public void getCoinBalance() throws DaoException { assertEquals(10, userService.getCoinBalance(user.getId())); } - //Integer id, Date date, User user, Card card, TransactionStatus transactionStatus, int cost) { @Test public void calculateUserLvl() throws DaoException { - User user = new User(100, "kuba", "buba", "kubabuba@kupa.pl", null, null, new UserType(0, "student")); TransactionDAO transactionDAOMock = getTransactionDAOMock(); ConfigurationDAO configurationDAOMock = getConfigurationDAOMock(); @@ -49,7 +46,6 @@ public void calculateUserLvl() throws DaoException { private TransactionDAO getTransactionDAOMock() throws DaoException { TransactionDAO transactionDAOMock = mock(TransactionDAOSql.class); - UserType userType = new UserType(1, "accepted"); List transactionList = new ArrayList<>(); transactionList.add(new Transaction( 0,