From cacb78ea3c09457e8e587ea139c6cbf2f465d57b Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 09:24:10 +0530 Subject: [PATCH 01/67] Adding jenkins file --- Jenkinsfilev1 | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Jenkinsfilev1 diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 new file mode 100644 index 0000000..6c10646 --- /dev/null +++ b/Jenkinsfilev1 @@ -0,0 +1,29 @@ +pipeline { + agent any + +stages { + stage('Initializing') { + steps { + echo 'Initializing. . . . . .!' + } + } + + stage('Testing') { + steps { + echo 'Testing. . . . . .!' + } + } + + stage('Building') { + steps { + echo 'Building. . . . . .!' + } + } + + stage('Deploying') { + steps { + echo 'Deploying. . . . . .!' + } + } + } +} \ No newline at end of file From eaadce3c352b303730f2b993e228559783a685da Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 09:30:48 +0530 Subject: [PATCH 02/67] Adding new stages in jenkins file --- Jenkinsfilev1 | 51 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 6c10646..c7831b4 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -1,29 +1,44 @@ pipeline { agent any + tools { + maven 'localmaven' + } + stages{ + stage('Clean') { + steps { + bat 'mvn clean' + } + } + stage('Test') { + steps { + bat 'mvn test' + } + post { + success { + echo 'Test Success.' + } -stages { - stage('Initializing') { - steps { - echo 'Initializing. . . . . .!' + failure { + echo ' Test failed.' + } } - } - - stage('Testing') { + } + stage('Build'){ steps { - echo 'Testing. . . . . .!' + bat 'mvn clean package' } - } - - stage('Building') { - steps { - echo 'Building. . . . . .!' + post { + success { + echo 'Now Archiving...' + archiveArtifacts artifacts: '**/target/*.war' + } } } - - stage('Deploying') { - steps { - echo 'Deploying. . . . . .!' - } + stage ('Deploy to Staging'){ + echo 'Deploying. to staging environment .' + } + stage ('Deploy to Production'){ + echo 'Deploying. to production environment . ' } } } \ No newline at end of file From be21884cc7587db04bc443e100145fdd6a370285 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 09:35:02 +0530 Subject: [PATCH 03/67] Correcting the jenkins file --- Jenkinsfilev1 | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index c7831b4..290aa05 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -4,41 +4,41 @@ pipeline { maven 'localmaven' } stages{ - stage('Clean') { + stage('Cleaning') { steps { bat 'mvn clean' } } - stage('Test') { + stage('Running Unit Tests') { steps { bat 'mvn test' } post { - success { - echo 'Test Success.' - } - failure { - echo ' Test failed.' + echo ' Unit test cases failed' } } } - stage('Build'){ + stage('Build') { steps { bat 'mvn clean package' } post { success { - echo 'Now Archiving...' + echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.war' } } } - stage ('Deploy to Staging'){ - echo 'Deploying. to staging environment .' + stage ('Deploy to Staging') { + steps { + echo 'Deploying to staging' + } } stage ('Deploy to Production'){ - echo 'Deploying. to production environment . ' + steps { + echo 'Deploying to production' + } } } } \ No newline at end of file From 03e69d05b6500c2fb9cb54f2b5e5d3d9ffbae30a Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 09:57:57 +0530 Subject: [PATCH 04/67] Correct the war to jar for archiving the artifacts. --- Jenkinsfilev1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 290aa05..ffc9bf0 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -26,7 +26,7 @@ pipeline { post { success { echo 'Now Archiving' - archiveArtifacts artifacts: '**/target/*.war' + archiveArtifacts artifacts: '**/target/*.jar' } } } From 948d7d53e4c454f85a669104e4d097ef2a712f24 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 10:37:09 +0530 Subject: [PATCH 05/67] Adding user input stages for entering jira in jenkins file --- Jenkinsfilev1 | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index ffc9bf0..f08292e 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -4,11 +4,27 @@ pipeline { maven 'localmaven' } stages{ + stage('Input Jira Id') { + steps { + script { + try { + timeout(time:30, unit:'SECONDS') { + jira_id_map = input(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') + echo ("User has following jira id= " + jira_id_map['Jira Id'] + ", and is approved by= " + jira_id_map['APPROVER']) + } + } catch (err) { + + } + } + } + } + stage('Cleaning') { steps { bat 'mvn clean' } } + stage('Running Unit Tests') { steps { bat 'mvn test' @@ -19,6 +35,7 @@ pipeline { } } } + stage('Build') { steps { bat 'mvn clean package' @@ -30,15 +47,18 @@ pipeline { } } } + stage ('Deploy to Staging') { steps { echo 'Deploying to staging' } } - stage ('Deploy to Production'){ + + stage ('Deploy to Production') { steps { echo 'Deploying to production' } } + } } \ No newline at end of file From d7cf7a686a9e200c8d1f11f885140c4cdce4e069 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 10:47:45 +0530 Subject: [PATCH 06/67] Making parameters are global. --- Jenkinsfilev1 | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index f08292e..38c553f 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -3,14 +3,19 @@ pipeline { tools { maven 'localmaven' } + + parameters { + input(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') + } + stages{ + stage('Input Jira Id') { steps { script { try { timeout(time:30, unit:'SECONDS') { - jira_id_map = input(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') - echo ("User has following jira id= " + jira_id_map['Jira Id'] + ", and is approved by= " + jira_id_map['APPROVER']) + echo "Hello ${params.APPROVER}" } } catch (err) { @@ -31,7 +36,7 @@ pipeline { } post { failure { - echo ' Unit test cases failed' + echo ' Unit test cases failed. Creating jira ticket.' } } } @@ -50,13 +55,13 @@ pipeline { stage ('Deploy to Staging') { steps { - echo 'Deploying to staging' + echo 'Deploying to staging.' } } stage ('Deploy to Production') { steps { - echo 'Deploying to production' + echo 'Deploying to production.' } } From 0ee82ac2efdd62404dcebd82d54e86721c2cdb19 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 10:49:48 +0530 Subject: [PATCH 07/67] Correcting the input parameters --- Jenkinsfilev1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 38c553f..a1ec701 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -5,7 +5,7 @@ pipeline { } parameters { - input(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') + string(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') } stages{ From d626c64020e39217990bdcf3539d01ff35c6cfb1 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 10:52:37 +0530 Subject: [PATCH 08/67] reverting changes --- Jenkinsfilev1 | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index a1ec701..5aa5374 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -3,11 +3,7 @@ pipeline { tools { maven 'localmaven' } - - parameters { - string(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') - } - + stages{ stage('Input Jira Id') { @@ -15,7 +11,8 @@ pipeline { script { try { timeout(time:30, unit:'SECONDS') { - echo "Hello ${params.APPROVER}" + jira_id_map = input(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') + echo ("User has following jira id= " + jira_id_map['Jira Id'] + ", and is approved by= " + jira_id_map['APPROVER']) } } catch (err) { From 15060fcbce1e1af7b5d0d548ac7d6c618321319d Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 10:56:24 +0530 Subject: [PATCH 09/67] Adding jira id as global param --- Jenkinsfilev1 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 5aa5374..6357989 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -3,8 +3,10 @@ pipeline { tools { maven 'localmaven' } - - stages{ + + def jira_id_map; + + stages{ stage('Input Jira Id') { steps { @@ -45,6 +47,7 @@ pipeline { post { success { echo 'Now Archiving' + echo ("User has following jira id= " + jira_id_map['Jira Id'] + ", and is approved by= " + jira_id_map['APPROVER']) archiveArtifacts artifacts: '**/target/*.jar' } } From c2c47ceaaf0f387f69df917ebe032f2ce7ee3d7c Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 10:57:26 +0530 Subject: [PATCH 10/67] Adding jira id as global param --- Jenkinsfilev1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 6357989..ae00a9d 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -4,7 +4,7 @@ pipeline { maven 'localmaven' } - def jira_id_map; + def jira_id_map = ""; stages{ From 1e7ed59fccaaef0e485128d7f24356886fd6f75b Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 11:01:00 +0530 Subject: [PATCH 11/67] Adding jira id as global param --- Jenkinsfilev1 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index ae00a9d..d96fc81 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -1,11 +1,10 @@ +def jira_id_map pipeline { agent any tools { maven 'localmaven' } - - def jira_id_map = ""; - + stages{ stage('Input Jira Id') { From b48e2aa1534f6219e19a671c4bd6a8db608acd78 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 11:27:32 +0530 Subject: [PATCH 12/67] Add code to add comment to jira --- Jenkinsfilev1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index d96fc81..dbb9e3e 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -13,7 +13,6 @@ pipeline { try { timeout(time:30, unit:'SECONDS') { jira_id_map = input(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') - echo ("User has following jira id= " + jira_id_map['Jira Id'] + ", and is approved by= " + jira_id_map['APPROVER']) } } catch (err) { @@ -45,8 +44,9 @@ pipeline { } post { success { - echo 'Now Archiving' echo ("User has following jira id= " + jira_id_map['Jira Id'] + ", and is approved by= " + jira_id_map['APPROVER']) + jiraAddComment comment: 'Build successfull. Making deployment to the servers.', idOrKey: 'SSP-13', site: 'localjira' + echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } } From bee898b29262c3f071a9feda6d71f9a0d018b9ff Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 11:38:09 +0530 Subject: [PATCH 13/67] Testing upper case and build number --- Jenkinsfilev1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index dbb9e3e..1ede7c1 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -44,8 +44,9 @@ pipeline { } post { success { - echo ("User has following jira id= " + jira_id_map['Jira Id'] + ", and is approved by= " + jira_id_map['APPROVER']) - jiraAddComment comment: 'Build successfull. Making deployment to the servers.', idOrKey: 'SSP-13', site: 'localjira' + echo "current build number: ${currentBuild.number}" + echo ("User has following jira id= " + jira_id_map['Jira Id'].toString().toUpperCase() + ", and is approved by= " + jira_id_map['APPROVER']) + jiraAddComment comment: 'Build successful. Making deployment to the servers.', idOrKey: jira_id_map['Jira Id'], site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } From 5fe8318f7c958e8b17c1edfeeb2fbd40fa3af36a Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 11:43:25 +0530 Subject: [PATCH 14/67] Making changes to jenkins file for build number --- Jenkinsfilev1 | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 1ede7c1..99f3c89 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -33,7 +33,7 @@ pipeline { } post { failure { - echo ' Unit test cases failed. Creating jira ticket.' + echo ' Unit test cases failed. Logging a jira ticket for jira id jira_id_map['Jira Id'].' } } } @@ -44,9 +44,7 @@ pipeline { } post { success { - echo "current build number: ${currentBuild.number}" - echo ("User has following jira id= " + jira_id_map['Jira Id'].toString().toUpperCase() + ", and is approved by= " + jira_id_map['APPROVER']) - jiraAddComment comment: 'Build successful. Making deployment to the servers.', idOrKey: jira_id_map['Jira Id'], site: 'localjira' + jiraAddComment comment: 'Build ${currentBuild.number} is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } From 88d0fe298f403d89f7b4325bf7c42f986cde911f Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 11:45:43 +0530 Subject: [PATCH 15/67] Logger corrections to jenkins file --- Jenkinsfilev1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 99f3c89..bb971be 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -33,7 +33,7 @@ pipeline { } post { failure { - echo ' Unit test cases failed. Logging a jira ticket for jira id jira_id_map['Jira Id'].' + echo ("Unit test cases failed. Logging a jira ticket for jira id= " + jira_id_map['Jira Id'] + ".") } } } From 8df91e9136b9bc4832e6e09e47182ee754086eee Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 11:47:59 +0530 Subject: [PATCH 16/67] Making build number changes --- Jenkinsfilev1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index bb971be..123f673 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -44,7 +44,7 @@ pipeline { } post { success { - jiraAddComment comment: 'Build ${currentBuild.number} is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' + jiraAddComment comment: 'Build ' + ${currentBuild.number} + ' is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } From b263ac359d5dc253fa30f71b5313c5d277d86069 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 11:53:48 +0530 Subject: [PATCH 17/67] Testing build number --- Jenkinsfilev1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 123f673..2a782a7 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -44,7 +44,10 @@ pipeline { } post { success { - jiraAddComment comment: 'Build ' + ${currentBuild.number} + ' is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' + echo "current build number: ${currentBuild.number}" + def test = ${currentBuild.number} + println "hello ${test}" + jiraAddComment comment: 'Build is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } From 1b3d087828dfd7e239da700086b6d86547fc60ed Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 11:55:06 +0530 Subject: [PATCH 18/67] Testing build number --- Jenkinsfilev1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 2a782a7..1ac4552 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -45,8 +45,6 @@ pipeline { post { success { echo "current build number: ${currentBuild.number}" - def test = ${currentBuild.number} - println "hello ${test}" jiraAddComment comment: 'Build is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' From 844cf30465c0d1a7d9d06c69d0cd41e614d2fc99 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 12:23:25 +0530 Subject: [PATCH 19/67] Making changes for the build number --- Jenkinsfilev1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 1ac4552..efbf405 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -45,7 +45,8 @@ pipeline { post { success { echo "current build number: ${currentBuild.number}" - jiraAddComment comment: 'Build is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' + def comment = [ body: 'Build ${currentBuild.number} is successful. Making deployment to the servers!' ] + jiraAddComment input: comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } From 67258a7e3cf921447db27be1100988846ffab7a0 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 12:24:22 +0530 Subject: [PATCH 20/67] Making changes for the build number --- Jenkinsfilev1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index efbf405..f7ec46b 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -45,8 +45,8 @@ pipeline { post { success { echo "current build number: ${currentBuild.number}" - def comment = [ body: 'Build ${currentBuild.number} is successful. Making deployment to the servers!' ] - jiraAddComment input: comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' + def comment = 'Build ${currentBuild.number} is successful. Making deployment to the servers!' + jiraAddComment comment: comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } From b85b50dd730c9414cbb464ca534184ff6231cec7 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 12:25:44 +0530 Subject: [PATCH 21/67] changing variable name --- Jenkinsfilev1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index f7ec46b..be06fc9 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -45,8 +45,8 @@ pipeline { post { success { echo "current build number: ${currentBuild.number}" - def comment = 'Build ${currentBuild.number} is successful. Making deployment to the servers!' - jiraAddComment comment: comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' + my_comment = 'Build ${currentBuild.number} is successful. Making deployment to the servers!' + jiraAddComment comment: my_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } From 0ce66b2882fb72fc8e1e42c490768d7ffdb6bf89 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 12:28:36 +0530 Subject: [PATCH 22/67] changing variable name --- Jenkinsfilev1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index be06fc9..123f673 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -44,9 +44,7 @@ pipeline { } post { success { - echo "current build number: ${currentBuild.number}" - my_comment = 'Build ${currentBuild.number} is successful. Making deployment to the servers!' - jiraAddComment comment: my_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' + jiraAddComment comment: 'Build ' + ${currentBuild.number} + ' is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } From 3c9e559eb7a728426c146f3c204d66710ab4598c Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 12:37:14 +0530 Subject: [PATCH 23/67] Changes to the build number --- Jenkinsfilev1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 123f673..641cabb 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -44,7 +44,7 @@ pipeline { } post { success { - jiraAddComment comment: 'Build ' + ${currentBuild.number} + ' is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' + jiraAddComment comment: 'Build $env.BUILD_NUMBER is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } From d35ea257b3b5121982eaa21775d2a5e54ed4190d Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 12:40:21 +0530 Subject: [PATCH 24/67] Changes to the build number --- Jenkinsfilev1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 641cabb..ba8e350 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -44,7 +44,9 @@ pipeline { } post { success { - jiraAddComment comment: 'Build $env.BUILD_NUMBER is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' + buildnumber = $env.BUILD_NUMBER + mycomment = 'Build ' + $buildnumber + 'is successful. Making deployment to the servers!' + jiraAddComment comment: $mycomment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } From b6ee44ff45687d454ed0d9c913a1dbf79ad16507 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 12:42:23 +0530 Subject: [PATCH 25/67] Changes to the build number --- Jenkinsfilev1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index ba8e350..2cc4954 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -44,7 +44,7 @@ pipeline { } post { success { - buildnumber = $env.BUILD_NUMBER + buildnumber = BUILD_NUMBER mycomment = 'Build ' + $buildnumber + 'is successful. Making deployment to the servers!' jiraAddComment comment: $mycomment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' From 70f8d9e023e01675170f969b0178d6c82fbfff9a Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 12:44:36 +0530 Subject: [PATCH 26/67] Reverting the changes --- Jenkinsfilev1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 2cc4954..f792b7e 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -44,9 +44,7 @@ pipeline { } post { success { - buildnumber = BUILD_NUMBER - mycomment = 'Build ' + $buildnumber + 'is successful. Making deployment to the servers!' - jiraAddComment comment: $mycomment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' + jiraAddComment comment: 'Build is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } From b501341c439a677c70122ab168c86e4ff00e764a Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 13:25:13 +0530 Subject: [PATCH 27/67] testing build parameter --- Jenkinsfilev1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index f792b7e..72afa31 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -44,6 +44,9 @@ pipeline { } post { success { + echo "build ${currentBuild.number} is successful. Making deployment to the servers!" + def mybuild = "build ${currentBuild.number} is successful. Making deployment to the servers!" + echo $mybuild; jiraAddComment comment: 'Build is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' From e59355a66d0da1d7230b1e65e0cd6ba315450f4e Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 13:26:01 +0530 Subject: [PATCH 28/67] testing build parameter --- Jenkinsfilev1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 72afa31..7ac6213 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -45,8 +45,8 @@ pipeline { post { success { echo "build ${currentBuild.number} is successful. Making deployment to the servers!" - def mybuild = "build ${currentBuild.number} is successful. Making deployment to the servers!" - echo $mybuild; + mybuild = "build ${currentBuild.number} is successful. Making deployment to the servers!" + echo mybuild; jiraAddComment comment: 'Build is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' From 9f8e91ad9b9edbfd3c1c81c8793f348c2e194e89 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 13:27:24 +0530 Subject: [PATCH 29/67] testing build parameter --- Jenkinsfilev1 | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 7ac6213..1eaa026 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -44,9 +44,12 @@ pipeline { } post { success { - echo "build ${currentBuild.number} is successful. Making deployment to the servers!" - mybuild = "build ${currentBuild.number} is successful. Making deployment to the servers!" - echo mybuild; + script { + echo "build ${currentBuild.number} is successful. Making deployment to the servers!" + mybuild = "build ${currentBuild.number} is successful. Making deployment to the servers!" + echo mybuild; + } + jiraAddComment comment: 'Build is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' From d8329b8293eb98839a4f41d083f4e557669c88b4 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 13:30:26 +0530 Subject: [PATCH 30/67] Testing build number parameter --- Jenkinsfilev1 | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 1eaa026..4ddb44a 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -4,9 +4,7 @@ pipeline { tools { maven 'localmaven' } - stages{ - stage('Input Jira Id') { steps { script { @@ -20,13 +18,11 @@ pipeline { } } } - stage('Cleaning') { steps { bat 'mvn clean' } } - stage('Running Unit Tests') { steps { bat 'mvn test' @@ -37,7 +33,6 @@ pipeline { } } } - stage('Build') { steps { bat 'mvn clean package' @@ -45,29 +40,24 @@ pipeline { post { success { script { - echo "build ${currentBuild.number} is successful. Making deployment to the servers!" - mybuild = "build ${currentBuild.number} is successful. Making deployment to the servers!" - echo mybuild; + build_info_comment = "Build ${currentBuild.number} is successful. Making deployment to the servers!" + echo build_info_comment; } - - jiraAddComment comment: 'Build is successful. Making deployment to the servers!', idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' + jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } } } - stage ('Deploy to Staging') { steps { echo 'Deploying to staging.' } } - stage ('Deploy to Production') { steps { echo 'Deploying to production.' } } - } } \ No newline at end of file From a0c68ec2edeac4e77e3224588d695157dad5c573 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 13:33:46 +0530 Subject: [PATCH 31/67] Adding administrator name who started the deployment build --- Jenkinsfilev1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 4ddb44a..4b4fc4d 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -40,7 +40,7 @@ pipeline { post { success { script { - build_info_comment = "Build ${currentBuild.number} is successful. Making deployment to the servers!" + build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." echo build_info_comment; } jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' From a425da6c22e6663faffae46b6e3d5b81c1b1c433 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 13:38:33 +0530 Subject: [PATCH 32/67] Testing empty param --- Jenkinsfilev1 | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 4b4fc4d..3784c80 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -40,10 +40,16 @@ pipeline { post { success { script { - build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." - echo build_info_comment; + if(jira_id_map['Jira Id'] != null) { + build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." + echo build_info_comment; + jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' + } else { + def testIssue = [fields: [ project: [key: 'SSP'], + summary: 'New bug.', description: 'New JIRA Created from Jenkins.', issuetype: [name: 'Bug']]] + response = jiraNewIssue issue: testIssue + } } - jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' echo 'Now Archiving' archiveArtifacts artifacts: '**/target/*.jar' } From c0871979bcc99da380d6b260451ba80365812d72 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 13:47:50 +0530 Subject: [PATCH 33/67] Testing for empty jira id param --- Jenkinsfilev1 | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 3784c80..450db76 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -40,14 +40,12 @@ pipeline { post { success { script { - if(jira_id_map['Jira Id'] != null) { + if(jira_id_map['Jira Id'].toString().trim()) { build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." - echo build_info_comment; jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' } else { - def testIssue = [fields: [ project: [key: 'SSP'], - summary: 'New bug.', description: 'New JIRA Created from Jenkins.', issuetype: [name: 'Bug']]] - response = jiraNewIssue issue: testIssue + def new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: 'Jenkins job failed at build and test step. Please check!', issuetype: [name: 'Bug']]] + jiraNewIssue issue: new_issue } } echo 'Now Archiving' From 719fa9c8642fbd0a86d5ceb038576981eb9c55b2 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 13:48:29 +0530 Subject: [PATCH 34/67] Testing for empty jira id param --- Jenkinsfilev1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 450db76..1e6b59f 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -40,7 +40,7 @@ pipeline { post { success { script { - if(jira_id_map['Jira Id'].toString().trim()) { + if(!(jira_id_map['Jira Id'].toString().trim())) { build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' } else { From 7195b3b789bff203e4a2bdce1e560f1a1bd45531 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 13:51:36 +0530 Subject: [PATCH 35/67] Testing for empty jira id param --- Jenkinsfilev1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 1e6b59f..defa5d1 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -40,10 +40,12 @@ pipeline { post { success { script { - if(!(jira_id_map['Jira Id'].toString().trim())) { + if(!(jira_id_map['Jira Id'].toString()?.trim())) { + echo "Adding comment to existing jira id" build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' } else { + echo "Creating new jira ticket in the backlog" def new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: 'Jenkins job failed at build and test step. Please check!', issuetype: [name: 'Bug']]] jiraNewIssue issue: new_issue } From be92384f700c951435b3505e4167238885e428ef Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 13:57:01 +0530 Subject: [PATCH 36/67] Testing for empty jira id param --- Jenkinsfilev1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index defa5d1..397e006 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -40,14 +40,14 @@ pipeline { post { success { script { - if(!(jira_id_map['Jira Id'].toString()?.trim())) { + if(jira_id_map['Jira Id'].toString()?.trim()) { echo "Adding comment to existing jira id" build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' } else { echo "Creating new jira ticket in the backlog" def new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: 'Jenkins job failed at build and test step. Please check!', issuetype: [name: 'Bug']]] - jiraNewIssue issue: new_issue + jiraNewIssue issue: new_issue, site: 'localjira' } } echo 'Now Archiving' From e2b4458b1ce029c25119c9262b346961c91aa50c Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 14:44:51 +0530 Subject: [PATCH 37/67] testing --- Jenkinsfilev1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 397e006..b1b6fd8 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -40,7 +40,7 @@ pipeline { post { success { script { - if(jira_id_map['Jira Id'].toString()?.trim()) { + if(jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { echo "Adding comment to existing jira id" build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' From 7f20ebf443090739f50607ba44995327ab93de83 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 14:48:34 +0530 Subject: [PATCH 38/67] testing --- Jenkinsfilev1 | 1 + 1 file changed, 1 insertion(+) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index b1b6fd8..15608e9 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -40,6 +40,7 @@ pipeline { post { success { script { + echo("jira id=" + jira_id_map['Jira Id']); if(jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { echo "Adding comment to existing jira id" build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." From 36f71e3072549dec0ff524e33eb40806abede4fc Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 15:42:30 +0530 Subject: [PATCH 39/67] Adding null check --- Jenkinsfilev1 | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 15608e9..ca7cb99 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -40,13 +40,10 @@ pipeline { post { success { script { - echo("jira id=" + jira_id_map['Jira Id']); - if(jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { - echo "Adding comment to existing jira id" + if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' } else { - echo "Creating new jira ticket in the backlog" def new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: 'Jenkins job failed at build and test step. Please check!', issuetype: [name: 'Bug']]] jiraNewIssue issue: new_issue, site: 'localjira' } From 1a1d63ead5f6b09133f482f7af49615b31df8fbd Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 20:15:31 +0530 Subject: [PATCH 40/67] Adding new jenkins file to check the vulnerabilties in the code. --- Jenkinsfilev2 | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Jenkinsfilev2 diff --git a/Jenkinsfilev2 b/Jenkinsfilev2 new file mode 100644 index 0000000..350147c --- /dev/null +++ b/Jenkinsfilev2 @@ -0,0 +1,69 @@ +def jira_site_name = 'localjira' +def jira_id_map +pipeline { + agent any + tools { + maven 'localmaven' + } + stages{ + stage('Input details') { + steps { + script { + try { + timeout(time:30, unit:'SECONDS') { + jira_id_map = input(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') + } + } catch (err) { + + } + } + } + } + stage('Cleaning project directory') { + steps { + bat 'mvn clean' + } + } + stage('Run unit tests') { + steps { + bat 'mvn test' + } + post { + success { + println ("All unit test cases failed. Publishing the unit results test coverage for the team.") + } + failure { + println ("Unit test cases failed. Logging a jira ticket for the developers!") + build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " failed while running unit tests." + if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { + jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name + } else { + new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: build_info_comment, issuetype: [name: 'Bug']]] + jiraNewIssue issue: new_issue, site: jira_site_name + } + } + } + } + stage('Build') { + steps { + bat 'mvn clean package' + } + post { + success { + println ("Now Archiving.") + archiveArtifacts artifacts: '**/target/*.jar' + } + } + } + stage('Running vulnerability check') { + steps { + println("Running vulnerability check.") + } + } + stage ('Push To ECS and Deploy') { + steps { + println ("Deploying to AWS environment.") + } + } + } +} \ No newline at end of file From ad75a028965070ed6ff4fc79a10cda5cf36a66d3 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 20:19:28 +0530 Subject: [PATCH 41/67] Adding file changes --- Jenkinsfilev2 | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Jenkinsfilev2 b/Jenkinsfilev2 index 350147c..62c9492 100644 --- a/Jenkinsfilev2 +++ b/Jenkinsfilev2 @@ -34,13 +34,15 @@ pipeline { } failure { println ("Unit test cases failed. Logging a jira ticket for the developers!") - build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " failed while running unit tests." - if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { - jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name + script { + build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " failed while running unit tests." + if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { + jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name } else { - new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: build_info_comment, issuetype: [name: 'Bug']]] - jiraNewIssue issue: new_issue, site: jira_site_name + new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: build_info_comment, issuetype: [name: 'Bug']]] + jiraNewIssue issue: new_issue, site: jira_site_name } + } } } } From e0126f0bc09365f16f2052b0a4571504249faf33 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 20:28:07 +0530 Subject: [PATCH 42/67] Correcting echo text --- Jenkinsfilev2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev2 b/Jenkinsfilev2 index 62c9492..02810f3 100644 --- a/Jenkinsfilev2 +++ b/Jenkinsfilev2 @@ -30,7 +30,7 @@ pipeline { } post { success { - println ("All unit test cases failed. Publishing the unit results test coverage for the team.") + println ("All unit test cases passed. Publishing the unit results test coverage for the team.") } failure { println ("Unit test cases failed. Logging a jira ticket for the developers!") From af6df1d7bab48be8508466ab16fa69fe80f29f20 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 21:11:42 +0530 Subject: [PATCH 43/67] adding code for testing common message --- Jenkinsfilev1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index ca7cb99..df5e72f 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -40,12 +40,12 @@ pipeline { post { success { script { + build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " failed while running unit tests." if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { - build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." - jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' + jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name } else { - def new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: 'Jenkins job failed at build and test step. Please check!', issuetype: [name: 'Bug']]] - jiraNewIssue issue: new_issue, site: 'localjira' + new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: build_info_comment, issuetype: [name: 'Bug']]] + jiraNewIssue issue: new_issue, site: jira_site_name } } echo 'Now Archiving' From 2d8f696e543e5f4442b113f4de894c8235ce09d7 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 21:21:34 +0530 Subject: [PATCH 44/67] adding code for testing common message --- Jenkinsfilev1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index df5e72f..faa80e1 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -10,7 +10,7 @@ pipeline { script { try { timeout(time:30, unit:'SECONDS') { - jira_id_map = input(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') + jira_id_map = input(message: 'Enter Jira Id (or leave blank)!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']]) } } catch (err) { @@ -40,7 +40,7 @@ pipeline { post { success { script { - build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " failed while running unit tests." + build_info_comment = "Build ${currentBuild.number} started by " + ${env.BUILD_USER_ID} + " failed while running unit tests." if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name } else { From ce6bb37aa27f71f209d3589e18cb957ca73fe37d Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 21:25:24 +0530 Subject: [PATCH 45/67] adding code for testing common message --- Jenkinsfilev1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index faa80e1..8a4c517 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -40,7 +40,7 @@ pipeline { post { success { script { - build_info_comment = "Build ${currentBuild.number} started by " + ${env.BUILD_USER_ID} + " failed while running unit tests." + build_info_comment = "Build ${currentBuild.number} started by ${BUILD_USER} failed while running unit tests." if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name } else { From be15ad7c9ecbe3e9f59bba44c3a840d9ddff3ed1 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 21:49:52 +0530 Subject: [PATCH 46/67] testing --- Jenkinsfilev1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 8a4c517..36a476e 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -40,7 +40,7 @@ pipeline { post { success { script { - build_info_comment = "Build ${currentBuild.number} started by ${BUILD_USER} failed while running unit tests." + build_info_comment = "Build ${currentBuild.number} started by failed while running unit tests." if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name } else { From 49f620becb771c25f713b8733d5570e89ed72963 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 21:54:40 +0530 Subject: [PATCH 47/67] Reverting code in the jenkinsfilev1 and adding testing code in jenkinsfilev2 --- Jenkinsfilev1 | 10 +++++----- Jenkinsfilev2 | 9 +++------ 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 index 36a476e..ca7cb99 100644 --- a/Jenkinsfilev1 +++ b/Jenkinsfilev1 @@ -10,7 +10,7 @@ pipeline { script { try { timeout(time:30, unit:'SECONDS') { - jira_id_map = input(message: 'Enter Jira Id (or leave blank)!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']]) + jira_id_map = input(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') } } catch (err) { @@ -40,12 +40,12 @@ pipeline { post { success { script { - build_info_comment = "Build ${currentBuild.number} started by failed while running unit tests." if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { - jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name + build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." + jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' } else { - new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: build_info_comment, issuetype: [name: 'Bug']]] - jiraNewIssue issue: new_issue, site: jira_site_name + def new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: 'Jenkins job failed at build and test step. Please check!', issuetype: [name: 'Bug']]] + jiraNewIssue issue: new_issue, site: 'localjira' } } echo 'Now Archiving' diff --git a/Jenkinsfilev2 b/Jenkinsfilev2 index 02810f3..bcd7f3c 100644 --- a/Jenkinsfilev2 +++ b/Jenkinsfilev2 @@ -11,7 +11,7 @@ pipeline { script { try { timeout(time:30, unit:'SECONDS') { - jira_id_map = input(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') + jira_id_map = input(message: 'Enter Jira Id (or leave blank)!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']]) } } catch (err) { @@ -29,13 +29,10 @@ pipeline { bat 'mvn test' } post { - success { - println ("All unit test cases passed. Publishing the unit results test coverage for the team.") - } - failure { + success { println ("Unit test cases failed. Logging a jira ticket for the developers!") script { - build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " failed while running unit tests." + build_info_comment = "Build ${currentBuild.number} failed while running unit tests." if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name } else { From 03d076da8639a55364a207cdf60895ffc14dbb37 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 22:43:16 +0530 Subject: [PATCH 48/67] Adding stage skip boolean --- Jenkinsfilev2 | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Jenkinsfilev2 b/Jenkinsfilev2 index bcd7f3c..693f8fd 100644 --- a/Jenkinsfilev2 +++ b/Jenkinsfilev2 @@ -11,7 +11,7 @@ pipeline { script { try { timeout(time:30, unit:'SECONDS') { - jira_id_map = input(message: 'Enter Jira Id (or leave blank)!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']]) + jira_id_map = input(message: 'Enter details (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', name: 'Jira Id'], [$class: 'BooleanParameterDefinition', defaultValue: true, name: 'Skip Vulnerability stage?']], submitterParameter: 'APPROVER') } } catch (err) { @@ -32,11 +32,11 @@ pipeline { success { println ("Unit test cases failed. Logging a jira ticket for the developers!") script { - build_info_comment = "Build ${currentBuild.number} failed while running unit tests." + build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is failing while running unit tests." if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name } else { - new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: build_info_comment, issuetype: [name: 'Bug']]] + new_issue = [fields: [ project: [key: 'SSP'], summary: 'Unit Test Cases failed', description: build_info_comment, issuetype: [name: 'Bug']]] jiraNewIssue issue: new_issue, site: jira_site_name } } @@ -55,6 +55,9 @@ pipeline { } } stage('Running vulnerability check') { + when { + expression { jira_id_map['Skip Vulnerability stage?'] == true } + } steps { println("Running vulnerability check.") } From a9bf64b5c8522176e9e1192e73656831f2b5d4f2 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 22:50:41 +0530 Subject: [PATCH 49/67] Adding stage skip boolean --- Jenkinsfilev2 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Jenkinsfilev2 b/Jenkinsfilev2 index 693f8fd..3fc5a9a 100644 --- a/Jenkinsfilev2 +++ b/Jenkinsfilev2 @@ -11,7 +11,7 @@ pipeline { script { try { timeout(time:30, unit:'SECONDS') { - jira_id_map = input(message: 'Enter details (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', name: 'Jira Id'], [$class: 'BooleanParameterDefinition', defaultValue: true, name: 'Skip Vulnerability stage?']], submitterParameter: 'APPROVER') + jira_id_map = input(message: 'Enter details (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', name: 'Jira Id'], [$class: 'BooleanParameterDefinition', defaultValue: false, name: 'Skip Vulnerability stage?']], submitterParameter: 'APPROVER') } } catch (err) { @@ -55,10 +55,13 @@ pipeline { } } stage('Running vulnerability check') { + is_allowed = jira_id_map['Skip Vulnerability stage?'] + println(is_allowed) when { - expression { jira_id_map['Skip Vulnerability stage?'] == true } + expression { is_allowed == true } } steps { + println("Running vulnerability check.") } } From 6b6417a53ceb1d8db0835be137304df710f3e2ac Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 22:52:44 +0530 Subject: [PATCH 50/67] Adding stage skip boolean --- Jenkinsfilev2 | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Jenkinsfilev2 b/Jenkinsfilev2 index 3fc5a9a..812901a 100644 --- a/Jenkinsfilev2 +++ b/Jenkinsfilev2 @@ -55,14 +55,11 @@ pipeline { } } stage('Running vulnerability check') { - is_allowed = jira_id_map['Skip Vulnerability stage?'] - println(is_allowed) when { - expression { is_allowed == true } + expression { jira_id_map['Skip Vulnerability stage?'] == true } } steps { - - println("Running vulnerability check.") + println("Running vulnerability check stage.") } } stage ('Push To ECS and Deploy') { From b69f7a28e1e84fd9af246de38b5415318a8ad785 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 23:03:48 +0530 Subject: [PATCH 51/67] Making changes to the boolean variable. --- Jenkinsfilev2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfilev2 b/Jenkinsfilev2 index 812901a..cc246ad 100644 --- a/Jenkinsfilev2 +++ b/Jenkinsfilev2 @@ -56,7 +56,7 @@ pipeline { } stage('Running vulnerability check') { when { - expression { jira_id_map['Skip Vulnerability stage?'] == true } + expression { jira_id_map['Skip Vulnerability stage?'] == false } } steps { println("Running vulnerability check stage.") From 4caa2a6222a403e941c33f11473748cddd9a9fb7 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Fri, 6 Sep 2019 23:10:32 +0530 Subject: [PATCH 52/67] Reverting the post block for build and test stage --- Jenkinsfilev2 | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Jenkinsfilev2 b/Jenkinsfilev2 index cc246ad..33ef4c9 100644 --- a/Jenkinsfilev2 +++ b/Jenkinsfilev2 @@ -29,8 +29,11 @@ pipeline { bat 'mvn test' } post { - success { - println ("Unit test cases failed. Logging a jira ticket for the developers!") + success { + println ("All unit test cases successfully passed. Publishing the unit results test coverage for the team.") + } + failure { + println ("Unit test cases failed. Logging a jira ticket for the developers.") script { build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is failing while running unit tests." if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { From 43dea0c2fd50ec520a127e3678e2becfa555736f Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Sat, 7 Sep 2019 00:44:44 +0530 Subject: [PATCH 53/67] Adding stage to validate the input form parameters --- Jenkinsfilev3 | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Jenkinsfilev3 diff --git a/Jenkinsfilev3 b/Jenkinsfilev3 new file mode 100644 index 0000000..9e3d799 --- /dev/null +++ b/Jenkinsfilev3 @@ -0,0 +1,91 @@ +def jira_site_name = 'localjira' +def jira_id_map +pipeline { + agent any + tools { + maven 'localmaven' + } + stages{ + + stage('Input details') { + steps { + script { + try { + timeout(time:30, unit:'SECONDS') { + jira_id_map = input(message: 'Enter details (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', name: 'Jira Id'], [$class: 'BooleanParameterDefinition', defaultValue: false, name: 'Skip Vulnerability stage?']], submitterParameter: 'APPROVER') + } + } catch (err) { + + } + } + } + } + + stage('Validate input details') { + steps { + script { + if(jira_id_map == null || jira_id_map['APPROVER'] == null) { + currentBuild.result = 'ABORTED' + error('Stopping early… as input form is empty!') + } + } + } + } + + stage('Cleaning project directory') { + steps { + bat 'mvn clean' + } + } + + stage('Run unit tests') { + steps { + bat 'mvn test' + } + post { + success { + println ("All unit test cases successfully passed. Publishing the unit results test coverage for the team.") + } + failure { + println ("Unit test cases failed. Logging a jira ticket for the developers.") + script { + build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is failing while running unit tests." + if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { + jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name + } else { + new_issue = [fields: [ project: [key: 'SSP'], summary: 'Unit Test Cases failed', description: build_info_comment, issuetype: [name: 'Bug']]] + jiraNewIssue issue: new_issue, site: jira_site_name + } + } + } + } + } + + stage('Build') { + steps { + bat 'mvn clean package' + } + post { + success { + println ("Now Archiving.") + archiveArtifacts artifacts: '**/target/*.jar' + } + } + } + + stage('Running vulnerability check') { + when { + expression { jira_id_map['Skip Vulnerability stage?'] == false } + } + steps { + println("Running vulnerability check stage.") + } + } + + stage ('Push To ECS and Deploy') { + steps { + println ("Deploying to AWS environment.") + } + } + } +} \ No newline at end of file From e9925f7e9b1510d841f796ba03a87a5ceefe702e Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Sat, 7 Sep 2019 00:57:20 +0530 Subject: [PATCH 54/67] testing the stage for the input parameters --- Jenkinsfilev3 | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Jenkinsfilev3 b/Jenkinsfilev3 index 9e3d799..6ac5e2b 100644 --- a/Jenkinsfilev3 +++ b/Jenkinsfilev3 @@ -7,7 +7,7 @@ pipeline { } stages{ - stage('Input details') { + stage('Enter input details') { steps { script { try { @@ -26,7 +26,7 @@ pipeline { script { if(jira_id_map == null || jira_id_map['APPROVER'] == null) { currentBuild.result = 'ABORTED' - error('Stopping early… as input form is empty!') + error('Stopping early... as input form is empty!') } } } @@ -43,14 +43,11 @@ pipeline { bat 'mvn test' } post { - success { - println ("All unit test cases successfully passed. Publishing the unit results test coverage for the team.") - } - failure { + success { println ("Unit test cases failed. Logging a jira ticket for the developers.") script { build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is failing while running unit tests." - if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { + if(jira_id_map['Jira Id'].toString()?.trim()) { jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name } else { new_issue = [fields: [ project: [key: 'SSP'], summary: 'Unit Test Cases failed', description: build_info_comment, issuetype: [name: 'Bug']]] From f23bc8a27bceaa0c5917119ac46c8feb565efcd7 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Sat, 7 Sep 2019 01:04:22 +0530 Subject: [PATCH 55/67] Reverting changes for final testing --- Jenkinsfilev3 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Jenkinsfilev3 b/Jenkinsfilev3 index 6ac5e2b..cc70a87 100644 --- a/Jenkinsfilev3 +++ b/Jenkinsfilev3 @@ -43,7 +43,10 @@ pipeline { bat 'mvn test' } post { - success { + success { + println ("All unit test cases successfully passed. Publishing the unit results test coverage for the team.") + } + failure { println ("Unit test cases failed. Logging a jira ticket for the developers.") script { build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is failing while running unit tests." From fc4172828bd33d2ae5f64f0893f0f5a236806d70 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Sat, 7 Sep 2019 01:28:13 +0530 Subject: [PATCH 56/67] Adding code to validate the input jira id --- Jenkinsfilev3 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Jenkinsfilev3 b/Jenkinsfilev3 index cc70a87..65e658d 100644 --- a/Jenkinsfilev3 +++ b/Jenkinsfilev3 @@ -28,6 +28,12 @@ pipeline { currentBuild.result = 'ABORTED' error('Stopping early... as input form is empty!') } + + get_issue = jiraGetIssue idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name + if(get_issue == null) { + currentBuild.result = 'ABORTED' + error('Stopping early... as invalid jira id is given!') + } } } } From a2d6646e93127b8dc89c879300333ce9fa5cb324 Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Sat, 7 Sep 2019 12:22:22 +0530 Subject: [PATCH 57/67] Adding indentation changes in the jenkinsfilev3 --- Jenkinsfilev3 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Jenkinsfilev3 b/Jenkinsfilev3 index 65e658d..32958b5 100644 --- a/Jenkinsfilev3 +++ b/Jenkinsfilev3 @@ -1,10 +1,14 @@ def jira_site_name = 'localjira' def jira_id_map + pipeline { - agent any + + agent any + tools { maven 'localmaven' } + stages{ stage('Enter input details') { From 6a23b7ff30355ce459dda517a6e40b4e2f844d15 Mon Sep 17 00:00:00 2001 From: Yatin Date: Sun, 27 Jun 2021 09:57:24 +0530 Subject: [PATCH 58/67] master: fixing testcases and code --- Jenkinsfilev1 | 67 ---------- Jenkinsfilev2 | 74 ----------- Jenkinsfilev3 | 101 --------------- pom.xml | 117 ++++++++--------- .../java/com/example/service/ToDoService.java | 3 +- .../com/example/service/ToDoServiceImpl.java | 7 +- .../com/example/service/ToDoServiceTest.java | 121 +++++++++--------- 7 files changed, 125 insertions(+), 365 deletions(-) delete mode 100644 Jenkinsfilev1 delete mode 100644 Jenkinsfilev2 delete mode 100644 Jenkinsfilev3 diff --git a/Jenkinsfilev1 b/Jenkinsfilev1 deleted file mode 100644 index ca7cb99..0000000 --- a/Jenkinsfilev1 +++ /dev/null @@ -1,67 +0,0 @@ -def jira_id_map -pipeline { - agent any - tools { - maven 'localmaven' - } - stages{ - stage('Input Jira Id') { - steps { - script { - try { - timeout(time:30, unit:'SECONDS') { - jira_id_map = input(message: 'Enter Jira Id (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', defaultValue: '', description: '', name: 'Jira Id']], submitterParameter: 'APPROVER') - } - } catch (err) { - - } - } - } - } - stage('Cleaning') { - steps { - bat 'mvn clean' - } - } - stage('Running Unit Tests') { - steps { - bat 'mvn test' - } - post { - failure { - echo ("Unit test cases failed. Logging a jira ticket for jira id= " + jira_id_map['Jira Id'] + ".") - } - } - } - stage('Build') { - steps { - bat 'mvn clean package' - } - post { - success { - script { - if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { - build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is successful." - jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: 'localjira' - } else { - def new_issue = [fields: [ project: [key: 'SSP'], summary: 'Build error.', description: 'Jenkins job failed at build and test step. Please check!', issuetype: [name: 'Bug']]] - jiraNewIssue issue: new_issue, site: 'localjira' - } - } - echo 'Now Archiving' - archiveArtifacts artifacts: '**/target/*.jar' - } - } - } - stage ('Deploy to Staging') { - steps { - echo 'Deploying to staging.' - } - } - stage ('Deploy to Production') { - steps { - echo 'Deploying to production.' - } - } - } -} \ No newline at end of file diff --git a/Jenkinsfilev2 b/Jenkinsfilev2 deleted file mode 100644 index 33ef4c9..0000000 --- a/Jenkinsfilev2 +++ /dev/null @@ -1,74 +0,0 @@ -def jira_site_name = 'localjira' -def jira_id_map -pipeline { - agent any - tools { - maven 'localmaven' - } - stages{ - stage('Input details') { - steps { - script { - try { - timeout(time:30, unit:'SECONDS') { - jira_id_map = input(message: 'Enter details (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', name: 'Jira Id'], [$class: 'BooleanParameterDefinition', defaultValue: false, name: 'Skip Vulnerability stage?']], submitterParameter: 'APPROVER') - } - } catch (err) { - - } - } - } - } - stage('Cleaning project directory') { - steps { - bat 'mvn clean' - } - } - stage('Run unit tests') { - steps { - bat 'mvn test' - } - post { - success { - println ("All unit test cases successfully passed. Publishing the unit results test coverage for the team.") - } - failure { - println ("Unit test cases failed. Logging a jira ticket for the developers.") - script { - build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is failing while running unit tests." - if(jira_id_map != null && jira_id_map['Jira Id'] != null && jira_id_map['Jira Id'].toString()?.trim()) { - jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name - } else { - new_issue = [fields: [ project: [key: 'SSP'], summary: 'Unit Test Cases failed', description: build_info_comment, issuetype: [name: 'Bug']]] - jiraNewIssue issue: new_issue, site: jira_site_name - } - } - } - } - } - stage('Build') { - steps { - bat 'mvn clean package' - } - post { - success { - println ("Now Archiving.") - archiveArtifacts artifacts: '**/target/*.jar' - } - } - } - stage('Running vulnerability check') { - when { - expression { jira_id_map['Skip Vulnerability stage?'] == false } - } - steps { - println("Running vulnerability check stage.") - } - } - stage ('Push To ECS and Deploy') { - steps { - println ("Deploying to AWS environment.") - } - } - } -} \ No newline at end of file diff --git a/Jenkinsfilev3 b/Jenkinsfilev3 deleted file mode 100644 index 32958b5..0000000 --- a/Jenkinsfilev3 +++ /dev/null @@ -1,101 +0,0 @@ -def jira_site_name = 'localjira' -def jira_id_map - -pipeline { - - agent any - - tools { - maven 'localmaven' - } - - stages{ - - stage('Enter input details') { - steps { - script { - try { - timeout(time:30, unit:'SECONDS') { - jira_id_map = input(message: 'Enter details (or leave blank) and press [Proceed]!!', parameters: [[$class: 'TextParameterDefinition', name: 'Jira Id'], [$class: 'BooleanParameterDefinition', defaultValue: false, name: 'Skip Vulnerability stage?']], submitterParameter: 'APPROVER') - } - } catch (err) { - - } - } - } - } - - stage('Validate input details') { - steps { - script { - if(jira_id_map == null || jira_id_map['APPROVER'] == null) { - currentBuild.result = 'ABORTED' - error('Stopping early... as input form is empty!') - } - - get_issue = jiraGetIssue idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name - if(get_issue == null) { - currentBuild.result = 'ABORTED' - error('Stopping early... as invalid jira id is given!') - } - } - } - } - - stage('Cleaning project directory') { - steps { - bat 'mvn clean' - } - } - - stage('Run unit tests') { - steps { - bat 'mvn test' - } - post { - success { - println ("All unit test cases successfully passed. Publishing the unit results test coverage for the team.") - } - failure { - println ("Unit test cases failed. Logging a jira ticket for the developers.") - script { - build_info_comment = "Build ${currentBuild.number} started by " + jira_id_map['APPROVER'] + " is failing while running unit tests." - if(jira_id_map['Jira Id'].toString()?.trim()) { - jiraAddComment comment: build_info_comment, idOrKey: jira_id_map['Jira Id'].toString().toUpperCase(), site: jira_site_name - } else { - new_issue = [fields: [ project: [key: 'SSP'], summary: 'Unit Test Cases failed', description: build_info_comment, issuetype: [name: 'Bug']]] - jiraNewIssue issue: new_issue, site: jira_site_name - } - } - } - } - } - - stage('Build') { - steps { - bat 'mvn clean package' - } - post { - success { - println ("Now Archiving.") - archiveArtifacts artifacts: '**/target/*.jar' - } - } - } - - stage('Running vulnerability check') { - when { - expression { jira_id_map['Skip Vulnerability stage?'] == false } - } - steps { - println("Running vulnerability check stage.") - } - } - - stage ('Push To ECS and Deploy') { - steps { - println ("Deploying to AWS environment.") - } - } - } -} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 6e67f52..9c2a635 100644 --- a/pom.xml +++ b/pom.xml @@ -1,59 +1,62 @@ - - 4.0.0 - - com.example - demo - 0.0.1-SNAPSHOT - jar - - demo - Demo project for Spring Boot - - - org.springframework.boot - spring-boot-starter-parent - 1.4.2.RELEASE - - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-web - - - - com.h2database - h2 - runtime - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - + + 4.0.0 + + com.example + demo + 0.0.1-SNAPSHOT + jar + + demo + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 2.5.2 + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + com.h2database + h2 + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + junit + junit + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/src/main/java/com/example/service/ToDoService.java b/src/main/java/com/example/service/ToDoService.java index d92ad63..982d3d8 100644 --- a/src/main/java/com/example/service/ToDoService.java +++ b/src/main/java/com/example/service/ToDoService.java @@ -2,11 +2,12 @@ import java.util.List; +import com.example.exception.ToDoException; import com.example.model.ToDo; public interface ToDoService { public List getAllToDo(); - public ToDo getToDoById(long id); + public ToDo getToDoById(long id) throws ToDoException; public ToDo saveToDo(ToDo todo); public void removeToDo(ToDo todo); } diff --git a/src/main/java/com/example/service/ToDoServiceImpl.java b/src/main/java/com/example/service/ToDoServiceImpl.java index c7061e2..33eb9c0 100644 --- a/src/main/java/com/example/service/ToDoServiceImpl.java +++ b/src/main/java/com/example/service/ToDoServiceImpl.java @@ -2,6 +2,7 @@ import java.util.List; +import com.example.exception.ToDoException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -20,8 +21,8 @@ public List getAllToDo() { } @Override - public ToDo getToDoById(long id) { - return toDoRepository.findOne(id); + public ToDo getToDoById(long id) throws ToDoException { + return toDoRepository.findById(id).orElseThrow(() -> new ToDoException("Entity not found")); } @Override @@ -33,6 +34,4 @@ public ToDo saveToDo(ToDo todo) { public void removeToDo(ToDo todo) { toDoRepository.delete(todo); } - - } diff --git a/src/test/java/com/example/service/ToDoServiceTest.java b/src/test/java/com/example/service/ToDoServiceTest.java index cd3348d..5053554 100644 --- a/src/test/java/com/example/service/ToDoServiceTest.java +++ b/src/test/java/com/example/service/ToDoServiceTest.java @@ -1,13 +1,8 @@ package com.example.service; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.List; - +import com.example.exception.ToDoException; +import com.example.model.ToDo; +import com.example.repository.ToDoRepository; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -16,63 +11,67 @@ import org.mockito.MockitoAnnotations; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.example.model.ToDo; -import com.example.repository.ToDoRepository; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.*; @RunWith(SpringJUnit4ClassRunner.class) public class ToDoServiceTest { - - @Mock - private ToDoRepository toDoRepository; - - @InjectMocks - private ToDoServiceImpl toDoService; - - @Before - public void setup(){ - MockitoAnnotations.initMocks(this); - } - - @Test - public void testGetAllToDo(){ - List toDoList = new ArrayList(); - toDoList.add(new ToDo(1,"Todo Sample 1",true)); - toDoList.add(new ToDo(2,"Todo Sample 2",true)); - toDoList.add(new ToDo(3,"Todo Sample 3",false)); - when(toDoRepository.findAll()).thenReturn(toDoList); - - List result = toDoService.getAllToDo(); - assertEquals(3, result.size()); - } - - @Test - public void testGetToDoById(){ - ToDo toDo = new ToDo(1,"Todo Sample 1",true); - when(toDoRepository.findOne(1L)).thenReturn(toDo); - ToDo result = toDoService.getToDoById(1); - assertEquals(1, result.getId()); - assertEquals("Todo Sample 1", result.getText()); - assertEquals(true, result.isCompleted()); - } - - @Test - public void saveToDo(){ - ToDo toDo = new ToDo(8,"Todo Sample 8",true); - when(toDoRepository.save(toDo)).thenReturn(toDo); - ToDo result = toDoService.saveToDo(toDo); - assertEquals(8, result.getId()); - assertEquals("Todo Sample 8", result.getText()); - assertEquals(true, result.isCompleted()); - } - - @Test - public void removeToDo(){ - ToDo toDo = new ToDo(8,"Todo Sample 8",true); - toDoService.removeToDo(toDo); + + @Mock + private ToDoRepository toDoRepository; + + @InjectMocks + private ToDoServiceImpl toDoService; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testGetAllToDo() { + List toDoList = new ArrayList(); + toDoList.add(new ToDo(1, "Todo Sample 1", true)); + toDoList.add(new ToDo(2, "Todo Sample 2", true)); + toDoList.add(new ToDo(3, "Todo Sample 3", false)); + when(toDoRepository.findAll()).thenReturn(toDoList); + + List result = toDoService.getAllToDo(); + assertEquals(3, result.size()); + } + + @Test + public void testGetToDoById() throws ToDoException { + ToDo toDo = new ToDo(1, "Todo Sample 1", true); + when(toDoRepository.findById(1L)).thenReturn(Optional.of(toDo)); + ToDo result = toDoService.getToDoById(1); + assertEquals(1, result.getId()); + assertEquals("Todo Sample 1", result.getText()); + assertTrue(result.isCompleted()); + } + + @Test + public void saveToDo() { + ToDo toDo = new ToDo(8, "Todo Sample 8", true); + when(toDoRepository.save(toDo)).thenReturn(toDo); + ToDo result = toDoService.saveToDo(toDo); + assertEquals(8, result.getId()); + assertEquals("Todo Sample 8", result.getText()); + assertTrue(result.isCompleted()); + } + + @Test + public void removeToDo() { + ToDo toDo = new ToDo(8, "Todo Sample 8", true); + toDoService.removeToDo(toDo); verify(toDoRepository, times(1)).delete(toDo); - } - - + } + } From 80f39825bb4c6683e293ddc457a3b9c0efbfe21e Mon Sep 17 00:00:00 2001 From: Yatin Date: Sun, 27 Jun 2021 11:20:20 +0530 Subject: [PATCH 59/67] master: fixing testcases --- .gitignore | 77 ++++++ pom.xml | 29 +- .../com/example/DemoApplicationTests.java | 14 +- .../com/example/service/ToDoServiceTest.java | 18 +- .../example/util/PayloadValidatorTest.java | 31 +-- .../com/example/web/ToDoControllerTest.java | 254 +++++++++--------- 6 files changed, 258 insertions(+), 165 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99b9d55 --- /dev/null +++ b/.gitignore @@ -0,0 +1,77 @@ +# Created by .ignore support plugin (hsz.mobi) +### Maven template +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +### Java template +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/workspace.xml +.idea/tasks.xml +.idea/dictionaries +.idea/vcs.xml +.idea/jsLibraryMappings.xml + +# Sensitive or high-churn files: +.idea/dataSources.ids +.idea/dataSources.xml +.idea/dataSources.local.xml +.idea/sqlDataSources.xml +.idea/dynamic.xml +.idea/uiDesigner.xml + +# Gradle: +.idea/gradle.xml +.idea/libraries + +# Mongo Explorer plugin: +.idea/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +.idea/ +*.iml + +/main/src/main/resources/doc/* + +*.project +*.classpath diff --git a/pom.xml b/pom.xml index 9c2a635..92d0e3d 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,9 @@ UTF-8 UTF-8 1.8 + ${java.version} + ${java.version} + 5.3.1 @@ -45,8 +48,15 @@ test - junit - junit + org.junit.jupiter + junit-jupiter-api + ${junit-platform.version} + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit-platform.version} test @@ -57,6 +67,21 @@ org.springframework.boot spring-boot-maven-plugin + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.0 + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.22.0 + diff --git a/src/test/java/com/example/DemoApplicationTests.java b/src/test/java/com/example/DemoApplicationTests.java index 5ad5ac2..1250511 100644 --- a/src/test/java/com/example/DemoApplicationTests.java +++ b/src/test/java/com/example/DemoApplicationTests.java @@ -1,16 +1,16 @@ package com.example; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest public class DemoApplicationTests { - @Test - public void contextLoads() { - } + @Test + public void contextLoads() { + } } diff --git a/src/test/java/com/example/service/ToDoServiceTest.java b/src/test/java/com/example/service/ToDoServiceTest.java index 5053554..c16059b 100644 --- a/src/test/java/com/example/service/ToDoServiceTest.java +++ b/src/test/java/com/example/service/ToDoServiceTest.java @@ -3,23 +3,23 @@ import com.example.exception.ToDoException; import com.example.model.ToDo; import com.example.repository.ToDoRepository; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import java.util.ArrayList; import java.util.List; import java.util.Optional; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.*; -@RunWith(SpringJUnit4ClassRunner.class) +@ExtendWith(SpringExtension.class) public class ToDoServiceTest { @Mock @@ -28,7 +28,7 @@ public class ToDoServiceTest { @InjectMocks private ToDoServiceImpl toDoService; - @Before + @BeforeEach public void setup() { MockitoAnnotations.initMocks(this); } @@ -71,7 +71,5 @@ public void removeToDo() { toDoService.removeToDo(toDo); verify(toDoRepository, times(1)).delete(toDo); } - - } diff --git a/src/test/java/com/example/util/PayloadValidatorTest.java b/src/test/java/com/example/util/PayloadValidatorTest.java index 8e35155..3edc9ca 100644 --- a/src/test/java/com/example/util/PayloadValidatorTest.java +++ b/src/test/java/com/example/util/PayloadValidatorTest.java @@ -1,25 +1,22 @@ package com.example.util; -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - import com.example.model.ToDo; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; public class PayloadValidatorTest { - @Test - public void validatePayLoad() { - ToDo toDo = new ToDo(1, "Sample ToDo 1", true); - assertEquals(false, PayloadValidator.validateCreatePayload(toDo)); - } - - @Test - public void validateInvalidPayLoad() { - ToDo toDo = new ToDo(0, "Sample ToDo 1", true); - assertEquals(true, PayloadValidator.validateCreatePayload(toDo)); - } - - + @Test + public void validatePayLoad() { + ToDo toDo = new ToDo(1, "Sample ToDo 1", true); + assertFalse(PayloadValidator.validateCreatePayload(toDo)); + } + @Test + public void validateInvalidPayLoad() { + ToDo toDo = new ToDo(0, "Sample ToDo 1", true); + assertTrue(PayloadValidator.validateCreatePayload(toDo)); + } } diff --git a/src/test/java/com/example/web/ToDoControllerTest.java b/src/test/java/com/example/web/ToDoControllerTest.java index de868ee..07d4f4c 100644 --- a/src/test/java/com/example/web/ToDoControllerTest.java +++ b/src/test/java/com/example/web/ToDoControllerTest.java @@ -1,151 +1,147 @@ package com.example.web; -import static org.hamcrest.collection.IsCollectionWithSize.hasSize; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; - -import org.junit.Before; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; +import com.example.DemoApplication; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import com.example.DemoApplication; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -@RunWith(SpringJUnit4ClassRunner.class) +@ExtendWith(SpringExtension.class) @ContextConfiguration(classes = DemoApplication.class) @SpringBootTest -@FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ToDoControllerTest { - private MockMvc mockMvc; - - @Autowired + private MockMvc mockMvc; + + @Autowired private WebApplicationContext wac; - @Before - public void setup() { + @BeforeEach + public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); - } - - @Test - public void verifyAllToDoList() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/todo").accept(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$", hasSize(4))).andDo(print()); - } - - @Test - public void verifyToDoById() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/todo/3").accept(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.id").exists()) - .andExpect(jsonPath("$.text").exists()) - .andExpect(jsonPath("$.completed").exists()) - .andExpect(jsonPath("$.id").value(3)) - .andExpect(jsonPath("$.text").value("Build the artifacts")) - .andExpect(jsonPath("$.completed").value(false)) - .andDo(print()); - } - - @Test - public void verifyInvalidToDoArgument() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/todo/f").accept(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.errorCode").value(400)) - .andExpect(jsonPath("$.message").value("The request could not be understood by the server due to malformed syntax.")) - .andDo(print()); - } - - @Test - public void verifyInvalidToDoId() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/todo/0").accept(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.errorCode").value(404)) - .andExpect(jsonPath("$.message").value("ToDo doesn´t exist")) - .andDo(print()); - } - - @Test - public void verifyNullToDo() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/todo/6").accept(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.errorCode").value(404)) - .andExpect(jsonPath("$.message").value("ToDo doesn´t exist")) - .andDo(print()); - } - - @Test - public void verifyDeleteToDo() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.delete("/todo/4").accept(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.status").value(200)) - .andExpect(jsonPath("$.message").value("ToDo has been deleted")) - .andDo(print()); - } - - @Test - public void verifyInvalidToDoIdToDelete() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.delete("/todo/9").accept(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.errorCode").value(404)) - .andExpect(jsonPath("$.message").value("ToDo to delete doesn´t exist")) - .andDo(print()); - } - - - @Test - public void verifySaveToDo() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.post("/todo/") - .contentType(MediaType.APPLICATION_JSON) - .content("{\"text\" : \"New ToDo Sample\", \"completed\" : \"false\" }") - .accept(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.id").exists()) - .andExpect(jsonPath("$.text").exists()) - .andExpect(jsonPath("$.completed").exists()) - .andExpect(jsonPath("$.text").value("New ToDo Sample")) - .andExpect(jsonPath("$.completed").value(false)) - .andDo(print()); - } - - @Test - public void verifyMalformedSaveToDo() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.post("/todo/") - .contentType(MediaType.APPLICATION_JSON) - .content("{ \"id\": \"8\", \"text\" : \"New ToDo Sample\", \"completed\" : \"false\" }") - .accept(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.errorCode").value(404)) - .andExpect(jsonPath("$.message").value("Payload malformed, id must not be defined")) - .andDo(print()); - } - - @Test - public void verifyUpdateToDo() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.patch("/todo/") - .contentType(MediaType.APPLICATION_JSON) - .content("{ \"id\": \"1\", \"text\" : \"New ToDo Text\", \"completed\" : \"false\" }") - .accept(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.id").exists()) - .andExpect(jsonPath("$.text").exists()) - .andExpect(jsonPath("$.completed").exists()) - .andExpect(jsonPath("$.id").value(1)) - .andExpect(jsonPath("$.text").value("New ToDo Text")) - .andExpect(jsonPath("$.completed").value(false)) - .andDo(print()); - } - - @Test - public void verifyInvalidToDoUpdate() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.patch("/todo/") - .contentType(MediaType.APPLICATION_JSON) - .content("{ \"idd\": \"8\", \"text\" : \"New ToDo Sample\", \"completed\" : \"false\" }") - .accept(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.errorCode").value(404)) - .andExpect(jsonPath("$.message").value("ToDo to update doesn´t exist")) - .andDo(print()); - } + } + + @Test + public void verifyAllToDoList() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/todo").accept(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$", hasSize(4))).andDo(print()); + } + + @Test + public void verifyToDoById() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/todo/3").accept(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.id").exists()) + .andExpect(jsonPath("$.text").exists()) + .andExpect(jsonPath("$.completed").exists()) + .andExpect(jsonPath("$.id").value(3)) + .andExpect(jsonPath("$.text").value("Build the artifacts")) + .andExpect(jsonPath("$.completed").value(false)) + .andDo(print()); + } + + @Test + public void verifyInvalidToDoArgument() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/todo/f").accept(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.errorCode").value(400)) + .andExpect(jsonPath("$.message").value("The request could not be understood by the server due to malformed syntax.")) + .andDo(print()); + } + + @Test + public void verifyInvalidToDoId() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/todo/0").accept(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.errorCode").value(404)) + .andExpect(jsonPath("$.message").value("Entity not found")) + .andDo(print()); + } + + @Test + public void verifyNullToDo() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/todo/6").accept(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.errorCode").value(404)) + .andExpect(jsonPath("$.message").value("Entity not found")) + .andDo(print()); + } + + @Test + public void verifyDeleteToDo() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.delete("/todo/4").accept(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.status").value(200)) + .andExpect(jsonPath("$.message").value("ToDo has been deleted")) + .andDo(print()); + } + + @Test + public void verifyInvalidToDoIdToDelete() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.delete("/todo/9").accept(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.errorCode").value(404)) + .andExpect(jsonPath("$.message").value("Entity not found")) + .andDo(print()); + } + + + @Test + public void verifySaveToDo() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.post("/todo/") + .contentType(MediaType.APPLICATION_JSON) + .content("{\"text\" : \"New ToDo Sample\", \"completed\" : \"false\" }") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.id").exists()) + .andExpect(jsonPath("$.text").exists()) + .andExpect(jsonPath("$.completed").exists()) + .andExpect(jsonPath("$.text").value("New ToDo Sample")) + .andExpect(jsonPath("$.completed").value(false)) + .andDo(print()); + } + + @Test + public void verifyMalformedSaveToDo() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.post("/todo/") + .contentType(MediaType.APPLICATION_JSON) + .content("{ \"id\": \"8\", \"text\" : \"New ToDo Sample\", \"completed\" : \"false\" }") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.errorCode").value(404)) + .andExpect(jsonPath("$.message").value("Payload malformed, id must not be defined")) + .andDo(print()); + } + + @Test + public void verifyUpdateToDo() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.patch("/todo/") + .contentType(MediaType.APPLICATION_JSON) + .content("{ \"id\": \"1\", \"text\" : \"New ToDo Text\", \"completed\" : \"false\" }") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.id").exists()) + .andExpect(jsonPath("$.text").exists()) + .andExpect(jsonPath("$.completed").exists()) + .andExpect(jsonPath("$.id").value(1)) + .andExpect(jsonPath("$.text").value("New ToDo Text")) + .andExpect(jsonPath("$.completed").value(false)) + .andDo(print()); + } + + @Test + public void verifyInvalidToDoUpdate() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.patch("/todo/") + .contentType(MediaType.APPLICATION_JSON) + .content("{ \"idd\": \"8\", \"text\" : \"New ToDo Sample\", \"completed\" : \"false\" }") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.errorCode").value(404)) + .andExpect(jsonPath("$.message").value("Entity not found")) + .andDo(print()); + } } From 940eff871d4202f424a542e1e7ad9b6e652b380a Mon Sep 17 00:00:00 2001 From: Yatin Date: Sun, 27 Jun 2021 11:26:59 +0530 Subject: [PATCH 60/67] master: making code changes --- .../java/com/example/DemoApplication.java | 37 +++---- .../com/example/exception/ErrorResponse.java | 30 ++--- .../exception/RestExceptionHandler.java | 32 +++--- .../com/example/exception/ToDoException.java | 26 ++--- src/main/java/com/example/model/Response.java | 56 +++++----- src/main/java/com/example/model/ToDo.java | 98 +++++++++-------- .../example/repository/ToDoRepository.java | 5 +- .../java/com/example/service/ToDoService.java | 15 ++- .../com/example/service/ToDoServiceImpl.java | 55 +++++----- .../com/example/util/PayloadValidator.java | 9 +- .../java/com/example/web/ToDoController.java | 103 +++++++++--------- src/main/resources/application.properties | 0 src/main/resources/application.yml | 23 ++++ 13 files changed, 253 insertions(+), 236 deletions(-) delete mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/application.yml diff --git a/src/main/java/com/example/DemoApplication.java b/src/main/java/com/example/DemoApplication.java index 6a9650f..399ad65 100644 --- a/src/main/java/com/example/DemoApplication.java +++ b/src/main/java/com/example/DemoApplication.java @@ -1,5 +1,7 @@ package com.example; +import com.example.model.ToDo; +import com.example.repository.ToDoRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.CommandLineRunner; @@ -7,26 +9,23 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; -import com.example.model.ToDo; -import com.example.repository.ToDoRepository; - @SpringBootApplication public class DemoApplication { - - private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class); - public static void main(String[] args) { - SpringApplication.run(DemoApplication.class, args); - } - - @Bean - public CommandLineRunner setup(ToDoRepository toDoRepository) { - return (args) -> { - toDoRepository.save(new ToDo("Remove unused imports", true)); - toDoRepository.save(new ToDo("Clean the code", true)); - toDoRepository.save(new ToDo("Build the artifacts", false)); - toDoRepository.save(new ToDo("Deploy the jar file", true)); - logger.info("The sample data has been generated"); - }; - } + private static final Logger logger = LoggerFactory.getLogger(DemoApplication.class); + + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } + + @Bean + public CommandLineRunner setup(ToDoRepository toDoRepository) { + return (args) -> { + toDoRepository.save(new ToDo("Remove unused imports", true)); + toDoRepository.save(new ToDo("Clean the code", true)); + toDoRepository.save(new ToDo("Build the artifacts", false)); + toDoRepository.save(new ToDo("Deploy the jar file", true)); + logger.info("The sample data has been generated"); + }; + } } diff --git a/src/main/java/com/example/exception/ErrorResponse.java b/src/main/java/com/example/exception/ErrorResponse.java index 7e42ea3..ff06504 100644 --- a/src/main/java/com/example/exception/ErrorResponse.java +++ b/src/main/java/com/example/exception/ErrorResponse.java @@ -1,23 +1,23 @@ package com.example.exception; public class ErrorResponse { - - private int errorCode; - private String message; - public int getErrorCode() { - return errorCode; - } + private int errorCode; + private String message; - public void setErrorCode(int errorCode) { - this.errorCode = errorCode; - } + public int getErrorCode() { + return errorCode; + } - public String getMessage() { - return message; - } + public void setErrorCode(int errorCode) { + this.errorCode = errorCode; + } - public void setMessage(String message) { - this.message = message; - } + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } } \ No newline at end of file diff --git a/src/main/java/com/example/exception/RestExceptionHandler.java b/src/main/java/com/example/exception/RestExceptionHandler.java index c03dfd5..43d8a5d 100644 --- a/src/main/java/com/example/exception/RestExceptionHandler.java +++ b/src/main/java/com/example/exception/RestExceptionHandler.java @@ -7,21 +7,21 @@ @ControllerAdvice public class RestExceptionHandler { - - @ExceptionHandler(ToDoException.class) - public ResponseEntity exceptionToDoHandler(Exception ex) { - ErrorResponse error = new ErrorResponse(); - error.setErrorCode(HttpStatus.NOT_FOUND.value()); - error.setMessage(ex.getMessage()); - return new ResponseEntity(error, HttpStatus.NOT_FOUND); - } - - @ExceptionHandler(Exception.class) - public ResponseEntity exceptionHandler(Exception ex) { - ErrorResponse error = new ErrorResponse(); - error.setErrorCode(HttpStatus.BAD_REQUEST.value()); - error.setMessage("The request could not be understood by the server due to malformed syntax."); - return new ResponseEntity(error, HttpStatus.BAD_REQUEST); - } + + @ExceptionHandler(ToDoException.class) + public ResponseEntity exceptionToDoHandler(Exception ex) { + ErrorResponse error = new ErrorResponse(); + error.setErrorCode(HttpStatus.NOT_FOUND.value()); + error.setMessage(ex.getMessage()); + return new ResponseEntity(error, HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity exceptionHandler(Exception ex) { + ErrorResponse error = new ErrorResponse(); + error.setErrorCode(HttpStatus.BAD_REQUEST.value()); + error.setMessage("The request could not be understood by the server due to malformed syntax."); + return new ResponseEntity(error, HttpStatus.BAD_REQUEST); + } } diff --git a/src/main/java/com/example/exception/ToDoException.java b/src/main/java/com/example/exception/ToDoException.java index e2a2ac6..0f275df 100644 --- a/src/main/java/com/example/exception/ToDoException.java +++ b/src/main/java/com/example/exception/ToDoException.java @@ -1,20 +1,20 @@ package com.example.exception; public class ToDoException extends Exception { - - private static final long serialVersionUID = 1L; - private String errorMessage; - public String getErrorMessage() { - return errorMessage; - } + private static final long serialVersionUID = 1L; + private String errorMessage; - public ToDoException(String errorMessage) { - super(errorMessage); - this.errorMessage = errorMessage; - } + public ToDoException(String errorMessage) { + super(errorMessage); + this.errorMessage = errorMessage; + } - public ToDoException() { - super(); - } + public ToDoException() { + super(); + } + + public String getErrorMessage() { + return errorMessage; + } } diff --git a/src/main/java/com/example/model/Response.java b/src/main/java/com/example/model/Response.java index b8bca06..543fbb6 100644 --- a/src/main/java/com/example/model/Response.java +++ b/src/main/java/com/example/model/Response.java @@ -2,33 +2,33 @@ public class Response { - private int status; - private String message; - - public Response() { - super(); - } - - public Response(int status, String message) { - super(); - this.status = status; - this.message = message; - } - - public int getStatus() { - return status; - } - - public void setStatus(int status) { - this.status = status; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } + private int status; + private String message; + + public Response() { + super(); + } + + public Response(int status, String message) { + super(); + this.status = status; + this.message = message; + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } } diff --git a/src/main/java/com/example/model/ToDo.java b/src/main/java/com/example/model/ToDo.java index 0820ed1..1a6342a 100644 --- a/src/main/java/com/example/model/ToDo.java +++ b/src/main/java/com/example/model/ToDo.java @@ -6,53 +6,55 @@ @Entity public class ToDo { - - @Id - @GeneratedValue - private long id; - private String text; - private boolean completed; - - public ToDo() { - super(); - } - - - public ToDo(long id, String text, boolean completed) { - super(); - this.id = id; - this.text = text; - this.completed = completed; - } - - - public ToDo(String text, boolean completed) { - super(); - this.text = text; - this.completed = completed; - } - public long getId() { - return id; - } - public void setId(long id) { - this.id = id; - } - - public String getText() { - return text; - } - public void setText(String text) { - this.text = text; - } - public boolean isCompleted() { - return completed; - } - public void setCompleted(boolean completed) { - this.completed = completed; - } - - - - + + @Id + @GeneratedValue + private long id; + private String text; + private boolean completed; + + public ToDo() { + super(); + } + + + public ToDo(long id, String text, boolean completed) { + super(); + this.id = id; + this.text = text; + this.completed = completed; + } + + + public ToDo(String text, boolean completed) { + super(); + this.text = text; + this.completed = completed; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public boolean isCompleted() { + return completed; + } + + public void setCompleted(boolean completed) { + this.completed = completed; + } + } diff --git a/src/main/java/com/example/repository/ToDoRepository.java b/src/main/java/com/example/repository/ToDoRepository.java index 4473b41..1e03409 100644 --- a/src/main/java/com/example/repository/ToDoRepository.java +++ b/src/main/java/com/example/repository/ToDoRepository.java @@ -1,11 +1,10 @@ package com.example.repository; +import com.example.model.ToDo; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import com.example.model.ToDo; - @Repository("toDoRepository") -public interface ToDoRepository extends JpaRepository{ +public interface ToDoRepository extends JpaRepository { } diff --git a/src/main/java/com/example/service/ToDoService.java b/src/main/java/com/example/service/ToDoService.java index 982d3d8..3d0fcc7 100644 --- a/src/main/java/com/example/service/ToDoService.java +++ b/src/main/java/com/example/service/ToDoService.java @@ -1,13 +1,16 @@ package com.example.service; -import java.util.List; - import com.example.exception.ToDoException; import com.example.model.ToDo; +import java.util.List; + public interface ToDoService { - public List getAllToDo(); - public ToDo getToDoById(long id) throws ToDoException; - public ToDo saveToDo(ToDo todo); - public void removeToDo(ToDo todo); + List getAllToDo(); + + ToDo getToDoById(long id) throws ToDoException; + + ToDo saveToDo(ToDo todo); + + void removeToDo(ToDo todo); } diff --git a/src/main/java/com/example/service/ToDoServiceImpl.java b/src/main/java/com/example/service/ToDoServiceImpl.java index 33eb9c0..4e17182 100644 --- a/src/main/java/com/example/service/ToDoServiceImpl.java +++ b/src/main/java/com/example/service/ToDoServiceImpl.java @@ -1,37 +1,36 @@ package com.example.service; -import java.util.List; - import com.example.exception.ToDoException; +import com.example.model.ToDo; +import com.example.repository.ToDoRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import com.example.model.ToDo; -import com.example.repository.ToDoRepository; +import java.util.List; @Service("toDoService") -public class ToDoServiceImpl implements ToDoService{ - - @Autowired - private ToDoRepository toDoRepository; - - @Override - public List getAllToDo() { - return toDoRepository.findAll(); - } - - @Override - public ToDo getToDoById(long id) throws ToDoException { - return toDoRepository.findById(id).orElseThrow(() -> new ToDoException("Entity not found")); - } - - @Override - public ToDo saveToDo(ToDo todo) { - return toDoRepository.save(todo); - } - - @Override - public void removeToDo(ToDo todo) { - toDoRepository.delete(todo); - } +public class ToDoServiceImpl implements ToDoService { + + @Autowired + private ToDoRepository toDoRepository; + + @Override + public List getAllToDo() { + return toDoRepository.findAll(); + } + + @Override + public ToDo getToDoById(long id) throws ToDoException { + return toDoRepository.findById(id).orElseThrow(() -> new ToDoException("Entity not found")); + } + + @Override + public ToDo saveToDo(ToDo todo) { + return toDoRepository.save(todo); + } + + @Override + public void removeToDo(ToDo todo) { + toDoRepository.delete(todo); + } } diff --git a/src/main/java/com/example/util/PayloadValidator.java b/src/main/java/com/example/util/PayloadValidator.java index c7b7814..50c47cd 100644 --- a/src/main/java/com/example/util/PayloadValidator.java +++ b/src/main/java/com/example/util/PayloadValidator.java @@ -3,12 +3,9 @@ import com.example.model.ToDo; public class PayloadValidator { - - public static boolean validateCreatePayload(ToDo toDo){ - if (toDo.getId() > 0){ - return false; - } - return true; + + public static boolean validateCreatePayload(ToDo toDo) { + return toDo.getId() <= 0; } } diff --git a/src/main/java/com/example/web/ToDoController.java b/src/main/java/com/example/web/ToDoController.java index 483a783..ac4a691 100644 --- a/src/main/java/com/example/web/ToDoController.java +++ b/src/main/java/com/example/web/ToDoController.java @@ -1,76 +1,71 @@ package com.example.web; -import java.util.List; - +import com.example.exception.ToDoException; +import com.example.model.Response; +import com.example.model.ToDo; +import com.example.service.ToDoService; +import com.example.util.PayloadValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; -import com.example.exception.ToDoException; -import com.example.model.Response; -import com.example.model.ToDo; -import com.example.service.ToDoService; -import com.example.util.PayloadValidator; +import java.util.List; @RestController public class ToDoController { - - private static final Logger logger = LoggerFactory.getLogger(ToDoController.class); - @Autowired - private ToDoService toDoService; - - @RequestMapping(value="/todo", method=RequestMethod.GET) - public ResponseEntity> getAllToDo(){ - logger.info("Returning all the ToDo´s"); - return new ResponseEntity>(toDoService.getAllToDo(), HttpStatus.OK); - } - + private static final Logger logger = LoggerFactory.getLogger(ToDoController.class); + + @Autowired + private ToDoService toDoService; + + // http://localhost:9800/todo + @RequestMapping(value = "/todo", method = RequestMethod.GET) + public ResponseEntity> getAllToDo() { + logger.info("Returning all the ToDo´s"); + return new ResponseEntity<>(toDoService.getAllToDo(), HttpStatus.OK); + } + @RequestMapping(value = "/todo/{id}", method = RequestMethod.GET) - public ResponseEntity getToDoById(@PathVariable("id") long id) throws ToDoException{ - logger.info("ToDo id to return " + id); - ToDo toDo = toDoService.getToDoById(id); - if (toDo == null || toDo.getId() <= 0){ + public ResponseEntity getToDoById(@PathVariable("id") long id) throws ToDoException { + logger.info("ToDo id to return " + id); + ToDo toDo = toDoService.getToDoById(id); + if (toDo == null || toDo.getId() <= 0) { throw new ToDoException("ToDo doesn´t exist"); - } - return new ResponseEntity(toDoService.getToDoById(id), HttpStatus.OK); - } + } + return new ResponseEntity<>(toDoService.getToDoById(id), HttpStatus.OK); + } @RequestMapping(value = "/todo/{id}", method = RequestMethod.DELETE) - public ResponseEntity removeToDoById(@PathVariable("id") long id) throws ToDoException{ - logger.info("ToDo id to remove " + id); - ToDo toDo = toDoService.getToDoById(id); - if (toDo == null || toDo.getId() <= 0){ + public ResponseEntity removeToDoById(@PathVariable("id") long id) throws ToDoException { + logger.info("ToDo id to remove " + id); + ToDo toDo = toDoService.getToDoById(id); + if (toDo == null || toDo.getId() <= 0) { throw new ToDoException("ToDo to delete doesn´t exist"); - } - toDoService.removeToDo(toDo); - return new ResponseEntity(new Response(HttpStatus.OK.value(), "ToDo has been deleted"), HttpStatus.OK); - } - + } + toDoService.removeToDo(toDo); + return new ResponseEntity<>(new Response(HttpStatus.OK.value(), "ToDo has been deleted"), HttpStatus.OK); + } + @RequestMapping(value = "/todo", method = RequestMethod.POST) - public ResponseEntity saveToDo(@RequestBody ToDo payload) throws ToDoException{ - logger.info("Payload to save " + payload); - if (!PayloadValidator.validateCreatePayload(payload)){ + public ResponseEntity saveToDo(@RequestBody ToDo payload) throws ToDoException { + logger.info("Payload to save " + payload); + if (!PayloadValidator.validateCreatePayload(payload)) { throw new ToDoException("Payload malformed, id must not be defined"); - } - return new ResponseEntity(toDoService.saveToDo(payload), HttpStatus.OK); - } - + } + return new ResponseEntity<>(toDoService.saveToDo(payload), HttpStatus.OK); + } + @RequestMapping(value = "/todo", method = RequestMethod.PATCH) - public ResponseEntity updateToDo(@RequestBody ToDo payload) throws ToDoException{ - logger.info("Payload to update " + payload); - ToDo toDo = toDoService.getToDoById(payload.getId()); - if (toDo == null || toDo.getId() <= 0){ + public ResponseEntity updateToDo(@RequestBody ToDo payload) throws ToDoException { + logger.info("Payload to update " + payload); + ToDo toDo = toDoService.getToDoById(payload.getId()); + if (toDo == null || toDo.getId() <= 0) { throw new ToDoException("ToDo to update doesn´t exist"); - } - return new ResponseEntity(toDoService.saveToDo(payload), HttpStatus.OK); - } - + } + return new ResponseEntity<>(toDoService.saveToDo(payload), HttpStatus.OK); + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..751dffa --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,23 @@ +server: + error: + include-stacktrace: never + port: 9800 +spring: + application: + name: versioning-restful-services + datasource: + driverClassName: org.h2.Driver + password: '' + url: 'jdbc:h2:mem:testdb' + username: sa + h2: + console: + enabled: true + path: /h2-console + jpa: + database-platform: org.hibernate.dialect.H2Dialect + hibernate: + ddl-auto: create-drop + properties: + hibernate: + show_sql: true From e694f41bff266253cc5c0c44449a95e61a94f855 Mon Sep 17 00:00:00 2001 From: Yatin Date: Sun, 27 Jun 2021 15:44:07 +0530 Subject: [PATCH 61/67] master: adding the jenkins file --- Jenkinsfile | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Jenkinsfile diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..c0a55d6 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,67 @@ +node('master') { + try { + properties([ + buildDiscarder(logRotator(daysToKeepStr: '3', numToKeepStr: '3', artifactDaysToKeepStr: '15', artifactNumToKeepStr: '5')), + disableConcurrentBuilds() + ]) + + stage("Checkout project") { + echo "Checking out the project" + try { + checkout scm + } catch (Exception e) { + throw e + } + } + + stage("Running testcases") { + echo "Running the testcases" + sh "mvn test" + } + + stage("Bundling project") { + echo "Bundling the project" + sh "mvn clean install" + } + + stage("Deploying to nexus") { + echo "Deploying the bundled jar to the nexus" + } + + stage("Sonar analyzer") { + echo "Running sonar analyzer" + } + + stage("Running vulnerability check") { + echo "Running the vulnerability step" + } + + stage("Building docker image") { + echo "Performing docker setup" + } + + stage("Tag and publish to hub") { + echo "Tagging the docker image and pushing it to the hub" + } + + stage("Removing local image") { + echo "Clearing the work directory" + } + + stage("Performing deployment") { + echo "Performing the deployment" + } + + stage("Wipe out directory") { + echo "Wiping out the working directory" + } + + stage("Post action") { + echo "Performing the post action step. Setup email or slack notifications" + } + } + catch (Exception e) { + throw e + } +} + From bdcc4425ecf9313cb900cc43e0546f552350a478 Mon Sep 17 00:00:00 2001 From: Yatin Date: Sun, 27 Jun 2021 16:21:16 +0530 Subject: [PATCH 62/67] master: adding the vulnerability step check --- Jenkinsfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Jenkinsfile b/Jenkinsfile index c0a55d6..87e41e6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -34,6 +34,8 @@ node('master') { stage("Running vulnerability check") { echo "Running the vulnerability step" + // sh "mvn dependency-check:aggregate -DsuppressionFile.path=suppressions.xml" + sh "mvn dependency-check:aggregate" } stage("Building docker image") { From 2ef8a6fc2ee74a1fae89f0dbaf028748bbfa63b2 Mon Sep 17 00:00:00 2001 From: Yatin Date: Sun, 27 Jun 2021 16:24:52 +0530 Subject: [PATCH 63/67] master: adding the vulnerability changes --- pom.xml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pom.xml b/pom.xml index 92d0e3d..f4a4bb6 100644 --- a/pom.xml +++ b/pom.xml @@ -83,5 +83,18 @@ 2.22.0 + + + + + + org.owasp + dependency-check-maven + + false + + + + From e7fd903743183dd6362e2ec1a740fdc391348639 Mon Sep 17 00:00:00 2001 From: Yatin Date: Sun, 27 Jun 2021 16:32:47 +0530 Subject: [PATCH 64/67] master: adding the jenkins declarative file changes --- Jenkinsfile_declarative | 20 ++++++++++++++++++++ pom.xml | 2 ++ 2 files changed, 22 insertions(+) create mode 100644 Jenkinsfile_declarative diff --git a/Jenkinsfile_declarative b/Jenkinsfile_declarative new file mode 100644 index 0000000..97297e2 --- /dev/null +++ b/Jenkinsfile_declarative @@ -0,0 +1,20 @@ +pipeline { + agent any + + triggers { + pollSCM '* * * * *' + } + + stages { + stage('Running testcases') { + steps { + sh "mvn test" + } + } + stage('Bundling project') { + steps { + sh "mvn clean install" + } + } + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index f4a4bb6..09a00bc 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,7 @@ maven-compiler-plugin 3.8.0 + org.apache.maven.plugins maven-surefire-plugin @@ -85,6 +86,7 @@ + From 81c196eaa506a8d93d5d9b1b9a71f55370da2e25 Mon Sep 17 00:00:00 2001 From: Yatin Date: Sun, 27 Jun 2021 16:36:19 +0530 Subject: [PATCH 65/67] master: adding build discarder in jenkins declarative pipeline --- Jenkinsfile_declarative | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile_declarative b/Jenkinsfile_declarative index 97297e2..3bc2172 100644 --- a/Jenkinsfile_declarative +++ b/Jenkinsfile_declarative @@ -1,8 +1,12 @@ pipeline { agent any + options { + buildDiscarder(logRotator(daysToKeepStr: '3', numToKeepStr: '3', artifactDaysToKeepStr: '15', artifactNumToKeepStr: '5')) + } + triggers { - pollSCM '* * * * *' + pollSCM 'H * * * *' } stages { From 6dc7698e7b1bf3299dd3e108e6560662b6d2d53c Mon Sep 17 00:00:00 2001 From: Yatin Date: Sat, 3 Jul 2021 10:30:24 +0530 Subject: [PATCH 66/67] Creating develop branch without change From 6526daadf04de9a96c66eb27228ed02962c00ecb Mon Sep 17 00:00:00 2001 From: Yatin Batra Date: Tue, 17 Aug 2021 17:08:30 +0530 Subject: [PATCH 67/67] develop - making changes to jenkinsfile --- Jenkinsfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 87e41e6..4109dd3 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,3 +1,5 @@ +//scripted jenkins file + node('master') { try { properties([ @@ -35,7 +37,7 @@ node('master') { stage("Running vulnerability check") { echo "Running the vulnerability step" // sh "mvn dependency-check:aggregate -DsuppressionFile.path=suppressions.xml" - sh "mvn dependency-check:aggregate" + // sh "mvn dependency-check:aggregate" } stage("Building docker image") {