Skip to content
Open
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
## Release Notes

`1.3.2`

- Added `Strings.nonBlankOrDefault(String, String)` to return a default when the value is blank.
- Added `Strings.nonBlankOrGet(String, Supplier)` to return a supplied default when the value is blank.
- Changed `Headers.of` to use `CaseInsensitiveLinkedHashMap` from MorphiX.
- Updated `ApacheHC5ExchangeClient` to use `CaseInsensitiveLinkedHashMap` from MorphiX.
- Updated `OkHttpExchangeClient` to use `CaseInsensitiveLinkedHashMap` from MorphiX.
- Upgraded `oauth2-oidc-sdk` to `11.39`.
- Upgraded OkHttp to `5.5.1`, Maven users now resolve the JVM artifact as `okhttp-jvm`.
- Upgraded `morphix-all` to `1.1.0`.

---

`1.3.1`

- Preserved header insertion order in `Headers.of()`.
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Support is being developed for other HTTP clients like Netty, Jersey, etc. If yo

### Releases

Current release `1.3.1`
Current release `1.3.2`

### Getting Started

Expand All @@ -62,7 +62,7 @@ Maven: add this dependency to your `pom.xml`
<dependency>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany</artifactId>
<version>1.3.1</version>
<version>1.3.2</version>
</dependency>
```

Expand Down Expand Up @@ -146,7 +146,7 @@ By default Apiphany uses the JDK `java.net.http` client. You can switch to anoth

#### Apache HttpClient5

Add the `apiphany-httpclient5` dependency, then:
Add the `apiphany-httpclient5` and `org.apache.httpcomponents.client5:httpclient5` dependencies, then:

```java
import org.apiphany.client.http.ApacheHC5HttpExchangeClient;
Expand All @@ -162,7 +162,7 @@ String name = http(client)

#### OkHttp

Add the `apiphany-okhttp` dependency, then:
Add the `apiphany-okhttp` and `com.squareup.okhttp3:okhttp-jvm` dependencies, then:

```java
import org.apiphany.client.http.OkHttpExchangeClient;
Expand All @@ -176,6 +176,8 @@ String name = http(client)
.orRethrow();
```

If you want to use the most used OkHttp version `4.12.x` you need to add `com.squareup.okhttp3:okhttp` dependency.

#### Spring `RestClient`

Add the `apiphany-spring` dependency, then:
Expand Down
2 changes: 1 addition & 1 deletion apiphany-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.1</version>
<version>1.3.2</version>
</parent>

<artifactId>apiphany</artifactId>
Expand Down
5 changes: 2 additions & 3 deletions apiphany-core/src/main/java/org/apiphany/header/Headers.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
Expand All @@ -12,6 +11,7 @@

import org.apiphany.lang.Strings;
import org.morphix.lang.Nullables;
import org.morphix.lang.collections.CaseInsensitiveLinkedHashMap;
import org.morphix.lang.collections.Lists;
import org.morphix.lang.collections.Maps;

Expand Down Expand Up @@ -39,8 +39,7 @@ static Map<String, List<String>> of(final HeaderFunction... headerFunctions) {
if (null == headerFunctions || 0 == headerFunctions.length) {
return Collections.emptyMap();
}
// TODO: implement a case insensitive linked hash map
var map = new LinkedHashMap<String, List<String>>();
var map = new CaseInsensitiveLinkedHashMap<List<String>>();
for (HeaderFunction headerFunction : headerFunctions) {
headerFunction.addTo(map);
}
Expand Down
23 changes: 23 additions & 0 deletions apiphany-core/src/main/java/org/apiphany/lang/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Collection;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;

import org.apiphany.io.IOStreams;
import org.apiphany.io.ResourceLocation;
Expand Down Expand Up @@ -543,4 +544,26 @@ static boolean equalsIgnoreCase(final String a, final String b) {
}
return null != a && a.equalsIgnoreCase(b);
}

/**
* Returns the given string if it is not blank, otherwise the default value.
*
* @param value a string
* @param defaultValue default value if value is blank
* @return the given string if it is not blank, otherwise the default value
*/
static String nonBlankOrDefault(final String value, final String defaultValue) {
return isNotBlank(value) ? value : defaultValue;
}

/**
* Returns the given string if it is not blank, otherwise the supplied default value.
*
* @param value a string
* @param defaultValue default value if value is blank
* @return the given string if it is not blank, otherwise the supplied default value
*/
static String nonBlankOrGet(final String value, final Supplier<String> defaultValue) {
return isNotBlank(value) ? value : defaultValue.get();
}
}
11 changes: 11 additions & 0 deletions apiphany-core/src/test/java/org/apiphany/header/HeadersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,17 @@ void shouldPreserveHeaderInsertionOrder() {
assertThat(List.copyOf(resultHeaders.keySet()), equalTo(List.of("first", "second", "third")));
}

@Test
void shouldTreatHeaderNamesAsCaseInsensitive() {
Map<String, List<String>> resultHeaders = Headers.of(
HeaderFunction.header("Content-Type", "application/json"),
HeaderFunction.header("content-type", "text/plain"));

assertThat(resultHeaders.size(), equalTo(1));
assertThat(resultHeaders.get("CONTENT-TYPE"), equalTo(List.of("application/json", "text/plain")));
assertThat(List.copyOf(resultHeaders.keySet()), equalTo(List.of("Content-Type")));
}

