Skip to content

Commit

Permalink
make test code for Attribute injection.
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenchickenlove committed Apr 1, 2024
1 parent 2801cdd commit 3e809c7
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,7 @@ private Builder(AnnotatedElement annotatedElement, Type type, String name) {
*/
private Builder annotationType(Class<? extends Annotation> annotationType) {
assert annotationType == Param.class ||
annotationType == Attribute.class ||
annotationType == Header.class ||
annotationType == RequestObject.class : annotationType.getSimpleName();
this.annotationType = annotationType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,21 @@
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeMethod;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
Expand All @@ -66,13 +71,15 @@
import com.linecorp.armeria.server.RoutingResult;
import com.linecorp.armeria.server.RoutingResultBuilder;
import com.linecorp.armeria.server.ServiceRequestContext;
import com.linecorp.armeria.server.annotation.Attribute;
import com.linecorp.armeria.server.annotation.Default;
import com.linecorp.armeria.server.annotation.Get;
import com.linecorp.armeria.server.annotation.Header;
import com.linecorp.armeria.server.annotation.Param;
import com.linecorp.armeria.server.annotation.RequestObject;

import io.netty.util.AsciiString;
import io.netty.util.AttributeKey;

class AnnotatedValueResolverTest {

Expand Down Expand Up @@ -149,13 +156,25 @@ void ofMethods() {
try {
final List<AnnotatedValueResolver> elements = AnnotatedValueResolver.ofServiceMethod(
method, pathParams, objectResolvers, false, noopDependencyInjector, null);
elements.forEach(AnnotatedValueResolverTest::testResolver);
final Map<String, AttributeKey<?>> attrKeyMap = injectAttrKeyToServiceContextForAttributeTest();
elements.forEach(annotatedValueResolver ->
{
if (annotatedValueResolver.annotationType() == Attribute.class) {
testResolver(annotatedValueResolver, attrKeyMap);
} else {
testResolver(annotatedValueResolver);
}
}
);

// elements.forEach(annotatedValueResolver -> testResolver(annotatedValueResolver));
// elements.forEach(AnnotatedValueResolverTest::testResolver);
} catch (NoAnnotatedParameterException ignored) {
// Ignore this exception because MixedBean class has not annotated method.
}
});
}

// com/linecorp/armeria/internal/server/annotation/AnnotatedValueResolverTest.java
@Test
void ofFieldBean() throws NoSuchFieldException {
final FieldBean bean = new FieldBean();
Expand Down Expand Up @@ -238,6 +257,24 @@ private static <T> void testMethod(Method method, T bean) {
}
}

@SuppressWarnings("unchecked")
private static void testResolver(AnnotatedValueResolver resolver, Map<String, AttributeKey<?>> attrKeys) {
// When
final Object value = resolver.resolve(resolverContext);
logger.debug("Element {}: value {}", resolver, value);

if (resolver.annotationType() == Attribute.class) {
final AttributeKey<?> attrKey = attrKeys.get(resolver.httpElementName());
final Object expectedValue = resolverContext.context().attr(attrKey);

// Then
assertThat(value).isEqualTo(expectedValue);
assertThat(value).isNotNull();
} else {
testResolver(resolver);
}
}

@SuppressWarnings("unchecked")
private static void testResolver(AnnotatedValueResolver resolver) {
final Object value = resolver.resolve(resolverContext);
Expand Down Expand Up @@ -405,6 +442,21 @@ void redundant2(@Param @Default("defaultValue") String var1) {}
@Get("/r3/:var1")
void redundant3(@Param Optional<String> var1) {}

@Get("/attribute-test")
void attributeTest(
@Attribute(value = "primitiveIntWithPrimitivePrefix", prefix = int.class) int primitiveIntWithPrimitivePrefix,
@Attribute(value = "primitiveIntWithoutPrefix") int primitiveIntOutPrefix,
@Attribute(value = "primitiveIntWithReferencePrefix", prefix = Integer.class) int ReferenceIntWithReferencePrefix,
@Attribute(value = "primitiveIntWithoutType") int primitiveIntWithoutType,
@Attribute(value = "referenceIntWithPrimitivePrefix", prefix = int.class) Integer referenceIntWithPrimitivePrefix,
@Attribute(value = "referenceIntWithoutPrefix") Integer referenceIntWithoutPrefix,
@Attribute(value = "referenceIntWithoutType") Integer referenceIntWithoutType,
@Attribute(value = "referenceIntWithReferencePrefix", prefix = Integer.class) Integer referenceIntWithReferencePrefix,
@Attribute(value = "stringCollectionWithPrefix", prefix = List.class) List<String> stringCollectionWithPrefix,
@Attribute(value = "stringCollectionWithoutPrefix") List<String> stringCollectionWithoutPrefix,
@Attribute(value = "stringCollectionWithoutType") List<String> stringCollectionWithoutType) { }


void time(@Param @Default("PT20.345S") Duration duration,
@Param @Default("2007-12-03T10:15:30.00Z") Instant instant,
@Param @Default("2007-12-03") LocalDate localDate,
Expand All @@ -418,6 +470,53 @@ void time(@Param @Default("PT20.345S") Duration duration,
@Param @Default("+01:00:00") ZoneOffset zoneOffset) {}
}

private Map<String, AttributeKey<?>> injectAttrKeyToServiceContextForAttributeTest() {

ServiceRequestContext ctx = resolverContext.context();

AttributeKey<Integer> primitiveIntWithPrimitivePrefix = AttributeKey.valueOf(int.class, "primitiveIntWithPrimitivePrefix");
AttributeKey<Integer> primitiveIntWithoutPrefix = AttributeKey.valueOf(int.class, "primitiveIntWithoutPrefix");
AttributeKey<Integer> primitiveIntWithReferencePrefix = AttributeKey.valueOf(int.class, "primitiveIntWithReferencePrefix");
AttributeKey<Integer> primitiveIntWithoutType = AttributeKey.valueOf("primitiveIntWithoutType");
AttributeKey<Integer> referenceIntWithPrimitivePrefix = AttributeKey.valueOf(Integer.class, "referenceIntWithPrimitivePrefix");
AttributeKey<Integer> referenceIntWithoutPrefix = AttributeKey.valueOf(Integer.class, "referenceIntWithoutPrefix");
AttributeKey<Integer> referenceIntWithReferencePrefix = AttributeKey.valueOf(Integer.class, "referenceIntWithReferencePrefix");
AttributeKey<Integer> referenceIntWithoutType = AttributeKey.valueOf(Integer.class, "referenceIntWithoutType");
AttributeKey<List<String>> stringCollectionWithPrefix = AttributeKey.valueOf(List.class, "stringCollectionWithPrefix");
AttributeKey<List<String>> stringCollectionWithoutPrefix = AttributeKey.valueOf(List.class, "stringCollectionWithoutPrefix");
AttributeKey<List<String>> stringCollectionWithoutType = AttributeKey.valueOf(List.class, "stringCollectionWithoutType");

ctx.setAttr(primitiveIntWithPrimitivePrefix, 10);
ctx.setAttr(primitiveIntWithoutPrefix, 10);
ctx.setAttr(primitiveIntWithReferencePrefix, 10);
ctx.setAttr(primitiveIntWithoutType, 10);
ctx.setAttr(referenceIntWithPrimitivePrefix, 10);
ctx.setAttr(referenceIntWithoutPrefix, 10);
ctx.setAttr(referenceIntWithReferencePrefix, 10);
ctx.setAttr(referenceIntWithoutType, 10);

ArrayList<String> testValue = new ArrayList<>();
testValue.add("A");
ctx.setAttr(stringCollectionWithPrefix, testValue);
ctx.setAttr(stringCollectionWithoutPrefix, testValue);
ctx.setAttr(stringCollectionWithoutType, testValue);

Map<String, AttributeKey<?>> expectedCtx = new HashMap<>();
expectedCtx.put("primitiveIntWithPrimitivePrefix", primitiveIntWithPrimitivePrefix);
expectedCtx.put("primitiveIntWithoutPrefix", primitiveIntWithoutPrefix);
expectedCtx.put("primitiveIntWithReferencePrefix", primitiveIntWithReferencePrefix);
expectedCtx.put("primitiveIntWithoutType", primitiveIntWithoutType);
expectedCtx.put("referenceIntWithPrimitivePrefix", referenceIntWithPrimitivePrefix);
expectedCtx.put("referenceIntWithoutPrefix", referenceIntWithoutPrefix);
expectedCtx.put("referenceIntWithReferencePrefix", referenceIntWithReferencePrefix);
expectedCtx.put("referenceIntWithoutType", referenceIntWithoutType);
expectedCtx.put("stringCollectionWithPrefix", stringCollectionWithPrefix);
expectedCtx.put("stringCollectionWithoutPrefix", stringCollectionWithoutPrefix);
expectedCtx.put("stringCollectionWithoutType", stringCollectionWithoutType);

return expectedCtx;
}

interface Bean {
String var1();

Expand Down

0 comments on commit 3e809c7

Please sign in to comment.