Skip to content

Commit

Permalink
feat : adds negative tests and labels (#1385)
Browse files Browse the repository at this point in the history
* feat : adds negative tests and labels

* adds auto connection pooling
  • Loading branch information
rajadilipkolli authored Aug 22, 2024
1 parent ed7d688 commit 429255f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
- any-glob-to-any-file:
- r2dbc/boot-jooq-r2dbc-sample/**/*
- r2dbc/boot-r2dbc-sample/**/*
- r2dbc/boot-reactive-cache/**/*
"component: jobrunr":
- changed-files:
- any-glob-to-any-file:
Expand Down Expand Up @@ -150,6 +151,7 @@
- open-api-spring-boot/pom.xml
- r2dbc/boot-jooq-r2dbc-sample/pom.xml
- r2dbc/boot-r2dbc-sample/pom.xml
- r2dbc/boot-reactive-cache/pom.xml
- scheduler/boot-jobrunr-sample/pom.xml
- scheduler/boot-scheduler-quartz/pom.xml
- scheduler/boot-shedlock-sample/pom.xml
7 changes: 7 additions & 0 deletions r2dbc/boot-reactive-cache/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand All @@ -78,10 +79,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Expand All @@ -92,6 +98,7 @@
<artifactId>r2dbc-postgresql</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.example.cache.model.request.MovieRequest;
import com.example.cache.model.response.MovieResponse;
import com.example.cache.repositories.MovieRepository;
import com.example.cache.utils.AppConstants;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -37,22 +38,25 @@ public Flux<MovieResponse> findAll() {
.switchIfEmpty(movieRepository
.findAll()
// Persisting the fetched movies in the cache.
.flatMap(movie -> reactiveRedisTemplate.opsForValue().set("movie:" + movie.id(), movie))
.flatMap(movie ->
reactiveRedisTemplate.opsForValue().set(AppConstants.MOVIE_KEY + movie.id(), movie))
// Fetching the movies from the updated cache.
.thenMany(reactiveRedisTemplate.keys("movie:*").flatMap(key -> reactiveRedisTemplate
.opsForValue()
.get(key))))
.thenMany(reactiveRedisTemplate
.keys(AppConstants.MOVIE_KEY + "*")
.flatMap(key ->
reactiveRedisTemplate.opsForValue().get(key))))
.map(movieMapper::toResponse);
}

public Mono<MovieResponse> findMovieById(Long id) {
return reactiveRedisTemplate
.opsForValue()
.get("movie:" + id)
.get(AppConstants.MOVIE_KEY + id)
.switchIfEmpty(movieRepository
.findById(id)
.flatMap(movie -> reactiveRedisTemplate.opsForValue().set("movie:" + movie.id(), movie))
.then(reactiveRedisTemplate.opsForValue().get("movie:" + id)))
.flatMap(movie ->
reactiveRedisTemplate.opsForValue().set(AppConstants.MOVIE_KEY + movie.id(), movie))
.then(reactiveRedisTemplate.opsForValue().get(AppConstants.MOVIE_KEY + id)))
.map(movieMapper::toResponse);
}

Expand All @@ -62,8 +66,8 @@ public Mono<MovieResponse> saveMovie(MovieRequest movieRequest) {
.flatMap(movieRepository::save)
.flatMap(movie -> reactiveRedisTemplate
.opsForValue()
.set("movie:" + movie.id(), movie)
.then(reactiveRedisTemplate.opsForValue().get("movie:" + movie.id())))
.set(AppConstants.MOVIE_KEY + movie.id(), movie)
.then(reactiveRedisTemplate.opsForValue().get(AppConstants.MOVIE_KEY + movie.id())))
.map(movieMapper::toResponse);
}

Expand All @@ -75,13 +79,13 @@ public Mono<MovieResponse> updateMovie(Long id, MovieRequest movieRequest) {
.flatMap(movieRepository::save)
.flatMap(movie -> reactiveRedisTemplate
.opsForValue()
.set("movie:" + movie.id(), movie)
.then(reactiveRedisTemplate.opsForValue().get("movie:" + movie.id())))
.set(AppConstants.MOVIE_KEY + movie.id(), movie)
.then(reactiveRedisTemplate.opsForValue().get(AppConstants.MOVIE_KEY + movie.id())))
.map(movieMapper::toResponse);
}

