#1220 - Backport String constants for IanalinkRelations.
This commit adds String constants to IanaLinkRelations, so that IanaLinkRelations can be leveraged in places where compile time constants are required, such as Relation#itemRelation and Relation#collectionRelation. Original issue: #1216
This commit is contained in:
committed by
Greg Turnquist
parent
f0abb4d847
commit
25bc02c548
File diff suppressed because it is too large
Load Diff
@@ -20,9 +20,17 @@ import static org.assertj.core.api.Assertions.*;
|
||||
import lombok.Value;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Greg Turnquist
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
class IanaLinkRelationUnitTest {
|
||||
|
||||
@@ -31,7 +39,7 @@ class IanaLinkRelationUnitTest {
|
||||
*/
|
||||
@Test
|
||||
void extractingValueOfIanaLinkRelationShouldWork() {
|
||||
assertThat(IanaLinkRelations.ABOUT.value()).isEqualTo("about");
|
||||
assertThat(IanaLinkRelations.ABOUT.value()).isEqualTo(IanaLinkRelations.ABOUT_VALUE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,6 +85,32 @@ class IanaLinkRelationUnitTest {
|
||||
assertThat(IanaLinkRelations.isIanaRel(new CustomLinkRelation("something-new"))).isFalse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #1216
|
||||
*/
|
||||
@Test
|
||||
void testAllIanaLinkRelationsHaveStringConstant() {
|
||||
|
||||
Set<String> linkRelations = Arrays.stream(IanaLinkRelations.class.getDeclaredFields()) //
|
||||
.filter(ReflectionUtils::isPublicStaticFinal) //
|
||||
.filter(field -> LinkRelation.class.equals(field.getType())) //
|
||||
.map(it -> ReflectionUtils.getField(it, null)) //
|
||||
.filter(Objects::nonNull) //
|
||||
.map(LinkRelation.class::cast) //
|
||||
.map(LinkRelation::value) //
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
Set<String> stringConstants = Arrays.stream(IanaLinkRelations.class.getDeclaredFields()) //
|
||||
.filter(ReflectionUtils::isPublicStaticFinal) //
|
||||
.filter(field -> String.class.equals(field.getType()))
|
||||
.map(it -> ReflectionUtils.getField(it, null)) //
|
||||
.filter(Objects::nonNull) //
|
||||
.map(String.class::cast) //
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
assertThat(linkRelations).containsExactlyElementsOf(stringConstants);
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom implementation of the {@link LinkRelation} interface.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* https://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.springframework.hateoas.server.core;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.hateoas.IanaLinkRelations;
|
||||
import org.springframework.hateoas.LinkRelation;
|
||||
import org.springframework.hateoas.server.LinkRelationProvider;
|
||||
import org.springframework.hateoas.server.LinkRelationProvider.LookupContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
class AnnotationLinkRelationProviderUnitTest {
|
||||
|
||||
@Test
|
||||
void ianaLinkRelationsCanBeUsedForItemAndCollectionRelations() {
|
||||
|
||||
LinkRelationProvider linkRelationProvider = new AnnotationLinkRelationProvider();
|
||||
|
||||
assertThat(linkRelationProvider.getItemResourceRelFor(Resource.class)).isEqualTo(IanaLinkRelations.ITEM);
|
||||
assertThat(linkRelationProvider.getCollectionResourceRelFor(Resource.class)).isEqualTo(IanaLinkRelations.COLLECTION);
|
||||
}
|
||||
|
||||
@Relation(itemRelation = IanaLinkRelations.ITEM_VALUE, collectionRelation = IanaLinkRelations.COLLECTION_VALUE)
|
||||
static class Resource {}
|
||||
}
|
||||
Reference in New Issue
Block a user