|
| 1 | +/* |
| 2 | + * Copyright 2025 Flamingock (https://www.flamingock.io) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.flamingock.support.stages; |
| 17 | + |
| 18 | +import io.flamingock.internal.core.builder.BuilderAccessor; |
| 19 | +import io.flamingock.support.domain.AuditEntryExpectation; |
| 20 | +import io.flamingock.support.validation.ValidationHandler; |
| 21 | +import io.flamingock.support.validation.Validator; |
| 22 | +import io.flamingock.support.validation.ValidatorFactory; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.function.Consumer; |
| 27 | + |
| 28 | +final class ThenStageImpl implements ThenStage { |
| 29 | + |
| 30 | + private final List<Validator> validators = new ArrayList<>(); |
| 31 | + private final ValidatorFactory validatorFactory; |
| 32 | + private final BuilderAccessor builderAccessor; |
| 33 | + |
| 34 | + ThenStageImpl(BuilderAccessor builderAccessor) { |
| 35 | + this.builderAccessor = builderAccessor; |
| 36 | + validatorFactory = new ValidatorFactory(builderAccessor); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public ThenStage andExpectAuditSequenceStrict(AuditEntryExpectation... expectations) { |
| 41 | + validators.add(validatorFactory.getAuditSeqStrictValidator(expectations)); |
| 42 | + return this; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public ThenStage andExpectException(Class<? extends Throwable> exceptionClass, Consumer<Throwable> exceptionConsumer) { |
| 47 | + validators.add(validatorFactory.getExceptionValidator(exceptionClass, exceptionConsumer)); |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void verify() throws AssertionError { |
| 53 | + |
| 54 | + ValidationHandler validationHandler; |
| 55 | + try { |
| 56 | + builderAccessor.run(); |
| 57 | + validationHandler = new ValidationHandler(validators); |
| 58 | + |
| 59 | + } catch (Throwable actualException) { |
| 60 | + validationHandler = new ValidationHandler(validators, actualException); |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + validationHandler.validate(); |
| 65 | + } |
| 66 | +} |
0 commit comments