Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "fallback cases" for relevant enums in :nessie-model #6634

Merged
merged 8 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions api/model/src/main/java/org/projectnessie/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package org.projectnessie.error;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Function;

Expand Down Expand Up @@ -60,4 +65,23 @@ public static Optional<Exception> asException(NessieError error) {
.flatMap(e -> Optional.ofNullable(e.exceptionBuilder))
.map(b -> b.apply(error));
}

public static ErrorCode parse(String errorCode) {
try {
if (errorCode != null) {
return ErrorCode.valueOf(errorCode.toUpperCase(Locale.ROOT));
}
return null;
} catch (IllegalArgumentException e) {
return UNKNOWN;
}
}

public static final class Deserializer extends JsonDeserializer<ErrorCode> {
@Override
public ErrorCode deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String name = p.readValueAs(String.class);
return name != null ? ErrorCode.parse(name) : null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ default String getMessage() {

/** Nessie-specific error code. */
@Value.Default
@JsonDeserialize(using = ErrorCode.Deserializer.class)
default ErrorCode getErrorCode() {
return ErrorCode.UNKNOWN;
}
Expand Down
16 changes: 14 additions & 2 deletions api/model/src/main/java/org/projectnessie/model/Conflict.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
*/
package org.projectnessie.model;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.io.IOException;
import java.util.Locale;
import javax.annotation.Nullable;
import org.immutables.value.Value;
import org.projectnessie.model.Util.ConflictTypeDeserializer;

@Value.Immutable
@JsonSerialize(as = ImmutableConflict.class)
Expand All @@ -29,7 +32,7 @@ public interface Conflict {
@Value.Parameter(order = 1)
@Nullable
@jakarta.annotation.Nullable
@JsonDeserialize(using = ConflictTypeDeserializer.class)
@JsonDeserialize(using = ConflictType.Deserializer.class)
ConflictType conflictType();

@Value.Parameter(order = 2)
Expand Down Expand Up @@ -94,5 +97,14 @@ public static ConflictType parse(String conflictType) {
return UNKNOWN;
}
}

public static final class Deserializer extends JsonDeserializer<ConflictType> {
@Override
public ConflictType deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException {
String name = p.readValueAs(String.class);
return name != null ? ConflictType.parse(name) : null;
}
}
}
}
12 changes: 12 additions & 0 deletions api/model/src/main/java/org/projectnessie/model/FetchOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.projectnessie.model;

import java.util.Locale;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -50,4 +51,15 @@ public static String getFetchOptionName(
@Nullable @jakarta.annotation.Nullable FetchOption fetchOption) {
return null == fetchOption ? FetchOption.MINIMAL.name() : fetchOption.name();
}

