From 312ebfba07d9412ab20cdcbc97518cae6c3353d9 Mon Sep 17 00:00:00 2001
From: Rhys Evans <EvansRM17@cardiff.ac.uk>
Date: Fri, 16 Feb 2024 22:24:48 +0000
Subject: [PATCH] Full process work on FirefoxDriver Ful lfeature test tbc.

---
 .../Team5/SmartTowns/landmarks/Landmarks.java |  2 +-
 .../landmarks/locationApprovalFormTh.html     |  5 +-
 .../SmartTowns/ContainerMockMVCTests.java     |  7 +-
 .../SmartTowns/FirefoxDriverPagesTests.java   | 74 ++++++++++++++++++-
 .../Team5/SmartTowns/JUnitSimpleTests.java    | 55 +++++++++++++-
 5 files changed, 135 insertions(+), 8 deletions(-)

diff --git a/src/main/java/Team5/SmartTowns/landmarks/Landmarks.java b/src/main/java/Team5/SmartTowns/landmarks/Landmarks.java
index 8f7471fc..512915a7 100644
--- a/src/main/java/Team5/SmartTowns/landmarks/Landmarks.java
+++ b/src/main/java/Team5/SmartTowns/landmarks/Landmarks.java
@@ -25,7 +25,7 @@ public class Landmarks {
     @NotEmpty(message = "You must type in a username.")
     private String landmarkName;
     @NotEmpty(message = "You must attach a contact address.") // Requires @NotEmpty for form validation
-    @Email(message = "You must attach a contact address.")
+    @Email(message = "You must attach a valid contact address.")
     private String landmarkEmail;
     private String landmarkDescription;
     private String landmarkLocation;
diff --git a/src/main/resources/templates/landmarks/locationApprovalFormTh.html b/src/main/resources/templates/landmarks/locationApprovalFormTh.html
index df465b66..d2ee7b0b 100644
--- a/src/main/resources/templates/landmarks/locationApprovalFormTh.html
+++ b/src/main/resources/templates/landmarks/locationApprovalFormTh.html
@@ -31,10 +31,13 @@
         <label> Location Latitude:
             <input type="text" th:field="*{locationCoordsLat}" placeholder="Latitude Here">
             <br><br>
+        </label>
+<!--        //todo check this-->
             <label> Location Longitude:
                 <input type="text" th:field="*{locationCoordsLong}" placeholder="Longitude Here">
         </label><br><br>
-            <input type="submit">
+<!--            <input type="submit">-->
+        <button class="button" type="submit">Submit</button>
 
     </form>
 
diff --git a/src/test/java/Team5/SmartTowns/ContainerMockMVCTests.java b/src/test/java/Team5/SmartTowns/ContainerMockMVCTests.java
index f2d3b662..e6405061 100644
--- a/src/test/java/Team5/SmartTowns/ContainerMockMVCTests.java
+++ b/src/test/java/Team5/SmartTowns/ContainerMockMVCTests.java
@@ -13,9 +13,14 @@ import org.springframework.test.context.ActiveProfiles;
 import org.springframework.test.web.servlet.MockMvc;
 import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
 
+
+
+
+
+
 @SpringBootTest
 @AutoConfigureMockMvc
-@ActiveProfiles("dev")
+@ActiveProfiles("dev") //change to MockDev?
 public class ContainerMockMVCTests {
     @Autowired
     private MockMvc mockMvc;
diff --git a/src/test/java/Team5/SmartTowns/FirefoxDriverPagesTests.java b/src/test/java/Team5/SmartTowns/FirefoxDriverPagesTests.java
index d720f43d..2ab21e63 100644
--- a/src/test/java/Team5/SmartTowns/FirefoxDriverPagesTests.java
+++ b/src/test/java/Team5/SmartTowns/FirefoxDriverPagesTests.java
@@ -20,9 +20,11 @@ import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.firefox.FirefoxDriver;
 import org.openqa.selenium.firefox.FirefoxOptions;
+import org.openqa.selenium.support.ui.Select;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.context.annotation.Profile;
+import org.springframework.test.annotation.DirtiesContext;
 import org.springframework.test.context.ActiveProfiles;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
 import org.springframework.test.context.junit4.SpringRunner;
@@ -30,8 +32,10 @@ import org.springframework.test.context.junit4.SpringRunner;
 import io.github.bonigarcia.wdm.WebDriverManager;
 
 
+import static javax.swing.text.html.CSS.getAttribute;
 import static org.junit.Assert.assertTrue;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 
 //@RunWith(SpringRunner.class)
 @ExtendWith(SpringExtension.class)
@@ -51,7 +55,7 @@ public class FirefoxDriverPagesTests {
     @BeforeEach
     void setupTest() {
         FirefoxOptions options = new FirefoxOptions();
-        options.addArguments("--headless");
+//        options.addArguments("--headless");
         webDriver = new FirefoxDriver(options);
 //        webDriver = new FirefoxDriver();
     }
@@ -73,6 +77,74 @@ public class FirefoxDriverPagesTests {
 
 
     }
+//test false entries, log ina dn log out?
+@Test
+@DirtiesContext
+public void testingEntireFeature(){
+        //homePage
+    this.webDriver.get("http://localhost:" + Integer.toString(port) + "/mobile-home");
+    assertTrue(webDriver.findElement(By.cssSelector("main")).getText().contains("Welcome to VZTA Smart Towns!"));
+    assertTrue(webDriver.findElements(By.xpath("/html/body/main/div[1]/a[1]/button")).size() > 0);
+    assertTrue(webDriver.findElement(By.xpath("/html/body/main/div[1]/a[1]/button")).getText().contains("Submit Landmark"));
+    this.webDriver.findElement(By.xpath("/html/body/main/div[1]/a[1]/button")).click();
+    //Landmark Sign Up Form
+    assertTrue(webDriver.findElement(By.cssSelector("main form h1")).getText().contains("Interested in joining our trails? Sign up Here!"));
+    this.webDriver.findElement(By.name("landmarkName")).sendKeys("MockLocation");
+    this.webDriver.findElement(By.name("landmarkEmail")).sendKeys("MockAddress@Contact.co.uk");
+    this.webDriver.findElement(By.name("landmarkDescription")).sendKeys("This is a mock description of this business.");
+    new Select(this.webDriver.findElement(By.name("landmarkLocation"))).selectByVisibleText("Caerphilly");
+    new Select(this.webDriver.findElement(By.name("trailID"))).selectByVisibleText("(Caerphilly) Heritage Trail");
+    assertTrue(webDriver.findElement(By.cssSelector("main form ")).getText().contains("Submit"));
+    assertTrue(webDriver.findElements(By.xpath("/html/body/main/form/button")).size() > 0);
+    this.webDriver.findElement(By.xpath("/html/body/main/form/button")).click();
+    //return to home page
+    assertTrue(webDriver.findElement(By.cssSelector("main div")).getText().contains("Welcome to VZTA Smart Towns!"));
+    assertFalse(webDriver.findElement(By.cssSelector("main div")).getText().contains("Review Landmark!"));
+    //Log In as Administrator (This user-feature is not what's being tested, as such it won't be covered apart from here)
+    assertTrue(webDriver.findElement(By.cssSelector("header nav")).getText().contains("Log In"));
+    assertTrue(webDriver.findElement(By.xpath("/html/body/header/nav/ul/li[4]")).getText().contains("Log In"));
+//    assertEquals(webDriver.findElement(By.xpath("/html/body/header/nav/ul/li[4]")).getAttribute("href"),("localhost:" + Integer.toString(port) + "/login"));
+    assertTrue(webDriver.findElements(By.xpath("/html/body/header/nav/ul/li[4]")).size() > 0);
+    this.webDriver.findElement(By.xpath("/html/body/header/nav/ul/li[4]/a")).click();
+
+    //sign in
+    assertTrue(webDriver.findElement(By.xpath("/html/body/main/div/div[2]/h1")).getText().contains("Sign In"));
+    this.webDriver.findElement(By.xpath("/html/body/main/div/div[2]/form/label[1]/input")).sendKeys("Admin");
+    this.webDriver.findElement(By.xpath("/html/body/main/div/div[2]/form/label[2]/input")).sendKeys("admin");
+    assertTrue(webDriver.findElement(By.xpath("/html/body/main/div/div[2]/form/button")).getText().contains("SIGN IN"));
+    assertTrue(webDriver.findElements(By.xpath("/html/body/main/div/div[2]/form/button")).size() > 0);
+    this.webDriver.findElement(By.xpath("/html/body/main/div/div[2]/form/button")).click();
+
+    //check review shows for admin
+    assertTrue(webDriver.findElement(By.cssSelector("main div")).getText().contains("Review Landmark!"));
+    assertTrue(webDriver.findElements(By.xpath("/html/body/main/div[1]/a[2]")).size() > 0);
+    this.webDriver.findElement(By.xpath("/html/body/main/div[1]/a[2]")).click();
+
+
+    assertTrue(webDriver.findElement(By.xpath("/html/body/main/section/h2")).getText().contains("To Be Approved"));
+    assertTrue(webDriver.findElement(By.cssSelector("main h1")).getText().contains("Locations To Be Approved:"));
+//todo FIX THIS!!
+    assertTrue(webDriver.findElement(By.cssSelector("main table tbody ")).getText().contains("MockLocation (0103)"));
+    assertTrue(webDriver.findElement(By.cssSelector("main section table tbody")).getText().contains("MockAddress@Contact.co.uk"));
+    assertTrue(webDriver.findElement(By.cssSelector("main section table tbody")).getText().contains("This is a mock description of this business."));
+    //Can't test Caerphilly;ly as others already there todo? look into Risca
+    //todo test verification of this form
+    new Select(this.webDriver.findElement(By.name("locationName"))).selectByVisibleText("MockLocation (0103)");
+    this.webDriver.findElement(By.name("locationCoordsLat")).sendKeys("51.57815");
+    this.webDriver.findElement(By.name("locationCoordsLong")).sendKeys("-3.22510");
+    assertTrue(webDriver.findElement(By.xpath("/html/body/main/form/button")).getText().contains("Submit"));
+    assertTrue(webDriver.findElements(By.xpath("/html/body/main/form/button")).size() > 0);
+    this.webDriver.findElement(By.xpath("/html/body/main/form/button")).click();
+
+    assertTrue(webDriver.findElement(By.cssSelector("main")).getText().contains("Welcome to VZTA Smart Towns!"));
+    this.webDriver.findElement(By.xpath("/html/body/main/div[1]/a[2]")).click();
+    assertFalse(webDriver.findElement(By.cssSelector("main table")).getText().contains("MockLocation (0103)"));
+
+    assertTrue(webDriver.findElements(By.xpath("/html/body/header/nav/ul/li[1]")).size() > 0); //todo fix tghis bit
+    this.webDriver.findElement(By.xpath("/html/body/header/nav/ul/li[1]/a")).click();
+    assertTrue(webDriver.findElement(By.cssSelector("main")).getText().contains("Welcome to VZTA Smart Towns!"));
 
 
+
+}
 }
diff --git a/src/test/java/Team5/SmartTowns/JUnitSimpleTests.java b/src/test/java/Team5/SmartTowns/JUnitSimpleTests.java
index 5888e245..58949d98 100644
--- a/src/test/java/Team5/SmartTowns/JUnitSimpleTests.java
+++ b/src/test/java/Team5/SmartTowns/JUnitSimpleTests.java
@@ -6,24 +6,71 @@ import Team5.SmartTowns.landmarks.Landmarks;
 import Team5.SmartTowns.placeswithcoordinates.LocationsCoordinates;
 import Team5.SmartTowns.placeswithcoordinates.PlacesController;
 import Team5.SmartTowns.placeswithcoordinates.TownWithTrails;
+import jakarta.validation.Path;
+import jakarta.validation.constraints.AssertTrue;
 import org.junit.jupiter.api.MethodOrderer;
 import org.junit.jupiter.api.Order;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.TestMethodOrder;
+//import org.junit.platform.engine.support.hierarchical.Node;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.env.Environment;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.List;
+import java.util.*;
 
+import static java.util.Arrays.compare;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.jupiter.api.Assertions.*;
 
+import jakarta.validation.ConstraintViolation; //- (https://davidvlijmincx.com/posts/test-annotation-validation/)
+import jakarta.validation.Validation;
+import jakarta.validation.Validator;
+
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
 @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
 public class JUnitSimpleTests {
 
+    private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
+    private Set<ConstraintViolation<Landmarks>> violationsName;
+
+    @Test
+public void landmarkJakartaValidationTest(){
+    Landmarks landmark = new Landmarks("99",1,"MockLandmark","Mock@Mock2.com",
+            "MockLandmarkDesc","MockRisca","MockImage");
+    Set<ConstraintViolation<Landmarks>> violations = validator.validate(landmark);
+    //username blank, email blank, and email not email
+    assertTrue(violations.isEmpty());
+    System.out.println(violations);
+    landmark.setLandmarkName("");
+    Set<ConstraintViolation<Landmarks>> violationsName = validator.validate(landmark);
+    System.out.println(violationsName);
+    assertFalse(violationsName.isEmpty());
+    List<String> fieldName = violationsName.stream().map((ConstraintViolation::getPropertyPath)).map(Object::toString).toList();
+    System.out.println(fieldName.get(0).equals("landmarkName"));//assert
+    List<String> fieldMessage = violationsName.stream().map((ConstraintViolation::getMessage)).map(Object::toString).toList();
+    System.out.println(fieldMessage.get(0).equals("You must type in a username.")); //assert
+
+    landmark.setLandmarkName("Mock");
+    landmark.setLandmarkEmail("aaa");
+
+    Set<ConstraintViolation<Landmarks>> violationsEmail = validator.validate(landmark);
+//    List<String> fieldName = violationsName.stream().map((ConstraintViolation::getPropertyPath)).map(Object::toString).toList();
+//    System.out.println(fieldName.get(0).equals("landmarkName"));//assert
+//    List<String> fieldMessage = violationsName.stream().map((ConstraintViolation::getMessage)).map(Object::toString).toList();
+//    System.out.println(fieldMessage.get(0).equals("You must type in a username.")); //assert
+   //  System.out.println(violationsEmail.get(0));
+
+
+
+
+
+
+
+}
+
     @Test
     @Order(1)
     public void landmarksTest(){ // generic for all landmarks, trails+dragons tale
-- 
GitLab