• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JaidenAshmore / java-dynamic-sqs-listener / #2000

pending completion
#2000

push

github-actions

web-flow
#refs 395: upgrade to Java 17, Spring Boot 3 and other dependencies (#397)

236 of 236 new or added lines in 21 files covered. (100.0%)

2180 of 2263 relevant lines covered (96.33%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.97
/core/src/main/java/com/jashmore/sqs/argument/attribute/MessageSystemAttributeArgumentResolver.java
1
package com.jashmore.sqs.argument.attribute;
2

3
import static java.time.ZoneOffset.UTC;
4
import static software.amazon.awssdk.services.sqs.model.MessageSystemAttributeName.APPROXIMATE_FIRST_RECEIVE_TIMESTAMP;
5
import static software.amazon.awssdk.services.sqs.model.MessageSystemAttributeName.SENT_TIMESTAMP;
6

7
import com.jashmore.sqs.QueueProperties;
8
import com.jashmore.sqs.argument.ArgumentResolutionException;
9
import com.jashmore.sqs.argument.ArgumentResolver;
10
import com.jashmore.sqs.argument.MethodParameter;
11
import com.jashmore.sqs.util.annotation.AnnotationUtils;
12
import java.time.Instant;
13
import java.time.OffsetDateTime;
14
import java.util.Optional;
15
import software.amazon.awssdk.services.sqs.model.Message;
16
import software.amazon.awssdk.services.sqs.model.MessageSystemAttributeName;
17

18
public class MessageSystemAttributeArgumentResolver implements ArgumentResolver<Object> {
1✔
19

20
    @Override
21
    public boolean canResolveParameter(final MethodParameter methodParameter) {
22
        return AnnotationUtils.findParameterAnnotation(methodParameter, MessageSystemAttribute.class).isPresent();
1✔
23
    }
24

25
    @Override
26
    public Object resolveArgumentForParameter(
27
        final QueueProperties queueProperties,
28
        final MethodParameter methodParameter,
29
        final Message message
30
    ) throws ArgumentResolutionException {
31
        final MessageSystemAttribute annotation = AnnotationUtils
1✔
32
            .findParameterAnnotation(methodParameter, MessageSystemAttribute.class)
1✔
33
            .orElseThrow(() ->
1✔
34
                new ArgumentResolutionException("Parameter passed in does not contain the MessageSystemAttribute annotation when it should")
×
35
            );
36

37
        final MessageSystemAttributeName messageSystemAttributeName = annotation.value();
1✔
38
        final Optional<String> optionalAttributeValue = Optional.ofNullable(message.attributes().get(messageSystemAttributeName));
1✔
39

40
        if (!optionalAttributeValue.isPresent()) {
1✔
41
            if (annotation.required()) {
1✔
42
                throw new ArgumentResolutionException("Missing system attribute with name: " + messageSystemAttributeName.toString());
1✔
43
            }
44

45
            return null;
1✔
46
        }
47
        final String attributeValue = optionalAttributeValue.get();
1✔
48

49
        final Class<?> parameterType = methodParameter.getParameter().getType();
1✔
50
        try {
51
            if (parameterType == String.class) {
1✔
52
                return attributeValue;
1✔
53
            }
54

55
            if (parameterType == Integer.class || parameterType == int.class) {
1✔
56
                return Integer.parseInt(attributeValue);
1✔
57
            }
58

59
            if (parameterType == Long.class || parameterType == long.class) {
1✔
60
                return Long.parseLong(attributeValue);
1✔
61
            }
62
        } catch (final RuntimeException exception) {
1✔
63
            throw new ArgumentResolutionException("Error parsing message attribute: " + messageSystemAttributeName.toString(), exception);
1✔
64
        }
1✔
65

66
        if (messageSystemAttributeName == SENT_TIMESTAMP || messageSystemAttributeName == APPROXIMATE_FIRST_RECEIVE_TIMESTAMP) {
1✔
67
            return handleTimeStampAttributes(methodParameter.getParameter().getType(), messageSystemAttributeName, attributeValue);
1✔
68
        }
69

70
        throw new ArgumentResolutionException(
1✔
71
            "Unsupported parameter type " + parameterType.getName() + " for system attribute " + messageSystemAttributeName.toString()
1✔
72
        );
73
    }
74

75
    private Object handleTimeStampAttributes(
76
        final Class<?> parameterType,
77
        final MessageSystemAttributeName messageSystemAttributeName,
78
        final String attributeValue
79
    ) {
80
        if (parameterType == Instant.class) {
1✔
81
            return Instant.ofEpochMilli(Long.parseLong(attributeValue));
1✔
82
        }
83

84
        if (parameterType == OffsetDateTime.class) {
1✔
85
            return OffsetDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(attributeValue)), UTC);
1✔
86
        }
87

88
        throw new ArgumentResolutionException(
1✔
89
            "Unsupported parameter type " + parameterType.getName() + " for system attribute " + messageSystemAttributeName.toString()
1✔
90
        );
91
    }
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc