Skip to content
Open
25 changes: 25 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,34 @@
<version>20180813</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<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.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>



<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/queststore/DAO/DBCPDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

This file was deleted.

13 changes: 10 additions & 3 deletions src/main/java/com/queststore/Services/ItemCardAdd.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -53,14 +62,12 @@ public void handle(HttpExchange httpExchange) throws IOException {
}

public List<Card> addCardToDB(List<String> items) throws DaoException {

CardDAO cardDAO = new CardDAOSql();
List<Card> 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;
}

Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/queststore/Services/ItemCardUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -51,8 +60,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
}


private List<Card> updateCardInDB(List<String> items) throws DaoException {
CardDAO cardDAO = new CardDAOSql();
List<Card> updateCardInDB(List<String> items) throws DaoException {
List<Card> cardList = new ArrayList<>();
int artifactTypeId = 2;

Expand All @@ -61,7 +69,5 @@ private List<Card> updateCardInDB(List<String> items) throws DaoException {
cardDAO.update(createCart);
cardList.addAll(cardDAO.getCardsOfType(cardDAO.getCardTypeById(artifactTypeId)));
return cardList;

}

}
12 changes: 10 additions & 2 deletions src/main/java/com/queststore/Services/UserCardAdd.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -70,8 +79,7 @@ private List<User> addUser(List<String> items) throws DaoException {
return userList;
}

private User createStudent (List<String> items) throws DaoException {
ClassDAO classDAO = new ClassDAOSql();
User createStudent (List<String> items) throws DaoException {

User newUser = new User.UserBuilder()
.firstName(items.get(items.size()-2))
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/queststore/Services/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +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();
}

UserService(UserDAO userDAO, ClassDAO classDAO, TransactionDAO transactionDAO, ConfigurationDAO configurationDAO) {
this(userDAO, classDAO, transactionDAO);
this.configurationDAOsql = configurationDAO;
}

List<User> getAllStudentsInMentorClass(int mentorId) throws DaoException {
Expand All @@ -42,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();
Expand All @@ -58,12 +65,12 @@ public int getCoinBalance(int userId) throws DaoException {
return coinBalance;
}

public String calculateUserLvl(int userId) throws DaoException{
public String calculateUserLvl(int userId) throws DaoException {
List<Transaction> questsList = new ArrayList<>();
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();
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/queststore/helpers/CookieHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CookieHelper {

private static final String SESSION_COOKIE_NAME = "session_id";

private List<HttpCookie> parseCookies(String cookieStr) {
public List<HttpCookie> parseCookies(String cookieStr) {
List<HttpCookie> cookies = new ArrayList<>();

if (cookieStr == null || cookieStr.isEmpty()) {
Expand All @@ -23,11 +23,10 @@ private List<HttpCookie> parseCookies(String cookieStr) {
String[] pair = cookie.split("=");
cookies.add(new HttpCookie(pair[0].trim(), pair[1].trim().replaceAll("\"", "")));
}

return cookies;
}

private Optional<HttpCookie> getCookieByName(List<HttpCookie> cookies) {
public Optional<HttpCookie> getCookieByName(List<HttpCookie> cookies) {
for (HttpCookie cookie : cookies) {
if (cookie.getName().equals(SESSION_COOKIE_NAME)) {
return Optional.of(cookie);
Expand All @@ -43,6 +42,7 @@ public HttpCookie generateNewSessionIdCookie() {

public Optional<HttpCookie> getSessionIdCookie(HttpExchange exchange) {
String cookieStr = exchange.getRequestHeaders().getFirst("Cookie");
System.out.println(cookieStr);
List<HttpCookie> cookies = parseCookies(cookieStr);
return getCookieByName(cookies);
}
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/com/queststore/Services/ItemCardAddTest.java
Original file line number Diff line number Diff line change
@@ -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;

public class ItemCardAddTest {

@Test
public void addCardToDB() throws DaoException {
CardDAO cardDAOMock = mock(CardDAO.class);
ItemCardAdd itemCardAdd = new ItemCardAdd(cardDAOMock);

List<String> 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<Card> 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());
}

}
41 changes: 41 additions & 0 deletions src/test/java/com/queststore/Services/ItemCardUpdateTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<Card> listOfCards = new ArrayList<>();
listOfCards.add(card);

when(cardDAOMock.getCardsOfType(cardDAOMock.getCardTypeById(2))).thenReturn(listOfCards);

assertEquals(listOfCards, itemCardUpdate.updateCardInDB(items));
}
}
50 changes: 50 additions & 0 deletions src/test/java/com/queststore/Services/JSONparserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.queststore.Services;

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
void parseJSONlistToArray() throws java.io.IOException {
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));
}

@Test
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));
}
}
Loading