1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Diagnostics ;
34using System . Linq ;
45using System . Reflection ;
89using GherkinSyncTool . Synchronizers . AllureTestOps . Client ;
910using GherkinSyncTool . Synchronizers . AllureTestOps . Content ;
1011using GherkinSyncTool . Synchronizers . AllureTestOps . Exception ;
12+ using GherkinSyncTool . Synchronizers . AllureTestOps . Model ;
1113using NLog ;
1214using Quantori . AllureTestOpsClient . Model ;
1315using Scenario = Gherkin . Ast . Scenario ;
@@ -35,6 +37,7 @@ public void Sync(List<IFeatureFile> featureFiles)
3537 Log . Info ( "# Start synchronization with Allure TestOps" ) ;
3638
3739 var allureTestCases = _allureClientWrapper . GetAllTestCases ( ) . ToList ( ) ;
40+ var featureFilesTagIds = new List < ulong > ( ) ;
3841
3942 foreach ( var featureFile in featureFiles )
4043 {
@@ -44,14 +47,14 @@ public void Sync(List<IFeatureFile> featureFiles)
4447 {
4548 var tagId = scenario . Tags . FirstOrDefault ( tag => tag . Name . Contains ( _gherkinSyncToolConfig . TagIdPrefix ) ) ;
4649
47- var caseRequest = _caseContentBuilder . BuildCaseRequest ( scenario , featureFile ) ;
50+ var caseRequestExtended = _caseContentBuilder . BuildCaseRequest ( scenario , featureFile ) ;
4851
4952 // Create test case for feature file which is getting synced for the first time, so no tag id present.
5053 if ( tagId is null )
5154 {
52- var newTestCase = CreateTestCase ( caseRequest ) ;
53- if ( newTestCase is null ) continue ;
54-
55+ var newTestCase = CreateNewTestCase ( caseRequestExtended ) ;
56+ if ( newTestCase is null ) continue ;
57+
5558 var lineNumberToInsert = scenario . Location . Line - 1 + insertedTagIdsCount ;
5659 var formattedTagId = GherkinHelper . FormatTagId ( newTestCase . Id . ToString ( ) ) ;
5760 TextFilesEditMethods . InsertLineToTheFile ( featureFile . AbsolutePath , lineNumberToInsert ,
@@ -63,14 +66,15 @@ public void Sync(List<IFeatureFile> featureFiles)
6366 if ( tagId is not null )
6467 {
6568 var caseIdFromFile = GherkinHelper . GetTagId ( tagId ) ;
66- //featureFilesTagIds.Add(caseId);
69+ featureFilesTagIds . Add ( caseIdFromFile ) ;
70+
6771 var allureTestCase = allureTestCases . FirstOrDefault ( c => c . Id == caseIdFromFile ) ;
6872 if ( allureTestCase is null )
6973 {
7074 Log . Warn ( $ "Case with id { caseIdFromFile } not found. Recreating missing case") ;
71- var newTestCase = CreateTestCase ( caseRequest ) ;
72- if ( newTestCase is null ) continue ;
73-
75+ var newTestCase = CreateNewTestCase ( caseRequestExtended ) ;
76+ if ( newTestCase is null ) continue ;
77+
7478 var formattedTagId = GherkinHelper . FormatTagId ( newTestCase . Id . ToString ( ) ) ;
7579 TextFilesEditMethods . ReplaceLineInTheFile ( featureFile . AbsolutePath ,
7680 tagId . Location . Line - 1 + insertedTagIdsCount , formattedTagId ) ;
@@ -79,35 +83,56 @@ public void Sync(List<IFeatureFile> featureFiles)
7983 {
8084 try
8185 {
82- _allureClientWrapper . UpdateTestCase ( allureTestCase , caseRequest ) ;
86+ _allureClientWrapper . UpdateTestCase ( allureTestCase , caseRequestExtended ) ;
8387 }
8488 catch ( AllureException e )
8589 {
86- Log . Error ( e , $ "The test case has not been updated: { caseRequest . Name } ") ;
90+ Log . Error ( e , $ "The test case has not been updated: { caseRequestExtended . CreateTestCaseRequest . Name } ") ;
8791 _context . IsRunSuccessful = false ;
8892 }
8993 }
9094 }
9195 }
9296 }
93-
97+ //TODO:
98+ //DeleteNotExistingScenarios(allureTestCases, featureFilesTagIds);
9499 Log . Debug ( @$ "Synchronization with Allure TestOps finished in: { stopwatch . Elapsed : mm\:ss\.fff} ") ;
95100 }
96101
97- private TestCase CreateTestCase ( CreateTestCaseRequest caseRequest )
102+ private void DeleteNotExistingScenarios ( List < TestCaseContent > allureTestCases , List < ulong > featureFilesTagIds )
103+ {
104+ throw new NotImplementedException ( "The method should be implemented in future versions" ) ;
105+ }
106+
107+ private TestCase CreateNewTestCase ( CreateTestCaseRequestExtended caseRequestExtended )
98108 {
99- TestCase result = null ;
109+ TestCase newTestCase = null ;
100110 try
101111 {
102- result = _allureClientWrapper . AddTestCase ( caseRequest ) ;
103- return result ;
112+ newTestCase = _allureClientWrapper . AddTestCase ( caseRequestExtended . CreateTestCaseRequest ) ;
104113 }
105114 catch ( AllureException e )
106115 {
107- Log . Error ( e , $ "The case has not been created: { caseRequest . Name } ") ;
116+ Log . Error ( e , $ "The case has not been created: { caseRequestExtended . CreateTestCaseRequest . Name } ") ;
108117 _context . IsRunSuccessful = false ;
109- return result ;
110118 }
119+
120+ if ( newTestCase is null ) return null ;
121+
122+ if ( caseRequestExtended . StepsAttachments . Any ( ) )
123+ {
124+ try
125+ {
126+ _allureClientWrapper . UploadStepAttachments ( caseRequestExtended , newTestCase ) ;
127+ }
128+ catch ( AllureException e )
129+ {
130+ Log . Error ( e , $ "The test case steps attachment has not been uploaded: [{ newTestCase . Id } ]{ caseRequestExtended . CreateTestCaseRequest . Name } ") ;
131+ _context . IsRunSuccessful = false ;
132+ }
133+ }
134+
135+ return newTestCase ;
111136 }
112137 }
113138}
0 commit comments