public static FetchOption parse(String fetchOption) {
try {
if (fetchOption != null) {
return FetchOption.valueOf(fetchOption.toUpperCase(Locale.ROOT));
}
return null;
} catch (IllegalArgumentException e) {
return ALL;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import javax.annotation.Nullable;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
Expand Down Expand Up @@ -89,6 +94,7 @@ interface ContentKeyDetails {
MergeBehavior getMergeBehavior();

@Value.Default
@JsonDeserialize(using = ContentKeyConflict.Deserializer.class)
default ContentKeyConflict getConflictType() {
return ContentKeyConflict.NONE;
}
Expand All @@ -111,6 +117,26 @@ default ContentKeyConflict getConflictType() {

enum ContentKeyConflict {
NONE,
UNRESOLVABLE
UNRESOLVABLE;

public static ContentKeyConflict parse(String mergeBehavior) {
try {
if (mergeBehavior != null) {
return ContentKeyConflict.valueOf(mergeBehavior.toUpperCase(Locale.ROOT));
}
return null;
} catch (IllegalArgumentException e) {
return UNRESOLVABLE;
}
}

static final class Deserializer extends JsonDeserializer<ContentKeyConflict> {
@Override
public ContentKeyConflict deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException {
String name = p.readValueAs(String.class);
return name != null ? ContentKeyConflict.parse(name) : null;
}
}
}
}
9 changes: 0 additions & 9 deletions api/model/src/main/java/org/projectnessie/model/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.projectnessie.model.Conflict.ConflictType;
import org.projectnessie.model.types.ContentTypes;

final class Util {
Expand Down Expand Up @@ -83,14 +82,6 @@ public static String toPathStringRef(String name, String hash) {
return builder.toString();
}

static final class ConflictTypeDeserializer extends JsonDeserializer<ConflictType> {
@Override
public ConflictType deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
String name = p.readValueAs(String.class);
return name != null ? ConflictType.parse(name) : null;
}
}

static final class ContentTypeDeserializer extends JsonDeserializer<Content.Type> {
@Override
public Content.Type deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
Expand Down
193 changes: 193 additions & 0 deletions api/model/src/test/java/org/projectnessie/model/TestEnums.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*
* Copyright (C) 2023 Dremio
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.projectnessie.model;

import static org.assertj.core.api.Assumptions.assumeThat;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.function.Function;
import org.assertj.core.api.SoftAssertions;
import org.assertj.core.api.junit.jupiter.InjectSoftAssertions;
import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.projectnessie.error.ErrorCode;
import org.projectnessie.error.NessieError;
import org.projectnessie.model.Conflict.ConflictType;
import org.projectnessie.model.MergeResponse.ContentKeyConflict;
import org.projectnessie.model.MergeResponse.ContentKeyDetails;

@ExtendWith(SoftAssertionsExtension.class)
public class TestEnums {
@InjectSoftAssertions protected SoftAssertions soft;

@SuppressWarnings({
"unused",
"JUnit3StyleTestMethodInJUnit4Class"
}) // IntelliJ false positive warnings :(
abstract class AbstractEnum<E extends Enum<E>> {
final Function<String, E> parse;
final E unknownValue;
final Function<ObjectMapper, E> unknownJsonTest;

AbstractEnum(
Function<String, E> parse, E unknownValue, Function<ObjectMapper, E> unknownJsonTest) {
this.parse = parse;
this.unknownValue = unknownValue;
this.unknownJsonTest = unknownJsonTest;
}

@Test
public void testUnknown() {
if (unknownValue == null) {
soft.assertThatIllegalArgumentException().isThrownBy(() -> parse.apply("FOO_BAR"));
} else {
soft.assertThat(parse.apply("FOO_BAR")).isSameAs(unknownValue);
}
}

@Test
public void testUnknownJson() {
assumeThat(unknownJsonTest).isNotNull();

ObjectMapper mapper = new ObjectMapper();
if (unknownValue == null) {
soft.assertThatThrownBy(() -> unknownJsonTest.apply(mapper))
.hasCauseInstanceOf(JsonMappingException.class)
.hasMessageContaining(".FOO_BAR ");
} else {
soft.assertThat(unknownJsonTest.apply(mapper)).isSameAs(unknownValue);
}
}

@Test
public void testNull() {
soft.assertThat(parse.apply(null)).isNull();
}

void conversion(E value) {
soft.assertThat(parse.apply(value.name())).isSameAs(value);
}
}

@Nested
public class TestErrorCode extends AbstractEnum<ErrorCode> {
TestErrorCode() {
super(
ErrorCode::parse,
ErrorCode.UNKNOWN,
mapper -> {
try {
return mapper
.readValue(
"{"
+ "\"status\": 42,"
+ "\"reason\": \"reason\","
+ "\"errorCode\": \"FOO_BAR\""
+ "}",
NessieError.class)
.getErrorCode();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}

@ParameterizedTest
@EnumSource(value = ErrorCode.class)
public void testConversion(ErrorCode errorCode) {
super.conversion(errorCode);
}
}

@Nested
public class TestFetchOption extends AbstractEnum<FetchOption> {
TestFetchOption() {
super(FetchOption::parse, FetchOption.ALL, null);
}

@ParameterizedTest
@EnumSource(value = FetchOption.class)
public void testConversion(FetchOption fetchOption) {
super.conversion(fetchOption);
}
}

@Nested
public class TestConflictType extends AbstractEnum<ConflictType> {
TestConflictType() {
super(
ConflictType::parse,
ConflictType.UNKNOWN,
mapper -> {
try {
return mapper
.readValue(
"{"
+ "\"key\": { \"elements\": [\"a\"] },"
+ "\"message\": \"reason\","
+ "\"conflictType\": \"FOO_BAR\""
+ "}",
Conflict.class)
.conflictType();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}

@ParameterizedTest
@EnumSource(value = ConflictType.class)
public void testConversion(ConflictType conflictType) {
conversion(conflictType);
}
}

@Nested
public class TestContentKeyConflict extends AbstractEnum<ContentKeyConflict> {
TestContentKeyConflict() {
super(
ContentKeyConflict::parse,
ContentKeyConflict.UNRESOLVABLE,
mapper -> {
try {
return mapper
.readValue(
"{"
+ "\"key\": { \"elements\": [\"a\"] },"
+ "\"mergeBehavior\": \"NORMAL\","
+ "\"conflictType\": \"FOO_BAR\""
+ "}",
ContentKeyDetails.class)
.getConflictType();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});
}

@ParameterizedTest
@EnumSource(value = ContentKeyConflict.class)
public void testConversion(ContentKeyConflict contentKeyConflict) {
conversion(contentKeyConflict);
}
}
}