diff --git a/events_mgmt/pom.xml b/events_mgmt/pom.xml index ac095f01..48dba3f3 100644 --- a/events_mgmt/pom.xml +++ b/events_mgmt/pom.xml @@ -33,25 +33,35 @@ aio-lib-java-core ${project.version} - - - org.junit.jupiter - junit-jupiter + com.adobe.aio + aio-lib-java-ims + ${project.version} + org.slf4j slf4j-simple - - com.adobe.aio - aio-lib-java-ims - ${project.version} - io.openapitools.jackson.dataformat jackson-dataformat-hal + + + + org.junit.jupiter + junit-jupiter + + + org.mockito + mockito-core + + + org.mockito + mockito-junit-jupiter + + diff --git a/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/ConflictException.java b/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/ConflictException.java index 4c18a23c..9d643c8a 100644 --- a/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/ConflictException.java +++ b/events_mgmt/src/main/java/com/adobe/aio/event/management/feign/ConflictException.java @@ -13,16 +13,17 @@ import feign.FeignException; import feign.Response; -import java.util.Optional; +import java.util.Collection; public class ConflictException extends FeignException { + public static final String X_CONFLICTING_ID = "x-conflicting-id"; private final String conflictingId; public ConflictException(Response response, FeignException exception) { super(response.status(), exception.getMessage(), response.request(), exception); - Optional conflictingIdOptional = response.headers().get("x-conflicting-id").stream().findFirst(); - conflictingId = conflictingIdOptional.isPresent() ? conflictingIdOptional.get() : null; + Collection conflictingIdHeader = response.headers().get(X_CONFLICTING_ID); + conflictingId = conflictingIdHeader!=null ? conflictingIdHeader.stream().findFirst().orElse(null) : null; } public String getConflictingId() { diff --git a/events_mgmt/src/test/java/com/adobe/aio/event/management/feign/ConflictExceptionTest.java b/events_mgmt/src/test/java/com/adobe/aio/event/management/feign/ConflictExceptionTest.java new file mode 100644 index 00000000..1d246256 --- /dev/null +++ b/events_mgmt/src/test/java/com/adobe/aio/event/management/feign/ConflictExceptionTest.java @@ -0,0 +1,60 @@ +package com.adobe.aio.event.management.feign; + +import static com.adobe.aio.event.management.feign.ConflictException.X_CONFLICTING_ID; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.when; + +import feign.FeignException; +import feign.Request; +import feign.Response; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class ConflictExceptionTest { + + @Mock + private Response response ; + + @Mock + private Request request; + + @Mock + private FeignException feignException; + + @BeforeEach + void beforeeach() { + when(response.request()).thenReturn(request); + } + + @Test + void withNoConflictIdHeader() { + when(response.headers()).thenReturn(Collections.emptyMap()); + assertEquals(null, new ConflictException(response, feignException).getConflictingId()); + } + + @Test + void withEmptyConflictIdHeader() { + Map> headers = new HashMap<>(); + headers.put(X_CONFLICTING_ID, Collections.emptyList()); + when(response.headers()).thenReturn(headers); + assertEquals(null, new ConflictException(response, feignException).getConflictingId()); + } + + @Test + void withConflictIdHeader() { + String conflictingId = "someId"; + Map> headers = new HashMap<>(); + headers.put(X_CONFLICTING_ID, Collections.singletonList(conflictingId)); + when(response.headers()).thenReturn(headers); + assertEquals(conflictingId, new ConflictException(response, feignException).getConflictingId()); + } + +}