Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmetAksunger committed Sep 19, 2023
2 parents 74d3eaa + b350bf2 commit 560da1d
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 90 deletions.
109 changes: 109 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Case Study - Book Delivery

<p align="center">
<img src="" alt="Main Information" width="500" height="180">
</p>

### 📖 Information

<ul style="list-style-type:disc">
<li><b> Book Delivery</b></li>
<li>Here is the explanation of the example
<ul>
</ul>
</li>
</ul>

### Explore Rest APIs

<table style="width:100%">
<tr>
<th>Method</th>
<th>Url</th>
<th>Description</th>
<th>Valid Path Variable</th>
<th>No Path Variable</th>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</table>



### Technologies

---
- Java 17
- Spring Boot 3.0
- Restful API
- Lombok
- Maven
- Junit5
- Mockito
- Integration Tests
- Docker
- Docker Compose
- CI/CD (Github Actions)
- Prometheues and Grafana
- Postman
- Actuator
- Swagger 3

### Prerequisites

---
- Maven or Docker
---


### Docker Run
The application can be built and run by the `Docker` engine. The `Dockerfile` has multistage build, so you do not need to build and run separately.

Please follow directions shown below in order to build and run the application with Docker Compose file;

```sh
$ cd bookdelivery
$ docker-compose up -d
```

If you change anything in the project and run it on Docker, you can also use this command shown below

```sh
$ cd bookdelivery
$ docker-compose up --build
```

---
### Maven Run
To build and run the application with `Maven`, please follow the directions shown below;

```sh
$ cd bookdelivery
$ mvn clean install
$ mvn spring-boot:run
```

### Screenshots

<details>
<summary>Click here to show the screenshots of project</summary>
<p> Figure 1 </p>
<img src ="">
<p> Figure 2 </p>
<img src ="">
<p> Figure 3 </p>
<img src ="">
<p> Figure 4 </p>
<img src ="">
</details>


### Contributors

