Skip to content
Snippets Groups Projects
Commit 73ff41f9 authored by Gabriel Copat's avatar Gabriel Copat
Browse files

Edited main branch so that I can do my tests for the PPP coursework.

Also, Rhys N. asked if he could use this branch to do his tests as well
parent 605d12fc
No related branches found
No related tags found
No related merge requests found
Showing
with 53 additions and 869 deletions
...@@ -16,10 +16,7 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -16,10 +16,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Controller @Controller
...@@ -39,21 +36,21 @@ public class PlacesController { ...@@ -39,21 +36,21 @@ public class PlacesController {
@GetMapping("/towns") // @GetMapping("/towns")
public ModelAndView getTownPages(){ // public ModelAndView getTownPages(){
ModelAndView modelAndView = new ModelAndView("towns/townsPageList.html"); // ModelAndView modelAndView = new ModelAndView("towns/townsPageList.html");
List<TownWithTrails> townsList = placeRepo.getAllTownCoords(); // List<TownWithTrails> townsList = placeRepo.getAllTownCoords();
List<Trail> trailslocations = trailsRepo.getAllTrails(); // List<Trail> trailslocations = trailsRepo.getAllTrails();
modelAndView.addObject("trails", trailslocations); // modelAndView.addObject("trails", trailslocations);
modelAndView.addObject("towns", townsList); // modelAndView.addObject("towns", townsList);
return modelAndView; // return modelAndView;
} // }
@RequestMapping(value="/town", method= RequestMethod.POST) // @RequestMapping(value="/town", method= RequestMethod.POST)
public String sendHtmlFragmentTown(Model map) { // public String sendHtmlFragmentTown(Model map) {
map.addAttribute("foo", "bar"); // map.addAttribute("foo", "bar");
return "checkpoint/checkpoint"; // return "checkpoint/checkpoint";
} // }
@GetMapping("/towns/{town}") @GetMapping("/towns/{town}")
public ModelAndView getResultBySearchKeyTowns(@PathVariable String town) { public ModelAndView getResultBySearchKeyTowns(@PathVariable String town) {
......
...@@ -46,7 +46,7 @@ public class UserController { ...@@ -46,7 +46,7 @@ public class UserController {
@PostMapping("/register") @PostMapping("/register")
public ModelAndView registerUser(@Valid @ModelAttribute("user") NewUser user, BindingResult bindingResult, Model model) { public ModelAndView registerUser(@Valid @ModelAttribute("user") NewUser user, BindingResult bindingResult, Model model) {
ModelAndView mav = new ModelAndView("users/login", model.asMap()); ModelAndView mav = new ModelAndView("users/login", model.asMap());
// TODO VALIDATE EMAIL INPUT
mav.addObject("status", "active"); mav.addObject("status", "active");
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
ModelAndView modelAndView = new ModelAndView("users/login"); ModelAndView modelAndView = new ModelAndView("users/login");
...@@ -109,6 +109,29 @@ public class UserController { ...@@ -109,6 +109,29 @@ public class UserController {
mav.addObject("user", userRepository.findUserByName(username)); mav.addObject("user", userRepository.findUserByName(username));
return mav; return mav;
} }
// @GetMapping("/packInfo/{username}/{packID}")
// public ModelAndView betterGetPackInfo(@PathVariable String username, @PathVariable int packID) {
// /* Displays on page the stickers present in the pack and colour the ones the
// * user has acquired */
// ModelAndView mav = new ModelAndView("users/userFrags :: stickersBox");
// mav.addObject("currentUser", userRepository.findUserByName(username));
// mav.addObject("displayedPack", rewardsRepository.findPackByID(packID));
//
// List<Sticker> allStickers = rewardsRepository.getAllStickersFromPack(packID);
// List<Pack> allPacks = rewardsRepository.getAllPacks()
// .stream()
// .filter(pack -> pack.getId() != packID) //Gets all but current pack
// .toList();
// mav.addObject("packs", allPacks);
//
// List<Sticker> allUnlockedStickers = setStickerVisibility(allStickers,
// userRepository.getUserStickersFromPack(username, packID) //From database
// );
// mav.addObject("stickers", allUnlockedStickers);
// mav.addObject("packProgress", getPackProgress(allUnlockedStickers));
//
// return mav;
// }
public int getPackProgress(List<Sticker> userStickers){ public int getPackProgress(List<Sticker> userStickers){
/* Returns the % of completion of given userStickers */ /* Returns the % of completion of given userStickers */
......
spring.datasource.url=jdbc:mariadb://localhost:3306/towns spring.datasource.url=jdbc:mariadb://localhost:3306/
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password=comsc spring.datasource.password=comsc
......
delete from trails;
insert into trails ( trailID, trailName, trailNumber, city) value ( 0101,'Caerphilly Castle Trail','0101', 'Caerphilly'); insert into trails ( trailID, trailName, trailNumber, city) value ( 0101,'Caerphilly Castle Trail','0101', 'Caerphilly');
insert into trails ( trailID, trailName, trailNumber, city) value ( 0102,'Caerphilly Pub Trail','0102', 'Caerphilly'); insert into trails ( trailID, trailName, trailNumber, city) value ( 0102,'Caerphilly Pub Trail','0102', 'Caerphilly');
insert into trails ( trailID, trailName, trailNumber, city) value ( 0103,'Caerphilly Heritage Trail','0103', 'Caerphilly'); insert into trails ( trailID, trailName, trailNumber, city) value ( 0103,'Caerphilly Heritage Trail','0103', 'Caerphilly');
insert into trails ( trailID, trailName, trailNumber, city) value ( 0201,'Risca Heritage Trail','0201', 'Risca'); insert into trails ( trailID, trailName, trailNumber, city) value ( 0201,'Risca Heritage Trail','0201', 'Risca');
insert into trails ( trailID, trailName, trailNumber, city) value ( 0301,'Penarth Esplanade Trail','0301', 'Penarth'); insert into trails ( trailID, trailName, trailNumber, city) value ( 0301,'Penarth Esplanade Trail','0301', 'Penarth');
delete from locations;
insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'St Cenydd','','Location description here','Caerphilly',0101, false); insert into locations ( locationName , locationEmail,locationDescription,locationPlace, locationTrailID, locationApproved) value ( 'St Cenydd','','Location description here','Caerphilly',0101, false);
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 ( '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 ( 'Medieval Trades','','Location description here','Caerphilly',0101, true);
...@@ -55,7 +55,7 @@ INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (3, 2, ...@@ -55,7 +55,7 @@ INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (3, 2,
INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (3, 3, 'Welsh Spoon', 'Welsh Heritage', '1'); INSERT INTO stickers (packID, stickerID, name, description, rarity) VALUE (3, 3, 'Welsh Spoon', 'Welsh Heritage', '1');
delete from locationCoordinates;
insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLong) value (2, 51.57623, -3.21910 ); insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLong) value (2, 51.57623, -3.21910 );
insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLong) value (3, 51.575372, -3.219186); insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLong) value (3, 51.575372, -3.219186);
insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLong) value (4, 51.576363, -3.220712 ); insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLong) value (4, 51.576363, -3.220712 );
...@@ -69,21 +69,19 @@ insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLon ...@@ -69,21 +69,19 @@ insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLon
insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLong) value (20, 51.43547, -3.16789 ); insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLong) value (20, 51.43547, -3.16789 );
delete from localauthority;
insert into localauthority ( localAuthorityName, address1, address2, city, county, postcode, website) value ( 'Caerphilly County Borough Council', 'Tredomen Park','', 'Ystrad Mynach, Hengoed', '', 'CF82 7PG', 'https://www.caerphilly.gov.uk/main.aspx?lang=en-GB'); insert into localauthority ( localAuthorityName, address1, address2, city, county, postcode, website) value ( 'Caerphilly County Borough Council', 'Tredomen Park','', 'Ystrad Mynach, Hengoed', '', 'CF82 7PG', 'https://www.caerphilly.gov.uk/main.aspx?lang=en-GB');
insert into localauthority ( localAuthorityName, address1, address2, city, county, postcode, website) value ( 'Risca Town Council', 'Unit B, 75 Tredegar Street', '', 'Risca', '', 'NP11 6BW', 'https://www.riscatowncouncil.org.uk/'); insert into localauthority ( localAuthorityName, address1, address2, city, county, postcode, website) value ( 'Risca Town Council', 'Unit B, 75 Tredegar Street', '', 'Risca', '', 'NP11 6BW', 'https://www.riscatowncouncil.org.uk/');
insert into localauthority ( localAuthorityName, address1, address2, city, county, postcode, website) value ( 'Penarth Town Council West House', 'Stanwell Road', '', 'Penarth', '', 'CF64 2YG', 'https://www.penarthtowncouncil.gov.uk/your-council/'); insert into localauthority ( localAuthorityName, address1, address2, city, county, postcode, website) value ( 'Penarth Town Council West House', 'Stanwell Road', '', 'Penarth', '', 'CF64 2YG', 'https://www.penarthtowncouncil.gov.uk/your-council/');
delete from businesses;
insert into businesses ( businessName, address1, address2, city, county, postcode, website) value ( 'Caerphilly Castle', 'Castle Street','', 'Caerphilly', '', 'CF836 1JD', 'https://cadw.gov.wales/visit/places-to-visit/caerphilly-castle'); insert into businesses ( businessName, address1, address2, city, county, postcode, website) value ( 'Caerphilly Castle', 'Castle Street','', 'Caerphilly', '', 'CF836 1JD', 'https://cadw.gov.wales/visit/places-to-visit/caerphilly-castle');
insert into businesses ( businessName, address1, address2, city, county, postcode, website) value ( 'Risca Museum', 'Grove Road', '', 'Risca', '', 'NP11 6GN', 'https://riscamuseum.wales/'); insert into businesses ( businessName, address1, address2, city, county, postcode, website) value ( 'Risca Museum', 'Grove Road', '', 'Risca', '', 'NP11 6GN', 'https://riscamuseum.wales/');
insert into businesses ( businessName, address1, address2, city, county, postcode, website) value ( 'Penarth Pier Pavillion Cinema', 'Windsor Court', 'The Esplanade', 'Penarth', '', 'CF64 3AU', 'https://www.valeofglamorgan.gov.uk/en/enjoying/Coast-and-Countryside/Dog-Beach-Ban.aspx'); insert into businesses ( businessName, address1, address2, city, county, postcode, website) value ( 'Penarth Pier Pavillion Cinema', 'Windsor Court', 'The Esplanade', 'Penarth', '', 'CF64 3AU', 'https://www.valeofglamorgan.gov.uk/en/enjoying/Coast-and-Countryside/Dog-Beach-Ban.aspx');
delete from townsWithTrails;
insert into townsWithTrails (townName, townCentreCoordsLat, townCentreCoordsLong, townUppermostCoordsLat, townLowermostCoordsLat, townLeftmostCoordsLong, townRightmostCoordsLong) value ('Caerphilly', '51.57903','-3.22075 ','51.60418','51.55093','-3.25222','-3.17696'); insert into townsWithTrails (townName, townCentreCoordsLat, townCentreCoordsLong, townUppermostCoordsLat, townLowermostCoordsLat, townLeftmostCoordsLong, townRightmostCoordsLong) value ('Caerphilly', '51.57903','-3.22075 ','51.60418','51.55093','-3.25222','-3.17696');
insert into townsWithTrails (townName, townCentreCoordsLat, townCentreCoordsLong, townUppermostCoordsLat, townLowermostCoordsLat, townLeftmostCoordsLong, townRightmostCoordsLong) value ('Risca','51.61195','-3.09648','51.63039','51.59175','-3.12129','-3.06438'); insert into townsWithTrails (townName, townCentreCoordsLat, townCentreCoordsLong, townUppermostCoordsLat, townLowermostCoordsLat, townLeftmostCoordsLong, townRightmostCoordsLong) value ('Risca','51.61195','-3.09648','51.63039','51.59175','-3.12129','-3.06438');
insert into townsWithTrails (townName, townCentreCoordsLat, townCentreCoordsLong, townUppermostCoordsLat, townLowermostCoordsLat, townLeftmostCoordsLong, townRightmostCoordsLong) value ('Penarth','51.43893','-3.17354','51.44878','51.41233','-3.20271','-3.16005'); insert into townsWithTrails (townName, townCentreCoordsLat, townCentreCoordsLong, townUppermostCoordsLat, townLowermostCoordsLat, townLeftmostCoordsLong, townRightmostCoordsLong) value ('Penarth','51.43893','-3.17354','51.44878','51.41233','-3.20271','-3.16005');
delete from dragonstale;
insert into dragonstale(landmarkID, landmarkName, landmarkDescription, imgPath) value (1, 'A scent of...Dragon', 'The Dragon has been spotted near by, find the QR code to continue', '~/images/dragonstale/DTLM1.png');
\ No newline at end of file
import Team5.SmartTowns.data.Calculator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
//import org.springframework.dao.DataAccessException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
//import org.junit.platform.engine.TestDescriptor;
//import org.junit.platform.commons.util.Preconditions;
//@SpringBootTest
public class CalculatorTest {
// String asd= getLegacyReportingName("das");
private static Calculator calculator;
@BeforeAll
public static void before() {
calculator = new Calculator();
}
@Test
public void whenDivideNumbersThenReturnDivision() {
assertEquals(2, calculator.divide(4, 2));
}
@Test
public void whenDivideByZeroThenThrowException() {
assertThrows(ArithmeticException.class, () -> calculator.divide(1, 0));
}
@Test
public void whenAddingThingsThenAdd(){
assertEquals(10,calculator.add(6,4));
}
@Test
public void whenSubtractingThingsThenSubtract(){
assertEquals(-5,calculator.subtract(5,10));
}
@Test
public void whenMultiplyingThingsThenMultiply(){
assertEquals(-60,calculator.multiply(-3,20));
}
}
...@@ -3,7 +3,11 @@ package Team5.SmartTowns.users; ...@@ -3,7 +3,11 @@ package Team5.SmartTowns.users;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.jdbc.Sql;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.util.Arrays; import java.util.Arrays;
...@@ -22,6 +26,7 @@ class UserTests { ...@@ -22,6 +26,7 @@ class UserTests {
@Autowired @Autowired
JdbcTemplate jdbcTemplate; JdbcTemplate jdbcTemplate;
@BeforeEach @BeforeEach
public void resetUserTable(){ public void resetUserTable(){
/*Deletes table as each test creates the table it needs for its own testing*/ /*Deletes table as each test creates the table it needs for its own testing*/
...@@ -64,6 +69,7 @@ class UserTests { ...@@ -64,6 +69,7 @@ class UserTests {
void doesUserExist() { void doesUserExist() {
jdbcTemplate.update("INSERT INTO users (username, email, password) VALUE ('UserExists', 'email@test.com', 'test')" ); jdbcTemplate.update("INSERT INTO users (username, email, password) VALUE ('UserExists', 'email@test.com', 'test')" );
assertTrue( userRepository.doesUserExist("email@test.com") ); assertTrue( userRepository.doesUserExist("email@test.com") );
assertFalse( userRepository.doesUserExist("user@doesnot.exist"));
} }
@Test @Test
...@@ -84,5 +90,6 @@ class UserTests { ...@@ -84,5 +90,6 @@ class UserTests {
name); name);
User user = userRepository.findUserByName(name); User user = userRepository.findUserByName(name);
assertEquals(name, user.getName()); assertEquals(name, user.getName());
assertNull(userRepository.findUserByName("Jack"));
} }
} }
\ No newline at end of file
package Team5.SmartTownsOld;
import org.junit.jupiter.api.Test;
//import static org.testing.AssertJUnit.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.testng.annotations.Test;
//import org.testng.annotations.Test;
//import org.apiguardian.api;
//import org.junit.platform.engine.TestDescriptor;
//import static org.junit.jupiter.api.Assertions.assertEquals;
public class DBUnitTestCase {
int bb=4;
int aa=5;
@Test
void whenDivideNumbersThenReturnDivision() {
int aa=5;
assertEquals(5, aa);
}
}
\ No newline at end of file
package Team5.SmartTowns;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@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;
}
}
\ No newline at end of file
package Team5.SmartTownsOld;
import Team5.SmartTowns.data.Location;
import Team5.SmartTowns.data.LocationRepository;
import Team5.SmartTowns.data.TrailsRepository;
import Team5.SmartTowns.placeswithcoordinates.LocationsCoordinates;
import Team5.SmartTowns.placeswithcoordinates.PlacesCoordinatesRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static junit.framework.TestCase.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest
public class LocationRepositoryTest {
@Autowired
TrailsRepository trailsRepo;
@Autowired
LocationRepository locationRepository;
@Autowired
PlacesCoordinatesRepository placesRepository;
@Autowired
JdbcTemplate jdbcTemplate;
// @BeforeEach
// public void beforeEachTest(){
// jdbcTemplate.update("DELETE FROM locations");
// }
@Test
public void testGetAllApprovedLocations() { // test to amke sure all approved locations are called
List<Location> approvedLocations = locationRepository.getAllApprovedLocations();
List<Location> allLocations = locationRepository.getAllLocation();
for (int i=0;i<allLocations.size();i++){ // iterate over all location, removing authorised=true
for (Location app : approvedLocations){
if (Objects.equals(allLocations.get(i).getLocationName(), app.getLocationName())){
allLocations.remove(allLocations.get(i));
}
}
} boolean noApporvedLeft=false;
for (Location loc2: allLocations){
if (loc2.isLocationApproved()){
noApporvedLeft=false;
break;
} else{
noApporvedLeft=true;
}
} assertTrue(noApporvedLeft);
}
@Test
public void testGetAllUnapprovedLocations() { // test to make sure all unapproved coordinates are called
List<Location> unapprovedLocations = locationRepository.getAllUnapprovedLocations();
List<Location> allLocations = locationRepository.getAllLocation();
for (int i=0;i<allLocations.size();i++){ // iterate over all location, removing authorised=false
for (Location app : unapprovedLocations){
if (Objects.equals(allLocations.get(i).getLocationName(), app.getLocationName())){
allLocations.remove(allLocations.get(i));
}
}
} boolean noUnapprovedLeft=false;
for (Location loc2: allLocations){
if (!loc2.isLocationApproved()){
noUnapprovedLeft=false;
break;
} else{
noUnapprovedLeft=true;
}
} assertTrue(noUnapprovedLeft);
}
@Test// test to ensure that the number of approved locations and number of associated locations with coordinates are equal
public void ensureApprovedLocationsAndCoordinatessAreTheSameSize(){
List<Location> approvedLocations = locationRepository.getAllApprovedLocations();
List<LocationsCoordinates> coordinatesLocations = placesRepository.getAllLocationCoords();
assertSame(approvedLocations.size(),coordinatesLocations.size() );
}
@Test // test to ensure that all default locations and location coordinates match
public void ensureApprovedLocationsAndCoordinatessTableLineUpTest(){
List<Location> approvedLocations = locationRepository.getAllApprovedLocations();
List<LocationsCoordinates> coordinatesLocations = placesRepository.getAllLocationCoords();
boolean doTheyMatch=false;
for (int i=0;i<coordinatesLocations.size();i++){
int locID=coordinatesLocations.get(i).getLocationID();
String coordName = jdbcTemplate.queryForObject("SELECT locationName FROM locations WHERE locationID=?", String.class, locID);
if (Objects.equals(coordName, approvedLocations.get(i).getLocationName())){
doTheyMatch=true;
} else{
doTheyMatch=false;
break;
}
// for loop goes through entire list, if there were any discrepancies, the loop would break, resulting in a fail
} assertTrue(doTheyMatch);
}
@Test
public void doesApprovalUpdateTest(){ //tests whether locations that are approved have their database approval changed to true
int approvedLocationsTotal = locationRepository.getAllApprovedLocations().size();
Location unapprovedLocation = new Location("test","test@email","","Caerphilly","102",false);
locationRepository.addLocation(unapprovedLocation);
int newID=locationRepository.nametoLocationID( unapprovedLocation.getLocationName());
locationRepository.updateApprovalStatus(newID);
int newApprovedLocationsTotal = locationRepository.getAllApprovedLocations().size();
assertEquals(1,(newApprovedLocationsTotal-approvedLocationsTotal));
}
}
//package Team5.SmartTowns;
//
//import Team5.SmartTowns.data.DatabaseController;
//import Team5.SmartTowns.data.Location;
//import Team5.SmartTowns.data.LocationRepository;
//import Team5.SmartTowns.data.LocationRepositoryJDBC;
//import Team5.SmartTowns.landmarks.Landmarks;
//import org.junit.jupiter.api.BeforeAll;
//import org.junit.jupiter.api.Test;
//import org.mockito.InjectMocks;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.jdbc.core.JdbcTemplate;
//
//import static org.junit.jupiter.api.Assertions.assertEquals;
//
//import java.lang.annotation.Inherited;
//import java.util.List;
//
//
//
//@DataJpaTest
//@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
////@SpringBootTest
//class SmartTownsApplicationTests {
//
// @Autowired
// private LocationRepository locationRepository;
//
//
// @Test
// void contextLoads() {
// }
//
// @Test
// void test(){
// Location location = new Location();
// location.setLocationName("Test");
// location.setLocationEmail("test@test");
// location.setLocationDescription("Description Here");
// location.setLocationPlace("Caerphilly");
// location.setLocationTrailID("0103");
// location.setLocationApproved(false);
//
// int locSize1 = locationRepository.getAllLocation().size();
// locationRepository.addLocation(location);
// int locSize2 = locationRepository.getAllLocation().size();
// assertEquals(1, (locSize2-locSize1));
//
//
// }
//
//
//
//
//
//// private static Location location;
//// private static locationRepositoryJDBC locationRepo;
//// private static JdbcTemplate jdbc;
//// @BeforeAll
//// public static void before() {
//// location = new Location();
//// locationRepo = new locationRepositoryJDBC(jdbc);}
//// @BeforeAll
//// public static void before2() {
//// locationRepo = new locationRepositoryJDBC(jdbc); }
//// @Autowired
//// private locationRepository locationRepository;
//
//// @Test
//// public void whenAddingLocationsNonApprovedLocationsDontShowInTrails(){
//// /// Discover number of approved/unapproved locations before adding tests
//// List<Location> approvedNumber = locationRepo.approvedLocations();
//// }
////// Location loc1= new Location("TestFail", "Test@PleaseFail.test", "Fail Description here",
////// "Caerphilly", 103, false);
////// Location loc2= new Location("TestFail", "Test@PleaseFail2.test", "Fail Description here",
////// "Caerphilly", 103, false);
////// Location loc3= new Location("TestPass", "Test@PleasePass.test", "Pass Description here",
////// "Caerphilly", 103, true);
////// locationRepository.addLocation(loc1);
////// locationRepository.addLocation(loc2);
////// locationRepository.addLocation(loc3);
////// List<Location> ApprovedNumberAfter=locationRepository.approvedLocations();
//////
////// assertEquals(1,( ApprovedNumber.size()-ApprovedNumberAfter.size()));
//// }
// }
//
//// create test where getFullListLocations and getFullApproved/Unapporved lcopations from lcoationcooirds matches locationJDBC
package Team5.SmartTownsOld;
//import Team5.SmartTowns.Data.Location;
//import Team5.SmartTowns.Data.LocationRepositoryJDBC;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
//import org.springframework.jdbc.core.JdbcTemplate;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringJUnitConfig
public class Test4 {
// @Mock
// JdbcTemplate jdbcTemplate;
// @BeforeAll
// static void before() {
// Location location = new Location();
// }
@Test
public void whenMockJdbcTemplate_thenReturnCorrectEmployeeCount() {
// Location aa=new Location();
// LocationRepositoryJDBC employeeDAO = new LocationRepositoryJDBC();
// ReflectionTestUtils.setField(employeeDAO, "jdbcTemplate", jdbcTemplate);
// Mockito.when(jdbcTemplate.queryForObject("SELECT COUNT(*) FROM locations", Integer.class))
// .thenReturn(4);
assertEquals(4, 4);
}
}
package Team5.SmartTownsOld;
import Team5.SmartTowns.data.LocationRepository;
//import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
//import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
//@JdbcTest
//
@Sql({"/resources/schema.sql", "/resources/test-data.sql"})
public class Test5 {
@Autowired
private LocationRepository locationRepo;
@Test
// test 1
void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount() {
// LocationRepositoryJDBC employeeDAO = new LocationRepositoryJDBC(ajdbc);
int aa=2;
int bb=2;
// employeeDAO.setJdbc(ajdbc);
assertEquals(4, (aa+bb));
}
}
//package Team5.SmartTowns;
//
//import Team5.SmartTowns.Data.Location;
//import Team5.SmartTowns.Data.LocationRepository;
//import Team5.SmartTowns.Data.LocationRepositoryJDBC;
//import org.junit.Before;
////import org.junit.runner.RunWith;
////import org.mockito.Mockito;
////import org.mockito.internal.invocation.InvocationsFinder;
//import org.junit.jupiter.api.BeforeAll;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.boot.test.context.TestConfiguration;
//
//import org.springframework.boot.test.mock.mockito.MockBean;
//import org.springframework.context.annotation.Bean;
//import org.springframework.jdbc.core.JdbcTemplate;
////import org.springframework.test.context.junit4.SpringRunner;
//import org.testng.annotations.Test;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import static org.testng.AssertJUnit.assertEquals;
//
////@RunWith(SpringRunner.class)
//@SpringBootTest
//public class Test6Mock {
//
// @TestConfiguration
// static class EmployeeServiceImplTestContextConfiguration {
//
// @Bean
// public Location location() {
// return new Location();
// }
// }
//
//
// @Autowired
// private LocationRepository locationRepo;
// @Autowired
// private LocationRepositoryJDBC locationRepoJ;
//
//
// @MockBean
// private JdbcTemplate jdbc;
//
//
// @BeforeAll
// public static void setUp() {
// LocationRepository LocationRepository= new LocationRepository() {
// @Override
// public List<Location> getAllLocation() {
// return null;
// }
//
// @Override
// public void addLocation(Location loc) {
//
// }
// };
//
//// Location alex = new Location("House","House@Bricks","Description Here","Caerphilly",103);
////
//// Mockito.when(LocationRepository.getAllLocation())
//// .thenReturn(LocationRepository.getAllLocation());
// }
//
//
// @Test
// public void whenValidName_thenEmployeeShouldBeFound() {
// LocationRepositoryJDBC s= new LocationRepositoryJDBC(jdbc);
// List<Location> locations = LocationRepository.getAllLocation();
// assertEquals(18,locations.size());
//
// }
// }
//
package Team5.SmartTownsOld;
import Team5.SmartTowns.data.LocationRepository;
import Team5.SmartTowns.data.TrailsRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
public class TrailsRepositoryTest {
@Autowired
TrailsRepository trailsRepo;
@Autowired
JdbcTemplate jdbcTemplate;
@Test
public void getTrailNameWithIDTest(){
String trailsID="101";
String trailName= trailsRepo.getTrailNameWithID(trailsID);
assertEquals("Caerphilly Castle Trail",trailName);
trailsID="102";
trailName= trailsRepo.getTrailNameWithID(trailsID);
assertEquals("Caerphilly Pub Trail",trailName);
trailsID="103";
trailName= trailsRepo.getTrailNameWithID(trailsID);
assertEquals("Caerphilly Heritage Trail",trailName);
trailsID="201";
trailName= trailsRepo.getTrailNameWithID(trailsID);
assertEquals("Risca Heritage Trail",trailName);
trailsID="301";
trailName= trailsRepo.getTrailNameWithID(trailsID);
assertEquals("Penarth Esplanade Trail",trailName);
} // test whether function works correctly for all instances
}
//package Team5.SmartTowns.data;
//
//import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.jdbc.core.RowMapper;
//
//@SpringBootTest
//class LocationRepositoryJDBCTest {
// private JdbcTemplate jdbcTemplate;
// private LocationRepositoryJDBC locationRepositoryJDBC;
//
// private RowMapper<Location> locationMapper;
//
//
// @Autowired
// public void setlocationMapper(){
// locationMapper = (rs, i) -> new Location(
//
// rs.getString("locationName"),
// rs.getString("locationEmail"),
// rs.getString("locationDescription"),
// rs.getString("locationPlace"),
// rs.getString("locationTrailID"),
// rs.getBoolean("locationApproved")
// );
// }
// @Autowired
// public void LocationRepositoryJDBC(JdbcTemplate jdbc){
// this.jdbcTemplate = jdbc;
// locationRepositoryJDBC = new LocationRepositoryJDBC(jdbc);
// }
//
//
//
//
// @Test
// public void test(){
//
// }
//
//
//}
\ No newline at end of file
package Team5.SmartTownsOld.data;
class LocationTest {
}
\ No newline at end of file
package Team5.SmartTowns.placeswithcoordinates;
import Team5.SmartTowns.data.Location;
import Team5.SmartTowns.data.LocationRepository;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest
class PlacesCoordinatesTest {
@Autowired
PlacesCoordinatesRepository placesCoordinatesRepository;
@Autowired
LocationRepository locationRepository;
@Autowired
JdbcTemplate jdbcTemplate;
@BeforeEach
void setUp() {
/* Ensures that each test starts with a clean table*/
jdbcTemplate.update("DELETE FROM locationCoordinates");
}
@AfterEach
void tearDown() {
jdbcTemplate.update("DELETE FROM locationCoordinates");
}
@Test
void getAllLocationCoords() {
jdbcTemplate.update("insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLong) value (2, 51.57623, -3.21910 )");
jdbcTemplate.update("insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLong) value (3, 51.57623, -3.21910 )");
jdbcTemplate.update("insert into locationCoordinates(locationID, locationCoordsLat, locationCoordsLong) value (4, 51.57623, -3.21910 )");
List<Long> expected = jdbcTemplate.queryForList("SELECT locationID FROM locationcoordinates", Long.class);
List<LocationsCoordinates> allObjects = placesCoordinatesRepository.getAllLocationCoords();
assertEquals(expected.size(), allObjects.size());
assertEquals(expected.get(0), allObjects.get(0).getLocationID());
assertEquals(expected.get(1), allObjects.get(1).getLocationID());
assertEquals(expected.get(2), allObjects.get(2).getLocationID());
}
@Test
void getAllTownCoords() {
}
@Test
void addLocationCoord() {
int locationID = 1;
double locationCoordsLat = 1.5;
double locationCoordsLong = 1.5;
placesCoordinatesRepository.addLocationCoord(
new LocationsCoordinates(1, locationCoordsLat, locationCoordsLong)
);
double resultLat = jdbcTemplate.queryForObject(
"SELECT locationCoordsLat FROM locationcoordinates WHERE locationID=?",
Double.class, locationID);
double resultLong = jdbcTemplate.queryForObject(
"SELECT locationCoordsLat FROM locationcoordinates WHERE locationID=?",
Double.class, locationID);
assertEquals(locationCoordsLat, resultLat);
assertEquals(locationCoordsLong, resultLong);
}
@Test
void getFullApprovedLocations() {
}
@Test
void getLocationTableIDValue() {
}
@Test
public void nameToLocationIDTest(){
List<Location> locationList = locationRepository.getAllLocation();
String firstLocationName=locationList.get(0).getLocationName();
String lastLocationName=locationList.get(locationList.size()-1).getLocationName();
int firstLocationID= locationRepository.nametoLocationID(firstLocationName);
int lastLocationID= locationRepository.nametoLocationID(lastLocationName);
// if first and last location are chosen and if SQL ID starts at 1 , while an array index starts at 0, then the following should be equal;
String firstLocationTest=locationList.get(firstLocationID-1).getLocationName();
String lastLocationTest=locationList.get(lastLocationID-1).getLocationName();
assertEquals(firstLocationName,firstLocationTest);
assertEquals(lastLocationName,lastLocationTest);
}
@Test // test to see if
public void checkIfCoordsAreWithinTownBoundaryTest(){
// initiate second instance of location without Caerphilly bounds.
List<Location> locationList = locationRepository.getAllLocation();
Location unapprovedLocation = new Location("test","test@email","","Caerphilly","102",false);
locationRepository.addLocation(unapprovedLocation);
int newID=locationRepository.nametoLocationID( unapprovedLocation.getLocationName());
LocationsCoordinates newCoord = new LocationsCoordinates(newID,0.00,0.00);
boolean falseIfOutOfBounds = placesCoordinatesRepository.checkIfCoordsAreWithinTownBoundary(newCoord);
// initiate second instance of location within Caerphilly bounds.
Location unapprovedLocationTwo = new Location("test2","test2@email","","Caerphilly","103",false);
locationRepository.addLocation(unapprovedLocationTwo);
int newIDTwo=locationRepository.nametoLocationID( unapprovedLocationTwo.getLocationName());
LocationsCoordinates newCoordTwo = new LocationsCoordinates(newIDTwo,51.57903,-3.22075 );
boolean falseIfOutOfBoundsTwo = placesCoordinatesRepository.checkIfCoordsAreWithinTownBoundary(newCoordTwo);
assertNotEquals(falseIfOutOfBounds,falseIfOutOfBoundsTwo);
}
}
\ No newline at end of file
package Team5.SmartTownsOld;
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(){ // tests if users can unlock stickers
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(){ // tests if users who unlock stickers can view them
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);
}
}
//import org.junit.jupiter.api.Test;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
//import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
//
//import javax.sql.DataSource;
//
//import static org.junit.jupiter.api.Assertions.assertEquals;
//
//
//public class Test3 {
//
// @Test
//// test2
// public void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount1() {
// DataSource dataSource = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
// .addScript("classpath:schema.sql")
// .addScript("classpath:data.sql")
// .build();
//// .addScript("classpath:jdbc/test-data.sql")
//
//// LocationRepositoryJDBC employeeDAO = new LocationRepositoryJDBC();
//// employeeDAO.setDataSource(dataSource);
//
//// assertEquals(4, employeeDAO.getCountOfEmployees());}
// assertEquals(4, 4);}
//}
////package Team5.SmartTowns.data;
//
////import org.junit.platform.commons.util.Preconditions;
////
//import Team5.SmartTowns.data.Location;
//import Team5.SmartTowns.data.LocationRepository;
//import Team5.SmartTowns.data.LocationRepositoryJDBC;
//import org.junit.jupiter.api.BeforeAll;
//import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.jdbc.core.RowMapper;
//
//import static org.junit.jupiter.api.Assertions.assertEquals;
//
//
////@SpringBootTest
//public class testTwo {
//
//private JdbcTemplate jdbcTemplate;
//private LocationRepositoryJDBC locationRepositoryJDBC;
//
//private RowMapper<Location> locationMapper;
//
//@Autowired
//private LocationRepository locationRepository;
//
//
//@Autowired
// public void LocationRepositoryJDBC(JdbcTemplate jdbc){
// this.jdbcTemplate = jdbc;
// locationRepositoryJDBC = new LocationRepositoryJDBC(jdbc);
//}
//
//
//@BeforeAll
//public static void setUp(){
// locationRepository = new LocationRepositoryJDBC.getAllLocation();
//// locationRepository = (LocationRepositoryJDBC) locationRepository.getAllLocation();
//}
//
//@Test
//public void test(){
// int aa=1;
// assertEquals(1,aa);
//
// }
//
//
//
//}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment