2626
2727import static io .qameta .allure .AllureConstants .ATTACHMENT_FILE_SUFFIX ;
2828
29-
3029/**
31- * @author charlie (Dmitry Baev) .
30+ * The class contains Allure context and methods to change it .
3231 */
32+ @ SuppressWarnings ("PMD.TooManyMethods" )
3333public class AllureLifecycle {
3434
3535 private static final Logger LOGGER = LoggerFactory .getLogger (AllureLifecycle .class );
@@ -39,9 +39,9 @@ public class AllureLifecycle {
3939 private final ThreadLocal <LinkedList <String >> currentStepContext =
4040 InheritableThreadLocal .withInitial (LinkedList ::new );
4141
42- private AllureResultsWriter writer ;
42+ private final AllureResultsWriter writer ;
4343
44- public AllureLifecycle (AllureResultsWriter writer ) {
44+ public AllureLifecycle (final AllureResultsWriter writer ) {
4545 this .writer = writer ;
4646 }
4747
@@ -54,82 +54,82 @@ private static FileSystemResultsWriter getDefaultWriter() {
5454 return new FileSystemResultsWriter (Paths .get (path ));
5555 }
5656
57- public void startTestContainer (String parentUuid , TestResultContainer container ) {
57+ public void startTestContainer (final String parentUuid , final TestResultContainer container ) {
5858 get (parentUuid , TestResultContainer .class )
5959 .getChildren ().add (container .getUuid ());
6060 startTestContainer (container );
6161 }
6262
63- public void startTestContainer (TestResultContainer container ) {
63+ public void startTestContainer (final TestResultContainer container ) {
6464 LOGGER .debug ("Start test result container {}" , container .getUuid ());
6565 put (container .getUuid (), container )
6666 .withStart (System .currentTimeMillis ());
6767 }
6868
69- public void updateTestContainer (String uuid , Consumer <TestResultContainer > update ) {
69+ public void updateTestContainer (final String uuid , final Consumer <TestResultContainer > update ) {
7070 LOGGER .debug ("Update test result container {}" , uuid );
7171 update .accept (get (uuid , TestResultContainer .class ));
7272 }
7373
74- public void stopTestContainer (String uuid ) {
75- LOGGER .debug ("Update test result container {}" , uuid );
74+ public void stopTestContainer (final String uuid ) {
75+ LOGGER .debug ("Stop test result container {}" , uuid );
7676 get (uuid , TestResultContainer .class )
7777 .withStop (System .currentTimeMillis ());
7878 }
7979
80- public void writeTestContainer (String uuid ) {
80+ public void writeTestContainer (final String uuid ) {
8181 LOGGER .debug ("Stop test group {}" , uuid );
8282 writer .write (remove (uuid , TestResultContainer .class ));
8383 }
8484
85- public void startBeforeFixture (String parentUuid , String uuid , FixtureResult result ) {
85+ public void startBeforeFixture (final String parentUuid , final String uuid , final FixtureResult result ) {
8686 LOGGER .debug ("Start test before {} with parent {}" , uuid , parentUuid );
8787 startFixture (parentUuid , uuid , result , TestResultContainer ::getBefores );
8888 }
8989
90- public void startAfterFixture (String parentUuid , String uuid , FixtureResult result ) {
90+ public void startAfterFixture (final String parentUuid , final String uuid , final FixtureResult result ) {
9191 LOGGER .debug ("Start test after {} with parent {}" , uuid , parentUuid );
9292 startFixture (parentUuid , uuid , result , TestResultContainer ::getAfters );
9393 }
9494
95- private void startFixture (String parentUuid , String uuid , FixtureResult result ,
96- Function <TestResultContainer , List <FixtureResult >> fixturesGetter ) {
95+ private void startFixture (final String parentUuid , final String uuid , final FixtureResult result ,
96+ final Function <TestResultContainer , List <FixtureResult >> fixturesGetter ) {
9797 put (uuid , result )
9898 .withStage (Stage .RUNNING )
9999 .withStart (System .currentTimeMillis ());
100- TestResultContainer container = get (parentUuid , TestResultContainer .class );
100+ final TestResultContainer container = get (parentUuid , TestResultContainer .class );
101101 fixturesGetter .apply (container ).add (result );
102102 currentStepContext .remove ();
103103 currentStepContext .get ().push (uuid );
104104 }
105105
106- public void updateFixture (String uuid , Consumer <FixtureResult > update ) {
106+ public void updateFixture (final String uuid , final Consumer <FixtureResult > update ) {
107107 LOGGER .debug ("Update test group {}" , uuid );
108108 update .accept (get (uuid , FixtureResult .class ));
109109 }
110110
111- public void stopFixture (String uuid ) {
111+ public void stopFixture (final String uuid ) {
112112 LOGGER .debug ("Stop test before {}" , uuid );
113113 currentStepContext .remove ();
114114 remove (uuid , FixtureResult .class )
115115 .withStage (Stage .FINISHED )
116116 .withStop (System .currentTimeMillis ());
117117 }
118118
119- public void scheduleTestCase (String parentUuid , TestResult result ) {
119+ public void scheduleTestCase (final String parentUuid , final TestResult result ) {
120120 LOGGER .debug ("Add test case {} to {}" , result .getUuid (), parentUuid );
121121 get (parentUuid , TestResultContainer .class )
122122 .getChildren ().add (result .getUuid ());
123123 scheduleTestCase (result );
124124 }
125125
126- public void scheduleTestCase (TestResult result ) {
126+ public void scheduleTestCase (final TestResult result ) {
127127 LOGGER .debug ("Schedule test case {}" , result .getUuid ());
128128 put (result .getUuid (), result )
129129 .withStage (Stage .SCHEDULED );
130130 }
131131
132- public void startTestCase (String uuid ) {
132+ public void startTestCase (final String uuid ) {
133133 LOGGER .debug ("Start test case {}" , uuid );
134134 get (uuid , TestResult .class )
135135 .withStage (Stage .RUNNING )
@@ -138,37 +138,40 @@ public void startTestCase(String uuid) {
138138 currentStepContext .get ().push (uuid );
139139 }
140140
141- public void updateTestCase (String uuid , Consumer <TestResult > update ) {
141+ public void updateTestCase (final String uuid , final Consumer <TestResult > update ) {
142142 LOGGER .debug ("Update test case {}" , uuid );
143143 update .accept (get (uuid , TestResult .class ));
144144 }
145145
146- public void stopTestCase (String uuid ) {
146+ public void stopTestCase (final String uuid ) {
147147 LOGGER .debug ("Stop test case {}" , uuid );
148148 currentStepContext .remove ();
149149 get (uuid , TestResult .class )
150150 .withStage (Stage .FINISHED )
151151 .withStop (System .currentTimeMillis ());
152152 }
153153
154- public void writeTestCase (String uuid ) {
154+ public void writeTestCase (final String uuid ) {
155155 LOGGER .debug ("Close test case {}" , uuid );
156156 writer .write (remove (uuid , TestResult .class ));
157157 }
158158
159- public void addAttachment (String name , String type , String fileExtension , byte [] body ) {
159+ public void addAttachment (final String name , final String type ,
160+ final String fileExtension , final byte [] body ) {
160161 addAttachment (name , type , fileExtension , new ByteArrayInputStream (body ));
161162 }
162163
163- public void addAttachment (String name , String type , String fileExtension , InputStream stream ) {
164- String uuid = currentStepContext .get ().getFirst ();
164+ @ SuppressWarnings ({"PMD.NullAssignment" , "PMD.UseObjectForClearerAPI" })
165+ public void addAttachment (final String name , final String type ,
166+ final String fileExtension , final InputStream stream ) {
167+ final String uuid = currentStepContext .get ().getFirst ();
165168 LOGGER .debug ("Adding attachment to item with uuid {}" , uuid );
166- String extension = Optional .ofNullable (fileExtension )
169+ final String extension = Optional .ofNullable (fileExtension )
167170 .filter (ext -> !ext .isEmpty ())
168- .map (ext -> ext .startsWith ( "." ) ? ext : "." + ext )
171+ .map (ext -> ext .charAt ( 0 ) == '.' ? ext : "." + ext )
169172 .orElse ("" );
170- String source = UUID .randomUUID ().toString () + ATTACHMENT_FILE_SUFFIX + extension ;
171- Attachment attachment = new Attachment ()
173+ final String source = UUID .randomUUID ().toString () + ATTACHMENT_FILE_SUFFIX + extension ;
174+ final Attachment attachment = new Attachment ()
172175 .withName (isEmpty (name ) ? null : name )
173176 .withType (isEmpty (type ) ? null : type )
174177 .withSource (source );
@@ -177,16 +180,17 @@ public void addAttachment(String name, String type, String fileExtension, InputS
177180 get (uuid , WithAttachments .class ).getAttachments ().add (attachment );
178181 }
179182
180- public void addStep (StepResult result ) {
183+ public void addStep (final StepResult result ) {
181184 get (currentStepContext .get ().getFirst (), WithSteps .class ).getSteps ().add (result );
182185 }
183186
184- public void startStep (String uuid , StepResult result ) {
185- LinkedList <String > uuids = currentStepContext .get ();
187+ @ SuppressWarnings ("PMD.NullAssignment" )
188+ public void startStep (final String uuid , final StepResult result ) {
189+ final LinkedList <String > uuids = currentStepContext .get ();
186190 startStep (uuids .isEmpty () ? null : uuids .getFirst (), uuid , result );
187191 }
188192
189- public void startStep (String parentUuid , String uuid , StepResult result ) {
193+ public void startStep (final String parentUuid , final String uuid , final StepResult result ) {
190194 LOGGER .debug ("Start step {} with parent {}" , uuid , parentUuid );
191195 put (uuid , result )
192196 .withStage (Stage .RUNNING )
@@ -198,11 +202,11 @@ public void startStep(String parentUuid, String uuid, StepResult result) {
198202 }
199203 }
200204
201- public void updateStep (Consumer <StepResult > update ) {
205+ public void updateStep (final Consumer <StepResult > update ) {
202206 updateStep (currentStepContext .get ().getFirst (), update );
203207 }
204208
205- public void updateStep (String uuid , Consumer <StepResult > update ) {
209+ public void updateStep (final String uuid , final Consumer <StepResult > update ) {
206210 LOGGER .debug ("Update step {}" , uuid );
207211 update .accept (get (uuid , StepResult .class ));
208212 }
@@ -211,40 +215,46 @@ public void stopStep() {
211215 stopStep (currentStepContext .get ().getFirst ());
212216 }
213217
214- public void stopStep (String uuid ) {
218+ public void stopStep (final String uuid ) {
215219 LOGGER .debug ("Stop step {}" , uuid );
216220 remove (uuid , StepResult .class )
217221 .withStage (Stage .FINISHED )
218222 .withStop (System .currentTimeMillis ());
219223 currentStepContext .get ().pop ();
220224 }
221225
222- private <T > T put (String uuid , T item ) {
223- Objects .requireNonNull (uuid , "Uuid can't be null" );
226+ private <T > T put (final String uuid , final T item ) {
227+ Objects .requireNonNull (uuid , "Can't put item to storage: uuid can't be null" );
224228 storage .put (uuid , item );
225229 return item ;
226230 }
227231
228- private <T > T get (String uuid , Class <T > clazz ) {
229- Objects .requireNonNull (uuid , "Uuid can't be null" );
230- Object obj = Objects .requireNonNull (storage .get (uuid ), "Could not find " + clazz + " by uuid " + uuid );
232+ private <T > T get (final String uuid , final Class <T > clazz ) {
233+ Objects .requireNonNull (uuid , "Can't get item from storage: uuid can't be null" );
234+ final Object obj = Objects .requireNonNull (
235+ storage .get (uuid ),
236+ String .format ("Could not get %s by uuid %s" , clazz , uuid )
237+ );
231238 return cast (obj , clazz );
232239 }
233240
234- private <T > T remove (String uuid , Class <T > clazz ) {
235- Objects .requireNonNull (uuid , "Uuid can't be null" );
236- Object obj = Objects .requireNonNull (storage .remove (uuid ), "Could not find " + clazz + " by uuid " + uuid );
241+ private <T > T remove (final String uuid , final Class <T > clazz ) {
242+ Objects .requireNonNull (uuid , "Can't remove item from storage: uuid can't be null" );
243+ final Object obj = Objects .requireNonNull (
244+ storage .remove (uuid ),
245+ String .format ("Could not remove %s by uuid %s" , clazz , uuid )
246+ );
237247 return cast (obj , clazz );
238248 }
239249
240- private <T > T cast (Object obj , Class <T > clazz ) {
250+ private <T > T cast (final Object obj , final Class <T > clazz ) {
241251 if (clazz .isInstance (obj )) {
242252 return clazz .cast (obj );
243253 }
244254 throw new IllegalStateException ("Can not cast " + obj + " to " + clazz );
245255 }
246256
247- private boolean isEmpty (String s ) {
257+ private boolean isEmpty (final String s ) {
248258 return Objects .isNull (s ) || s .isEmpty ();
249259 }
250260}
0 commit comments