From a723c89c6268739d134e1dd9b90b76bd1179c69c Mon Sep 17 00:00:00 2001 From: econwar Date: Fri, 26 Apr 2019 14:57:47 +0000 Subject: [PATCH] Add syntax check before pushing code Checks whole file (with changes) for code style violations If you can't fix these violations you can use the --no-verify option Issue-ID: DMAAP-1173 Change-Id: If97f90685836cec7fa3febb450f6f8ce844272f3 Signed-off-by: econwar --- ci_scripts/onap-style-java.xml | 225 +++++++++++++++++++++ ci_scripts/pre-commit.sh | 56 +++++ ci_scripts/suppressedFilesForStyleCheck.xml | 27 +++ .../datarouter/provisioning/FeedServletTest.java | 13 -- 4 files changed, 308 insertions(+), 13 deletions(-) create mode 100644 ci_scripts/onap-style-java.xml create mode 100755 ci_scripts/pre-commit.sh create mode 100644 ci_scripts/suppressedFilesForStyleCheck.xml diff --git a/ci_scripts/onap-style-java.xml b/ci_scripts/onap-style-java.xml new file mode 100644 index 00000000..7f9e4710 --- /dev/null +++ b/ci_scripts/onap-style-java.xml @@ -0,0 +1,225 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ci_scripts/pre-commit.sh b/ci_scripts/pre-commit.sh new file mode 100755 index 00000000..636f8ff3 --- /dev/null +++ b/ci_scripts/pre-commit.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# +# ============LICENSE_START======================================================= +# Copyright (C) 2019 Nordix Foundation. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END========================================================= +# +# Pre-commit hook for running checkstyle on changed Java sources +# +# To use this you need: +# 1. Checkstyle's jar file downloaded *version is important checkstyle-8.13-all.jar +# 2. To configure git: +# * git config --add checkstyle.jar +# 3. Copy this file to your .git/hooks directory as pre-commit +# +# Now, when you commit, you will be disallowed from doing so +# until you pass your checkstyle checks. + +changed_files=" " +for file in $(git diff --cached --name-status | grep -E '\.(java)$' | grep -vE '^D' | awk '{print $2}') +do + changed_files+="$file " +done + +printf "Using checkstyle sheet " +checkstlye_jar_command='git config --get checkstyle.jar' + +if ! ($checkstlye_jar_command) +then + printf "You must configure checkstyle in your git config" + exit 1 +fi + +checkstyle_warnings=$(java -jar $($checkstlye_jar_command) -c ci_scripts/onap-style-java.xml $changed_files | grep WARN) +if [ $? == 0 ] +then + printf "\nWarnings found\n\n" + echo "$checkstyle_warnings" + printf "\n###############################################################\n\nFix warnings before committing\n\n" + exit 1 +else + printf "\nCode checkstyle passed.\n" +fi diff --git a/ci_scripts/suppressedFilesForStyleCheck.xml b/ci_scripts/suppressedFilesForStyleCheck.xml new file mode 100644 index 00000000..ed56442a --- /dev/null +++ b/ci_scripts/suppressedFilesForStyleCheck.xml @@ -0,0 +1,27 @@ + + + + + + + \ No newline at end of file diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java index f4eac05f..a1f714bb 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java @@ -109,7 +109,6 @@ public class FeedServletTest extends DrServletTestBase { verifyEnteringExitCalled(listAppender); } - @Test public void Given_Request_Is_HTTP_DELETE_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception { @@ -118,7 +117,6 @@ public class FeedServletTest extends DrServletTestBase { verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); } - @Test public void Given_Request_Is_HTTP_DELETE_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn(null); @@ -126,7 +124,6 @@ public class FeedServletTest extends DrServletTestBase { verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); } - @Test public void Given_Request_Is_HTTP_DELETE_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/123"); @@ -134,7 +131,6 @@ public class FeedServletTest extends DrServletTestBase { verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } - @Test public void Given_Request_Is_HTTP_DELETE_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception { setAuthoriserToReturnRequestNotAuthorized(); @@ -158,7 +154,6 @@ public class FeedServletTest extends DrServletTestBase { verifyEnteringExitCalled(listAppender); } - @Test public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Fails_An_Internal_Server_Error_Is_Reported() throws Exception { @@ -172,7 +167,6 @@ public class FeedServletTest extends DrServletTestBase { .sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class))); } - @Test public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Succeeds_A_NO_CONTENT_Response_Is_Generated() throws Exception { feedServlet.doDelete(request, response); @@ -198,7 +192,6 @@ public class FeedServletTest extends DrServletTestBase { verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); } - @Test public void Given_Request_Is_HTTP_GET_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() throws Exception { @@ -207,7 +200,6 @@ public class FeedServletTest extends DrServletTestBase { verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); } - @Test public void Given_Request_Is_HTTP_GET_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception { @@ -216,7 +208,6 @@ public class FeedServletTest extends DrServletTestBase { verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } - @Test public void Given_Request_Is_HTTP_GET_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception { @@ -226,7 +217,6 @@ public class FeedServletTest extends DrServletTestBase { verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); } - @Test public void Given_Request_Is_HTTP_GET_And_Request_Succeeds() throws Exception { ServletOutputStream outStream = mock(ServletOutputStream.class); @@ -237,7 +227,6 @@ public class FeedServletTest extends DrServletTestBase { verifyEnteringExitCalled(listAppender); } - @Test public void Given_Request_Is_HTTP_PUT_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception { @@ -255,7 +244,6 @@ public class FeedServletTest extends DrServletTestBase { verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); } - @Test public void Given_Request_Is_HTTP_PUT_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() throws Exception { @@ -264,7 +252,6 @@ public class FeedServletTest extends DrServletTestBase { verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); } - @Test public void Given_Request_Is_HTTP_PUT_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception { -- 2.16.6