2525import org .wso2 .testgrid .common .TestPlan ;
2626import org .wso2 .testgrid .common .TestScenario ;
2727import org .wso2 .testgrid .common .exception .CommandExecutionException ;
28+ import org .wso2 .testgrid .common .exception .TestGridException ;
2829import org .wso2 .testgrid .common .util .FileUtil ;
2930import org .wso2 .testgrid .common .util .TestGridUtil ;
3031import org .wso2 .testgrid .dao .TestGridDAOException ;
32+ import org .wso2 .testgrid .dao .uow .ProductUOW ;
3133import org .wso2 .testgrid .dao .uow .TestPlanUOW ;
3234import org .wso2 .testgrid .logging .plugins .LogFilePathLookup ;
3335
3436import java .io .IOException ;
37+ import java .nio .file .Files ;
38+ import java .nio .file .Path ;
3539import java .nio .file .Paths ;
3640import java .util .ArrayList ;
41+ import java .util .Iterator ;
3742import java .util .List ;
3843import java .util .Optional ;
44+ import java .util .stream .Collectors ;
45+ import java .util .stream .Stream ;
3946
4047/**
4148 * Resolves the invalid statuses caused by any failures.
@@ -57,22 +64,28 @@ public class FinalizeRunTestplan implements Command {
5764 aliases = { "-f" })
5865 private String testPlanYamlFilePath = "" ;
5966
67+ @ Option (name = "--workspace" ,
68+ usage = "Workspace Of The Job" ,
69+ aliases = { "-w" })
70+ private String workspace = "" ;
71+
6072 private TestPlanUOW testPlanUOW ;
73+ private ProductUOW productUOW ;
6174 private List <TestPlan > testPlans ;
6275
6376 public FinalizeRunTestplan () {
6477 testPlanUOW = new TestPlanUOW ();
78+ productUOW = new ProductUOW ();
6579 }
6680
6781 @ Override
6882 public void execute () throws CommandExecutionException {
6983
7084 LogFilePathLookup .setLogFilePath (
7185 TestGridUtil .deriveTestGridLogFilePath (productName , TestGridConstants .TESTGRID_LOG_FILE_NAME ));
72-
73- if (Paths .get (testPlanYamlFilePath ).toFile ().exists ()) {
74- //Read test plan id from test-plan yaml file
75- try {
86+ try {
87+ if (Paths .get (testPlanYamlFilePath ).toFile ().exists ()) {
88+ //Read test plan id from test-plan yaml file
7689 TestPlan testPlan = FileUtil .readYamlFile (testPlanYamlFilePath , TestPlan .class );
7790 Optional <TestPlan > testPlanEntity = testPlanUOW .getTestPlanById (testPlan .getId ());
7891 if (testPlanEntity .isPresent ()) {
@@ -81,21 +94,17 @@ public void execute() throws CommandExecutionException {
8194 } else {
8295 testPlans = testPlanUOW .getTestPlansOlderThan (TIME_DURATION , TIME_UNIT );
8396 }
84- } catch (IOException e ) {
85- logger .error ("Error occurred while trying to read " + testPlanYamlFilePath );
86- } catch (TestGridDAOException e ) {
87- logger .error ("Error while fetching test plan from database." );
97+
98+ } else {
99+ testPlans = testPlanUOW .getTestPlansOlderThan (TIME_DURATION , TIME_UNIT );
88100 }
89- } else {
90- testPlans = testPlanUOW .getTestPlansOlderThan (TIME_DURATION , TIME_UNIT );
91- }
92101
93- logger .info ("Finalizing test plan status..." );
94- boolean isExistsFailedScenarios = false ;
95- for (TestPlan testPlan : testPlans ) {
96- //Set statuses of scenarios
97- for (TestScenario testScenario : testPlan .getTestScenarios ()) {
98- switch (testScenario .getStatus ()) {
102+ logger .info ("Finalizing test plan status..." );
103+ boolean isExistsFailedScenarios = false ;
104+ for (TestPlan testPlan : testPlans ) {
105+ //Set statuses of scenarios
106+ for (TestScenario testScenario : testPlan .getTestScenarios ()) {
107+ switch (testScenario .getStatus ()) {
99108 case PENDING :
100109 testScenario .setStatus (Status .DID_NOT_RUN );
101110 break ;
@@ -109,25 +118,34 @@ public void execute() throws CommandExecutionException {
109118 break ;
110119 default :
111120 break ;
121+ }
112122 }
113- }
114- //Set statuses of testplans
115- switch (testPlan .getStatus ()) {
123+ //Set statuses of testplans
124+ switch (testPlan .getStatus ()) {
116125 case PENDING :
117126 testPlan .setStatus (Status .DID_NOT_RUN );
118127 persistTestPlan (testPlan );
119128 break ;
120129 case RUNNING :
121130 if (isExistsFailedScenarios ) {
122131 testPlan .setStatus (Status .FAIL );
132+ isExistsFailedScenarios = false ;
123133 } else {
124134 testPlan .setStatus (Status .INCOMPLETE );
125135 }
126136 persistTestPlan (testPlan );
127137 break ;
128138 default :
129139 break ;
140+ }
130141 }
142+ updateProductStatus ();
143+ } catch (IOException e ) {
144+ logger .error ("Error occurred while trying to read " + testPlanYamlFilePath , e );
145+ } catch (TestGridDAOException e ) {
146+ logger .error ("Error while fetching test plan from database." , e );
147+ } catch (TestGridException e ) {
148+ logger .error ("Error occured while updating the product status." , e );
131149 }
132150 }
133151
@@ -143,4 +161,48 @@ private void persistTestPlan(TestPlan testPlan) {
143161 logger .error ("Error occurred while persisting the test plan. " , e );
144162 }
145163 }
164+
165+ /**
166+ * Update the last success timestamp of the product build or last failure timestamp of the product build
167+ * by considering status of test plans.
168+ *
169+ */
170+ private void updateProductStatus () throws TestGridException {
171+ Path source = Paths .get (workspace , "test-plans" );
172+ String productId ;
173+ Boolean isCompleteBuild = true ;
174+ try (Stream <Path > stream = Files .list (source ).filter (Files ::isRegularFile )) {
175+ List <Path > paths = stream .sorted ().collect (Collectors .toList ());
176+ for (Iterator <Path > iterator = paths .iterator (); iterator .hasNext (); ) {
177+ Path path = iterator .next ();
178+ if (!path .toFile ().exists ()) {
179+ throw new TestGridException (
180+ "Test Plan File doesn't exist. File path is " + path .toAbsolutePath ().toString ());
181+ }
182+ TestPlan testPlan = FileUtil .readYamlFile (path .toAbsolutePath ().toString (), TestPlan .class );
183+ Optional <TestPlan > testPlanEntity = testPlanUOW .getTestPlanById (testPlan .getId ());
184+ if (testPlanEntity .isPresent ()) {
185+ if (Status .FAIL .equals (testPlanEntity .get ().getStatus ())) {
186+ productId = testPlanEntity .get ().getDeploymentPattern ().getProduct ().getId ();
187+ productUOW .updateProductStatusTimestamp (Status .FAIL , productId );
188+ break ;
189+ } else if ((Status .DID_NOT_RUN .equals (testPlanEntity .get ().getStatus ()) || Status .INCOMPLETE
190+ .equals (testPlanEntity .get ().getStatus ())) && isCompleteBuild ) {
191+ isCompleteBuild = false ;
192+ }
193+ } else {
194+ throw new TestGridException (
195+ "Test Plan doesn't exist in the TG database. Test Plan id: " + testPlan .getId ());
196+ }
197+ if (!iterator .hasNext () && isCompleteBuild ) {
198+ productId = testPlanEntity .get ().getDeploymentPattern ().getProduct ().getId ();
199+ productUOW .updateProductStatusTimestamp (Status .SUCCESS , productId );
200+ }
201+ }
202+ } catch (TestGridDAOException e ) {
203+ logger .error ("Error occured when updating the product table of TG" , e );
204+ } catch (IOException e ) {
205+ logger .error ("Error occured when reading a test plan yaml file" , e );
206+ }
207+ }
146208}
0 commit comments