Skip to content

Commit

Permalink
Merge pull request #34 from The-Socialites/Testing
Browse files Browse the repository at this point in the history
Testing
  • Loading branch information
Rachel853 authored Jun 28, 2024
2 parents ab88689 + c9aae5d commit 928861a
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 21 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@
<version>0.15</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>


<!-- Async HTTP Client dependency -->
<dependency>
Expand All @@ -139,6 +147,11 @@
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>


</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

public class ThirdPartyVenue {
private String full_address;

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.view.RedirectView;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Optional;

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@



spring.datasource.url=jdbc:postgresql://localhost:5432/socialites_springboot_test
spring.datasource.username=
spring.datasource.password=
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/fragments/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<a href="/events/new" class="block py-3 text-md focus-visible:outline-none lg:px-4 lg:py-2 lg:text-base">Create event</a>
</div>
<div class="first:pt-4 lg:first:pt-0">
<a href="/events/third-party-events" class="block py-3 text-md focus-visible:outline-none lg:px-4 lg:py-2 lg:text-base">Find events</a>
<a href="/events/third-party-events" class="block py-3 text-md focus-visible:outline-none lg:px-4 lg:py-2 lg:text-base">External events</a>
</div>
<div class="first:pt-4 lg:first:pt-0">
<a href="/account" class="block py-3 text-md focus-visible:outline-none lg:px-4 lg:py-2 lg:text-base">Account</a>
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/com/makersacademy/acebook/model/AttendeeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.makersacademy.acebook.model;

import com.makersacademy.acebook.repository.AttendeesRepository;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.Optional;

import static org.assertj.core.api.InstanceOfAssertFactories.type;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

@SpringBootTest
public class AttendeeTest {

@Autowired
private AttendeesRepository attendeesRepository;

@Test
public void testAttendeeCreation() {
Event event = new Event();
User user = new User();
Attendee attendee = new Attendee();
attendee.setEvent(event);
attendee.setUser(user);


assertNotNull(attendee);
assertEquals(user, attendee.getUser());
assertEquals(event, attendee.getEvent());
}

// @Transactional
// @Test
// public void test_unique_constraint_on_user_event_combination() {
// User user = new User();
// Event event = new Event();
//
// Attendee attendee1 = new Attendee();
// attendee1.setUser(user);
// attendee1.setEvent(event);
//
// Attendee attendee2 = new Attendee();
// attendee2.setUser(user);
// attendee2.setEvent(event);
//
//
// assertThrows(PersistenceException.class, () -> {
// attendeesRepository.save(attendee1);
// attendeesRepository.save(attendee2);
// });

}




16 changes: 16 additions & 0 deletions src/test/java/com/makersacademy/acebook/model/AuthorityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.makersacademy.acebook.model;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class AuthorityTest {

@Test
public void test_create_authority_with_valid_username_and_authority() {
Authority authority = new Authority("user1", "ROLE_USER");
assertNotNull(authority);
assertEquals("user1", authority.getUsername());
assertEquals("ROLE_USER", authority.getAuthority());
}
}
64 changes: 64 additions & 0 deletions src/test/java/com/makersacademy/acebook/model/CommentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.makersacademy.acebook.model;

import org.junit.jupiter.api.Test;
import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class CommentTest {

@Test
public void test_create_comment_instance_with_valid_data() {
User user = new User("john_doe", "password123", "john.doe@example.com", true);
Event event = new Event();
Comment comment = new Comment();
comment.setContent("This is a sample comment");
comment.setCreatedAt(new Date());
comment.setUser(user);
comment.setEvent(event);

assertEquals("This is a sample comment", comment.getContent());
assertNotNull(comment.getCreatedAt());
assertEquals(user, comment.getUser());
assertEquals(event, comment.getEvent());
}

@Test
public void test_large_content_handling() {
Comment comment = new Comment();
String largeContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
comment.setContent(largeContent);
assertEquals(largeContent, comment.getContent());
}

@Test
public void test_handles_multiple_associations() {
User user = new User("john_doe", "password", "john.doe@example.com", true);
Event event = new Event();
Comment comment1 = new Comment();
Comment comment2 = new Comment();

comment1.setUser(user);
comment1.setEvent(event);

comment2.setUser(user);
comment2.setEvent(event);

assertEquals(user, comment1.getUser());
assertEquals(user, comment2.getUser());
assertEquals(event, comment1.getEvent());
assertEquals(event, comment2.getEvent());
}

// @Test
// public void test_formatted_created_at_valid_date() {
// Comment comment = new Comment();
// LocalDateTime now = LocalDateTime.now();
// comment.setCreatedAt(Date.from(now.atZone(ZoneId.systemDefault()).toInstant()));
//
// String formattedDate = comment.getFormattedCreatedAt();
//
// assertEquals(now.format(DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm")), formattedDate);
// }

}
41 changes: 41 additions & 0 deletions src/test/java/com/makersacademy/acebook/model/EventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.makersacademy.acebook.model;

import org.junit.Test;

import java.util.Date;
import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class EventTest {
@Test
public void testEventCreation() {
User user = new User();
Event event = new Event();
Date createdDate = new Date(2024, 01, 01);
Date scheduledDate = new Date(2024, 12, 31);
event.setCreatedAt(createdDate);
event.setScheduledDate(scheduledDate);
event.setScheduledStartTime("00:00");
event.setScheduledEndTime("21:59");
event.setLocation("Newcastle-Upon-Tyne");
event.setUser(user);
event.setEventImageUrl("www.image.com");
event.setAttendees(1L);
event.setUserAttending(true);


assertNotNull(event);
assertEquals(new Date(2024, 01, 01), event.getCreatedAt());
assertEquals(new Date(2024, 12, 31), event.getScheduledDate());
assertEquals("00:00", event.getScheduledStartTime());
assertEquals("21:59", event.getScheduledEndTime());
assertEquals("Newcastle-Upon-Tyne", event.getLocation());
assertEquals(user, event.getUser());
assertEquals("www.image.com", event.getEventImageUrl());
assertEquals(Long.valueOf(1L), event.getAttendees());
assertEquals(true, event.getUserAttending());

}
}
20 changes: 0 additions & 20 deletions src/test/java/com/makersacademy/acebook/model/PostTest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.makersacademy.acebook.model;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;


public class ThirdPartyEventTest {

@Test
public void test_create_third_party_event_instance_with_valid_data() {
ThirdPartyEvent thirdPartyEvent = new ThirdPartyEvent();
thirdPartyEvent.setName("Music Gig");
thirdPartyEvent.setLink("www.musicgig.com");
thirdPartyEvent.setDescription("Greatest music gig ever! You must come along");
thirdPartyEvent.setStartTime("2024-12-01 00:00:00");
thirdPartyEvent.setEndTime("2024-12-01 23:59:12");
thirdPartyEvent.setThumbnail("https://example.com/thumbnail.jpg");
ThirdPartyVenue venue = new ThirdPartyVenue();
thirdPartyEvent.setVenue(venue);

assertEquals("Music Gig", thirdPartyEvent.getName());
assertEquals("www.musicgig.com", thirdPartyEvent.getLink());
assertEquals("Greatest music gig ever! You must come along", thirdPartyEvent.getDescription());
assertEquals("2024-12-01 00:00:00", thirdPartyEvent.getStartTime());
assertEquals("2024-12-01 23:59:12", thirdPartyEvent.getEndTime());
assertEquals("https://example.com/thumbnail.jpg", thirdPartyEvent.getThumbnail());
assertEquals(venue, thirdPartyEvent.getVenue());
}

@Test
public void test_correctly_formats_start_time() {
ThirdPartyEvent event = new ThirdPartyEvent();
event.setStartTime("2023-10-01 15:30:00");
String formattedStartTime = event.getFormattedStartTime();
assertEquals("01-10-2023 15:30", formattedStartTime);
}

@Test
public void test_correctly_formats_end_time() {
ThirdPartyEvent event = new ThirdPartyEvent();
event.setEndTime("2022-12-31 23:59:59");
String formattedEndTime = event.getFormattedEndTime();
assertEquals("31-12-2022 23:59", formattedEndTime);
}

@Test
public void test_handles_empty_start_time_string() {
ThirdPartyEvent event = new ThirdPartyEvent();
event.setStartTime("");
String formattedStartTime = event.getFormattedStartTime();
assertNull(formattedStartTime);
}

@Test
public void test_handles_empty_end_time_string() {
ThirdPartyEvent event = new ThirdPartyEvent();
event.setEndTime("");
String formattedEndTime = event.getFormattedEndTime();
assertNull(formattedEndTime);
}

@Test
public void test_ignores_unknown_json_properties() {
ThirdPartyEvent event = new ThirdPartyEvent();
String json = "{\"event_id\":\"12345\",\"name\":\"Test Event\",\"link\":\"www.testevent.com\",\"unknown\":\"This is unknown\",\"description\":\"This is a test event\"}";
ObjectMapper mapper = new ObjectMapper();
try {
event = mapper.readValue(json, ThirdPartyEvent.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
assertNull(event.getStartTime());
assertNull(event.getEndTime());
assertNull(event.getVenue());
assertNull(event.getThumbnail());
assertEquals("12345", event.getEventId());
assertEquals("Test Event", event.getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.makersacademy.acebook.model;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ThirdPartyVenueTest {
@Test
public void testCreateThirdPartyVenue(){
ThirdPartyVenue venue = new ThirdPartyVenue();
venue.setFull_address("1 Station Road, Wandsworth, London, UK SW17 9RT");

assertEquals("1 Station Road, Wandsworth, London, UK SW17 9RT", venue.getFull_address());
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/makersacademy/acebook/model/UserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.makersacademy.acebook.model;

import org.junit.Test;

import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class UserTest {

@Test
public void createUser() {
User user = new User();
user.setUsername("ShaniaTwain");
user.setPassword("password123");
user.setEmail("test@email.com");
user.setProfilePictureUrl("www.image.com");
user.setLanguage("Spanish");
user.setCity("Barcelona");

assertNotNull(user);
assertEquals("ShaniaTwain", user.getUsername());
assertEquals("password123", user.getPassword());
assertEquals("test@email.com", user.getEmail());
assertEquals("www.image.com", user.getProfilePictureUrl());
assertEquals("Spanish", user.getLanguage());
assertEquals("Barcelona", user.getCity());
}
}

0 comments on commit 928861a

Please sign in to comment.