-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathAssertJChain.java
More file actions
123 lines (107 loc) · 4.17 KB
/
Copy pathAssertJChain.java
File metadata and controls
123 lines (107 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright 2016-2026 Qameta Software Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.assertj;
import io.qameta.allure.AllureExternalKey;
import io.qameta.allure.model.Stage;
import io.qameta.allure.model.Status;
import io.qameta.allure.model.StatusDetails;
import io.qameta.allure.model.StepResult;
import org.assertj.core.api.AbstractAssert;
import java.util.Optional;
/**
* Parent Allure step for one AssertJ assertion chain.
*
* <p>A chain is the stable container for all meaningful fluent operations produced by one AssertJ assertion object.
* {@link AssertJRecorder} creates it when user code calls an AssertJ factory such as {@code assertThat(actual)},
* stores it by assertion object identity, and appends one {@link AssertJOperation} child for every reported fluent
* call. Methods such as {@code extracting}, {@code first}, or {@code asInstanceOf} can return another assertion
* object, but they should still read as the same assertion story, so the returned assertion is associated with this
* chain instead of creating an unrelated top-level step.</p>
*
* <p>For a scalar assertion:</p>
* <pre>{@code
* assertThat("Data").hasSize(4)
*
* assert "Data"
* has size 4
* }</pre>
*
* <p>For an assertion with a description, the parent step is renamed while the operation history stays visible:</p>
* <pre>{@code
* assertThat(user).as("user profile").isNotNull()
*
* assert user profile
* described as "user profile"
* is not null
* }</pre>
*
* <p>For navigation or extraction, later checks remain under the same parent:</p>
* <pre>{@code
* assertThat(results).extracting(Result::getName).containsExactly("passed")
*
* assert 1 Result item
* extracts Result::getName -> 1 string
* contains exactly ["passed"]
* }</pre>
*
* <p>This class is intentionally only a small mutable model around the retained {@link StepResult}. It owns the
* parent step name, status, timing, and child operation list. It does not decide which AssertJ methods are meaningful
* or how subjects and arguments are rendered; those decisions belong to {@link AssertJRecorder},
* {@link AssertJMethodSupport}, and {@link AssertJValueRenderer}.</p>
*/
final class AssertJChain {
private static final String ASSERTJ_STEP_PREFIX = "assert ";
private final AllureExternalKey key;
private final AbstractAssert<?, ?> assertion;
private final StepResult step;
AssertJChain(final AbstractAssert<?, ?> assertion, final String subject) {
this.key = AllureExternalKey.random(AllureAspectJ.class);
this.assertion = assertion;
this.step = new StepResult()
.setName(chainName(subject))
.setStatus(Status.PASSED)
.setStage(Stage.FINISHED)
.setStart(System.currentTimeMillis())
.setStop(System.currentTimeMillis());
}
AllureExternalKey getKey() {
return key;
}
AbstractAssert<?, ?> getAssertion() {
return assertion;
}
StepResult getStep() {
return step;
}
void addOperation(final AssertJOperation operation) {
step.getSteps().add(operation.getStep());
}
void rename(final Optional<String> description) {
description.ifPresent(value -> step.setName(chainName(value)));
}
void updateStatus(final Status status, final StatusDetails details) {
step
.setStatus(status)
.setStatusDetails(details);
finish();
}
void finish() {
step.setStop(System.currentTimeMillis());
}
private String chainName(final String subject) {
return AssertJValueRenderer.truncateStepName(ASSERTJ_STEP_PREFIX + subject);
}
}