Verified Commit 9e4aa537 authored by Adam Paulen's avatar Adam Paulen
Browse files

fix: things from m2

parent 65c95705
Loading
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -16,12 +16,17 @@ import java.util.*;
@Table(name = "AbstractBook")
public class AbstractBook extends BaseModel {

    @Column(name = "title")
    @Column(name = "title", length = 255, nullable = false)
    private String title;

    @Column(name = "author")
    @Column(name = "author", length = 120, nullable = false)
    private String author;

    // todo uncomment for next milestone too many things use abstractBookId now
//    @ManyToOne
//    @JoinColumn(name = "abstract_book_id", nullable = false)
//    private AbstractBook abstractBook;

    @ElementCollection
    @CollectionTable(name = "abstract_book_physical_books", joinColumns = @JoinColumn(name = "abstract_book_id"))
    @Column(name = "physical_book_id")
@@ -29,7 +34,7 @@ public class AbstractBook extends BaseModel {

    @ElementCollection
    @CollectionTable(name = "genres_books", joinColumns = @JoinColumn(name = "abstract_book_id"))
    @Column(name = "genre")
    @Column(name = "genre", length = 100)
    private Set<String> genre = new HashSet<>();

    @Override
+5 −0
Original line number Diff line number Diff line
@@ -15,6 +15,11 @@ import java.util.UUID;
@Table(name = "PhysicalBook")
public class PhysicalBook extends BaseModel {

// todo uncomment for next milestone too many things use abstractBookId now
//    @ManyToOne(optional = false)
//    @JoinColumn(name = "abstract_book_id", nullable = false)
//    private AbstractBook abstractBook;

    @Column(name = "abstractBookId")
    private UUID abstractBookId;

+0 −2
Original line number Diff line number Diff line
@@ -88,8 +88,6 @@ public class AbstractBookFacade {
        List<PhysicalBookDto> physicalBooks = physicalBookService.findByIds(physicalBookIds)
                .stream().map(physicalBookMapper::toDto).collect(Collectors.toList());

        System.out.println(physicalBooks);

        dto.setPhysicalBooks(physicalBooks);
        return dto;
    }
+0 −3
Original line number Diff line number Diff line
@@ -6,19 +6,16 @@ import cz.muni.fi.pa165.commons.dto.book.abstractbook.AbstractBookCreateDto;
import cz.muni.fi.pa165.commons.dto.book.abstractbook.AbstractBookDetailedDto;
import cz.muni.fi.pa165.commons.dto.book.abstractbook.AbstractBookDto;
import cz.muni.fi.pa165.commons.dto.book.abstractbook.AbstractBookUpdateDto;
import cz.muni.fi.pa165.commons.exceptions.ResourceNotFoundException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

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

@RestController
+96 −0
Original line number Diff line number Diff line
package cz.muni.fi.pa165.books.integration;

import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.fi.pa165.books.data.model.AbstractBook;
import cz.muni.fi.pa165.books.data.repository.AbstractBookRepository;
import org.junit.jupiter.api.BeforeAll;
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.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.UUID;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@Transactional
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class AbstractBookRestControllerIT {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private AbstractBookRepository abstractBookRepository;

    @Autowired
    private ObjectMapper objectMapper;

    private AbstractBook savedBook;

    @BeforeAll
    void setup() {
        objectMapper.findAndRegisterModules();
    }

    @BeforeEach
    void init() {
        abstractBookRepository.deleteAll();

        AbstractBook book = new AbstractBook();
        book.setTitle("Test Book");
        book.setAuthor("John Doe");
        book.setGenre(Collections.singleton("Fiction"));
        savedBook = abstractBookRepository.save(book);
    }

    @Test
    void testGetAllAbstractBooks_returnsBooks() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/books/abstract-books")
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$[0].id").value(savedBook.getId().toString()))
                .andExpect(jsonPath("$[0].title").value("Test Book"));
    }

    @Test
    void testGetAbstractBookById_found_returnsBook() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/books/abstract-books/" + savedBook.getId())
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.title").value("Test Book"));
    }

    @Test
    void testGetAbstractBookById_notFound_returns404() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/books/abstract-books/" + UUID.randomUUID())
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isNotFound());
    }

    @Test
    void testDeleteAbstractBook_success_returnsNoContent() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.delete("/books/abstract-books/" + savedBook.getId())
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isNoContent());
    }

    @Test
    void testSearchAbstractBooks_matchByTitle_returnsBook() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/books/abstract-books/search")
                        .param("title", "Test")
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$[0].id").value(savedBook.getId().toString()));
    }
}
Loading