- [Sercan Noyan Germiyanoğlu](https://github.com/Rapter1990)
- [Ahmet Aksünger](https://github.com/AhmetAksunger)
- [Muhammet Oğuzhan Aydoğdu](https://github.com/moaydogdu)
6 changes: 3 additions & 3 deletions src/main/java/com/example/demo/controller/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.example.demo.payload.request.book.BookCreateRequest;
import com.example.demo.payload.request.book.BookUpdateRequest;
import com.example.demo.payload.request.book.BookUpdateStockRequest;
import com.example.demo.payload.request.pagination.PaginatedFindAllRequest;
import com.example.demo.payload.request.pagination.PaginationRequest;
import com.example.demo.payload.response.CustomPageResponse;
import com.example.demo.payload.response.CustomResponse;
import com.example.demo.payload.response.book.BookCreatedResponse;
Expand Down Expand Up @@ -104,8 +104,8 @@ public CustomResponse<BookGetResponse> getBookById(@PathVariable("bookId") final
*/
@PostMapping("/all")
@PreAuthorize("hasAnyAuthority('ROLE_ADMIN','ROLE_CUSTOMER')")
public CustomResponse<CustomPageResponse<BookGetResponse>> getBooks(@RequestBody @Valid PaginatedFindAllRequest paginatedFindAllRequest) {
final Page<BookDTO> bookEntitiesFromDb = bookService.getAllBooks(paginatedFindAllRequest);
public CustomResponse<CustomPageResponse<BookGetResponse>> getBooks(@RequestBody @Valid PaginationRequest paginationRequest) {
final Page<BookDTO> bookEntitiesFromDb = bookService.getAllBooks(paginationRequest);
final CustomPageResponse<BookGetResponse> responses = BookMapper
.toGetResponse(bookEntitiesFromDb);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/example/demo/service/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.example.demo.payload.request.book.BookCreateRequest;
import com.example.demo.payload.request.book.BookUpdateRequest;
import com.example.demo.payload.request.book.BookUpdateStockRequest;
import com.example.demo.payload.request.pagination.PaginatedFindAllRequest;
import com.example.demo.payload.request.pagination.PaginationRequest;
import org.springframework.data.domain.Page;

/**
Expand Down Expand Up @@ -40,10 +40,10 @@ public interface BookService {
/**
* Retrieves a paginated list of all books based on the provided request.
*
* @param paginatedFindAllRequest The request containing pagination information.
* @param paginationRequest The request containing pagination information.
* @return A {@link Page} of {@link BookDTO} objects representing the list of books.
*/
Page<BookDTO> getAllBooks(PaginatedFindAllRequest paginatedFindAllRequest);
Page<BookDTO> getAllBooks(PaginationRequest paginationRequest);

/**
* Updates a book by its unique identifier.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.example.demo.payload.request.book.BookCreateRequest;
import com.example.demo.payload.request.book.BookUpdateRequest;
import com.example.demo.payload.request.book.BookUpdateStockRequest;
import com.example.demo.payload.request.pagination.PaginatedFindAllRequest;
import com.example.demo.payload.request.pagination.PaginationRequest;
import com.example.demo.repository.BookRepository;
import com.example.demo.service.BookService;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -76,14 +76,14 @@ public BookDTO updateBookStockById(String bookId, BookUpdateStockRequest request
/**
* Retrieves a paginated list of all books based on the provided request.
*
* @param paginatedFindAllRequest The request containing pagination information.
* @param paginationRequest The request containing pagination information.
* @return A {@link Page} of {@link BookDTO} objects representing the list of books.
*/
@Override
public Page<BookDTO> getAllBooks(PaginatedFindAllRequest paginatedFindAllRequest) {
public Page<BookDTO> getAllBooks(PaginationRequest paginationRequest) {

return bookRepository
.findAll(paginatedFindAllRequest.getPaginationRequest().toPageable())
.findAll(paginationRequest.toPageable())
.map(BookMapper::toDTO);
}

Expand Down
44 changes: 4 additions & 40 deletions src/test/java/com/example/demo/controller/BookControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import com.example.demo.payload.request.book.BookCreateRequest;
import com.example.demo.payload.request.book.BookUpdateRequest;
import com.example.demo.payload.request.book.BookUpdateStockRequest;
import com.example.demo.payload.request.pagination.DateIntervalRequest;
import com.example.demo.payload.request.pagination.PaginatedFindAllRequest;
import com.example.demo.payload.request.pagination.PaginationRequest;
import com.example.demo.payload.response.CustomPageResponse;
import com.example.demo.payload.response.CustomResponse;
Expand All @@ -27,8 +25,6 @@
import org.springframework.http.MediaType;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -157,30 +153,14 @@ void givenPaginatedFindAllRequest_whenCustomerRoleAndBookListFound_ReturnPageRes
BookDTO bookDTO1 = BookMapper.toDTO(book1);
BookDTO bookDTO2 = BookMapper.toDTO(book2);

Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, 2000);
calendar1.set(Calendar.MONTH, Calendar.SEPTEMBER);
calendar1.set(Calendar.DAY_OF_MONTH, 10);

Calendar calendar2 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, 2000);
calendar1.set(Calendar.MONTH, Calendar.SEPTEMBER);
calendar1.set(Calendar.DAY_OF_MONTH, 13);

LocalDateTime startDate = calendar1.getTime().toInstant().atZone(calendar1.getTimeZone().toZoneId()).toLocalDateTime();
LocalDateTime endDate = calendar2.getTime().toInstant().atZone(calendar2.getTimeZone().toZoneId()).toLocalDateTime();

PaginatedFindAllRequest request = PaginatedFindAllRequest.builder()
.dateIntervalRequest(new DateIntervalRequest(startDate, endDate))
.paginationRequest(new PaginationRequest(1, 10))
.build();
PaginationRequest request = new PaginationRequest(1, 10);

Page<BookDTO> mockPageOfOrderDTOs = new PageImpl<>(List.of(bookDTO1, bookDTO2));

CustomPageResponse<BookGetResponse> customPageResponseOfBookGetResponse = BookMapper.toGetResponse(mockPageOfOrderDTOs);

// when
Mockito.when(bookService.getAllBooks(any(PaginatedFindAllRequest.class))).thenReturn(mockPageOfOrderDTOs);
Mockito.when(bookService.getAllBooks(any(PaginationRequest.class))).thenReturn(mockPageOfOrderDTOs);

// then
CustomResponse<CustomPageResponse<BookGetResponse>> response = CustomResponse.ok(customPageResponseOfBookGetResponse);
Expand Down Expand Up @@ -211,30 +191,14 @@ void givenPaginatedFindAllRequest_whenAdminRoleAndBookListFound_ReturnPageRespon
BookDTO bookDTO1 = BookMapper.toDTO(book1);
BookDTO bookDTO2 = BookMapper.toDTO(book2);

Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, 2000);
calendar1.set(Calendar.MONTH, Calendar.SEPTEMBER);
calendar1.set(Calendar.DAY_OF_MONTH, 10);

Calendar calendar2 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, 2000);
calendar1.set(Calendar.MONTH, Calendar.SEPTEMBER);
calendar1.set(Calendar.DAY_OF_MONTH, 13);

LocalDateTime startDate = calendar1.getTime().toInstant().atZone(calendar1.getTimeZone().toZoneId()).toLocalDateTime();
LocalDateTime endDate = calendar2.getTime().toInstant().atZone(calendar2.getTimeZone().toZoneId()).toLocalDateTime();

PaginatedFindAllRequest request = PaginatedFindAllRequest.builder()
.dateIntervalRequest(new DateIntervalRequest(startDate, endDate))
.paginationRequest(new PaginationRequest(1, 10))
.build();
PaginationRequest request = new PaginationRequest(1, 10);

Page<BookDTO> mockPageOfOrderDTOs = new PageImpl<>(List.of(bookDTO1, bookDTO2));

CustomPageResponse<BookGetResponse> customPageResponseOfBookGetResponse = BookMapper.toGetResponse(mockPageOfOrderDTOs);

// when
Mockito.when(bookService.getAllBooks(any(PaginatedFindAllRequest.class))).thenReturn(mockPageOfOrderDTOs);
Mockito.when(bookService.getAllBooks(any(PaginationRequest.class))).thenReturn(mockPageOfOrderDTOs);

// then
CustomResponse<CustomPageResponse<BookGetResponse>> response = CustomResponse.ok(customPageResponseOfBookGetResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import com.example.demo.payload.request.book.BookCreateRequest;
import com.example.demo.payload.request.book.BookUpdateRequest;
import com.example.demo.payload.request.book.BookUpdateStockRequest;
import com.example.demo.payload.request.pagination.DateIntervalRequest;
import com.example.demo.payload.request.pagination.PaginatedFindAllRequest;
import com.example.demo.payload.request.pagination.PaginationRequest;
import com.example.demo.repository.BookRepository;
import com.example.demo.util.RandomUtil;
Expand All @@ -25,8 +23,10 @@
import org.springframework.data.domain.Pageable;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -169,25 +169,9 @@ void givenValidBookIdAndBookUpdateStockRequest_whenBookNotFound_throwBookNotFoun

@Test
void givenPaginatedFindAllRequest_WhenNoBooksFound_throwBookNotFoundException() {
// Given

Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, 2000);
calendar1.set(Calendar.MONTH, Calendar.SEPTEMBER);
calendar1.set(Calendar.DAY_OF_MONTH, 10);

Calendar calendar2 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, 2000);
calendar1.set(Calendar.MONTH, Calendar.SEPTEMBER);
calendar1.set(Calendar.DAY_OF_MONTH, 13);

LocalDateTime startDate = calendar1.getTime().toInstant().atZone(calendar1.getTimeZone().toZoneId()).toLocalDateTime();
LocalDateTime endDate = calendar2.getTime().toInstant().atZone(calendar2.getTimeZone().toZoneId()).toLocalDateTime();

PaginatedFindAllRequest request = PaginatedFindAllRequest.builder()
.dateIntervalRequest(new DateIntervalRequest(startDate, endDate))
.paginationRequest(new PaginationRequest(1, 10))
.build();
// Given
PaginationRequest request = new PaginationRequest(1, 10);

Page<Book> emptyPage = new PageImpl<>(Collections.emptyList());

Expand All @@ -208,23 +192,8 @@ void givenPaginatedFindAllRequest_WhenNoBooksFound_throwBookNotFoundException()
@Test
void givenPaginatedFindAllRequest_WhenBooksFound_ReturnPageBookList() {

Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, 2000);
calendar1.set(Calendar.MONTH, Calendar.SEPTEMBER);
calendar1.set(Calendar.DAY_OF_MONTH, 10);

Calendar calendar2 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, 2000);
calendar1.set(Calendar.MONTH, Calendar.SEPTEMBER);
calendar1.set(Calendar.DAY_OF_MONTH, 13);

LocalDateTime startDate = calendar1.getTime().toInstant().atZone(calendar1.getTimeZone().toZoneId()).toLocalDateTime();
LocalDateTime endDate = calendar2.getTime().toInstant().atZone(calendar2.getTimeZone().toZoneId()).toLocalDateTime();

PaginatedFindAllRequest request = PaginatedFindAllRequest.builder()
.dateIntervalRequest(new DateIntervalRequest(startDate, endDate))
.paginationRequest(new PaginationRequest(1, 10))
.build();
// given
PaginationRequest request = new PaginationRequest(1, 10);

List<Book> books = Arrays.asList(
Book.builder()
Expand Down

0 comments on commit 560da1d

Please sign in to comment.