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

wistefan / tmforum-api / #49

29 Sep 2023 06:20AM UTC coverage: 67.488% (-4.3%) from 71.815%
#49

push

web-flow
Notifications (#23)

* Squashed commits

* Added cache invalidation for entity deletion

* Updated error message

618 of 618 new or added lines in 86 files covered. (100.0%)

2794 of 4140 relevant lines covered (67.49%)

0.67 hits per line

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

83.33
/common/src/main/java/org/fiware/tmforum/common/querying/SubscriptionQueryParser.java
1
package org.fiware.tmforum.common.querying;
2

3
import org.fiware.tmforum.common.exception.QueryException;
4
import org.fiware.tmforum.common.notification.EventConstants;
5
import org.fiware.tmforum.common.util.StringUtils;
6

7
import java.util.ArrayList;
8
import java.util.Arrays;
9
import java.util.List;
10
import java.util.stream.Collectors;
11

12
import static org.fiware.tmforum.common.querying.Operator.EQUALS;
13

14
public class SubscriptionQueryParser {
×
15

16
        // Keys for the "well-known" fields
17
        public static final String FIELDS_KEY = "fields";
18
        public static final String FIELDS_SEPARATOR = ",";
19
        public static final String EVENT_TYPE_KEY = "eventType";
20
        public static final String EVENT_PREFIX = "event.";
21

22
        // the ";" in tm-forum parameters is an or
23
        public static final String TMFORUM_OR_KEY = ";";
24
        public static final String TMFORUM_AND = "&";
25

26

27
        public static SubscriptionQuery parse(String queryString, List<String> defaultEventGroups) {
28
                SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
1✔
29

30
                if (queryString != null && !queryString.isEmpty()) {
1✔
31
                        List<String> parameters;
32
                        LogicalOperator logicalOperator = LogicalOperator.AND;
1✔
33
                        // tmforum does not define queries combining AND and OR
34
                        if (queryString.contains(TMFORUM_AND) && queryString.contains(TMFORUM_OR_KEY)) {
1✔
35
                                throw new QueryException("Combining AND(&) and OR(;) on query level is not supported by the TMForum API.");
×
36
                        }
37
                        if (queryString.contains(TMFORUM_AND)) {
1✔
38
                                parameters = Arrays.asList(queryString.split(TMFORUM_AND));
1✔
39
            } else if (queryString.contains(TMFORUM_OR_KEY)) {
1✔
40
                                parameters = Arrays.asList(queryString.split(TMFORUM_OR_KEY));
1✔
41
                                logicalOperator = LogicalOperator.OR;
1✔
42
                        } else {
43
                                //query is just a single parameter query
44
                                parameters = List.of(queryString);
1✔
45
                        }
46

47
                        List<String> queryParams = new ArrayList<>();
1✔
48
                        parameters.forEach(parameter -> {
1✔
49
                                if (parameter.startsWith(EVENT_TYPE_KEY)) {
1✔
50
                                        subscriptionQuery.addEventType(getParamValue(parameter));
1✔
51
                                } else if (parameter.startsWith(FIELDS_KEY)) {
1✔
52
                                        subscriptionQuery.setFields(parseFields(getParamValue(parameter)));
1✔
53
                                } else {
54
                                        queryParams.add(removeEventPrefixFromAttributePath(parameter));
1✔
55
                                }
56
                        });
1✔
57

58
                        subscriptionQuery.setQuery(String.join(logicalOperator == LogicalOperator.AND ? TMFORUM_AND : TMFORUM_OR_KEY,
1✔
59
                                        queryParams));
60

61
                        checkLogicalOperator(logicalOperator, subscriptionQuery);
1✔
62
                }
63

64
                if (subscriptionQuery.getEventTypes().isEmpty()) {
1✔
65
                        subscriptionQuery.setEventTypes(
1✔
66
                                        defaultEventGroups.stream().flatMap(eventGroup ->
1✔
67
                                                        EventConstants.ALLOWED_EVENT_TYPES.get(eventGroup).stream().map(
1✔
68
                                                                        eventType -> eventGroup + eventType)).toList());
1✔
69
                }
70

71
                subscriptionQuery.setEventGroups(subscriptionQuery.getEventTypes().stream()
1✔
72
                                .map(StringUtils::getEventGroupName).collect(Collectors.toSet()));
1✔
73

74
                return subscriptionQuery;
1✔
75
        }
76

77
        private static void checkLogicalOperator(LogicalOperator logicalOperator, SubscriptionQuery subscriptionQuery) {
78
                if (logicalOperator == LogicalOperator.OR) {
1✔
79
                        if (!subscriptionQuery.getFields().isEmpty()) {
1✔
80
                                throw new QueryException("Logical operator OR(;) cannot be used with 'fields' selector");
×
81
                        }
82
                        if (!subscriptionQuery.getEventTypes().isEmpty() && !subscriptionQuery.getQuery().isEmpty()) {
1✔
83
                                throw new QueryException("Logical operator OR(;) cannot be used when both 'eventType' and 'query' are defined");
×
84
                        }
85
                } else {
86
                        if (subscriptionQuery.getEventTypes().size() > 1) {
1✔
87
                                throw new QueryException("Logical operator AND(&) cannot be used when several 'eventType' are defined");
×
88
                        }
89
                }
90

91
        }
1✔
92

93
        private static List<String> parseFields(String fields) {
94
                return Arrays.stream(fields.split(FIELDS_SEPARATOR))
1✔
95
                                .map(SubscriptionQueryParser::removeEventPrefixFromAttributePath).toList();
1✔
96
        }
97

98
        private static String removeEventPrefixFromAttributePath(String attributePath) {
99
                if (attributePath.startsWith(EVENT_PREFIX)) {
1✔
100
                        return attributePath.substring(attributePath.indexOf(".") + 1);
1✔
101
                } else {
102
                        return attributePath;
×
103
                }
104
        }
105

106
        private static String getParamValue(String parameter) {
107
                String[] parameterParts = parameter.split(EQUALS.getTmForumOperator().operator());
1✔
108
                if (parameterParts.length != 2) {
1✔
109
                        throw new QueryException(String.format("%s is not a valid %s parameter.",
×
110
                                        parameter,
111
                                        EQUALS.getTmForumOperator().operator()));
×
112
                }
113

114
                return parameterParts[1];
1✔
115
        }
116
}
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