@Transactional
public Mono<Long> deleteMovieById(Long id) {
return movieRepository.deleteById(id).then(reactiveRedisTemplate.delete("movie:" + id));
return movieRepository.deleteById(id).then(reactiveRedisTemplate.delete(AppConstants.MOVIE_KEY + id));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.example.cache.utils;

public final class AppConstants {
public static final String PROFILE_PROD = "prod";
public static final String PROFILE_NOT_PROD = "!" + PROFILE_PROD;
public static final String PROFILE_TEST = "test";
public static final String PROFILE_NOT_TEST = "!" + PROFILE_TEST;
public static final String DEFAULT_PAGE_NUMBER = "0";
public static final String DEFAULT_PAGE_SIZE = "10";
public static final String DEFAULT_SORT_BY = "id";
public static final String DEFAULT_SORT_DIRECTION = "asc";
public static final String MOVIE_KEY = "movie:";
private static final String PROFILE_PROD = "prod";
public static final String PROFILE_NOT_PROD = "!" + PROFILE_PROD;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ void shouldFetchAllMovies() {
.isOk()
.expectBodyList(MovieResponse.class)
.hasSize(movieFlux.collectList().block().size());
// .andExpect(jsonPath("$.totalElements", is(3)))
// .andExpect(jsonPath("$.pageNumber", is(1)))
// .andExpect(jsonPath("$.totalPages", is(1)))
// .andExpect(jsonPath("$.isFirst", is(true)))
// .andExpect(jsonPath("$.isLast", is(true)))
// .andExpect(jsonPath("$.hasNext", is(false)))
// .andExpect(jsonPath("$.hasPrevious", is(false)));
}

@Test
Expand All @@ -73,6 +66,24 @@ void shouldFindMovieById() {
.isEqualTo(movie.title());
}

@Test
void shouldReturn404WhenFetchingNonExistingMovie() {
this.webTestClient
.get()
.uri("/api/movies/{id}", 10000)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus()
.isNotFound()
.expectHeader()
.contentType(MediaType.APPLICATION_PROBLEM_JSON)
.expectBody()
.json(
"""
{"type":"https://api.boot-reactive-cache.com/errors/not-found","title":"Not Found","status":404,"detail":"Movie with Id '10000' not found","instance":"/api/movies/10000","errorCategory":"Generic"}
""");
}

@Test
void shouldCreateNewMovie() {
MovieRequest movieRequest = new MovieRequest("New Movie");
Expand All @@ -87,6 +98,10 @@ void shouldCreateNewMovie() {
.isCreated()
.expectHeader()
.contentType(MediaType.APPLICATION_JSON)
.expectHeader()
.contentLength(29)
.expectHeader()
.exists("Location")
.expectBody(MovieResponse.class)
.value(movieResponse -> {
assertThat(movieResponse.id()).isNotNull();
Expand All @@ -95,7 +110,7 @@ void shouldCreateNewMovie() {
}

@Test
void shouldReturn400WhenCreateNewMovieWithoutText() throws Exception {
void shouldReturn400WhenCreateNewMovieWithoutTitle() {
MovieRequest movieRequest = new MovieRequest(null);

this.webTestClient
Expand All @@ -118,7 +133,7 @@ void shouldReturn400WhenCreateNewMovieWithoutText() throws Exception {
}

@Test
void shouldUpdateMovie() throws Exception {
void shouldUpdateMovie() {
Long movieId = movieFlux.blockLast().id();
MovieRequest movieRequest = new MovieRequest("Updated Movie");

Expand Down Expand Up @@ -157,6 +172,23 @@ void shouldDeleteMovie() {
.isEqualTo(movie.id())
.jsonPath("$.title")
.isEqualTo(movie.title());
;
}

@Test
void shouldReturn404WhenDeletingNonExistingMovie() {
this.webTestClient
.delete()
.uri("/api/movies/{id}", 10000)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus()
.isNotFound()
.expectHeader()
.contentType(MediaType.APPLICATION_PROBLEM_JSON)
.expectBody()
.json(
"""
{"type":"https://api.boot-reactive-cache.com/errors/not-found","title":"Not Found","status":404,"detail":"Movie with Id '10000' not found","instance":"/api/movies/10000","errorCategory":"Generic"}
""");
}
}

0 comments on commit 429255f

Please sign in to comment.