diff --git a/src/main/java/Team5/SmartTowns/users/UserRepositoryJDBC.java b/src/main/java/Team5/SmartTowns/users/UserRepositoryJDBC.java index a4ea02d6fde658ce1478cd180f954203ac756a6e..a88ac6e1c91fc85e5eb19fb6f0f7fcef3b9cb833 100644 --- a/src/main/java/Team5/SmartTowns/users/UserRepositoryJDBC.java +++ b/src/main/java/Team5/SmartTowns/users/UserRepositoryJDBC.java @@ -50,8 +50,11 @@ public class UserRepositoryJDBC implements UserRepository{ public boolean unlockSticker(String username, int packID, int stickerID){ /* Adds entry in the stickerprogress database, effectively unlocking the sticker for the user * Returns false if no sticker is unlocked */ - String query = "SELECT COUNT(id) FROM stickers WHERE (stickerID, packID) = (stickerID, packID)"; - if (jdbc.queryForObject(query, Integer.class) == 1){ //Checks if sticker exists + String query = "SELECT COUNT(id) FROM stickers WHERE (stickerID, packID) = (?, ?)"; + + int stickerCount = jdbc.queryForObject(query, Integer.class, stickerID, packID); + + if (stickerCount == 1){ //Checks if sticker exists String sql = "INSERT INTO stickerprogress (username, packID, stickerID) VALUES (?,?,?)"; jdbc.update(sql, username, packID, stickerID); return true; diff --git a/src/test/java/Team5/SmartTowns/DataSourceConfig.java b/src/test/java/Team5/SmartTowns/DataSourceConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..eb7623abb1831b13d72138f824b600d6ce361fa7 --- /dev/null +++ b/src/test/java/Team5/SmartTowns/DataSourceConfig.java @@ -0,0 +1,20 @@ +package Team5.SmartTowns; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.DriverManagerDataSource; + +import javax.sql.DataSource; +@Configuration +public class DataSourceConfig { + + @Bean + public DataSource dataSource(){ + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setUrl("jdbc:mariadb://localhost:3306/test_towns"); + dataSource.setUsername("root"); + dataSource.setPassword("comsc"); + return dataSource; + } + +} diff --git a/src/test/java/Team5/SmartTowns/testUsers.java b/src/test/java/Team5/SmartTowns/testUsers.java new file mode 100644 index 0000000000000000000000000000000000000000..2145c47165ed072b8a4c3478774da7410f881414 --- /dev/null +++ b/src/test/java/Team5/SmartTowns/testUsers.java @@ -0,0 +1,79 @@ +package Team5.SmartTowns; + +import Team5.SmartTowns.rewards.RewardsRepository; +import Team5.SmartTowns.users.NewUser; +import Team5.SmartTowns.users.User; +import Team5.SmartTowns.users.UserRepository; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.ArrayList; +import java.util.List; + +import static junit.framework.TestCase.*; + +@SpringBootTest +public class testUsers { + + + @Autowired + UserRepository userRepository; + @Autowired + RewardsRepository rewardsRepository; + + @Test + public void getAllUsersTest(){ // test if we can get all users, admin is sa known user + List<User> users = userRepository.getAllUsers(); + User user = new User("Admin","Admin"); + Assertions.assertEquals("Admin", users.get(0).getName()); + } + + @Test // test if new users can be added + public void addAUserTest(){ + int userNumberBeforeAdd = userRepository.getAllUsers().size(); + NewUser newuser = new NewUser("Meow","Woof","Cat@Dogs.com"); + boolean trueIfAdded= userRepository.addUser(newuser.getName(), newuser.getEmail(), newuser.getPassword()); + int userNumberAfterAdd = userRepository.getAllUsers().size(); + assertTrue(trueIfAdded); + } + + @Test // test if new users and inserted users can be found + public void doesUserExistTest(){ + Boolean insertedUserFoundByEmail = userRepository.doesUserExist("Kevin@Gmail.com"); + NewUser newuser = new NewUser("MeowMeow","WoofMeow","CatMeow@Dogs.com"); + Boolean newUser = userRepository.addUser(newuser.getName(), newuser.getEmail(), newuser.getPassword()); + Boolean newUserFoundByEmail = userRepository.doesUserExist(newuser.getEmail()); + int compareTwoSearches = Boolean.compare(insertedUserFoundByEmail, newUserFoundByEmail); + assertEquals(0,compareTwoSearches); // if 0, both values are the same + + + } + + @Test + public void canUsersUnlockStickersTest(){ + NewUser newuser = new NewUser("MeowMeowMeow","WoofMeowMeow","CatMeowMeow@Dogs.com"); + Boolean newUser = userRepository.addUser(newuser.getName(), newuser.getEmail(), newuser.getPassword()); + Boolean doesStickerUnlock = userRepository.unlockSticker(newuser.getName(),2,2); + System.out.println(doesStickerUnlock); + assertTrue(doesStickerUnlock); + } + @Test + public void canUsersUnlockStickersAndViewThemTest(){ + NewUser newuser = new NewUser("MeowMeowMeowMeow","WoofMeowMeowMeow","CatMeowMeowMeow@Dogs.com"); + NewUser newuserTwo = new NewUser("Jumper","Baa","Sheep@Wool.com"); + Boolean newUser = userRepository.addUser(newuser.getName(), newuser.getEmail(), newuser.getPassword()); + Boolean newUserTwo = userRepository.addUser(newuserTwo.getName(), newuserTwo.getEmail(), newuserTwo.getPassword()); + Boolean doesStickerUnlock = userRepository.unlockSticker(newuser.getName(),1,2); + List<Long> newUserStickerCollection = userRepository.getUserStickersFromPack(newuser.getName(),1); + List<Long> newUserStickerCollectionTwo = userRepository.getUserStickersFromPack(newuserTwo.getName(),1); // compare and see if only new suer that has unlocked a sticker ahs one in their collection for pack 1 + int newUserStickerList = newUserStickerCollection.size(); + int newUserStickerListTwo = newUserStickerCollectionTwo.size(); // should have different sizes + assertNotSame(newUserStickerList,newUserStickerListTwo); + + } + + +} diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 21175ee7cbfde438ed0062b7d07937e2c9e69e58..5abd1133a1282d079e81ddf4d5f0185fb43a5b40 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -3,5 +3,5 @@ spring.datasource.username=root spring.datasource.password=comsc spring.sql.init.mode=always -spring.sql.init.schema-locations=classpath:schema.sql -spring.sql.init.data-locations=classpath:data.sql +spring.sql.init.schema-locations=classpath:schema.sql, classpath:schema-test.sql +spring.sql.init.data-locations=classpath:data.sql, classpath:test-data.sql \ No newline at end of file diff --git a/src/test/resources/schema-test.sql b/src/test/resources/schema-test.sql new file mode 100644 index 0000000000000000000000000000000000000000..744c10245fa965d81b641fc0cf3231ea612782a7 --- /dev/null +++ b/src/test/resources/schema-test.sql @@ -0,0 +1,110 @@ + + + + +DROP DATABASE IF EXISTS test_towns; +CREATE DATABASE IF NOT EXISTS test_towns; +USE test_towns; +DROP TABLE IF EXISTS trails; +DROP TABLE IF EXISTS locations; +DROP TABLE IF EXISTS users; +DROP TABLE IF EXISTS stickers; +DROP TABLE IF EXISTS packs; +DROP TABLE IF EXISTS stickerProgress; + + +CREATE TABLE IF NOT EXISTS trails ( + trailID bigint auto_increment primary key, + name varchar(128), + tru boolean +) engine=InnoDB; + +drop table if exists locationCoordinates; +drop table if exists locations; +create table if not exists locations +( + locationID bigint auto_increment primary key, + locationName varchar(128), + locationEmail varchar(128), + locationDescription longtext, + locationPlace varchar(255), + locationTrailID varchar(128), + locationApproved boolean +) engine=InnoDB; +CREATE TABLE IF NOT EXISTS users ( + username varchar(30) primary key NOT NULL, + id bigint auto_increment unique, /*DEPRECATED COLUMN, LEFT IN WHILE SOME OTHER FUNCTIONS STILL USE IT*/ + email varchar(128), + password varchar(30) NOT NULL, + enabled boolean default true +); + +CREATE TABLE IF NOT EXISTS authorities ( + id bigint primary key auto_increment NOT NULL, + username varchar(30) NOT NULL , + authority varchar(45) NOT NULL +); + +CREATE TABLE IF NOT EXISTS packs ( + id bigint auto_increment primary key, + name varchar(20) NOT NULL, + description text +); + +CREATE TABLE IF NOT EXISTS stickers ( + id bigint auto_increment primary key, + packID bigint NOT NULL, + FOREIGN KEY (packID) REFERENCES packs(id) + ON DELETE CASCADE + ON UPDATE RESTRICT, + stickerID bigint NOT NULL, /*STICKER ID NUMBER WITHIN ITS OWN PACK*/ + name varchar(30) NOT NULL, + description text NOT NULL, + rarity tinyint +); + +CREATE TABLE IF NOT EXISTS stickerProgress ( + id bigint auto_increment primary key, + username varchar(30) NOT NULL, + FOREIGN KEY (username) REFERENCES users(username) + ON DELETE CASCADE + ON UPDATE RESTRICT, + packID bigint NOT NULL, + FOREIGN KEY (packID) REFERENCES packs(id) + ON DELETE CASCADE + ON UPDATE RESTRICT, + stickerID bigint NOT NULL, + FOREIGN KEY (stickerID) REFERENCES stickers(id) + ON DELETE CASCADE + ON UPDATE RESTRICT +); + +create table if not exists locationCoordinates +( + locationCoordID bigint auto_increment primary key, + locationID bigint, + Foreign Key (locationID) REFERENCES locations(locationID) + ON DELETE CASCADE + ON UPDATE RESTRICT, + locationCoordsLat DECIMAL(8,6), + locationCoordsLong DECIMAL(8,6) + + +)engine=InnoDB; + + +drop table if exists townsWithTrails; +create table if not exists townsWithTrails +( + townID bigint auto_increment primary key, + townName varchar(128), + townCentreCoordsLat varchar(128), + townCentreCoordsLong varchar(128), + townUppermostCoordsLat varchar(128), + townLowermostCoordsLat varchar(128), + townLeftmostCoordsLong varchar(128), + townRightmostCoordsLong varchar(128) + +)engine=InnoDB; + + diff --git a/src/test/resources/test-data.sql b/src/test/resources/test-data.sql new file mode 100644 index 0000000000000000000000000000000000000000..05db12e66b276dace05661710dff01ba513875d7 --- /dev/null +++ b/src/test/resources/test-data.sql @@ -0,0 +1,68 @@ +delete from trails; +insert into trails ( Name,tru) value ( 'Caerphilly Coffee Trail',false); +insert into trails ( Name,tru) value ( 'Penarth Dragon Trail',true); + +delete from locations; +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'St Cenydd','','Location description here','Caerphilly',0101, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'The Castle','','Location description here','Caerphilly',0101, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Medieval Trades','','Location description here','Caerphilly',0101, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'The Queen''s War','','Location description here','Caerphilly',0101, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'The Green Lady','','Location description here','Caerphilly',0101, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Armoury','','Location description here','Caerphilly',0101, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Architecture','','Location description here','Caerphilly',0101, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( '21st Century Landmark','','Location description here','Caerphilly',0101, true); + +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'JD Wetherspoons-Malcolm Uphill','','Location description here','Caerphilly',0102, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Caerphilly Cwtch','','Location description here','Caerphilly',0102, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Caerphilly Conservative Club','','Location description here','Caerphilly',0102, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'The King''s Arms','','Location description here','Caerphilly',0102, true); + +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Caerphilly Bus Station','','Location description here','Caerphilly',0103, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'The Medieval Courthouse','','Location description here','Caerphilly',0103, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ('Caerphilly Castle','','Location description here','Caerphilly',0103, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Ty Vaughan House','','Location description here','Caerphilly',0103, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Risca Colliery','','Location description here','Risca',0201, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'Black Vein Colliery Disaster','','Location description here','Risca',0201, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'The Esplanade','','Location description here','Penarth',0301, true); +insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'The Old Swimming Baths','','Location description here','Penarth',0301, true); + + + + + +DELETE FROM packs; +INSERT INTO packs (name, description) VALUE ('Wales Football Team', 'Pack of Welsh Football Players in the National Team'); +INSERT INTO packs (name, description) VALUE ('Wales Rugby Team', 'Pack of Welsh Rugby Players in the National Team'); +INSERT INTO packs (name, description) VALUE ('Welsh Heritage', 'Pack About Welsh Heritage'); + +DELETE FROM stickers; +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (1, 1, 'wayne_hennessey', 'Wales Football Team Player', '2'); +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (1, 2, 'neco_williams', 'Wales Football Team Player', '2'); +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (1, 3, 'joe_morrell', 'Wales Football Team Player', '2'); +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (1, 4, 'ethan_ampadu', 'Wales Football Team Player', '2'); +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (1, 5, 'connor_roberts', 'Wales Football Team Player', '2'); +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (2, 1, 'Taine_Basham', 'Wales Rugby Team Player', '1'); +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (2, 2, 'Adam Beard', 'Wales Rugby Team Player', '1'); +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (2, 3, 'Elliot Dee', 'Wales Rugby Team Player', '1'); +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (2, 4, 'Corey Domachowski', 'Wales Rugby Team Player', '1'); +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (2, 5, 'Ryan Elias', 'Wales Rugby Team Player', '1'); +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (3, 1, 'Welsh Lady', 'Welsh Heritage', '1'); +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (3, 2, 'Welsh Outline', 'Welsh Heritage', '1'); +INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (3, 3, 'Welsh Spoon', 'Welsh Heritage', '1'); + +INSERT INTO users (username, password) VALUE ('Admin', 'admin'); +INSERT INTO users (username, password) VALUE ('Hannah', 'root'); +INSERT INTO users (username, password) VALUE ('Nigel', 'root'); +INSERT INTO users (username, password) VALUE ('Oscar', 'root'); +INSERT INTO users (username, email, password) VALUE ('kevin','Kevin@Gmail.com' ,'root'); + +INSERT INTO authorities (username, authority) VALUE ('Admin', 'ADMIN'); +INSERT INTO authorities (username, authority) VALUE ('Hannah', 'USER'); + +DELETE FROM stickerprogress; +INSERT INTO stickerprogress (username, packID, stickerID) VALUE ('Admin', 1, 1); +INSERT INTO stickerprogress (username, packID, stickerID) VALUE ('Admin', 1, 2); +INSERT INTO stickerprogress (username, packID, stickerID) VALUE ('Admin', 1, 3); +INSERT INTO stickerprogress (username, packID, stickerID) VALUE ('Admin', 1, 5); +INSERT INTO stickerprogress (username, packID, stickerID) VALUE ('Admin', 2, 1); +INSERT INTO stickerprogress (username, packID, stickerID) VALUE ('Admin', 2, 3);