private void verify(ActiveRuleDoc doc1, QProfileDto profile, ActiveRuleDto activeRule) { assertThat(doc1) .matches(doc -> doc.getId().equals("" + activeRule.getId())) .matches(doc -> doc.getRuleProfileUuid().equals(profile.getRulesProfileUuid())) .matches(doc -> doc.getSeverity().equals(activeRule.getSeverityString())); }
@Test public void test_definition() { WebService.Action definition = ws.getDef(); assertThat(definition.isInternal()).isTrue(); assertThat(definition.key()).isEqualTo("app"); assertThat(definition.params()).hasSize(1); assertThat(definition.param("organization")) .matches(p -> p.isInternal()) .matches(p -> p.since().equals("6.4")) .matches(p -> !p.isRequired()); }
@Test public void conditionalFuseableTryOnNext() { ArrayList<Signal<Boolean>> signals = new ArrayList<>(); ConditionalSubscriber<Boolean> actual = new FluxPeekFuseableTest.ConditionalAssertSubscriber<Boolean>() { @Override public boolean tryOnNext(Boolean v) { super.tryOnNext(v); return v; } }; DoOnEachFuseableConditionalSubscriber<Boolean> test = new DoOnEachFuseableConditionalSubscriber<>(actual, signals::add); AssertQueueSubscription<Boolean> qs = new AssertQueueSubscription<>(); test.onSubscribe(qs); assertThat(test.tryOnNext(true)).isTrue(); assertThat(test.tryOnNext(false)).isFalse(); test.onComplete(); assertThat(signals).hasSize(2); assertThat(signals.get(0)).matches(Signal::isOnNext) .matches(s -> s.get() == Boolean.TRUE); assertThat(signals.get(1)).matches(Signal::isOnComplete); List<Boolean> actualTryNext = ((FluxPeekFuseableTest.ConditionalAssertSubscriber<Boolean>) actual).next; assertThat(actualTryNext).hasSize(2); assertThat(actualTryNext.get(0)).isTrue(); assertThat(actualTryNext.get(1)).isFalse(); }
@Test public void insertParam() { ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(Severity.CRITICAL); underTest.insert(dbSession, activeRule); ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo"); underTest.insertParam(dbSession, activeRule, activeRuleParam); List<ActiveRuleParamDto> reloaded = underTest.selectParamsByActiveRuleId(dbSession, activeRule.getId()); assertThat(reloaded).hasSize(1); assertThat(reloaded.get(0)) .matches(p -> Objects.equals(p.getId(), activeRuleParam.getId())) .matches(p -> p.getKey().equals(activeRuleParam.getKey())) .matches(p -> p.getActiveRuleId().equals(activeRule.getId())) .matches(p -> p.getRulesParameterId().equals(rule1Param1.getId())) .matches(p -> p.getValue().equals("foo")); }
.matches(param -> param.isRequired()) .matches(param -> "foo-company".equals(param.exampleValue())) .matches(param -> "Organization key".equals(param.description())); assertThat(action.param("name")) .matches(param -> !param.isRequired()) .matches(param -> "Foo Company".equals(param.exampleValue())) .matches(param -> param.minimumLength().equals(1)) .matches(param -> param.maximumLength().equals(255)) .matches(param -> param.description() != null); assertThat(action.param("description")) .matches(param -> !param.isRequired()) .matches(param -> "The Foo company produces quality software for Bar.".equals(param.exampleValue())) .matches(param -> param.description() != null); assertThat(action.param("url")) .matches(param -> !param.isRequired()) .matches(param -> "https://www.foo.com".equals(param.exampleValue())) .matches(param -> param.description() != null); assertThat(action.param("avatar")) .matches(param -> !param.isRequired()) .matches(param -> "https://www.foo.com/foo.png".equals(param.exampleValue())) .matches(param -> param.description() != null);
@Test public void definition() { WebService.Action action = wsTester.getDef(); assertThat(action.key()).isEqualTo("delete"); assertThat(action.isPost()).isTrue(); assertThat(action.description()).isEqualTo("Delete an organization.<br/>" + "Require 'Administer System' permission on the specified organization. Organization support must be enabled."); assertThat(action.isInternal()).isTrue(); assertThat(action.since()).isEqualTo("6.2"); assertThat(action.handler()).isNotNull(); assertThat(action.params()).hasSize(1); assertThat(action.responseExample()).isNull(); assertThat(action.param("organization")) .matches(param -> param.isRequired()) .matches(param -> "foo-company".equals(param.exampleValue())) .matches(param -> "Organization key".equals(param.description())); }
@Test public void updateParam() { ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(Severity.CRITICAL); underTest.insert(dbSession, activeRule); ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo"); underTest.insertParam(dbSession, activeRule, activeRuleParam); underTest.updateParam(dbSession, activeRuleParam.setValue("bar")); List<ActiveRuleParamDto> reloaded = underTest.selectParamsByActiveRuleId(dbSession, activeRule.getId()); assertThat(reloaded).hasSize(1); assertThat(reloaded.get(0)) .matches(p -> Objects.equals(p.getId(), activeRuleParam.getId())) .matches(p -> p.getKey().equals(activeRuleParam.getKey())) .matches(p -> p.getActiveRuleId().equals(activeRule.getId())) .matches(p -> p.getRulesParameterId().equals(rule1Param1.getId())) .matches(p -> p.getValue().equals("bar")); }
@Test public void testSimpleManipulationAsync() throws Exception { final HelloService.AsyncIface client = Clients.newClient( server.uri(BINARY, "/hello"), HelloService.AsyncIface.class); final BlockingQueue<Object> result = new ArrayBlockingQueue<>(1); final Callback callback = new Callback(result); try (SafeCloseable ignored = Clients.withHttpHeader(AUTHORIZATION, SECRET)) { client.hello("armeria", callback); } assertThat(result.poll(10, TimeUnit.SECONDS)).isEqualTo("Hello, armeria!"); // Ensure that the header manipulator set in the thread-local variable has been cleared. client.hello("bar", callback); assertThat(result.poll(10, TimeUnit.SECONDS)) .isInstanceOf(TException.class) .matches(o -> ((Throwable) o).getMessage().contains("not authorized"), "must fail with authorization failure"); }
@Test @Parameters(method = "sourcesTransactionError") public void apiRollbackGeneratingNull(Flux<String> transactionWithError) { final AtomicReference<TestResource> ref = new AtomicReference<>(); Flux<String> flux = Flux.usingWhen(Mono.fromCallable(TestResource::new), d -> { ref.set(d); return transactionWithError; }, TestResource::commitError, TestResource::rollbackNull); StepVerifier.create(flux) .expectNext("Transaction started") .expectNext("work in transaction") .verifyErrorSatisfies(e -> assertThat(e) .hasMessage("The asyncError returned a null Publisher") .isInstanceOf(NullPointerException.class) .hasSuppressedException(new IllegalStateException("boom"))); assertThat(ref.get()) .isNotNull() .matches(tr -> !tr.commitProbe.wasSubscribed(), "no commit") .matches(tr -> !tr.rollbackProbe.wasSubscribed(), "rollback method short-circuited"); }
@Test public void test_definition() { WebService.Action def = ws.getDef(); assertThat(def.key()).isEqualTo("search"); assertThat(def.isInternal()).isFalse(); assertThat(def.isPost()).isFalse(); assertThat(def.since()).isEqualTo("3.6"); assertThat(def.responseExampleAsString()).isNotEmpty(); assertThat(def.params()).extracting("key").containsExactlyInAnyOrder( "additionalFields", "asc", "assigned", "assignees", "authors", "componentKeys", "componentRootUuids", "componentRoots", "componentUuids", "components", "branch", "pullRequest", "organization", "createdAfter", "createdAt", "createdBefore", "createdInLast", "directories", "facetMode", "facets", "fileUuids", "issues", "languages", "moduleUuids", "onComponentOnly", "p", "projects", "ps", "resolutions", "resolved", "rules", "s", "severities", "sinceLeakPeriod", "statuses", "tags", "types", "owaspTop10", "sansTop25", "cwe"); assertThat(def.param("organization")) .matches(WebService.Param::isInternal) .matches(p -> p.since().equals("6.4")); WebService.Param branch = def.param(PARAM_BRANCH); assertThat(branch.isInternal()).isTrue(); assertThat(branch.isRequired()).isFalse(); assertThat(branch.since()).isEqualTo("6.6"); WebService.Param projectUuids = def.param("projects"); assertThat(projectUuids.description()).isEqualTo("To retrieve issues associated to a specific list of projects (comma-separated list of project keys). " + "This parameter is mostly used by the Issues page, please prefer usage of the componentKeys parameter. If this parameter is set, projectUuids must not be set."); }
@Test @Parameters(method = "sourcesTransactionError") public void apiRollbackFailure(Flux<String> transactionWithError) { final AtomicReference<TestResource> ref = new AtomicReference<>(); Flux<String> flux = Flux.usingWhen(Mono.fromCallable(TestResource::new), d -> { ref.set(d); return transactionWithError; }, TestResource::commitError, TestResource::rollbackError); StepVerifier.create(flux) .expectNext("Transaction started") .expectNext("work in transaction") .verifyErrorSatisfies(e -> assertThat(e) .hasMessage("Async resource cleanup failed after onError") .hasCauseInstanceOf(ArithmeticException.class) .hasSuppressedException(new IllegalStateException("boom"))); assertThat(ref.get()) .isNotNull() .matches(tr -> !tr.commitProbe.wasSubscribed(), "no commit") .matches(tr -> tr.rollbackProbe.wasSubscribed(), "rollback method used"); }
@Test @Parameters(method = "sourcesFullTransaction") public void commitGeneratingNull(Flux<String> fullTransaction) { final AtomicReference<TestResource> ref = new AtomicReference<>(); Flux<String> flux = Flux.usingWhen(Mono.fromCallable(TestResource::new), d -> { ref.set(d); return fullTransaction; }, TestResource::commitNull, TestResource::rollback); StepVerifier.create(flux) .expectNext("Transaction started") .expectNext("work in transaction") .expectNext("more work in transaction") .verifyErrorSatisfies(e -> assertThat(e) .hasMessage("The asyncComplete returned a null Publisher") .isInstanceOf(NullPointerException.class) .hasNoCause()); assertThat(ref.get()) .isNotNull() .matches(tr -> !tr.commitProbe.wasSubscribed(), "commit method short-circuited") .matches(tr -> !tr.rollbackProbe.wasSubscribed(), "no rollback"); }
@Test public void apiAsyncCleanup() { final AtomicReference<TestResource> ref = new AtomicReference<>(); Flux<String> flux = Flux.usingWhen(Mono.fromCallable(TestResource::new), d -> { ref.set(d); return d.data().concatWithValues("work in transaction"); }, TestResource::commit); StepVerifier.create(flux) .expectNext("Transaction started") .expectNext("work in transaction") .verifyComplete(); assertThat(ref.get()) .isNotNull() .matches(tr -> tr.commitProbe.wasSubscribed(), "commit method used") .matches(tr -> !tr.rollbackProbe.wasSubscribed(), "no rollback"); }
@Test @Parameters(method = "sourcesTransactionError") public void apiRollback(Flux<String> transactionWithError) { final AtomicReference<TestResource> ref = new AtomicReference<>(); Flux<String> flux = Flux.usingWhen(Mono.fromCallable(TestResource::new), d -> { ref.set(d); return transactionWithError; }, TestResource::commitError, TestResource::rollback); StepVerifier.create(flux) .expectNext("Transaction started") .expectNext("work in transaction") .verifyErrorSatisfies(e -> assertThat(e) .hasMessage("boom") .hasNoCause() .hasNoSuppressedExceptions()); assertThat(ref.get()) .isNotNull() .matches(tr -> !tr.commitProbe.wasSubscribed(), "no commit") .matches(tr -> tr.rollbackProbe.wasSubscribed(), "rollback method used"); }
@Test @Parameters(method = "sourcesFullTransaction") public void apiCommitFailure(Flux<String> fullTransaction) { final AtomicReference<TestResource> ref = new AtomicReference<>(); Flux<String> flux = Flux.usingWhen(Mono.fromCallable(TestResource::new), d -> { ref.set(d); return fullTransaction; }, TestResource::commitError, TestResource::rollback); StepVerifier.create(flux) .expectNext("Transaction started") .expectNext("work in transaction") .expectNext("more work in transaction") .verifyErrorSatisfies(e -> assertThat(e) .hasMessage("Async resource cleanup failed after onComplete") .hasCauseInstanceOf(ArithmeticException.class)); assertThat(ref.get()) .isNotNull() .matches(tr -> tr.commitProbe.wasSubscribed(), "commit method used") .matches(tr -> !tr.rollbackProbe.wasSubscribed(), "no rollback"); }
@Test public void scanRunOn() { Scannable publishOnScannable = Scannable.from( Flux.just(1).hide() .publishOn(Schedulers.elastic()) ); Scannable runOnScannable = publishOnScannable.scan(Scannable.Attr.RUN_ON); Assertions.assertThat(runOnScannable).isNotNull() .matches(Scannable::isScanAvailable, "isScanAvailable"); System.out.println(runOnScannable + " isScannable " + runOnScannable.isScanAvailable()); System.out.println(runOnScannable.scan(Scannable.Attr.NAME)); runOnScannable.parents().forEach(System.out::println); System.out.println(runOnScannable.scan(Scannable.Attr.BUFFERED)); } }
@Test public void scanCapacity() { Scheduler scheduler = Schedulers.newSingle(Thread::new); try { assertThat(scheduler) .matches(s -> Scannable.from(s).isScanAvailable(), "isScanAvailable") .satisfies(s -> assertThat(Scannable.from(s).scan(Scannable.Attr.CAPACITY)).isEqualTo(1)); } finally { scheduler.dispose(); } } }
@Test public void scanCapacity() { Scheduler scheduler = Schedulers.newParallel(12, Thread::new); try { assertThat(scheduler) .matches(s -> Scannable.from(s).isScanAvailable(), "isScanAvailable") .satisfies(s -> assertThat(Scannable.from(s).scan(Scannable.Attr.CAPACITY)).isEqualTo(12)); } finally { scheduler.dispose(); } } }
@Test @Parameters(method = "sourcesFullTransaction") public void apiCommit(Flux<String> fullTransaction) { final AtomicReference<TestResource> ref = new AtomicReference<>(); Flux<String> flux = Flux.usingWhen(Mono.fromCallable(TestResource::new), d -> { ref.set(d); return fullTransaction; }, TestResource::commit, TestResource::rollback); StepVerifier.create(flux) .expectNext("Transaction started") .expectNext("work in transaction") .expectNext("more work in transaction") .expectComplete() .verify(); assertThat(ref.get()) .isNotNull() .matches(tr -> tr.commitProbe.wasSubscribed(), "commit method used") .matches(tr -> !tr.rollbackProbe.wasSubscribed(), "no rollback"); }
@Test public void apiAsyncCleanupFailure() { final RuntimeException rollbackCause = new IllegalStateException("boom"); final AtomicReference<TestResource> ref = new AtomicReference<>(); Flux<String> flux = Flux.usingWhen(Mono.fromCallable(TestResource::new), d -> { ref.set(d); return d.data().concatWithValues("work in transaction") .concatWith(Mono.error(rollbackCause)); }, TestResource::commitError); StepVerifier.create(flux) .expectNext("Transaction started") .expectNext("work in transaction") .verifyErrorSatisfies(e -> assertThat(e) .hasMessage("Async resource cleanup failed after onError") .hasCauseInstanceOf(ArithmeticException.class) .hasSuppressedException(rollbackCause)); assertThat(ref.get()) .isNotNull() .matches(tr -> tr.commitProbe.wasSubscribed(), "commit method used despite error") .matches(tr -> !tr.rollbackProbe.wasSubscribed(), "no rollback"); }