@Test
void shouldCreateHeadersMapWithNoHeaders() {
Map<String, List<String>> resultHeaders = Headers.of();
Expand Down
71 changes: 71 additions & 0 deletions apiphany-core/src/test/java/org/apiphany/lang/StringsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -19,6 +21,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Stream;

import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -794,6 +797,74 @@ private static Stream<Arguments> provideUnequalIgnoreCasePairs() {
}
}

/**
* Tests for:
* <ul>
* <li>{@link Strings#nonBlankOrDefault(String, String)}</li>
* </ul>
*/
@Nested
class NonBlankOrDefaultTests {

@Test
void shouldReturnValueWhenNotBlank() {
assertThat(Strings.nonBlankOrDefault(TEST_STRING, "default"), equalTo(TEST_STRING));
assertThat(Strings.nonBlankOrDefault(NOT_BLANK_STRING, "default"), equalTo(NOT_BLANK_STRING));
}

@ParameterizedTest
@MethodSource("provideBlankValues")
void shouldReturnDefaultWhenValueIsBlank(final String blankValue) {
assertThat(Strings.nonBlankOrDefault(blankValue, "default"), equalTo("default"));
}

private static Stream<Arguments> provideBlankValues() {
return Stream.of(
Arguments.of((String) null),
Arguments.of(""),
Arguments.of(BLANK_STRING));
}
}

/**
* Tests for:
* <ul>
* <li>{@link Strings#nonBlankOrGet(String, Supplier)}</li>
* </ul>
*/
@Nested
class NonBlankOrGetTests {

@Test
void shouldReturnValueWhenNotBlankWithoutCallingSupplier() {
@SuppressWarnings("unchecked")
Supplier<String> defaultValue = mock(Supplier.class);

assertThat(Strings.nonBlankOrGet(TEST_STRING, defaultValue), equalTo(TEST_STRING));

verify(defaultValue, never()).get();
}

@ParameterizedTest
@MethodSource("provideBlankValues")
void shouldReturnSuppliedDefaultWhenValueIsBlank(final String blankValue) {
@SuppressWarnings("unchecked")
Supplier<String> defaultValue = mock(Supplier.class);
when(defaultValue.get()).thenReturn("default");

assertThat(Strings.nonBlankOrGet(blankValue, defaultValue), equalTo("default"));

verify(defaultValue).get();
}

private static Stream<Arguments> provideBlankValues() {
return Stream.of(
Arguments.of((String) null),
Arguments.of(""),
Arguments.of(BLANK_STRING));
}
}

@Nested
class EndsWithAnyTests {

Expand Down
2 changes: 1 addition & 1 deletion apiphany-httpclient5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.1</version>
<version>1.3.2</version>
</parent>

<artifactId>apiphany-httpclient5</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.Serializable;
import java.net.URI;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
Expand Down Expand Up @@ -51,6 +50,7 @@
import org.apiphany.io.InputStreamSupplier;
import org.morphix.lang.JavaObjects;
import org.morphix.lang.Nullables;
import org.morphix.lang.collections.CaseInsensitiveLinkedHashMap;
import org.morphix.lang.collections.Lists;
import org.morphix.lang.collections.Maps;
import org.morphix.lang.function.Predicates;
Expand Down Expand Up @@ -380,7 +380,7 @@ public static HttpUriRequest toHttpUriRequest(final URI uri, final String httpMe
* @return HTTP headers
*/
public static Map<String, List<String>> toHttpHeadersMap(final Header[] headers) {
var httpHeaders = new LinkedHashMap<String, List<String>>();
var httpHeaders = new CaseInsensitiveLinkedHashMap<List<String>>();
for (Header header : headers) {
Headers.addTo(httpHeaders, header.getName(), header.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,20 @@ void shouldAccumulateMultipleValuesForSameHeader() {

assertThat(result.get("Accept"), equalTo(List.of("text/html", "application/json")));
}

@Test
void shouldTreatHeaderNamesAsCaseInsensitive() {
Header[] headers = new Header[] {
new BasicHeader("Content-Type", "application/json"),
new BasicHeader("content-type", "text/plain")
};

Map<String, List<String>> result = ApacheHC5HttpExchangeClient.toHttpHeadersMap(headers);

assertThat(result.size(), equalTo(1));
assertThat(result.get("CONTENT-TYPE"), equalTo(List.of("application/json", "text/plain")));
assertThat(result.keySet().iterator().next(), equalTo("Content-Type"));
}
}

@Nested
Expand Down
2 changes: 1 addition & 1 deletion apiphany-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.1</version>
<version>1.3.2</version>
</parent>

<artifactId>apiphany-integration-tests</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions apiphany-okhttp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.1</version>
<version>1.3.2</version>
</parent>

<artifactId>apiphany-okhttp</artifactId>
Expand Down Expand Up @@ -65,7 +65,7 @@
<!-- HTTP Client -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<artifactId>${okhttp.artifact.id}</artifactId>
<scope>provided</scope>
</dependency>

Expand Down
Loading
Loading