Skip to content

Commit

Permalink
GH-207 aio-lib-java-events-mgmt: adding missing x-conflicting-id
Browse files Browse the repository at this point in the history
…header safe guard within `ConflictException` (#208)
  • Loading branch information
francoisledroff authored Jan 18, 2024
1 parent ed3c97e commit dccbe54
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 12 deletions.
28 changes: 19 additions & 9 deletions events_mgmt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,35 @@
<artifactId>aio-lib-java-core</artifactId>
<version>${project.version}</version>
</dependency>

<!-- test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<groupId>com.adobe.aio</groupId>
<artifactId>aio-lib-java-ims</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
<dependency>
<groupId>com.adobe.aio</groupId>
<artifactId>aio-lib-java-ims</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.openapitools.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-hal</artifactId>
</dependency>

<!-- test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> conflictingIdOptional = response.headers().get("x-conflicting-id").stream().findFirst();
conflictingId = conflictingIdOptional.isPresent() ? conflictingIdOptional.get() : null;
Collection<String> conflictingIdHeader = response.headers().get(X_CONFLICTING_ID);
conflictingId = conflictingIdHeader!=null ? conflictingIdHeader.stream().findFirst().orElse(null) : null;
}

public String getConflictingId() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Collection<String>> 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<String, Collection<String>> headers = new HashMap<>();
headers.put(X_CONFLICTING_ID, Collections.singletonList(conflictingId));
when(response.headers()).thenReturn(headers);
assertEquals(conflictingId, new ConflictException(response, feignException).getConflictingId());
}

}

0 comments on commit dccbe54

Please sign in to comment.