Skip to content

Commit

Permalink
1.1.17 (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodforGod authored Dec 24, 2024
1 parent ca506f0 commit c4056d2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@

## Build

Собрать классы:

```shell
./gradlew classes
```

Собрать артефакт:

```shell
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
groupId=ru.tinkoff.kora
koraVersion=1.1.11
koraVersion=1.1.17


##### GRADLE #####
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ru.tinkoff.kora.java.crud.controller;

import io.micrometer.core.instrument.config.validate.ValidationException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.tinkoff.kora.common.Component;
import ru.tinkoff.kora.common.Context;
import ru.tinkoff.kora.common.Tag;
Expand All @@ -11,10 +11,15 @@
import ru.tinkoff.kora.java.crud.openapi.http.server.model.MessageTO;
import ru.tinkoff.kora.json.common.JsonWriter;

import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeoutException;

@Tag(HttpServerModule.class)
@Component
public final class HttpExceptionHandler implements HttpServerInterceptor {

private static final Logger logger = LoggerFactory.getLogger(HttpExceptionHandler.class);

private final JsonWriter<MessageTO> errorJsonWriter;

public HttpExceptionHandler(JsonWriter<MessageTO> errorJsonWriter) {
Expand All @@ -31,10 +36,13 @@ public CompletionStage<HttpServerResponse> intercept(Context context, HttpServer

var body = HttpBody.json(errorJsonWriter.toByteArrayUnchecked(new MessageTO(e.getMessage())));
if (e instanceof IllegalArgumentException || e instanceof ValidationException) {
logger.warn("Request '{} {}' failed due to {}", request.method(), request.path(), e.getMessage());
return HttpServerResponse.of(400, body);
} else if (e instanceof TimeoutException) {
logger.warn("Request '{} {}' failed due to timeout", request.method(), request.path(), e);
return HttpServerResponse.of(408, body);
} else {
logger.error("Request '{} {}' failed", request.method(), request.path(), e);
return HttpServerResponse.of(500, body);
}
});
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/ru/tinkoff/kora/java/crud/service/PetService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.tinkoff.kora.java.crud.service;

import java.util.Optional;
import ru.tinkoff.kora.cache.annotation.CacheInvalidate;
import ru.tinkoff.kora.cache.annotation.CachePut;
import ru.tinkoff.kora.cache.annotation.Cacheable;
Expand All @@ -13,6 +12,8 @@
import ru.tinkoff.kora.resilient.retry.annotation.Retry;
import ru.tinkoff.kora.resilient.timeout.annotation.Timeout;

import java.util.Optional;

@Component
public class PetService {

Expand Down Expand Up @@ -47,14 +48,17 @@ public Optional<Pet> update(long id, PetUpdateTO updateTO) {
return Optional.empty();
}

if (existing.get().name().equals(updateTO.name()) && existing.get().status().equals(updateTO.status())) {
final Pet pet = existing.get();
if (pet.name().equals(updateTO.name())
&& updateTO.status() != null
&& pet.status().equals(toStatus(updateTO.status()))) {
return existing;
}

var status = (updateTO.status() == null)
? existing.get().status()
? pet.status()
: toStatus(updateTO.status());
var result = new Pet(existing.get().id(), updateTO.name(), status);
var result = new Pet(pet.id(), updateTO.name(), status);
petRepository.update(result);
return Optional.of(result);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ db {

pet-cache {
maximumSize = 1000
maximumSize = ${?CACHE_MAX_SIZE}
expireAfterWrite = ${?CACHE_EXPIRE_WRITE}
}

Expand Down Expand Up @@ -48,6 +49,7 @@ resilient {
delay = "500ms"
delayStep = "5s"
attempts = 3
attempts = ${?RETRY_ATTEMPTS}
}
}

Expand Down
20 changes: 19 additions & 1 deletion src/test/java/ru/tinkoff/kora/java/crud/BlackBoxTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public static void setup(@ConnectionPostgreSQL JdbcConnection connection) {
"POSTGRES_JDBC_URL", params.jdbcUrl(),
"POSTGRES_USER", params.username(),
"POSTGRES_PASS", params.password(),
"CACHE_EXPIRE_WRITE", "0s"));
"CACHE_MAX_SIZE", "0",
"RETRY_ATTEMPTS", "0"));

container.start();
}
Expand Down Expand Up @@ -113,6 +114,23 @@ void getPet() throws Exception {
JSONAssert.assertEquals(createResponseBody.toString(), getResponseBody.toString(), JSONCompareMode.LENIENT);
}

@Test
void getPetNotFound() throws Exception {
// given
var httpClient = HttpClient.newHttpClient();

// when
var getRequest = HttpRequest.newBuilder()
.GET()
.uri(container.getURI().resolve("/v3/pets/1"))
.timeout(Duration.ofSeconds(5))
.build();

// then
var getResponse = httpClient.send(getRequest, HttpResponse.BodyHandlers.ofString());
assertEquals(404, getResponse.statusCode(), getResponse.body());
}

@Test
void updatePet() throws Exception {
// given
Expand Down

0 comments on commit c4056d2

Please sign in to comment.