|
| 1 | +/* |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package neo4j.org.testkit.backend.messages.requests; |
| 20 | + |
| 21 | +import java.time.DateTimeException; |
| 22 | +import java.time.ZoneId; |
| 23 | +import java.time.ZonedDateTime; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import java.util.concurrent.CompletionStage; |
| 28 | +import lombok.Getter; |
| 29 | +import lombok.Setter; |
| 30 | +import neo4j.org.testkit.backend.TestkitState; |
| 31 | +import neo4j.org.testkit.backend.messages.responses.RunTest; |
| 32 | +import neo4j.org.testkit.backend.messages.responses.SkipTest; |
| 33 | +import neo4j.org.testkit.backend.messages.responses.TestkitResponse; |
| 34 | +import reactor.core.publisher.Mono; |
| 35 | + |
| 36 | +@Setter |
| 37 | +@Getter |
| 38 | +public class StartSubTest implements TestkitRequest { |
| 39 | + interface SkipDeciderInterface { |
| 40 | + SkipDecision check(Map<String, Object> params); |
| 41 | + } |
| 42 | + |
| 43 | + public static class SkipDecision { |
| 44 | + private final boolean skipped; |
| 45 | + private final String reason; |
| 46 | + |
| 47 | + public SkipDecision(boolean skipped, String reason) { |
| 48 | + this.skipped = skipped; |
| 49 | + this.reason = reason; |
| 50 | + } |
| 51 | + |
| 52 | + public boolean isSkipped() { |
| 53 | + return skipped; |
| 54 | + } |
| 55 | + |
| 56 | + public String getReason() { |
| 57 | + return reason; |
| 58 | + } |
| 59 | + |
| 60 | + static SkipDecision ofNonSkipped() { |
| 61 | + return new SkipDecision(false, null); |
| 62 | + } |
| 63 | + |
| 64 | + static SkipDecision ofSkipped(String reason) { |
| 65 | + return new SkipDecision(true, reason); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static final Map<String, SkipDeciderInterface> COMMON_SKIP_PATTERN_TO_CHECK = new HashMap<>(); |
| 70 | + private static final Map<String, SkipDeciderInterface> ASYNC_SKIP_PATTERN_TO_CHECK = new HashMap<>(); |
| 71 | + private static final Map<String, SkipDeciderInterface> REACTIVE_LEGACY_SKIP_PATTERN_TO_CHECK = new HashMap<>(); |
| 72 | + private static final Map<String, SkipDeciderInterface> REACTIVE_SKIP_PATTERN_TO_CHECK = new HashMap<>(); |
| 73 | + |
| 74 | + private static SkipDecision checkTzIdSupported(Map<String, Object> params) { |
| 75 | + String tzId = (String) params.get("tz_id"); |
| 76 | + try { |
| 77 | + ZoneId.of(tzId); |
| 78 | + return SkipDecision.ofNonSkipped(); |
| 79 | + } catch (DateTimeException e) { |
| 80 | + return SkipDecision.ofSkipped("Timezone not supported: " + tzId); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static SkipDecision checkDateTimeSupported(Map<String, Object> params) { |
| 85 | + @SuppressWarnings("unchecked") |
| 86 | + HashMap<String, Object> dt_param = (HashMap<String, Object>) params.get("dt"); |
| 87 | + if (dt_param == null) { |
| 88 | + throw new RuntimeException("params expected to contain 'dt'"); |
| 89 | + } |
| 90 | + @SuppressWarnings("unchecked") |
| 91 | + HashMap<String, Object> data = (HashMap<String, Object>) dt_param.get("data"); |
| 92 | + if (data == null) { |
| 93 | + throw new RuntimeException("param 'dt' expected to contain 'data'"); |
| 94 | + } |
| 95 | + Integer year = (Integer) data.get("year"); |
| 96 | + Integer month = (Integer) data.get("month"); |
| 97 | + Integer day = (Integer) data.get("day"); |
| 98 | + Integer hour = (Integer) data.get("hour"); |
| 99 | + Integer minute = (Integer) data.get("minute"); |
| 100 | + Integer second = (Integer) data.get("second"); |
| 101 | + Integer nano = (Integer) data.get("nanosecond"); |
| 102 | + Integer utcOffset = (Integer) data.get("utc_offset_s"); |
| 103 | + String tzId = (String) data.get("timezone_id"); |
| 104 | + try { |
| 105 | + ZonedDateTime dt = ZonedDateTime.of(year, month, day, hour, minute, second, nano, ZoneId.of(tzId)); |
| 106 | + if (dt.getOffset().getTotalSeconds() != utcOffset) { |
| 107 | + throw new DateTimeException(String.format( |
| 108 | + "Unmatched UTC offset. TestKit expected %d, local zone db yielded %d", |
| 109 | + utcOffset, dt.getOffset().getTotalSeconds())); |
| 110 | + } |
| 111 | + return SkipDecision.ofNonSkipped(); |
| 112 | + } catch (DateTimeException e) { |
| 113 | + return SkipDecision.ofSkipped("DateTime not supported: " + e.getMessage()); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + static { |
| 118 | + COMMON_SKIP_PATTERN_TO_CHECK.put( |
| 119 | + "neo4j\\.datatypes\\.test_temporal_types\\.TestDataTypes\\.test_should_echo_all_timezone_ids", |
| 120 | + StartSubTest::checkDateTimeSupported); |
| 121 | + COMMON_SKIP_PATTERN_TO_CHECK.put( |
| 122 | + "neo4j\\.datatypes\\.test_temporal_types\\.TestDataTypes\\.test_date_time_cypher_created_tz_id", |
| 123 | + StartSubTest::checkTzIdSupported); |
| 124 | + |
| 125 | + ASYNC_SKIP_PATTERN_TO_CHECK.putAll(COMMON_SKIP_PATTERN_TO_CHECK); |
| 126 | + |
| 127 | + REACTIVE_LEGACY_SKIP_PATTERN_TO_CHECK.putAll(COMMON_SKIP_PATTERN_TO_CHECK); |
| 128 | + |
| 129 | + REACTIVE_SKIP_PATTERN_TO_CHECK.putAll(COMMON_SKIP_PATTERN_TO_CHECK); |
| 130 | + } |
| 131 | + |
| 132 | + private StartSubTestBody data; |
| 133 | + |
| 134 | + public static boolean decidePerSubTest(String testName) { |
| 135 | + return skipPatternMatches(testName, COMMON_SKIP_PATTERN_TO_CHECK); |
| 136 | + } |
| 137 | + |
| 138 | + public static boolean decidePerSubTestAsync(String testName) { |
| 139 | + return skipPatternMatches(testName, ASYNC_SKIP_PATTERN_TO_CHECK); |
| 140 | + } |
| 141 | + |
| 142 | + public static boolean decidePerSubTestReactiveLegacy(String testName) { |
| 143 | + return skipPatternMatches(testName, REACTIVE_LEGACY_SKIP_PATTERN_TO_CHECK); |
| 144 | + } |
| 145 | + |
| 146 | + public static boolean decidePerSubTestReactive(String testName) { |
| 147 | + return skipPatternMatches(testName, REACTIVE_SKIP_PATTERN_TO_CHECK); |
| 148 | + } |
| 149 | + |
| 150 | + private static boolean skipPatternMatches( |
| 151 | + String testName, Map<String, SkipDeciderInterface> skipPatternToFunction) { |
| 152 | + return skipPatternToFunction.entrySet().stream().anyMatch(entry -> testName.matches(entry.getKey())); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public TestkitResponse process(TestkitState testkitState) { |
| 157 | + return createResponse(COMMON_SKIP_PATTERN_TO_CHECK); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public CompletionStage<TestkitResponse> processAsync(TestkitState testkitState) { |
| 162 | + TestkitResponse testkitResponse = createResponse(ASYNC_SKIP_PATTERN_TO_CHECK); |
| 163 | + return CompletableFuture.completedFuture(testkitResponse); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public Mono<TestkitResponse> processRx(TestkitState testkitState) { |
| 168 | + TestkitResponse testkitResponse = createResponse(REACTIVE_LEGACY_SKIP_PATTERN_TO_CHECK); |
| 169 | + return Mono.fromCompletionStage(CompletableFuture.completedFuture(testkitResponse)); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public Mono<TestkitResponse> processReactive(TestkitState testkitState) { |
| 174 | + TestkitResponse testkitResponse = createResponse(REACTIVE_SKIP_PATTERN_TO_CHECK); |
| 175 | + return Mono.fromCompletionStage(CompletableFuture.completedFuture(testkitResponse)); |
| 176 | + } |
| 177 | + |
| 178 | + private TestkitResponse createResponse(Map<String, SkipDeciderInterface> skipPatternToCheck) { |
| 179 | + System.out.println(data.getTestName()); |
| 180 | + return (TestkitResponse) skipPatternToCheck.entrySet().stream() |
| 181 | + .filter(entry -> data.getTestName().matches(entry.getKey())) |
| 182 | + .findFirst() |
| 183 | + .map(entry -> { |
| 184 | + SkipDecision decision = entry.getValue().check(data.getSubtestArguments()); |
| 185 | + if (decision.isSkipped()) { |
| 186 | + return SkipTest.builder() |
| 187 | + .data(SkipTest.SkipTestBody.builder() |
| 188 | + .reason(decision.getReason()) |
| 189 | + .build()) |
| 190 | + .build(); |
| 191 | + } |
| 192 | + return RunTest.builder().build(); |
| 193 | + }) |
| 194 | + .orElse(RunTest.builder().build()); |
| 195 | + } |
| 196 | + |
| 197 | + @Setter |
| 198 | + @Getter |
| 199 | + public static class StartSubTestBody { |
| 200 | + private String testName; |
| 201 | + private Map<String, Object> subtestArguments; |
| 202 | + } |
| 203 | +} |
0 commit comments