Skip to content

Commit

Permalink
Adding some Integration tests for the REST Api for community events
Browse files Browse the repository at this point in the history
  • Loading branch information
baldzhiyski committed Jul 13, 2024
1 parent b66ea59 commit dd7f172
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 7 deletions.
18 changes: 18 additions & 0 deletions crossfit-community-events/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
<artifactId>jakarta.validation-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.softuni.crossfitcommunityevents.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class ObjectNotFoundException extends RuntimeException{


public ObjectNotFoundException(String message) {
super(message);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Repository
public interface EventRepository extends JpaRepository<Event, UUID> {
public interface EventRepository extends JpaRepository<Event, Long> {


@Query("SELECT e FROM Event e ORDER BY RAND() desc")
List<Event> findSomeRandomEvents();

Optional<Event> findByEventName(String eventName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import com.softuni.crossfitcommunityevents.model.dto.EventDto;

import java.util.List;
import java.util.UUID;

public interface EventService {


EventDto getEventById(UUID id);
EventDto getEventById(Long id);

void createEvent(EventDto addEventDto);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.softuni.crossfitcommunityevents.service.impl;

import com.softuni.crossfitcommunityevents.exception.ObjectNotFoundException;
import com.softuni.crossfitcommunityevents.model.Event;
import com.softuni.crossfitcommunityevents.model.dto.EventDto;
import com.softuni.crossfitcommunityevents.repository.EventRepository;
import com.softuni.crossfitcommunityevents.service.EventService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class EventServiceImpl implements EventService {
Expand All @@ -18,8 +18,8 @@ public EventServiceImpl(EventRepository eventRepository) {
}

@Override
public EventDto getEventById(UUID id) {
Event event = this.eventRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("No such upcomming event !"));
public EventDto getEventById(Long id) {
Event event = this.eventRepository.findById(id).orElseThrow(() -> new ObjectNotFoundException("No such upcomming event !"));

return mapToDto(event);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.softuni.crossfitcommunityevents.web;

import com.softuni.crossfitcommunityevents.exception.ObjectNotFoundException;
import com.softuni.crossfitcommunityevents.model.dto.EventDto;
import com.softuni.crossfitcommunityevents.service.EventService;
import org.slf4j.Logger;
Expand All @@ -25,7 +26,7 @@ public EventsController(EventService eventService) {


@GetMapping("/events/find/{id}")
public ResponseEntity<EventDto> findById(@PathVariable("id") UUID id) {
public ResponseEntity<EventDto> findById(@PathVariable("id") Long id) {
return ResponseEntity
.ok(eventService.getEventById(id));
}
Expand Down Expand Up @@ -59,4 +60,10 @@ public ResponseEntity<List<EventDto>> findAllEvents() {
.ok(eventService.findAllEvents());
}

@ExceptionHandler(ObjectNotFoundException.class)
public ResponseEntity<String> handleObjectNotFoundException(ObjectNotFoundException ex) {
LOGGER.error("ObjectNotFoundException caught: {}", ex.getMessage());
return ResponseEntity.badRequest().body(ex.getMessage());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.softuni.crossfitcommunityevents.util;

import com.softuni.crossfitcommunityevents.model.Event;
import com.softuni.crossfitcommunityevents.repository.EventRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class TestData {


private EventRepository eventRepository;

public TestData(EventRepository eventRepository) {
this.eventRepository = eventRepository;
}


public Event createEvent(String eventName, String description, Date date, String address,String videoUrl){
Event event = Event.builder().eventName(eventName)
.description(description)
.date(date)
.address(address)
.videoUrl(videoUrl)
.build();

return this.eventRepository.saveAndFlush(event);
}
}
16 changes: 16 additions & 0 deletions crossfit-community-events/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
spring:
datasource:
url: jdbc:hsqldb:mem:testdb;sql.syntax_mys=true
driver-class-name: org.hsqldb.jdbcDriver
username: test
password: test
jpa:
defer-datasource-initialization: true
properties:
hibernate:
format_sql: true
hibernate:
ddl-auto: update
sql:
init:
mode: never

0 comments on commit dd7f172

Please sign in to comment.