diff --git a/build.gradle b/build.gradle index ec45db35..2540fb8a 100644 --- a/build.gradle +++ b/build.gradle @@ -50,12 +50,13 @@ subprojects { version = rootProject.version java { - sourceCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 } compileJava { if (javaCompiler.get().metadata.languageVersion.canCompileOrRun(10)) { - options.release = 8 + options.release = 11 } } diff --git a/ci/checkstyle/checkstyle.xml b/ci/checkstyle/checkstyle.xml deleted file mode 100644 index 92b99a49..00000000 --- a/ci/checkstyle/checkstyle.xml +++ /dev/null @@ -1,108 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/ci/checkstyle/java-copyright-header.txt b/ci/checkstyle/java-copyright-header.txt deleted file mode 100644 index 5bc18a3d..00000000 --- a/ci/checkstyle/java-copyright-header.txt +++ /dev/null @@ -1,5 +0,0 @@ -^/\*$ -^ \* Copyright \d\d\d\d .*? All Rights Reserved\.$ -^ \*$ -^ \* SPDX-License-Identifier: Apache-2\.0$ -^ \*/$ \ No newline at end of file diff --git a/fabric-chaincode-docker/Dockerfile b/fabric-chaincode-docker/Dockerfile index 144af895..25fbde49 100644 --- a/fabric-chaincode-docker/Dockerfile +++ b/fabric-chaincode-docker/Dockerfile @@ -40,8 +40,8 @@ RUN gradle \ fabric-chaincode-shim:publishToMavenLocal \ -x javadoc \ -x test \ - -x checkstyleMain \ - -x checkstyleTest + -x pmdMain \ + -x pmdTest WORKDIR /root/chaincode-java # Run the Gradle and Maven commands to generate the wrapper variants diff --git a/fabric-chaincode-integration-test/src/contracts/bare-gradle/build.gradle b/fabric-chaincode-integration-test/src/contracts/bare-gradle/build.gradle index 4162e3ae..42a075f9 100644 --- a/fabric-chaincode-integration-test/src/contracts/bare-gradle/build.gradle +++ b/fabric-chaincode-integration-test/src/contracts/bare-gradle/build.gradle @@ -7,7 +7,12 @@ group 'org.hyperledger.fabric-chaincode-java' version '1.0-SNAPSHOT' java { - sourceCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 +} + +compileJava { + options.compilerArgs.addAll(['--release', '11']) } repositories { diff --git a/fabric-chaincode-integration-test/src/contracts/bare-maven/pom.xml b/fabric-chaincode-integration-test/src/contracts/bare-maven/pom.xml index fc475a98..9740748d 100644 --- a/fabric-chaincode-integration-test/src/contracts/bare-maven/pom.xml +++ b/fabric-chaincode-integration-test/src/contracts/bare-maven/pom.xml @@ -7,7 +7,7 @@ - 8 + 11 UTF-8 UTF-8 diff --git a/fabric-chaincode-integration-test/src/contracts/fabric-ledger-api/build.gradle b/fabric-chaincode-integration-test/src/contracts/fabric-ledger-api/build.gradle index 7d5ec564..a4d9619d 100644 --- a/fabric-chaincode-integration-test/src/contracts/fabric-ledger-api/build.gradle +++ b/fabric-chaincode-integration-test/src/contracts/fabric-ledger-api/build.gradle @@ -7,13 +7,12 @@ group 'org.hyperledger.fabric-chaincode-java' version '1.0-SNAPSHOT' java { - sourceCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 } compileJava { - if (javaCompiler.get().metadata.languageVersion.canCompileOrRun(10)) { - options.release = 8 - } + options.compilerArgs.addAll(['--release', '11']) } repositories { diff --git a/fabric-chaincode-integration-test/src/contracts/fabric-shim-api/build.gradle b/fabric-chaincode-integration-test/src/contracts/fabric-shim-api/build.gradle index a57bef8b..79ef721a 100644 --- a/fabric-chaincode-integration-test/src/contracts/fabric-shim-api/build.gradle +++ b/fabric-chaincode-integration-test/src/contracts/fabric-shim-api/build.gradle @@ -7,13 +7,12 @@ group 'org.hyperledger.fabric-chaincode-java' version '1.0-SNAPSHOT' java { - sourceCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 } compileJava { - if (javaCompiler.get().metadata.languageVersion.canCompileOrRun(10)) { - options.release = 8 - } + options.compilerArgs.addAll(['--release', '11']) } repositories { diff --git a/fabric-chaincode-integration-test/src/contracts/wrapper-maven/.mvn/wrapper/maven-wrapper.jar b/fabric-chaincode-integration-test/src/contracts/wrapper-maven/.mvn/wrapper/maven-wrapper.jar index bf82ff01..7967f30d 100644 Binary files a/fabric-chaincode-integration-test/src/contracts/wrapper-maven/.mvn/wrapper/maven-wrapper.jar and b/fabric-chaincode-integration-test/src/contracts/wrapper-maven/.mvn/wrapper/maven-wrapper.jar differ diff --git a/fabric-chaincode-integration-test/src/contracts/wrapper-maven/.mvn/wrapper/maven-wrapper.properties b/fabric-chaincode-integration-test/src/contracts/wrapper-maven/.mvn/wrapper/maven-wrapper.properties index dc3affce..9548abd8 100644 --- a/fabric-chaincode-integration-test/src/contracts/wrapper-maven/.mvn/wrapper/maven-wrapper.properties +++ b/fabric-chaincode-integration-test/src/contracts/wrapper-maven/.mvn/wrapper/maven-wrapper.properties @@ -6,7 +6,7 @@ # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # -# https://www.apache.org/licenses/LICENSE-2.0 +# 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 @@ -14,5 +14,7 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. -distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.6/apache-maven-3.8.6-bin.zip -wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar +wrapperVersion=3.3.2 +distributionType=bin +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.2/maven-wrapper-3.3.2.jar diff --git a/fabric-chaincode-integration-test/src/contracts/wrapper-maven/mvnw b/fabric-chaincode-integration-test/src/contracts/wrapper-maven/mvnw index 41c0f0c2..5e9618ca 100755 --- a/fabric-chaincode-integration-test/src/contracts/wrapper-maven/mvnw +++ b/fabric-chaincode-integration-test/src/contracts/wrapper-maven/mvnw @@ -19,7 +19,7 @@ # ---------------------------------------------------------------------------- # ---------------------------------------------------------------------------- -# Maven Start Up Batch script +# Apache Maven Wrapper startup batch script, version 3.3.2 # # Required ENV vars: # ------------------ @@ -27,284 +27,306 @@ # # Optional ENV vars # ----------------- -# M2_HOME - location of maven2's installed home dir # MAVEN_OPTS - parameters passed to the Java VM when running Maven # e.g. to debug Maven itself, use # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 # MAVEN_SKIP_RC - flag to disable loading of mavenrc files # ---------------------------------------------------------------------------- -if [ -z "$MAVEN_SKIP_RC" ] ; then +if [ -z "$MAVEN_SKIP_RC" ]; then - if [ -f /etc/mavenrc ] ; then + if [ -f /usr/local/etc/mavenrc ]; then + . /usr/local/etc/mavenrc + fi + + if [ -f /etc/mavenrc ]; then . /etc/mavenrc fi - if [ -f "$HOME/.mavenrc" ] ; then + if [ -f "$HOME/.mavenrc" ]; then . "$HOME/.mavenrc" fi fi # OS specific support. $var _must_ be set to either true or false. -cygwin=false; -darwin=false; +cygwin=false +darwin=false mingw=false -case "`uname`" in - CYGWIN*) cygwin=true ;; - MINGW*) mingw=true;; - Darwin*) darwin=true - # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home - # See https://developer.apple.com/library/mac/qa/qa1170/_index.html - if [ -z "$JAVA_HOME" ]; then - if [ -x "/usr/libexec/java_home" ]; then - export JAVA_HOME="`/usr/libexec/java_home`" - else - export JAVA_HOME="/Library/Java/Home" - fi +case "$(uname)" in +CYGWIN*) cygwin=true ;; +MINGW*) mingw=true ;; +Darwin*) + darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + JAVA_HOME="$(/usr/libexec/java_home)" + export JAVA_HOME + else + JAVA_HOME="/Library/Java/Home" + export JAVA_HOME fi - ;; + fi + ;; esac -if [ -z "$JAVA_HOME" ] ; then - if [ -r /etc/gentoo-release ] ; then - JAVA_HOME=`java-config --jre-home` +if [ -z "$JAVA_HOME" ]; then + if [ -r /etc/gentoo-release ]; then + JAVA_HOME=$(java-config --jre-home) fi fi -if [ -z "$M2_HOME" ] ; then - ## resolve links - $0 may be a link to maven's home - PRG="$0" - - # need this for relative symlinks - while [ -h "$PRG" ] ; do - ls=`ls -ld "$PRG"` - link=`expr "$ls" : '.*-> \(.*\)$'` - if expr "$link" : '/.*' > /dev/null; then - PRG="$link" - else - PRG="`dirname "$PRG"`/$link" - fi - done - - saveddir=`pwd` - - M2_HOME=`dirname "$PRG"`/.. - - # make it fully qualified - M2_HOME=`cd "$M2_HOME" && pwd` - - cd "$saveddir" - # echo Using m2 at $M2_HOME -fi - # For Cygwin, ensure paths are in UNIX format before anything is touched -if $cygwin ; then - [ -n "$M2_HOME" ] && - M2_HOME=`cygpath --unix "$M2_HOME"` - [ -n "$JAVA_HOME" ] && - JAVA_HOME=`cygpath --unix "$JAVA_HOME"` - [ -n "$CLASSPATH" ] && - CLASSPATH=`cygpath --path --unix "$CLASSPATH"` +if $cygwin; then + [ -n "$JAVA_HOME" ] \ + && JAVA_HOME=$(cygpath --unix "$JAVA_HOME") + [ -n "$CLASSPATH" ] \ + && CLASSPATH=$(cygpath --path --unix "$CLASSPATH") fi # For Mingw, ensure paths are in UNIX format before anything is touched -if $mingw ; then - [ -n "$M2_HOME" ] && - M2_HOME="`(cd "$M2_HOME"; pwd)`" - [ -n "$JAVA_HOME" ] && - JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" +if $mingw; then + [ -n "$JAVA_HOME" ] && [ -d "$JAVA_HOME" ] \ + && JAVA_HOME="$( + cd "$JAVA_HOME" || ( + echo "cannot cd into $JAVA_HOME." >&2 + exit 1 + ) + pwd + )" fi if [ -z "$JAVA_HOME" ]; then - javaExecutable="`which javac`" - if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then + javaExecutable="$(which javac)" + if [ -n "$javaExecutable" ] && ! [ "$(expr "$javaExecutable" : '\([^ ]*\)')" = "no" ]; then # readlink(1) is not available as standard on Solaris 10. - readLink=`which readlink` - if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then - if $darwin ; then - javaHome="`dirname \"$javaExecutable\"`" - javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" + readLink=$(which readlink) + if [ ! "$(expr "$readLink" : '\([^ ]*\)')" = "no" ]; then + if $darwin; then + javaHome="$(dirname "$javaExecutable")" + javaExecutable="$(cd "$javaHome" && pwd -P)/javac" else - javaExecutable="`readlink -f \"$javaExecutable\"`" + javaExecutable="$(readlink -f "$javaExecutable")" fi - javaHome="`dirname \"$javaExecutable\"`" - javaHome=`expr "$javaHome" : '\(.*\)/bin'` + javaHome="$(dirname "$javaExecutable")" + javaHome=$(expr "$javaHome" : '\(.*\)/bin') JAVA_HOME="$javaHome" export JAVA_HOME fi fi fi -if [ -z "$JAVACMD" ] ; then - if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then +if [ -z "$JAVACMD" ]; then + if [ -n "$JAVA_HOME" ]; then + if [ -x "$JAVA_HOME/jre/sh/java" ]; then # IBM's JDK on AIX uses strange locations for the executables JAVACMD="$JAVA_HOME/jre/sh/java" else JAVACMD="$JAVA_HOME/bin/java" fi else - JAVACMD="`which java`" + JAVACMD="$( + \unset -f command 2>/dev/null + \command -v java + )" fi fi -if [ ! -x "$JAVACMD" ] ; then +if [ ! -x "$JAVACMD" ]; then echo "Error: JAVA_HOME is not defined correctly." >&2 echo " We cannot execute $JAVACMD" >&2 exit 1 fi -if [ -z "$JAVA_HOME" ] ; then - echo "Warning: JAVA_HOME environment variable is not set." +if [ -z "$JAVA_HOME" ]; then + echo "Warning: JAVA_HOME environment variable is not set." >&2 fi -CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher - # traverses directory structure from process work directory to filesystem root # first directory with .mvn subdirectory is considered project base directory find_maven_basedir() { - - if [ -z "$1" ] - then - echo "Path not specified to find_maven_basedir" + if [ -z "$1" ]; then + echo "Path not specified to find_maven_basedir" >&2 return 1 fi basedir="$1" wdir="$1" - while [ "$wdir" != '/' ] ; do - if [ -d "$wdir"/.mvn ] ; then + while [ "$wdir" != '/' ]; do + if [ -d "$wdir"/.mvn ]; then basedir=$wdir break fi # workaround for JBEAP-8937 (on Solaris 10/Sparc) if [ -d "${wdir}" ]; then - wdir=`cd "$wdir/.."; pwd` + wdir=$( + cd "$wdir/.." || exit 1 + pwd + ) fi # end of workaround done - echo "${basedir}" + printf '%s' "$( + cd "$basedir" || exit 1 + pwd + )" } # concatenates all lines of a file concat_lines() { if [ -f "$1" ]; then - echo "$(tr -s '\n' ' ' < "$1")" + # Remove \r in case we run on Windows within Git Bash + # and check out the repository with auto CRLF management + # enabled. Otherwise, we may read lines that are delimited with + # \r\n and produce $'-Xarg\r' rather than -Xarg due to word + # splitting rules. + tr -s '\r\n' ' ' <"$1" + fi +} + +log() { + if [ "$MVNW_VERBOSE" = true ]; then + printf '%s\n' "$1" fi } -BASE_DIR=`find_maven_basedir "$(pwd)"` +BASE_DIR=$(find_maven_basedir "$(dirname "$0")") if [ -z "$BASE_DIR" ]; then - exit 1; + exit 1 fi +MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} +export MAVEN_PROJECTBASEDIR +log "$MAVEN_PROJECTBASEDIR" + ########################################################################################## # Extension to allow automatically downloading the maven-wrapper.jar from Maven-central # This allows using the maven wrapper in projects that prohibit checking in binary data. ########################################################################################## -if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then - if [ "$MVNW_VERBOSE" = true ]; then - echo "Found .mvn/wrapper/maven-wrapper.jar" - fi +wrapperJarPath="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" +if [ -r "$wrapperJarPath" ]; then + log "Found $wrapperJarPath" else - if [ "$MVNW_VERBOSE" = true ]; then - echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..." - fi - if [ -n "$MVNW_REPOURL" ]; then - jarUrl="$MVNW_REPOURL/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + log "Couldn't find $wrapperJarPath, downloading it ..." + + if [ -n "$MVNW_REPOURL" ]; then + wrapperUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.3.2/maven-wrapper-3.3.2.jar" + else + wrapperUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.2/maven-wrapper-3.3.2.jar" + fi + while IFS="=" read -r key value; do + # Remove '\r' from value to allow usage on windows as IFS does not consider '\r' as a separator ( considers space, tab, new line ('\n'), and custom '=' ) + safeValue=$(echo "$value" | tr -d '\r') + case "$key" in wrapperUrl) + wrapperUrl="$safeValue" + break + ;; + esac + done <"$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties" + log "Downloading from: $wrapperUrl" + + if $cygwin; then + wrapperJarPath=$(cygpath --path --windows "$wrapperJarPath") + fi + + if command -v wget >/dev/null; then + log "Found wget ... using wget" + [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--quiet" + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget $QUIET "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" else - jarUrl="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" + wget $QUIET --http-user="$MVNW_USERNAME" --http-password="$MVNW_PASSWORD" "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" fi - while IFS="=" read key value; do - case "$key" in (wrapperUrl) jarUrl="$value"; break ;; - esac - done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties" - if [ "$MVNW_VERBOSE" = true ]; then - echo "Downloading from: $jarUrl" + elif command -v curl >/dev/null; then + log "Found curl ... using curl" + [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--silent" + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl $QUIET -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath" + else + curl $QUIET --user "$MVNW_USERNAME:$MVNW_PASSWORD" -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath" fi - wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" + else + log "Falling back to using Java to download" + javaSource="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.java" + javaClass="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.class" + # For Cygwin, switch paths to Windows format before running javac if $cygwin; then - wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"` + javaSource=$(cygpath --path --windows "$javaSource") + javaClass=$(cygpath --path --windows "$javaClass") fi - - if command -v wget > /dev/null; then - if [ "$MVNW_VERBOSE" = true ]; then - echo "Found wget ... using wget" - fi - if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then - wget "$jarUrl" -O "$wrapperJarPath" - else - wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" - fi - elif command -v curl > /dev/null; then - if [ "$MVNW_VERBOSE" = true ]; then - echo "Found curl ... using curl" - fi - if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then - curl -o "$wrapperJarPath" "$jarUrl" -f - else - curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f - fi - - else - if [ "$MVNW_VERBOSE" = true ]; then - echo "Falling back to using Java to download" - fi - javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java" - # For Cygwin, switch paths to Windows format before running javac - if $cygwin; then - javaClass=`cygpath --path --windows "$javaClass"` - fi - if [ -e "$javaClass" ]; then - if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then - if [ "$MVNW_VERBOSE" = true ]; then - echo " - Compiling MavenWrapperDownloader.java ..." - fi - # Compiling the Java class - ("$JAVA_HOME/bin/javac" "$javaClass") - fi - if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then - # Running the downloader - if [ "$MVNW_VERBOSE" = true ]; then - echo " - Running MavenWrapperDownloader.java ..." - fi - ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR") - fi - fi + if [ -e "$javaSource" ]; then + if [ ! -e "$javaClass" ]; then + log " - Compiling MavenWrapperDownloader.java ..." + ("$JAVA_HOME/bin/javac" "$javaSource") + fi + if [ -e "$javaClass" ]; then + log " - Running MavenWrapperDownloader.java ..." + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$wrapperUrl" "$wrapperJarPath") || rm -f "$wrapperJarPath" + fi fi + fi fi ########################################################################################## # End of extension ########################################################################################## -export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} -if [ "$MVNW_VERBOSE" = true ]; then - echo $MAVEN_PROJECTBASEDIR +# If specified, validate the SHA-256 sum of the Maven wrapper jar file +wrapperSha256Sum="" +while IFS="=" read -r key value; do + case "$key" in wrapperSha256Sum) + wrapperSha256Sum=$value + break + ;; + esac +done <"$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties" +if [ -n "$wrapperSha256Sum" ]; then + wrapperSha256Result=false + if command -v sha256sum >/dev/null; then + if echo "$wrapperSha256Sum $wrapperJarPath" | sha256sum -c >/dev/null 2>&1; then + wrapperSha256Result=true + fi + elif command -v shasum >/dev/null; then + if echo "$wrapperSha256Sum $wrapperJarPath" | shasum -a 256 -c >/dev/null 2>&1; then + wrapperSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 + echo "Please install either command, or disable validation by removing 'wrapperSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + fi + if [ $wrapperSha256Result = false ]; then + echo "Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised." >&2 + echo "Investigate or delete $wrapperJarPath to attempt a clean download." >&2 + echo "If you updated your Maven version, you need to update the specified wrapperSha256Sum property." >&2 + exit 1 + fi fi + MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" # For Cygwin, switch paths to Windows format before running java if $cygwin; then - [ -n "$M2_HOME" ] && - M2_HOME=`cygpath --path --windows "$M2_HOME"` - [ -n "$JAVA_HOME" ] && - JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` - [ -n "$CLASSPATH" ] && - CLASSPATH=`cygpath --path --windows "$CLASSPATH"` - [ -n "$MAVEN_PROJECTBASEDIR" ] && - MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` + [ -n "$JAVA_HOME" ] \ + && JAVA_HOME=$(cygpath --path --windows "$JAVA_HOME") + [ -n "$CLASSPATH" ] \ + && CLASSPATH=$(cygpath --path --windows "$CLASSPATH") + [ -n "$MAVEN_PROJECTBASEDIR" ] \ + && MAVEN_PROJECTBASEDIR=$(cygpath --path --windows "$MAVEN_PROJECTBASEDIR") fi # Provide a "standardized" way to retrieve the CLI args that will # work with both Windows and non-Windows executions. -MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@" +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $*" export MAVEN_CMD_LINE_ARGS WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain +# shellcheck disable=SC2086 # safe args exec "$JAVACMD" \ $MAVEN_OPTS \ + $MAVEN_DEBUG_OPTS \ -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ - "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/fabric-chaincode-integration-test/src/contracts/wrapper-maven/mvnw.cmd b/fabric-chaincode-integration-test/src/contracts/wrapper-maven/mvnw.cmd index 86115719..1204076a 100644 --- a/fabric-chaincode-integration-test/src/contracts/wrapper-maven/mvnw.cmd +++ b/fabric-chaincode-integration-test/src/contracts/wrapper-maven/mvnw.cmd @@ -1,182 +1,206 @@ -@REM ---------------------------------------------------------------------------- -@REM Licensed to the Apache Software Foundation (ASF) under one -@REM or more contributor license agreements. See the NOTICE file -@REM distributed with this work for additional information -@REM regarding copyright ownership. The ASF licenses this file -@REM to you under the Apache License, Version 2.0 (the -@REM "License"); you may not use this file except in compliance -@REM with the License. You may obtain a copy of the License at -@REM -@REM http://www.apache.org/licenses/LICENSE-2.0 -@REM -@REM Unless required by applicable law or agreed to in writing, -@REM software distributed under the License is distributed on an -@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -@REM KIND, either express or implied. See the License for the -@REM specific language governing permissions and limitations -@REM under the License. -@REM ---------------------------------------------------------------------------- - -@REM ---------------------------------------------------------------------------- -@REM Maven Start Up Batch script -@REM -@REM Required ENV vars: -@REM JAVA_HOME - location of a JDK home dir -@REM -@REM Optional ENV vars -@REM M2_HOME - location of maven2's installed home dir -@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands -@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending -@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven -@REM e.g. to debug Maven itself, use -@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files -@REM ---------------------------------------------------------------------------- - -@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' -@echo off -@REM set title of command window -title %0 -@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' -@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% - -@REM set %HOME% to equivalent of $HOME -if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") - -@REM Execute a user defined script before this one -if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre -@REM check for pre script, once with legacy .bat ending and once with .cmd ending -if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" -if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" -:skipRcPre - -@setlocal - -set ERROR_CODE=0 - -@REM To isolate internal variables from possible post scripts, we use another setlocal -@setlocal - -@REM ==== START VALIDATION ==== -if not "%JAVA_HOME%" == "" goto OkJHome - -echo. -echo Error: JAVA_HOME not found in your environment. >&2 -echo Please set the JAVA_HOME variable in your environment to match the >&2 -echo location of your Java installation. >&2 -echo. -goto error - -:OkJHome -if exist "%JAVA_HOME%\bin\java.exe" goto init - -echo. -echo Error: JAVA_HOME is set to an invalid directory. >&2 -echo JAVA_HOME = "%JAVA_HOME%" >&2 -echo Please set the JAVA_HOME variable in your environment to match the >&2 -echo location of your Java installation. >&2 -echo. -goto error - -@REM ==== END VALIDATION ==== - -:init - -@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". -@REM Fallback to current working directory if not found. - -set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% -IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir - -set EXEC_DIR=%CD% -set WDIR=%EXEC_DIR% -:findBaseDir -IF EXIST "%WDIR%"\.mvn goto baseDirFound -cd .. -IF "%WDIR%"=="%CD%" goto baseDirNotFound -set WDIR=%CD% -goto findBaseDir - -:baseDirFound -set MAVEN_PROJECTBASEDIR=%WDIR% -cd "%EXEC_DIR%" -goto endDetectBaseDir - -:baseDirNotFound -set MAVEN_PROJECTBASEDIR=%EXEC_DIR% -cd "%EXEC_DIR%" - -:endDetectBaseDir - -IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig - -@setlocal EnableExtensions EnableDelayedExpansion -for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a -@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% - -:endReadAdditionalConfig - -SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" -set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" -set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain - -set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" - -FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( - IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B -) - -@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central -@REM This allows using the maven wrapper in projects that prohibit checking in binary data. -if exist %WRAPPER_JAR% ( - if "%MVNW_VERBOSE%" == "true" ( - echo Found %WRAPPER_JAR% - ) -) else ( - if not "%MVNW_REPOURL%" == "" ( - SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar" - ) - if "%MVNW_VERBOSE%" == "true" ( - echo Couldn't find %WRAPPER_JAR%, downloading it ... - echo Downloading from: %DOWNLOAD_URL% - ) - - powershell -Command "&{"^ - "$webclient = new-object System.Net.WebClient;"^ - "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ - "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ - "}"^ - "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^ - "}" - if "%MVNW_VERBOSE%" == "true" ( - echo Finished downloading %WRAPPER_JAR% - ) -) -@REM End of extension - -@REM Provide a "standardized" way to retrieve the CLI args that will -@REM work with both Windows and non-Windows executions. -set MAVEN_CMD_LINE_ARGS=%* - -%MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* -if ERRORLEVEL 1 goto error -goto end - -:error -set ERROR_CODE=1 - -:end -@endlocal & set ERROR_CODE=%ERROR_CODE% - -if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost -@REM check for post script, once with legacy .bat ending and once with .cmd ending -if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" -if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" -:skipRcPost - -@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' -if "%MAVEN_BATCH_PAUSE%" == "on" pause - -if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% - -exit /B %ERROR_CODE% +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.3.2 +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* +if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. >&2 +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. >&2 +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. >&2 +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. >&2 +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set WRAPPER_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.2/maven-wrapper-3.3.2.jar" + +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET WRAPPER_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET WRAPPER_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.3.2/maven-wrapper-3.3.2.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %WRAPPER_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%WRAPPER_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM If specified, validate the SHA-256 sum of the Maven wrapper jar file +SET WRAPPER_SHA_256_SUM="" +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperSha256Sum" SET WRAPPER_SHA_256_SUM=%%B +) +IF NOT %WRAPPER_SHA_256_SUM%=="" ( + powershell -Command "&{"^ + "Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash;"^ + "$hash = (Get-FileHash \"%WRAPPER_JAR%\" -Algorithm SHA256).Hash.ToLower();"^ + "If('%WRAPPER_SHA_256_SUM%' -ne $hash){"^ + " Write-Error 'Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised.';"^ + " Write-Error 'Investigate or delete %WRAPPER_JAR% to attempt a clean download.';"^ + " Write-Error 'If you updated your Maven version, you need to update the specified wrapperSha256Sum property.';"^ + " exit 1;"^ + "}"^ + "}" + if ERRORLEVEL 1 goto error +) + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% ^ + %JVM_CONFIG_MAVEN_PROPS% ^ + %MAVEN_OPTS% ^ + %MAVEN_DEBUG_OPTS% ^ + -classpath %WRAPPER_JAR% ^ + "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ + %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" +if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%"=="on" pause + +if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% + +cmd /C exit /B %ERROR_CODE% diff --git a/fabric-chaincode-integration-test/src/contracts/wrapper-maven/pom.xml b/fabric-chaincode-integration-test/src/contracts/wrapper-maven/pom.xml index e70ceed1..f9e59238 100644 --- a/fabric-chaincode-integration-test/src/contracts/wrapper-maven/pom.xml +++ b/fabric-chaincode-integration-test/src/contracts/wrapper-maven/pom.xml @@ -7,7 +7,7 @@ - 8 + 11 UTF-8 UTF-8 diff --git a/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/contractinstall/ContractInstallTest.java b/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/contractinstall/ContractInstallTest.java index b05dbf81..f63dc8b6 100644 --- a/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/contractinstall/ContractInstallTest.java +++ b/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/contractinstall/ContractInstallTest.java @@ -22,7 +22,7 @@ public static void setUp() throws Exception { } @Test - public void TestInstall() { + public void testInstall() { InvokeHelper helper = InvokeHelper.newHelper("baregradlecc", "sachannel"); String text = helper.invoke("org1", "whoami"); diff --git a/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/ledgertests/LedgerIntegrationTest.java b/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/ledgertests/LedgerIntegrationTest.java index ad856978..815ab88c 100644 --- a/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/ledgertests/LedgerIntegrationTest.java +++ b/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/ledgertests/LedgerIntegrationTest.java @@ -23,7 +23,7 @@ public static void setUp() throws Exception { } @Test - public void TestLedgers() { + public void testLedgers() { InvokeHelper helper = InvokeHelper.newHelper("ledgercc", "sachannel"); String text = helper.invoke("org1", "accessLedgers"); diff --git a/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/shimtests/SACCIntegrationTest.java b/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/shimtests/SACCIntegrationTest.java index 94ebb378..93923ac6 100644 --- a/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/shimtests/SACCIntegrationTest.java +++ b/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/shimtests/SACCIntegrationTest.java @@ -22,7 +22,7 @@ public static void setUp() throws Exception { } @Test - public void TestLedger() { + public void testLedger() { InvokeHelper helper = InvokeHelper.newHelper("shimcc", "sachannel"); String text = helper.invoke("org1", "putBulkStates"); diff --git a/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/shimtests/SBECCIntegrationTest.java b/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/shimtests/SBECCIntegrationTest.java index 32e8ec56..02ffb16c 100644 --- a/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/shimtests/SBECCIntegrationTest.java +++ b/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/shimtests/SBECCIntegrationTest.java @@ -22,7 +22,7 @@ public static void setUp() throws Exception { } @Test - public void RunSBE_pub_setget() { + public void runSBE_pub_setget() { final String mode = "pub"; final InvokeHelper helper = InvokeHelper.newHelper("shimcc", "sachannel"); @@ -85,7 +85,7 @@ public void RunSBE_pub_setget() { } @Test - public void RunSBE_priv() { + public void runSBE_priv() { final String mode = "priv"; final InvokeHelper helper = InvokeHelper.newHelper("shimcc", "sachannel"); diff --git a/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/util/FabricState.java b/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/util/FabricState.java index 8483446e..916f31ea 100644 --- a/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/util/FabricState.java +++ b/fabric-chaincode-integration-test/src/test/java/org/hyperleder/fabric/shim/integration/util/FabricState.java @@ -9,19 +9,13 @@ import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.Semaphore; import org.hyperleder.fabric.shim.integration.util.Bash.BashBuilder; public final class FabricState { private static FabricState state; - private static final Map channelStarted = new HashMap<>(); - - // sempaphore to protect access - private static final Semaphore flag = new Semaphore(1); - - public static FabricState getState() { + public static synchronized FabricState getState() { if (state == null) { state = new FabricState(); } diff --git a/fabric-chaincode-shim/build.gradle b/fabric-chaincode-shim/build.gradle index b4c50500..5a42b332 100644 --- a/fabric-chaincode-shim/build.gradle +++ b/fabric-chaincode-shim/build.gradle @@ -8,22 +8,18 @@ plugins { id 'maven-publish' id 'jacoco' id 'signing' - id 'checkstyle' + id 'pmd' } -checkstyle { - toolVersion '10.18.1' - configFile file("../ci/checkstyle/checkstyle.xml") - configProperties = [root_dir: file("..") ] -} -checkstyleMain { - source ='src/main/java' -} -checkstyleMain.exclude("**/ChaincodeServerProperties.**") -checkstyleTest { - source ='src/test/java' +pmd { + toolVersion = '7.7.0' + ruleSetFiles = files('../pmd-ruleset.xml') + ruleSets = [] // explicitly set to empty to avoid using the default configuration + ignoreFailures = false } +pmdTest.enabled = false + configurations { runtimeClasspath { resolutionStrategy.activateDependencyLocking() diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logger.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logger.java index e1a55c90..35720635 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logger.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logger.java @@ -18,7 +18,7 @@ protected Logger(final String name) { super(name, null); // ensure that the parent logger is set - this.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric")); + super.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric")); } /** @@ -45,9 +45,9 @@ public void debug(final String msg) { */ public static Logger getLogger(final Class class1) { // important to add the logger to the log manager - final Logger l = Logger.getLogger(class1.getName()); - LogManager.getLogManager().addLogger(l); - return l; + final Logger result = Logger.getLogger(class1.getName()); + LogManager.getLogManager().addLogger(result); + return result; } /** @param message */ diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logging.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logging.java index 1d26eaf4..0901d75e 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logging.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logging.java @@ -7,8 +7,9 @@ import java.io.PrintWriter; import java.io.StringWriter; -import java.util.ArrayList; import java.util.Collections; +import java.util.List; +import java.util.Locale; import java.util.logging.Level; import java.util.logging.LogManager; @@ -50,7 +51,7 @@ public static String formatError(final Throwable throwable) { final Throwable cause = throwable.getCause(); if (cause != null) { buffer.append(".. caused by ..").append(System.lineSeparator()); - buffer.append(Logging.formatError(cause)); + buffer.append(formatError(cause)); } return buffer.toString(); @@ -67,11 +68,11 @@ public static void setLogLevel(final String newLevel) { final LogManager logManager = LogManager.getLogManager(); // slightly cumbersome approach - but the loggers don't have a 'get children' // so find those that have the correct stem. - final ArrayList allLoggers = Collections.list(logManager.getLoggerNames()); + final List allLoggers = Collections.list(logManager.getLoggerNames()); allLoggers.add("org.hyperledger"); allLoggers.stream() .filter(name -> name.startsWith("org.hyperledger")) - .map(name -> logManager.getLogger(name)) + .map(logManager::getLogger) .forEach(logger -> { if (logger != null) { logger.setLevel(l); @@ -81,7 +82,7 @@ public static void setLogLevel(final String newLevel) { private static Level mapLevel(final String level) { if (level != null) { - switch (level.toUpperCase().trim()) { + switch (level.toUpperCase(Locale.getDefault()).trim()) { case "ERROR": case "CRITICAL": return Level.SEVERE; diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ClientIdentity.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ClientIdentity.java index 4c10f704..2ce5cbbc 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ClientIdentity.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ClientIdentity.java @@ -30,11 +30,11 @@ * and attributes. Such information is useful in enforcing access control by the chaincode. */ public final class ClientIdentity { - private static Logger logger = Logger.getLogger(ContractRouter.class.getName()); + private static final Logger LOGGER = Logger.getLogger(ContractRouter.class.getName()); private final String mspId; private final X509Certificate cert; - private Map attrs; + private final Map attrs; private final String id; // special OID used by Fabric to save attributes in x.509 certificates private static final String FABRIC_CERT_ATTR_OID = "1.2.3.4.5.6.7.8.1"; @@ -47,7 +47,7 @@ public final class ClientIdentity { * @throws JSONException * @throws IOException */ - public ClientIdentity(final ChaincodeStub stub) throws CertificateException, JSONException, IOException { + public ClientIdentity(final ChaincodeStub stub) throws CertificateException, IOException { final byte[] signingId = stub.getCreator(); // Create a Serialized Identity protobuf @@ -60,11 +60,12 @@ public ClientIdentity(final ChaincodeStub stub) throws CertificateException, JSO CertificateFactory.getInstance("X509").generateCertificate(new ByteArrayInputStream(idBytes)); this.cert = cert; - this.attrs = new HashMap(); // Get the extension where the identity attributes are stored final byte[] extensionValue = cert.getExtensionValue(FABRIC_CERT_ATTR_OID); if (extensionValue != null) { this.attrs = parseAttributes(extensionValue); + } else { + this.attrs = new HashMap<>(); } // Populate identity @@ -100,7 +101,7 @@ public String getMSPID() { */ private Map parseAttributes(final byte[] extensionValue) throws IOException { - final Map attrMap = new HashMap(); + final Map attrMap = new HashMap<>(); // Create ASN1InputStream from extensionValue try (ByteArrayInputStream inStream = new ByteArrayInputStream(extensionValue); @@ -126,7 +127,7 @@ private Map parseAttributes(final byte[] extensionValue) throws } catch (final JSONException error) { // creating a JSON object failed // decoded extensionValue is not a string containing JSON - logger.error(() -> logger.formatError(error)); + LOGGER.error(() -> LOGGER.formatError(error)); // return empty map } return attrMap; @@ -142,11 +143,7 @@ private Map parseAttributes(final byte[] extensionValue) throws * @return {String | null} Value of the attribute or null if the invoking identity does not possess the attribute. */ public String getAttributeValue(final String attrName) { - if (this.attrs.containsKey(attrName)) { - return this.attrs.get(attrName); - } else { - return null; - } + return this.attrs.getOrDefault(attrName, null); } /** @@ -160,11 +157,7 @@ public String getAttributeValue(final String attrName) { * expected value. Otherwise, returns false. */ public boolean assertAttributeValue(final String attrName, final String attrValue) { - if (!this.attrs.containsKey(attrName)) { - return false; - } else { - return attrValue.equals(this.attrs.get(attrName)); - } + return this.attrs.containsKey(attrName) && attrValue.equals(this.attrs.get(attrName)); } /** diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContextFactory.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContextFactory.java index 64e12a1b..68514adb 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContextFactory.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContextFactory.java @@ -10,14 +10,11 @@ /** Factory to create {@link Context} from {@link ChaincodeStub} by wrapping stub with dynamic proxy. */ public final class ContextFactory { - private static ContextFactory cf; + private static final ContextFactory INSTANCE = new ContextFactory(); /** @return ContextFactory */ - public static synchronized ContextFactory getInstance() { - if (cf == null) { - cf = new ContextFactory(); - } - return cf; + public static ContextFactory getInstance() { + return INSTANCE; } /** @@ -25,7 +22,6 @@ public static synchronized ContextFactory getInstance() { * @return Context */ public Context createContext(final ChaincodeStub stub) { - final Context newContext = new Context(stub); - return newContext; + return new Context(stub); } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractInterface.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractInterface.java index c9bea5fc..e1db9de4 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractInterface.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractInterface.java @@ -75,7 +75,9 @@ default void unknownTransaction(final Context ctx) { * * @param ctx the context as created by {@link #createContext(ChaincodeStub)}. */ - default void beforeTransaction(final Context ctx) {} + default void beforeTransaction(final Context ctx) { + // Nothing by default + } /** * Invoked once after each transaction. @@ -86,5 +88,7 @@ default void beforeTransaction(final Context ctx) {} * @param result The object returned from the transaction function if any. As this is a Java object and therefore * pass-by-reference it is possible to modify this object. */ - default void afterTransaction(final Context ctx, final Object result) {} + default void afterTransaction(final Context ctx, final Object result) { + // Nothing by default + } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractRouter.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractRouter.java index 0219f71d..d7b6f9e5 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractRouter.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractRouter.java @@ -35,7 +35,7 @@ * @see ContractInterface */ public final class ContractRouter extends ChaincodeBase { - private static Logger logger = Logger.getLogger(ContractRouter.class.getName()); + private static final Logger LOGGER = Logger.getLogger(ContractRouter.class.getName()); private final RoutingRegistry registry; private final TypeRegistry typeRegistry; @@ -53,6 +53,7 @@ public final class ContractRouter extends ChaincodeBase { * @param args */ public ContractRouter(final String[] args) { + super(); super.initializeLogging(); super.processEnvironmentOptions(); super.processCommandLineOptions(args); @@ -62,7 +63,7 @@ public ContractRouter(final String[] args) { Metrics.initialize(props); Traces.initialize(props); - logger.fine("ContractRouter"); + LOGGER.fine("ContractRouter"); registry = new RoutingRegistryImpl(); typeRegistry = TypeRegistry.getRegistry(); @@ -72,15 +73,15 @@ public ContractRouter(final String[] args) { serializers.findAndSetContents(); } catch (InstantiationException | IllegalAccessException e) { final ContractRuntimeException cre = new ContractRuntimeException("Unable to locate Serializers", e); - logger.severe(() -> Logging.formatError(cre)); - throw new RuntimeException(cre); + LOGGER.severe(() -> Logging.formatError(cre)); + throw cre; } executor = ExecutionFactory.getInstance().createExecutionService(serializers); } /** Locate all the contracts that are available on the classpath. */ - protected void findAllContracts() { + void findAllContracts() { registry.findAndSetContracts(this.typeRegistry); } @@ -91,28 +92,29 @@ protected void findAllContracts() { * * @throws Exception */ + @SuppressWarnings("PMD.AvoidCatchingGenericException") void startRouting() { try { super.connectToPeer(); } catch (final Exception e) { - logger.severe(() -> Logging.formatError(e)); - final ContractRuntimeException cre = new ContractRuntimeException("Unable to start routing", e); - throw cre; + LOGGER.severe(() -> Logging.formatError(e)); + throw new ContractRuntimeException("Unable to start routing", e); } } + @SuppressWarnings("PMD.AvoidCatchingThrowable") private Response processRequest(final ChaincodeStub stub) { - logger.info(() -> "Got invoke routing request"); + LOGGER.info(() -> "Got invoke routing request"); try { - if (stub.getStringArgs().size() > 0) { - logger.info(() -> "Got the invoke request for:" + stub.getFunction() + " " + stub.getParameters()); - final InvocationRequest request = ExecutionFactory.getInstance().createRequest(stub); - final TxFunction txFn = getRouting(request); - logger.info(() -> "Got routing:" + txFn.getRouting()); - return executor.executeRequest(txFn, request, stub); - } else { + if (stub.getStringArgs().isEmpty()) { return ResponseUtils.newSuccessResponse(); } + + LOGGER.info(() -> "Got the invoke request for:" + stub.getFunction() + " " + stub.getParameters()); + final InvocationRequest request = ExecutionFactory.getInstance().createRequest(stub); + final TxFunction txFn = getRouting(request); + LOGGER.info(() -> "Got routing:" + txFn.getRouting()); + return executor.executeRequest(txFn, request, stub); } catch (final Throwable throwable) { return ResponseUtils.newErrorResponse(throwable); } @@ -139,7 +141,7 @@ TxFunction getRouting(final InvocationRequest request) { if (registry.containsRoute(request)) { return registry.getTxFn(request); } else { - logger.fine(() -> "Namespace is " + request); + LOGGER.fine(() -> "Namespace is " + request); final ContractDefinition contract = registry.getContract(request.getNamespace()); return contract.getUnknownRoute(); } @@ -150,34 +152,35 @@ TxFunction getRouting(final InvocationRequest request) { * * @param args */ + @SuppressWarnings("PMD.SignatureDeclareThrowsException") public static void main(final String[] args) throws Exception { final ContractRouter cfc = new ContractRouter(args); cfc.findAllContracts(); - logger.fine(cfc.getRoutingRegistry().toString()); + LOGGER.fine(() -> cfc.getRoutingRegistry().toString()); // Create the Metadata ahead of time rather than have to produce every // time MetadataBuilder.initialize(cfc.getRoutingRegistry(), cfc.getTypeRegistry()); - logger.info(() -> "Metadata follows:" + MetadataBuilder.debugString()); + LOGGER.info(() -> "Metadata follows:" + MetadataBuilder.debugString()); // check if this should be running in client or server mode if (cfc.isServer()) { - logger.info("Starting chaincode as server"); + LOGGER.info("Starting chaincode as server"); ChaincodeServer chaincodeServer = new NettyChaincodeServer(cfc, cfc.getChaincodeServerConfig()); chaincodeServer.start(); } else { - logger.info("Starting chaincode as client"); + LOGGER.info("Starting chaincode as client"); cfc.startRouting(); } } - protected TypeRegistry getTypeRegistry() { + TypeRegistry getTypeRegistry() { return this.typeRegistry; } - protected RoutingRegistry getRoutingRegistry() { + RoutingRegistry getRoutingRegistry() { return this.registry; } @@ -189,10 +192,10 @@ protected RoutingRegistry getRoutingRegistry() { public void startRouterWithChaincodeServer(final ChaincodeServer chaincodeServer) throws IOException, InterruptedException { findAllContracts(); - logger.fine(getRoutingRegistry().toString()); + LOGGER.fine(() -> getRoutingRegistry().toString()); MetadataBuilder.initialize(getRoutingRegistry(), getTypeRegistry()); - logger.info(() -> "Metadata follows:" + MetadataBuilder.debugString()); + LOGGER.info(() -> "Metadata follows:" + MetadataBuilder.debugString()); chaincodeServer.start(); } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractRuntimeException.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractRuntimeException.java index b51c218f..78d559a2 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractRuntimeException.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractRuntimeException.java @@ -14,6 +14,8 @@ *

FUTURE At some future point we wish to add more diagnostic information into this, for example current tx id */ public class ContractRuntimeException extends ChaincodeException { + /** Generated serial version id. */ + private static final long serialVersionUID = -884373036398750450L; /** @param string */ public ContractRuntimeException(final String string) { @@ -32,7 +34,4 @@ public ContractRuntimeException(final String string, final Throwable cause) { public ContractRuntimeException(final Throwable cause) { super(cause); } - - /** Generated serial version id. */ - private static final long serialVersionUID = -884373036398750450L; } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/ExecutionFactory.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/ExecutionFactory.java index fee47eb1..3cd7a5fc 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/ExecutionFactory.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/ExecutionFactory.java @@ -12,14 +12,11 @@ import org.hyperledger.fabric.shim.ChaincodeStub; public class ExecutionFactory { - private static ExecutionFactory rf; + private static final ExecutionFactory INSTANCE = new ExecutionFactory(); /** @return ExecutionFactory */ public static ExecutionFactory getInstance() { - if (rf == null) { - rf = new ExecutionFactory(); - } - return rf; + return INSTANCE; } /** diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/JSONTransactionSerializer.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/JSONTransactionSerializer.java index c1c99591..ac2e33b9 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/JSONTransactionSerializer.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/JSONTransactionSerializer.java @@ -11,8 +11,6 @@ import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; -import java.nio.charset.StandardCharsets; -import java.util.Iterator; import java.util.Map; import java.util.Set; import org.hyperledger.fabric.Logger; @@ -26,15 +24,13 @@ import org.json.JSONException; import org.json.JSONObject; -/** Used as a the default serialisation for transmission from SDK to Contract. */ +/** Used as the default serialisation for transmission from SDK to Contract. */ @Serializer() +@SuppressWarnings({"PMD.GodClass", "PMD.AvoidLiteralsInIfCondition", "PMD.AvoidDuplicateLiterals"}) public class JSONTransactionSerializer implements SerializerInterface { - private static Logger logger = Logger.getLogger(JSONTransactionSerializer.class.getName()); + private static final Logger LOGGER = Logger.getLogger(JSONTransactionSerializer.class.getName()); private final TypeRegistry typeRegistry = TypeRegistry.getRegistry(); - /** Create a new serialiser. */ - public JSONTransactionSerializer() {} - /** * Convert the value supplied to a byte array, according to the TypeSchema. * @@ -44,7 +40,7 @@ public JSONTransactionSerializer() {} */ @Override public byte[] toBuffer(final Object value, final TypeSchema ts) { - logger.debug(() -> "Schema to convert is " + ts); + LOGGER.debug(() -> "Schema to convert is " + ts); byte[] buffer = null; if (value != null) { final String type = ts.getType(); @@ -56,7 +52,7 @@ public byte[] toBuffer(final Object value, final TypeSchema ts) { break; case "string": final String format = ts.getFormat(); - if (format != null && format.contentEquals("uint16")) { + if ("utin16".equals(format)) { buffer = Character.valueOf((char) value).toString().getBytes(UTF_8); } else { buffer = ((String) value).getBytes(UTF_8); @@ -66,7 +62,7 @@ public byte[] toBuffer(final Object value, final TypeSchema ts) { case "integer": case "boolean": default: - buffer = (value).toString().getBytes(UTF_8); + buffer = value.toString().getBytes(UTF_8); } } else { // at this point we can assert that the value is @@ -76,7 +72,7 @@ public byte[] toBuffer(final Object value, final TypeSchema ts) { // it should have final DataTypeDefinition dtd = this.typeRegistry.getDataType(ts); final Set keySet = dtd.getProperties().keySet(); - final String[] propNames = keySet.toArray(new String[keySet.size()]); + final String[] propNames = keySet.toArray(new String[0]); // Note: whilst the current JSON library does pretty much // everything is required, this part is hard. @@ -105,6 +101,7 @@ public byte[] toBuffer(final Object value, final TypeSchema ts) { * @param ts Schema to normalise to * @return JSONArray */ + @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") private JSONArray normalizeArray(final JSONArray jsonArray, final TypeSchema ts) { JSONArray normalizedArray; @@ -112,22 +109,12 @@ private JSONArray normalizeArray(final JSONArray jsonArray, final TypeSchema ts) final TypeSchema items = ts.getItems(); final String type = items.getType(); - if (type != null && type != "array") { - // primitive - can return this directly - normalizedArray = jsonArray; - } else if (type != null && type == "array") { - // nested arrays, get the type of what it makes up - // Need to loop over all elements and normalize each one - normalizedArray = new JSONArray(); - for (int i = 0; i < jsonArray.length(); i++) { - normalizedArray.put(i, normalizeArray(jsonArray.getJSONArray(i), items)); - } - } else { + if (null == type) { // get the permitted propeties in the type, // then loop over the array and ensure they are correct final DataTypeDefinition dtd = this.typeRegistry.getDataType(items); final Set keySet = dtd.getProperties().keySet(); - final String[] propNames = keySet.toArray(new String[keySet.size()]); + final String[] propNames = keySet.toArray(new String[0]); normalizedArray = new JSONArray(); // array of objects @@ -136,7 +123,18 @@ private JSONArray normalizeArray(final JSONArray jsonArray, final TypeSchema ts) final JSONObject obj = new JSONObject(jsonArray.getJSONObject(i), propNames); normalizedArray.put(i, obj); } + } else if ("array".equals(type)) { + // nested arrays, get the type of what it makes up + // Need to loop over all elements and normalize each one + normalizedArray = new JSONArray(); + for (int i = 0; i < jsonArray.length(); i++) { + normalizedArray.put(i, normalizeArray(jsonArray.getJSONArray(i), items)); + } + } else { + // primitive - can return this directly + normalizedArray = jsonArray; } + return normalizedArray; } @@ -150,15 +148,10 @@ private JSONArray normalizeArray(final JSONArray jsonArray, final TypeSchema ts) @Override public Object fromBuffer(final byte[] buffer, final TypeSchema ts) { try { - final String stringData = new String(buffer, StandardCharsets.UTF_8); - Object value = null; - - value = convert(stringData, ts); - - return value; + final String stringData = new String(buffer, UTF_8); + return convert(stringData, ts); } catch (InstantiationException | IllegalAccessException e) { - final ContractRuntimeException cre = new ContractRuntimeException(e); - throw cre; + throw new ContractRuntimeException(e); } } @@ -172,102 +165,136 @@ public Object fromBuffer(final byte[] buffer, final TypeSchema ts) { * @return Class for the Object variant */ private Class mapPrimitive(final Class primitive) { - String primitiveType; - final boolean isArray = primitive.isArray(); - if (isArray) { - primitiveType = primitive.getComponentType().getName(); - } else { - primitiveType = primitive.getName(); + if (primitive.isArray()) { + return mapArrayPrimitive(primitive); } - switch (primitiveType) { + return mapBasicPrimitive(primitive); + } + + private Class mapArrayPrimitive(final Class primitive) { + switch (primitive.getComponentType().getName()) { case "int": - return isArray ? Integer[].class : Integer.class; + return Integer[].class; case "long": - return isArray ? Long[].class : Long.class; + return Long[].class; case "float": - return isArray ? Float[].class : Float.class; + return Float[].class; case "double": - return isArray ? Double[].class : Double.class; + return Double[].class; case "short": - return isArray ? Short[].class : Short.class; + return Short[].class; case "byte": - return isArray ? Byte[].class : Byte.class; + return Byte[].class; case "char": - return isArray ? Character[].class : Character.class; + return Character[].class; case "boolean": - return isArray ? Boolean[].class : Boolean.class; + return Boolean[].class; default: return primitive; } } - /* - * Internal method to do the conversion - */ + private Class mapBasicPrimitive(final Class primitive) { + switch (primitive.getName()) { + case "int": + return Integer.class; + case "long": + return Long.class; + case "float": + return Float.class; + case "double": + return Double.class; + case "short": + return Short.class; + case "byte": + return Byte.class; + case "char": + return Character.class; + case "boolean": + return Boolean.class; + default: + return primitive; + } + } + + /** Internal method to do the conversion */ private Object convert(final String stringData, final TypeSchema ts) - throws IllegalArgumentException, IllegalAccessException, InstantiationException { - logger.debug(() -> "Schema to convert is " + ts); + throws IllegalAccessException, InstantiationException { + LOGGER.debug(() -> "Schema to convert is " + ts); + String type = ts.getType(); + String format = null; - Object value = null; if (type == null) { type = "object"; final String ref = ts.getRef(); - format = ref.substring(ref.lastIndexOf("/") + 1); + format = ref.substring(ref.lastIndexOf('/') + 1); } - if (type.contentEquals("string")) { - final String strformat = ts.getFormat(); - if (strformat != null && strformat.contentEquals("uint16")) { - value = stringData.charAt(0); - } else { - value = stringData; - } - } else if (type.contentEquals("integer")) { - final String intFormat = ts.getFormat(); - switch (intFormat) { - case "int32": - value = Integer.parseInt(stringData); - break; - case "int8": - value = Byte.parseByte(stringData); - break; - case "int16": - value = Short.parseShort(stringData); - break; - case "int64": - value = Long.parseLong(stringData); - break; - default: - throw new RuntimeException("Unknown format for integer " + intFormat); - } + switch (type) { + case "string": + return convertString(stringData, ts); + case "integer": + return convertInteger(stringData, ts); + case "number": + return convertNumber(stringData, ts); + case "boolean": + return Boolean.parseBoolean(stringData); + case "object": + return createComponentInstance(format, stringData, ts); + case "array": + return convertArray(stringData, ts); + default: + return null; + } + } - } else if (type.contentEquals("number")) { - final String numFormat = ts.getFormat(); - if (numFormat.contentEquals("float")) { - value = Float.parseFloat(stringData); - } else { - value = Double.parseDouble(stringData); - } - } else if (type.contentEquals("boolean")) { - value = Boolean.parseBoolean(stringData); - } else if (type.contentEquals("object")) { - value = createComponentInstance(format, stringData, ts); - } else if (type.contentEquals("array")) { - final JSONArray jsonArray = new JSONArray(stringData); - final TypeSchema itemSchema = ts.getItems(); - - // note here that the type has to be converted in the case of primitives - final Object[] data = (Object[]) - Array.newInstance(mapPrimitive(itemSchema.getTypeClass(this.typeRegistry)), jsonArray.length()); - for (int i = 0; i < jsonArray.length(); i++) { - final Object convertedData = convert(jsonArray.get(i).toString(), itemSchema); - data[i] = convertedData; - } - value = data; + private Object convertArray(final String stringData, final TypeSchema ts) + throws IllegalAccessException, InstantiationException { + final JSONArray jsonArray = new JSONArray(stringData); + final TypeSchema itemSchema = ts.getItems(); + + // note here that the type has to be converted in the case of primitives + final Object[] data = (Object[]) + Array.newInstance(mapPrimitive(itemSchema.getTypeClass(this.typeRegistry)), jsonArray.length()); + for (int i = 0; i < jsonArray.length(); i++) { + final Object convertedData = convert(jsonArray.get(i).toString(), itemSchema); + data[i] = convertedData; + } + + return data; + } + + private Object convertNumber(final String stringData, final TypeSchema ts) { + if ("float".equals(ts.getFormat())) { + return Float.parseFloat(stringData); + } + + return Double.parseDouble(stringData); + } + + private Object convertInteger(final String stringData, final TypeSchema ts) { + switch (ts.getFormat()) { + case "int32": + return Integer.parseInt(stringData); + case "int8": + return Byte.parseByte(stringData); + case "int16": + return Short.parseShort(stringData); + case "int64": + return Long.parseLong(stringData); + default: + throw new IllegalArgumentException("Unknown format for integer " + ts.getFormat()); + } + } + + private Object convertString(final String stringData, final TypeSchema ts) { + if ("uint16".equals(ts.getFormat())) { + return stringData.charAt(0); } - return value; + + return stringData; } /** @@ -278,6 +305,7 @@ private Object convert(final String stringData, final TypeSchema ts) * @param ts TypeSchema * @return new object */ + @SuppressWarnings("PMD.AvoidAccessibilityAlteration") Object createComponentInstance(final String format, final String jsonString, final TypeSchema ts) { final DataTypeDefinition dtd = this.typeRegistry.getDataType(format); @@ -296,9 +324,7 @@ Object createComponentInstance(final String format, final String jsonString, fin ts.validate(json); try { final Map fields = dtd.getProperties(); - for (final Iterator iterator = fields.values().iterator(); iterator.hasNext(); ) { - final PropertyDefinition prop = iterator.next(); - + for (final PropertyDefinition prop : fields.values()) { final Field f = prop.getField(); f.setAccessible(true); final Object newValue = convert(json.get(prop.getName()).toString(), prop.getSchema()); diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/impl/ContractExecutionService.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/impl/ContractExecutionService.java index b8803269..509f60d9 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/impl/ContractExecutionService.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/impl/ContractExecutionService.java @@ -28,7 +28,7 @@ public class ContractExecutionService implements ExecutionService { - private static Logger logger = Logger.getLogger(ContractExecutionService.class.getName()); + private static final Logger LOGGER = Logger.getLogger(ContractExecutionService.class.getName()); private final SerializerRegistryImpl serializers; @@ -41,7 +41,7 @@ public ContractExecutionService(final SerializerRegistryImpl serializers) { @Override public Chaincode.Response executeRequest( final TxFunction txFn, final InvocationRequest req, final ChaincodeStub stub) { - logger.fine(() -> "Routing Request" + txFn); + LOGGER.fine(() -> "Routing Request" + txFn); final TxFunction.Routing rd = txFn.getRouting(); Chaincode.Response response; diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/impl/ContractInvocationRequest.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/impl/ContractInvocationRequest.java index a63d2ffc..7a3e0ff3 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/impl/ContractInvocationRequest.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/execution/impl/ContractInvocationRequest.java @@ -6,27 +6,36 @@ package org.hyperledger.fabric.contract.execution.impl; -import java.util.Collections; +import java.nio.charset.StandardCharsets; import java.util.List; -import java.util.stream.Collectors; +import java.util.regex.Pattern; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hyperledger.fabric.contract.execution.InvocationRequest; import org.hyperledger.fabric.shim.ChaincodeStub; -public class ContractInvocationRequest implements InvocationRequest { - private String namespace; - private String method; - private List args = Collections.emptyList(); +public final class ContractInvocationRequest implements InvocationRequest { + @SuppressWarnings("PMD.ProperLogger") // PMD 7.7.0 gives a false positive here + private static final Log LOGGER = LogFactory.getLog(ContractInvocationRequest.class); - private static Log logger = LogFactory.getLog(ContractInvocationRequest.class); + private static final Pattern NS_REGEX = Pattern.compile(":"); + + private final String namespace; + private final String method; + private final List args; /** @param context */ + @SuppressWarnings("PMD.AvoidLiteralsInIfCondition") public ContractInvocationRequest(final ChaincodeStub context) { - final String func = - context.getStringArgs().size() > 0 ? context.getStringArgs().get(0) : null; - final String[] funcParts = func.split(":"); - logger.debug(func); + List funcAndArgs = context.getArgs(); + if (funcAndArgs.isEmpty()) { + throw new IllegalArgumentException("Missing function name"); + } + + final String func = new String(funcAndArgs.get(0), StandardCharsets.UTF_8); + LOGGER.debug(func); + + final String[] funcParts = NS_REGEX.split(func); if (funcParts.length == 2) { namespace = funcParts[0]; method = funcParts[1]; @@ -35,8 +44,10 @@ public ContractInvocationRequest(final ChaincodeStub context) { method = funcParts[0]; } - args = context.getArgs().stream().skip(1).collect(Collectors.toList()); - logger.debug(namespace + " " + method + " " + args); + args = funcAndArgs.subList(1, funcAndArgs.size()); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(namespace + " " + method + " " + args); + } } /** */ diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/metadata/MetadataBuilder.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/metadata/MetadataBuilder.java index c7b5d0c0..e444efa7 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/metadata/MetadataBuilder.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/metadata/MetadataBuilder.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.Serializable; +import java.io.UncheckedIOException; import java.net.URI; import java.util.ArrayList; import java.util.Collection; @@ -40,16 +41,26 @@ *

This class is used to build up the JSON structure to be returned as the metadata It is not a store of information, * rather a set of functional data to process to and from metadata json to the internal data structure */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class MetadataBuilder { - private static Logger logger = Logger.getLogger(MetadataBuilder.class); + private static final Logger LOGGER = Logger.getLogger(MetadataBuilder.class); - private MetadataBuilder() {} + private static final int PADDING = 3; - @SuppressWarnings("serial") - static class MetadataMap extends HashMap { + // Metadata is composed of three primary sections + // each of which is stored in a map + private static Map> contractMap = new HashMap<>(); + private static Map overallInfoMap = new HashMap<>(); + private static Map componentMap = new HashMap<>(); + + // The schema client used to load any other referenced schemas + private static SchemaClient schemaClient = new DefaultSchemaClient(); + + static final class MetadataMap extends HashMap { + private static final long serialVersionUID = 1L; V putIfNotNull(final K key, final V value) { - logger.info(key + " " + value); + LOGGER.info(() -> key + " " + value); if (value != null && !value.toString().isEmpty()) { return put(key, value); } else { @@ -58,14 +69,7 @@ V putIfNotNull(final K key, final V value) { } } - // Metadata is composed of three primary sections - // each of which is stored in a map - private static Map> contractMap = new HashMap<>(); - private static Map overallInfoMap = new HashMap(); - private static Map componentMap = new HashMap(); - - // The schema client used to load any other referenced schemas - private static SchemaClient schemaClient = new DefaultSchemaClient(); + private MetadataBuilder() {} /** * Validation method. @@ -73,7 +77,7 @@ V putIfNotNull(final K key, final V value) { * @throws ValidationException if the metadata is not valid */ public static void validate() { - logger.info("Running schema test validation"); + LOGGER.info("Running schema test validation"); final ClassLoader cl = MetadataBuilder.class.getClassLoader(); try (InputStream contractSchemaInputStream = cl.getResourceAsStream("contract-schema.json"); InputStream jsonSchemaInputStream = cl.getResourceAsStream("json-schema-draft-04-schema.json")) { @@ -88,13 +92,13 @@ public static void validate() { schema.validate(metadata()); } catch (final IOException e) { - throw new RuntimeException(e); + throw new UncheckedIOException(e); } catch (final ValidationException e) { - logger.error(e.getMessage()); + LOGGER.error(e::getMessage); e.getCausingExceptions().stream() .map(ValidationException::getMessage) - .forEach(logger::info); - logger.error(debugString()); + .forEach(LOGGER::info); + LOGGER.error(MetadataBuilder::debugString); throw e; } } @@ -115,8 +119,8 @@ public static void initialize(final RoutingRegistry registry, final TypeRegistry // need to validate that the metadata that has been created is really valid // it should be as it's been created by code, but this is a valuable double // check - logger.info("Validating schema created"); - MetadataBuilder.validate(); + LOGGER.info("Validating schema created"); + validate(); } /** @@ -133,7 +137,7 @@ public static void addComponent(final DataTypeDefinition datatype) { component.put("additionalProperties", false); final Map propertiesMap = datatype.getProperties().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, e -> (e.getValue().getSchema()))); + .collect(Collectors.toMap(Entry::getKey, e -> e.getValue().getSchema())); component.put("properties", propertiesMap); componentMap.put(datatype.getSimpleName(), component); @@ -145,7 +149,7 @@ public static void addComponent(final DataTypeDefinition datatype) { * @param contractDefinition Class of the object to use as a contract * @return the key that the contract class is referred to in the metadata */ - @SuppressWarnings("serial") + @SuppressWarnings("PMD.LooseCoupling") public static String addContract(final ContractDefinition contractDefinition) { final String key = contractDefinition.getName(); @@ -153,40 +157,34 @@ public static String addContract(final ContractDefinition contractDefinition) { final Contract annotation = contractDefinition.getAnnotation(); final Info info = annotation.info(); - final HashMap infoMap = new HashMap(); + final HashMap infoMap = new HashMap<>(); infoMap.put("title", info.title()); infoMap.put("description", info.description()); infoMap.put("termsOfService", info.termsOfService()); - infoMap.put("contact", new MetadataMap() { - { - putIfNotNull("email", info.contact().email()); - putIfNotNull("name", info.contact().name()); - putIfNotNull("url", info.contact().url()); - } - }); - infoMap.put("license", new MetadataMap() { - { - put("name", info.license().name()); - putIfNotNull("url", info.license().url()); - } - }); + + MetadataMap contact = new MetadataMap<>(); + contact.putIfNotNull("email", info.contact().email()); + contact.putIfNotNull("name", info.contact().name()); + contact.putIfNotNull("url", info.contact().url()); + infoMap.put("contact", contact); + + MetadataMap license = new MetadataMap<>(); + license.put("name", info.license().name()); + license.putIfNotNull("url", info.license().url()); + infoMap.put("license", license); + infoMap.put("version", info.version()); - final HashMap contract = new HashMap(); + final HashMap contract = new HashMap<>(); contract.put("name", key); - contract.put("transactions", new ArrayList()); + contract.put("transactions", new ArrayList<>()); contract.put("info", infoMap); contractMap.put(key, contract); - final boolean defaultContract = true; - if (defaultContract) { - overallInfoMap.putAll(infoMap); - } + overallInfoMap.putAll(infoMap); final Collection fns = contractDefinition.getTxFunctions(); - fns.forEach(txFn -> { - MetadataBuilder.addTransaction(txFn, key); - }); + fns.forEach(txFn -> addTransaction(txFn, key)); return key; } @@ -204,7 +202,7 @@ public static void addTransaction(final TxFunction txFunction, final String cont transaction.put("returns", returnSchema); } - final ArrayList tags = new ArrayList(); + final List tags = new ArrayList<>(); tags.add(txFunction.getType()); if (txFunction.getType() == TransactionType.SUBMIT) { // add deprecated tags tags.add(TransactionType.INVOKE); @@ -216,7 +214,7 @@ public static void addTransaction(final TxFunction txFunction, final String cont @SuppressWarnings("unchecked") final List txs = (ArrayList) contract.get("transactions"); - final ArrayList paramsList = new ArrayList(); + final List paramsList = new ArrayList<>(); txFunction.getParamsList().forEach(pd -> { final TypeSchema paramMap = pd.getSchema(); paramMap.put("name", pd.getName()); @@ -225,7 +223,7 @@ public static void addTransaction(final TxFunction txFunction, final String cont transaction.put("parameters", paramsList); - if (tags.size() != 0) { + if (!tags.isEmpty()) { transaction.put("tags", tags.toArray()); transaction.put("name", txFunction.getName()); txs.add(transaction); @@ -241,8 +239,6 @@ public static String getMetadata() { return metadata().toString(); } - private static final int PADDING = 3; - /** * Returns the metadata as a JSON string (spaced out for humans). * @@ -258,15 +254,14 @@ public static String debugString() { * @return JSONObject of the metadata */ private static JSONObject metadata() { - final HashMap metadata = new HashMap(); + final Map metadata = new HashMap<>(); metadata.put("$schema", "https://fabric-shim.github.io/release-1.4/contract-schema.json"); metadata.put("info", overallInfoMap); metadata.put("contracts", contractMap); metadata.put("components", Collections.singletonMap("schemas", componentMap)); - final JSONObject joMetadata = new JSONObject(metadata); - return joMetadata; + return new JSONObject(metadata); } /** @return All the components indexed by name */ diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/metadata/TypeSchema.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/metadata/TypeSchema.java index 79453428..8b11eccd 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/metadata/TypeSchema.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/metadata/TypeSchema.java @@ -9,6 +9,7 @@ import java.lang.reflect.Array; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import org.everit.json.schema.Schema; import org.everit.json.schema.ValidationException; import org.everit.json.schema.loader.SchemaLoader; @@ -24,12 +25,16 @@ * *

Does not include the "schema" top level map */ -@SuppressWarnings("serial") +@SuppressWarnings({"PMD.LooseCoupling", "PMD.GodClass"}) public final class TypeSchema extends HashMap { - private static Logger logger = Logger.getLogger(TypeSchema.class.getName()); + private static final long serialVersionUID = 1L; + private static final Logger LOGGER = Logger.getLogger(TypeSchema.class.getName()); - /** */ - public TypeSchema() {} + private static final String SCHEMA_PROP = "schema"; + private static final String TYPE_PROP = "type"; + private static final String ITEMS_PROP = "items"; + private static final String FORMAT_PROP = "format"; + private static final String INTEGER_TYPE = "integer"; private Object putInternal(final String key, final Object value) { if (value != null && !value.toString().isEmpty()) { @@ -57,26 +62,26 @@ TypeSchema[] putIfNotNull(final String key, final TypeSchema[] value) { /** @return Return Type String */ public String getType() { - if (this.containsKey("schema")) { - final Map intermediateMap = (Map) this.get("schema"); - return (String) intermediateMap.get("type"); + if (this.containsKey(SCHEMA_PROP)) { + final Map intermediateMap = (Map) this.get(SCHEMA_PROP); + return (String) intermediateMap.get(TYPE_PROP); } - return (String) this.get("type"); + return (String) this.get(TYPE_PROP); } /** @return TypeSchema items */ public TypeSchema getItems() { - if (this.containsKey("schema")) { - final Map intermediateMap = (Map) this.get("schema"); - return (TypeSchema) intermediateMap.get("items"); + if (this.containsKey(SCHEMA_PROP)) { + final Map intermediateMap = (Map) this.get(SCHEMA_PROP); + return (TypeSchema) intermediateMap.get(ITEMS_PROP); } - return (TypeSchema) this.get("items"); + return (TypeSchema) this.get(ITEMS_PROP); } /** @return Reference */ public String getRef() { - if (this.containsKey("schema")) { - final Map intermediateMap = (Map) this.get("schema"); + if (this.containsKey(SCHEMA_PROP)) { + final Map intermediateMap = (Map) this.get(SCHEMA_PROP); return (String) intermediateMap.get("$ref"); } return (String) this.get("$ref"); @@ -84,11 +89,11 @@ public String getRef() { /** @return Format */ public String getFormat() { - if (this.containsKey("schema")) { - final Map intermediateMap = (Map) this.get("schema"); - return (String) intermediateMap.get("format"); + if (this.containsKey(SCHEMA_PROP)) { + final Map intermediateMap = (Map) this.get(SCHEMA_PROP); + return (String) intermediateMap.get(FORMAT_PROP); } - return (String) this.get("format"); + return (String) this.get(FORMAT_PROP); } /** @@ -96,65 +101,71 @@ public String getFormat() { * @return Class object */ public Class getTypeClass(final TypeRegistry typeRegistry) { - Class clz = null; - String type = getType(); - if (type == null) { - type = "object"; + String type = Optional.ofNullable(getType()).orElse("object"); + + switch (type) { + case "object": + return getObjectClass(typeRegistry); + case "string": + return getStringClass(); + case INTEGER_TYPE: + return getIntegerClass(); + case "number": + return getNumberClass(); + case "boolean": + return boolean.class; + case "array": + return getArrayClass(typeRegistry); + default: + return null; } + } - if (type.contentEquals("string")) { - final String format = getFormat(); - if (format != null && format.contentEquals("uint16")) { - clz = char.class; - } else { - clz = String.class; - } + private Class getArrayClass(final TypeRegistry typeRegistry) { + final TypeSchema typdef = this.getItems(); + final Class arrayType = typdef.getTypeClass(typeRegistry); + return Array.newInstance(arrayType, 0).getClass(); + } - } else if (type.contentEquals("integer")) { - // need to check the format - final String format = getFormat(); - switch (format) { - case "int8": - clz = byte.class; - break; - case "int16": - clz = short.class; - break; - case "int32": - clz = int.class; - break; - case "int64": - clz = long.class; - break; - default: - throw new RuntimeException("Unknown format for integer of " + format); - } - } else if (type.contentEquals("number")) { - // need to check the format - final String format = getFormat(); - switch (format) { - case "double": - clz = double.class; - break; - case "float": - clz = float.class; - break; - default: - throw new RuntimeException("Unknown format for number of " + format); - } - } else if (type.contentEquals("boolean")) { - clz = boolean.class; - } else if (type.contentEquals("object")) { - final String ref = this.getRef(); - final String format = ref.substring(ref.lastIndexOf("/") + 1); - clz = typeRegistry.getDataType(format).getTypeClass(); - } else if (type.contentEquals("array")) { - final TypeSchema typdef = this.getItems(); - final Class arrayType = typdef.getTypeClass(typeRegistry); - clz = Array.newInstance(arrayType, 0).getClass(); + private Class getNumberClass() { + switch (getFormat()) { + case "double": + return double.class; + case "float": + return float.class; + default: + throw new IllegalArgumentException("Unknown format for number of " + getFormat()); + } + } + + private Class getIntegerClass() { + // need to check the format + switch (getFormat()) { + case "int8": + return byte.class; + case "int16": + return short.class; + case "int32": + return int.class; + case "int64": + return long.class; + default: + throw new IllegalArgumentException("Unknown format for integer of " + getFormat()); } + } + + @SuppressWarnings("PMD.AvoidLiteralsInIfCondition") + private Class getStringClass() { + if ("uint16".equals(getFormat())) { + return char.class; + } + return String.class; + } - return clz; + private Class getObjectClass(final TypeRegistry typeRegistry) { + final String ref = this.getRef(); + final String format = ref.substring(ref.lastIndexOf('/') + 1); + return typeRegistry.getDataType(format).getTypeClass(); } /** @@ -163,81 +174,86 @@ public Class getTypeClass(final TypeRegistry typeRegistry) { * @param clz * @return TypeSchema */ + @SuppressWarnings({"PMD.ReturnEmptyCollectionRatherThanNull", "PMD.AvoidLiteralsInIfCondition"}) public static TypeSchema typeConvert(final Class clz) { - final TypeSchema returnschema = new TypeSchema(); String className = clz.getTypeName(); - if (className == "void") { + + if ("void".equals(className)) { return null; } - TypeSchema schema; + final TypeSchema result = new TypeSchema(); + TypeSchema schema = result; if (clz.isArray()) { - returnschema.put("type", "array"); + result.put(TYPE_PROP, "array"); + schema = new TypeSchema(); + final Class componentClass = clz.getComponentType(); + className = componentClass.getTypeName(); // double check the componentType - final Class componentClass = clz.getComponentType(); if (componentClass.isArray()) { // nested arrays - returnschema.put("items", TypeSchema.typeConvert(componentClass)); + result.put(ITEMS_PROP, typeConvert(componentClass)); } else { - returnschema.put("items", schema); + result.put(ITEMS_PROP, schema); } - - className = componentClass.getTypeName(); - } else { - schema = returnschema; } + updateSchemaForClass(schema, className); + + return result; + } + + @SuppressWarnings("PMD.CyclomaticComplexity") + private static void updateSchemaForClass(final TypeSchema schema, final String className) { switch (className) { case "java.lang.String": - schema.put("type", "string"); - break; + schema.put(TYPE_PROP, "string"); + return; case "char": case "java.lang.Character": - schema.put("type", "string"); - schema.put("format", "uint16"); - break; + schema.put(TYPE_PROP, "string"); + schema.put(FORMAT_PROP, "uint16"); + return; case "byte": case "java.lang.Byte": - schema.put("type", "integer"); - schema.put("format", "int8"); - break; + schema.put(TYPE_PROP, INTEGER_TYPE); + schema.put(FORMAT_PROP, "int8"); + return; case "short": case "java.lang.Short": - schema.put("type", "integer"); - schema.put("format", "int16"); - break; + schema.put(TYPE_PROP, INTEGER_TYPE); + schema.put(FORMAT_PROP, "int16"); + return; case "int": case "java.lang.Integer": - schema.put("type", "integer"); - schema.put("format", "int32"); - break; + schema.put(TYPE_PROP, INTEGER_TYPE); + schema.put(FORMAT_PROP, "int32"); + return; case "long": case "java.lang.Long": - schema.put("type", "integer"); - schema.put("format", "int64"); - break; + schema.put(TYPE_PROP, INTEGER_TYPE); + schema.put(FORMAT_PROP, "int64"); + return; case "double": case "java.lang.Double": - schema.put("type", "number"); - schema.put("format", "double"); - break; + schema.put(TYPE_PROP, "number"); + schema.put(FORMAT_PROP, "double"); + return; case "float": case "java.lang.Float": - schema.put("type", "number"); - schema.put("format", "float"); - break; + schema.put(TYPE_PROP, "number"); + schema.put(FORMAT_PROP, "float"); + return; case "boolean": case "java.lang.Boolean": - schema.put("type", "boolean"); - break; + schema.put(TYPE_PROP, "boolean"); + return; default: schema.put("$ref", "#/components/schemas/" + className.substring(className.lastIndexOf('.') + 1)); } - - return returnschema; } /** @@ -252,8 +268,8 @@ public void validate(final JSONObject obj) { toValidate.put("prop", obj); JSONObject schemaJSON; - if (this.containsKey("schema")) { - schemaJSON = new JSONObject((Map) this.get("schema")); + if (this.containsKey(SCHEMA_PROP)) { + schemaJSON = new JSONObject((Map) this.get(SCHEMA_PROP)); } else { schemaJSON = new JSONObject(this); } @@ -269,8 +285,9 @@ public void validate(final JSONObject obj) { e.getCausingExceptions().stream() .map(ValidationException::getMessage) .forEach(sb::append); - logger.info(sb.toString()); - throw new ContractRuntimeException(sb.toString(), e); + String message = sb.toString(); + LOGGER.info(message); + throw new ContractRuntimeException(message, e); } } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/ContractDefinitionImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/ContractDefinitionImpl.java index 056b3abc..8df5f3ab 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/ContractDefinitionImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/ContractDefinitionImpl.java @@ -24,20 +24,20 @@ *

Contains information about the contract, including transaction functions and unknown transaction routing */ public final class ContractDefinitionImpl implements ContractDefinition { - private static Logger logger = Logger.getLogger(ContractDefinitionImpl.class); + private static final Logger LOGGER = Logger.getLogger(ContractDefinitionImpl.class); private final Map txFunctions = new HashMap<>(); - private String name; + private final String name; private final boolean isDefault; private final Class contractClz; private final Contract contractAnnotation; - private TxFunction unknownTx; + private final TxFunction unknownTx; /** @param cl */ public ContractDefinitionImpl(final Class cl) { final Contract annotation = cl.getAnnotation(Contract.class); - logger.debug(() -> "Class Contract Annotation: " + annotation); + LOGGER.debug(() -> "Class Contract Annotation: " + annotation); final String annotationName = annotation.name(); @@ -52,18 +52,18 @@ public ContractDefinitionImpl(final Class cl) { contractClz = cl; try { - final Method m = cl.getMethod("unknownTransaction", new Class[] {Context.class}); + final Method m = cl.getMethod("unknownTransaction", Context.class); unknownTx = new TxFunctionImpl(m, this); unknownTx.setUnknownTx(true); } catch (NoSuchMethodException | SecurityException e) { final ContractRuntimeException cre = new ContractRuntimeException("Failure to find unknownTransaction method", e); - logger.severe(() -> logger.formatError(cre)); + LOGGER.severe(() -> LOGGER.formatError(cre)); throw cre; } - logger.info(() -> "Found class: " + cl.getCanonicalName()); - logger.debug(() -> "Namespace: " + this.name); + LOGGER.info(() -> "Found class: " + cl.getCanonicalName()); + LOGGER.debug(() -> "Namespace: " + this.name); } @Override @@ -83,13 +83,13 @@ public Class getContractImpl() { @Override public TxFunction addTxFunction(final Method m) { - logger.debug(() -> "Adding method " + m.getName()); + LOGGER.debug(() -> "Adding method " + m.getName()); final TxFunction txFn = new TxFunctionImpl(m, this); final TxFunction previousTxnFn = txFunctions.put(txFn.getName(), txFn); if (previousTxnFn != null) { final String message = String.format("Duplicate transaction method %s", previousTxnFn.getName()); final ContractRuntimeException cre = new ContractRuntimeException(message); - logger.severe(() -> logger.formatError(cre)); + LOGGER.severe(() -> LOGGER.formatError(cre)); throw cre; } return txFn; diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/DataTypeDefinitionImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/DataTypeDefinitionImpl.java index 5165bfbf..8d1ed867 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/DataTypeDefinitionImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/DataTypeDefinitionImpl.java @@ -7,6 +7,7 @@ import java.lang.reflect.Field; import java.util.HashMap; +import java.util.Locale; import java.util.Map; import java.util.stream.Stream; import org.hyperledger.fabric.contract.annotation.Property; @@ -22,6 +23,7 @@ public final class DataTypeDefinitionImpl implements DataTypeDefinition { private final Class clazz; /** @param componentClass */ + @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public DataTypeDefinitionImpl(final Class componentClass) { this.clazz = componentClass; this.name = componentClass.getName(); @@ -39,7 +41,7 @@ public DataTypeDefinitionImpl(final Class componentClass) { for (int i = 0; i < userSupplied.length; i += 2) { final String userKey = userSupplied[i]; Object userValue; - switch (userKey.toLowerCase()) { + switch (userKey.toLowerCase(Locale.getDefault())) { case "title": case "pattern": userValue = userSupplied[i + 1]; diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/RoutingRegistryImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/RoutingRegistryImpl.java index 5996137c..49180d38 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/RoutingRegistryImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/RoutingRegistryImpl.java @@ -96,8 +96,7 @@ public TxFunction.Routing getRoute(final InvocationRequest request) { @Override public TxFunction getTxFn(final InvocationRequest request) { - final TxFunction txFunction = contracts.get(request.getNamespace()).getTxFunction(request.getMethod()); - return txFunction; + return contracts.get(request.getNamespace()).getTxFunction(request.getMethod()); } /* @@ -145,7 +144,7 @@ public void findAndSetContracts(final TypeRegistry typeRegistry) { final List> dataTypeClasses = new ArrayList<>(); try (ScanResult scanResult = classGraph.scan()) { for (final ClassInfo classInfo : scanResult.getClassesWithAnnotation(Contract.class.getCanonicalName())) { - logger.debug("Found class with contract annotation: " + classInfo.getName()); + logger.debug(() -> "Found class with contract annotation: " + classInfo.getName()); try { final Class contractClass = classInfo.loadClass(); logger.debug("Loaded class"); @@ -155,18 +154,18 @@ public void findAndSetContracts(final TypeRegistry typeRegistry) { // compatible, // and not some random class with the same name. logger.debug("Class does not have compatible contract annotation"); - } else if (!ContractInterface.class.isAssignableFrom(contractClass)) { - logger.debug("Class is not assignable from ContractInterface"); - } else { + } else if (ContractInterface.class.isAssignableFrom(contractClass)) { logger.debug("Class is assignable from ContractInterface"); contractClasses.add((Class) contractClass); + } else { + logger.debug("Class is not assignable from ContractInterface"); } } catch (final IllegalArgumentException e) { - logger.debug("Failed to load class: " + e); + logger.debug(() -> "Failed to load class: " + e); } } for (final ClassInfo classInfo : scanResult.getClassesWithAnnotation(DataType.class.getCanonicalName())) { - logger.debug("Found class with data type annotation: " + classInfo.getName()); + logger.debug(() -> "Found class with data type annotation: " + classInfo.getName()); try { final Class dataTypeClass = classInfo.loadClass(); logger.debug("Loaded class"); @@ -181,7 +180,7 @@ public void findAndSetContracts(final TypeRegistry typeRegistry) { dataTypeClasses.add(dataTypeClass); } } catch (final IllegalArgumentException e) { - logger.debug("Failed to load class: " + e); + logger.debug(() -> "Failed to load class: " + e); } } } @@ -206,7 +205,7 @@ private void addContracts(final List> contractClasses) logger.debug("Searching annotated methods"); for (final Method m : contractClass.getMethods()) { if (m.getAnnotation(Transaction.class) != null) { - logger.debug("Found annotated method " + m.getName()); + logger.debug(() -> "Found annotated method " + m.getName()); contract.addTxFunction(m); } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/SerializerRegistryImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/SerializerRegistryImpl.java index 3e7c1f74..728966f0 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/SerializerRegistryImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/SerializerRegistryImpl.java @@ -22,13 +22,10 @@ *

It holds the serializers that have been defined. JSONTransactionSerializer is the default. */ public class SerializerRegistryImpl { - private static Logger logger = Logger.getLogger(SerializerRegistryImpl.class); + private static final Logger LOGGER = Logger.getLogger(SerializerRegistryImpl.class); private final Class annotationClass = Serializer.class; - /** */ - public SerializerRegistryImpl() {} - // Could index these by name and or type. private final Map contents = new HashMap<>(); @@ -45,17 +42,14 @@ public SerializerInterface getSerializer(final String name, final Serializer.TAR } private SerializerInterface add( - final String name, final Serializer.TARGET target, final Class clazz) { - logger.debug(() -> "Adding new Class " + clazz.getCanonicalName() + " for " + target); - try { - final String key = name + ":" + target; - final SerializerInterface newObj = clazz.newInstance(); - this.contents.put(key, newObj); + final String name, final Serializer.TARGET target, final Class clazz) + throws InstantiationException, IllegalAccessException { + LOGGER.debug(() -> "Adding new Class " + clazz.getCanonicalName() + " for " + target); + final String key = name + ":" + target; + final SerializerInterface newObj = clazz.newInstance(); + this.contents.put(key, newObj); - return newObj; - } catch (InstantiationException | IllegalAccessException e) { - throw new RuntimeException(e); - } + return newObj; } /** @@ -75,19 +69,14 @@ public void findAndSetContents() throws InstantiationException, IllegalAccessExc try (ScanResult scanResult = classGraph.scan()) { for (final ClassInfo classInfo : scanResult.getClassesWithAnnotation(this.annotationClass.getCanonicalName())) { - logger.debug("Found class with contract annotation: " + classInfo.getName()); - try { - final Class cls = (Class) classInfo.loadClass(); - logger.debug("Loaded class"); - - final String className = cls.getCanonicalName(); - if (!seenClass.contains(className)) { - seenClass.add(className); - this.add(className, Serializer.TARGET.TRANSACTION, cls); - } + LOGGER.debug(() -> "Found class with contract annotation: " + classInfo.getName()); + final Class cls = (Class) classInfo.loadClass(); + LOGGER.debug("Loaded class"); - } catch (final IllegalArgumentException e) { - logger.debug("Failed to load class: " + e); + final String className = cls.getCanonicalName(); + if (!seenClass.contains(className)) { + seenClass.add(className); + this.add(className, Serializer.TARGET.TRANSACTION, cls); } } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/TxFunctionImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/TxFunctionImpl.java index 3f61350a..26f336f5 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/TxFunctionImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/TxFunctionImpl.java @@ -7,9 +7,10 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import java.util.ArrayList; +import java.lang.reflect.Parameter; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import org.hyperledger.fabric.Logger; import org.hyperledger.fabric.contract.Context; import org.hyperledger.fabric.contract.ContractInterface; @@ -23,18 +24,17 @@ import org.hyperledger.fabric.contract.routing.TxFunction; public final class TxFunctionImpl implements TxFunction { - private static Logger logger = Logger.getLogger(TxFunctionImpl.class); + private static final Logger LOGGER = Logger.getLogger(TxFunctionImpl.class); private final Method method; private String name; private TransactionType type; - private TransactionType typeDeprecated; private final Routing routing; private TypeSchema returnSchema; - private List paramsList = new ArrayList<>(); + private List paramsList; private boolean isUnknownTx; - public final class RoutingImpl implements Routing { + public static final class RoutingImpl implements Routing { private final Method method; private final Class clazz; @@ -88,7 +88,7 @@ public TxFunctionImpl(final Method m, final ContractDefinition contract) { this.method = m; if (m.getAnnotation(Transaction.class) != null) { - logger.debug("Found Transaction method: " + m.getName()); + LOGGER.debug(() -> "Found Transaction method: " + m.getName()); if (m.getAnnotation(Transaction.class).intent() == Transaction.TYPE.SUBMIT) { this.type = TransactionType.SUBMIT; } else { @@ -113,18 +113,18 @@ public TxFunctionImpl(final Method m, final ContractDefinition contract) { this.returnSchema = TypeSchema.typeConvert(m.getReturnType()); // parameter processing - final List params = - new ArrayList(Arrays.asList(method.getParameters())); + this.paramsList = buildParameters(m); + } + private List buildParameters(final Method m) { + Parameter[] params = m.getParameters(); // validate the first one is a context object - if (params.size() == 0) { + if (params.length == 0) { throw new ContractRuntimeException("First argument should be of type Context"); - } else if (!Context.class.isAssignableFrom(params.get(0).getType())) { - throw new ContractRuntimeException("First argument should be of type Context " + method.getName() + " " - + params.get(0).getType()); - } else { - - params.remove(0); + } + if (!Context.class.isAssignableFrom(params[0].getType())) { + throw new ContractRuntimeException( + "First argument should be of type Context " + m.getName() + " " + params[0].getType()); } // FUTURE: if ever the method of creating the instance where to change, @@ -132,25 +132,27 @@ public TxFunctionImpl(final Method m, final ContractDefinition contract) { // here encapsulating the change. eg use an annotation to define where the // context goes - for (final java.lang.reflect.Parameter parameter : params) { - final TypeSchema paramMap = new TypeSchema(); - final TypeSchema schema = TypeSchema.typeConvert(parameter.getType()); - - final Property annotation = - parameter.getAnnotation(org.hyperledger.fabric.contract.annotation.Property.class); - if (annotation != null) { - final String[] userSupplied = annotation.schema(); - for (int i = 0; i < userSupplied.length; i += 2) { - schema.put(userSupplied[i], userSupplied[i + 1]); - } - } + return Arrays.stream(params) + .skip(1) + .map(TxFunctionImpl::newParameterDefinition) + .collect(Collectors.toList()); + } + + private static ParameterDefinitionImpl newParameterDefinition(final Parameter parameter) { + final TypeSchema paramMap = new TypeSchema(); + final TypeSchema schema = TypeSchema.typeConvert(parameter.getType()); - paramMap.put("name", parameter.getName()); - paramMap.put("schema", schema); - final ParameterDefinition pd = - new ParameterDefinitionImpl(parameter.getName(), parameter.getClass(), paramMap, parameter); - paramsList.add(pd); + final Property annotation = parameter.getAnnotation(Property.class); + if (annotation != null) { + final String[] userSupplied = annotation.schema(); + for (int i = 0; i < userSupplied.length; i += 2) { + schema.put(userSupplied[i], userSupplied[i + 1]); + } } + + paramMap.put("name", parameter.getName()); + paramMap.put("schema", schema); + return new ParameterDefinitionImpl(parameter.getName(), parameter.getClass(), paramMap, parameter); } @Override @@ -169,7 +171,7 @@ public Class getReturnType() { } @Override - public java.lang.reflect.Parameter[] getParameters() { + public Parameter[] getParameters() { return method.getParameters(); } @@ -194,7 +196,7 @@ public List getParamsList() { } /** @param paramsList */ - public void setParamsList(final ArrayList paramsList) { + public void setParamsList(final List paramsList) { this.paramsList = paramsList; } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/TypeRegistryImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/TypeRegistryImpl.java index 07377c77..54d2e995 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/TypeRegistryImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/routing/impl/TypeRegistryImpl.java @@ -14,8 +14,9 @@ /** Registry to hold the complex data types as defined in the contract. */ public final class TypeRegistryImpl implements TypeRegistry { + private static final TypeRegistryImpl INSTANCE = new TypeRegistryImpl(); - private static TypeRegistryImpl singletonInstance; + private final Map components = new HashMap<>(); /** * Get the TypeRegistry singleton instance. @@ -23,15 +24,9 @@ public final class TypeRegistryImpl implements TypeRegistry { * @return TypeRegistry */ public static TypeRegistry getInstance() { - if (singletonInstance == null) { - singletonInstance = new TypeRegistryImpl(); - } - - return singletonInstance; + return INSTANCE; } - private final Map components = new HashMap<>(); - /* * (non-Javadoc) * @@ -68,7 +63,7 @@ public DataTypeDefinition getDataType(final String name) { @Override public DataTypeDefinition getDataType(final TypeSchema schema) { final String ref = schema.getRef(); - final String format = ref.substring(ref.lastIndexOf("/") + 1); + final String format = ref.substring(ref.lastIndexOf('/') + 1); return getDataType(format); } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/systemcontract/SystemContract.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/systemcontract/SystemContract.java index 74fd07ab..762efe66 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/systemcontract/SystemContract.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/systemcontract/SystemContract.java @@ -21,16 +21,12 @@ description = "Provides information about the contracts within this container")) public final class SystemContract implements ContractInterface { - /** */ - public SystemContract() {} - /** * @param ctx * @return Metadata */ - @Transaction(submit = false, name = "GetMetadata") + @Transaction(intent = Transaction.TYPE.EVALUATE, name = "GetMetadata") public String getMetadata(final Context ctx) { - final String jsonmetadata = MetadataBuilder.getMetadata(); - return jsonmetadata; + return MetadataBuilder.getMetadata(); } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/ledger/Ledger.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/ledger/Ledger.java index 3f9c11cc..1d97ec71 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/ledger/Ledger.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/ledger/Ledger.java @@ -33,7 +33,6 @@ public interface Ledger { static Ledger getLedger(final Context ctx) { return new LedgerImpl(ctx); } - ; /** * Return the a collection based on the supplied name. diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/ledger/impl/CollectionImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/ledger/impl/CollectionImpl.java deleted file mode 100644 index c313ef77..00000000 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/ledger/impl/CollectionImpl.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2020 IBM All Rights Reserved. - * - * SPDX-License-Identifier: Apache-2.0 - */ -package org.hyperledger.fabric.ledger.impl; - -import org.hyperledger.fabric.ledger.Collection; - -/** Placeholder. */ -public class CollectionImpl implements Collection { - - private final String name; - - /** - * @param name - * @param ledgerImpl - */ - public CollectionImpl(final String name, final LedgerImpl ledgerImpl) { - this.name = name; - } - - @Override - public void placeholder() { - // TODO Auto-generated method stub - - } -} diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/ledger/impl/LedgerImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/ledger/impl/LedgerImpl.java index 9f8ed761..5d204ba2 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/ledger/impl/LedgerImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/ledger/impl/LedgerImpl.java @@ -8,26 +8,27 @@ import org.hyperledger.fabric.contract.Context; import org.hyperledger.fabric.ledger.Collection; import org.hyperledger.fabric.ledger.Ledger; -import org.hyperledger.fabric.shim.ChaincodeStub; public final class LedgerImpl implements Ledger { - // The Chaincode Stub or SPI to provide access to the underlying Fabric - // APIs - private final ChaincodeStub stub; - /** * New Ledger Implementation. * * @param ctx Context transactional context to use */ + @SuppressWarnings("PMD.UnusedFormalParameter") public LedgerImpl(final Context ctx) { - this.stub = ctx.getStub(); + // Empty stub } @Override public Collection getCollection(final String name) { - return new CollectionImpl(name, this); + return new Collection() { + @Override + public void placeholder() { + // Empty stub + } + }; } @Override diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/Metrics.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/Metrics.java index 74cd6247..b13e3708 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/Metrics.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/Metrics.java @@ -5,7 +5,6 @@ */ package org.hyperledger.fabric.metrics; -import java.lang.reflect.InvocationTargetException; import java.util.Properties; import java.util.logging.Logger; import org.hyperledger.fabric.metrics.impl.DefaultProvider; @@ -32,6 +31,7 @@ private Metrics() {} * @param props * @return The metrics provide */ + @SuppressWarnings("PMD.AvoidCatchingGenericException") public static MetricsProvider initialize(final Properties props) { if (Boolean.parseBoolean((String) props.get(CHAINCODE_METRICS_ENABLED))) { try { @@ -46,14 +46,8 @@ public static MetricsProvider initialize(final Properties props) { logger.info("Using default metrics provider (logs to org.hyperledger.Performance)"); provider = new DefaultProvider(); } - } catch (ClassNotFoundException - | InstantiationException - | IllegalAccessException - | IllegalArgumentException - | InvocationTargetException - | NoSuchMethodException - | SecurityException e) { - throw new RuntimeException("Unable to start metrics", e); + } catch (Exception e) { + throw new IllegalStateException("Unable to start metrics", e); } } else { // return a 'null' provider diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/MetricsProvider.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/MetricsProvider.java index 9609ea87..aa726b96 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/MetricsProvider.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/MetricsProvider.java @@ -28,8 +28,9 @@ public interface MetricsProvider { * * @param props */ - default void initialize(final Properties props) {} - ; + default void initialize(final Properties props) { + // Do nothing by default + } /** * Pass a reference to this task service for information gathering. This is related specifically to the handling of @@ -37,6 +38,7 @@ default void initialize(final Properties props) {} * * @param taskService */ - default void setTaskMetricsCollector(final TaskMetricsCollector taskService) {} - ; + default void setTaskMetricsCollector(final TaskMetricsCollector taskService) { + // Do nothing by default + } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/impl/DefaultProvider.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/impl/DefaultProvider.java index 841e4164..3f34028d 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/impl/DefaultProvider.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/impl/DefaultProvider.java @@ -15,13 +15,14 @@ /** Simple default provider that logs to the org.hyperledger.Performance logger the basic metrics. */ public final class DefaultProvider implements MetricsProvider { - private static Logger perflogger = Logger.getLogger(Logging.PERFLOGGER); + private static final Logger PERFLOGGER = Logger.getLogger(Logging.PERFLOGGER); + private static final int TIME_INTERVAL = 5000; private TaskMetricsCollector taskService; /** */ public DefaultProvider() { - perflogger.info("Default Metrics Provider started"); + PERFLOGGER.info("Default Metrics Provider started"); } @Override @@ -29,8 +30,6 @@ public void setTaskMetricsCollector(final TaskMetricsCollector taskService) { this.taskService = taskService; } - private static final int TIME_INTERVAL = 5000; - @Override public void initialize(final Properties props) { final Timer metricTimer = new Timer(true); @@ -45,26 +44,22 @@ public void run() { TIME_INTERVAL); } - protected void logMetrics() { - - perflogger.info(() -> { - if (DefaultProvider.this.taskService == null) { + void logMetrics() { + PERFLOGGER.info(() -> { + if (taskService == null) { return "No Metrics Provider service yet"; } - final StringBuilder sb = new StringBuilder(); - sb.append('{'); - sb.append(String.format(" \"active_count\":%d ", DefaultProvider.this.taskService.getActiveCount())) - .append(','); - sb.append(String.format(" \"pool_size\":%d ", DefaultProvider.this.taskService.getPoolSize())) - .append(','); - sb.append(String.format(" \"core_pool_size\":%d ", DefaultProvider.this.taskService.getCorePoolSize())) - .append(','); - sb.append(String.format( - " \"current_task_count\":%d ", DefaultProvider.this.taskService.getCurrentTaskCount())) - .append(','); - sb.append(String.format( - " \"current_queue_depth\":%d ", DefaultProvider.this.taskService.getCurrentQueueCount())); - return sb.append('}').toString(); + return '{' + + String.format(" \"active_count\":%d ", taskService.getActiveCount()) + + ',' + + String.format(" \"pool_size\":%d ", taskService.getPoolSize()) + + ',' + + String.format(" \"core_pool_size\":%d ", taskService.getCorePoolSize()) + + ',' + + String.format(" \"current_task_count\":%d ", taskService.getCurrentTaskCount()) + + ',' + + String.format(" \"current_queue_depth\":%d ", taskService.getCurrentQueueCount()) + + '}'; }); } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/impl/NullProvider.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/impl/NullProvider.java index 19639009..35be1fa6 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/impl/NullProvider.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/impl/NullProvider.java @@ -8,8 +8,4 @@ import org.hyperledger.fabric.metrics.MetricsProvider; /** Very simple provider that does absolutely nothing. Used when metrics are disabled. */ -public class NullProvider implements MetricsProvider { - - /** */ - public NullProvider() {} -} +public class NullProvider implements MetricsProvider {} diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/Chaincode.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/Chaincode.java index b8882487..ca576534 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/Chaincode.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/Chaincode.java @@ -46,6 +46,7 @@ class Response { * @param message a response message. * @param payload a response payload. */ + @SuppressWarnings("PMD.ArrayIsStoredDirectly") public Response(final Status status, final String message, final byte[] payload) { this.statusCode = status.getCode(); this.message = message; @@ -59,6 +60,7 @@ public Response(final Status status, final String message, final byte[] payload) * @param message a response message. * @param payload a response payload. */ + @SuppressWarnings("PMD.ArrayIsStoredDirectly") public Response(final int statusCode, final String message, final byte[] payload) { this.statusCode = statusCode; this.message = message; @@ -101,6 +103,7 @@ public String getMessage() { * * @return payload bytes. */ + @SuppressWarnings("PMD.MethodReturnsInternalArray") public byte[] getPayload() { return payload; } @@ -164,7 +167,7 @@ public static boolean hasStatusForCode(final int code) { } static { - for (final Status status : Status.values()) { + for (final Status status : values()) { CODETOSTATUS.put(status.code, status); } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeBase.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeBase.java index 0ce76bc4..a90e6bda 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeBase.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeBase.java @@ -6,7 +6,6 @@ package org.hyperledger.fabric.shim; -import static java.lang.String.format; import static java.util.logging.Level.ALL; import com.google.protobuf.InvalidProtocolBufferException; @@ -29,6 +28,7 @@ import java.nio.file.Paths; import java.security.Security; import java.util.Base64; +import java.util.Locale; import java.util.Properties; import java.util.logging.Level; import java.util.logging.LogRecord; @@ -56,6 +56,7 @@ * * @see org.hyperledger.fabric.contract */ +@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.GodClass"}) public abstract class ChaincodeBase implements Chaincode { /** */ @@ -64,15 +65,10 @@ public abstract class ChaincodeBase implements Chaincode { /** */ public static final String CORE_CHAINCODE_LOGGING_LEVEL = "CORE_CHAINCODE_LOGGING_LEVEL"; - @Override - public abstract Response init(ChaincodeStub stub); - - @Override - public abstract Response invoke(ChaincodeStub stub); - private static final Logger LOGGER = Logger.getLogger(ChaincodeBase.class.getName()); /** */ + @SuppressWarnings("PMD.AvoidUsingHardCodedIP") public static final String DEFAULT_HOST = "127.0.0.1"; /** */ @@ -83,7 +79,7 @@ public abstract class ChaincodeBase implements Chaincode { private String host = DEFAULT_HOST; private int port = DEFAULT_PORT; - private boolean tlsEnabled = false; + private boolean tlsEnabled; private String tlsClientKeyPath; private String tlsClientCertPath; private String tlsClientKeyFile; @@ -107,11 +103,18 @@ public abstract class ChaincodeBase implements Chaincode { private static final String MAX_INBOUND_MESSAGE_SIZE = "MAX_INBOUND_MESSAGE_SIZE"; private Properties props; private Level logLevel; + private CCState state = CCState.CREATED; static { Security.addProvider(new BouncyCastleProvider()); } + @Override + public abstract Response init(ChaincodeStub stub); + + @Override + public abstract Response invoke(ChaincodeStub stub); + private int getMaxInboundMessageSize() { if (this.props == null) { throw new IllegalStateException("Chaincode config not available"); @@ -129,6 +132,7 @@ private int getMaxInboundMessageSize() { * * @param args command line arguments */ + @SuppressWarnings("PMD.AvoidCatchingGenericException") public void start(final String[] args) { try { initializeLogging(); @@ -188,7 +192,7 @@ protected final void connectToPeer() throws IOException { // has stopped in the peer or the network comms, so also shutdown final StreamObserver requestObserver = chaincodeSupportClient .getStub() - .register(new StreamObserver() { + .register(new StreamObserver<>() { @Override public void onNext(final ChaincodeMessage chaincodeMessage) { // message off to the ITM... @@ -245,7 +249,7 @@ protected StreamObserver connectToPeer(final StreamObserver() { + return new StreamObserver<>() { @Override public void onNext(final ChaincodeMessage chaincodeMessage) { itm.onChaincodeMessage(chaincodeMessage); @@ -273,15 +277,16 @@ protected final void initializeLogging() { "java.util.logging.SimpleFormatter.format", "%1$tH:%1$tM:%1$tS:%1$tL %4$-7.7s %2$-80.80s %5$s%6$s%n"); final Logger rootLogger = Logger.getLogger(""); + var formatter = new SimpleFormatter() { + @Override + public String format(final LogRecord record) { + return Thread.currentThread() + " " + super.format(record); + } + }; + for (final java.util.logging.Handler handler : rootLogger.getHandlers()) { handler.setLevel(ALL); - handler.setFormatter(new SimpleFormatter() { - - @Override - public synchronized String format(final LogRecord record) { - return Thread.currentThread() + " " + super.format(record); - } - }); + handler.setFormatter(formatter); } rootLogger.info("Updated all handlers the format"); @@ -307,7 +312,7 @@ public synchronized String format(final LogRecord record) { private Level mapLevel(final String level) { if (level != null) { - switch (level.toUpperCase().trim()) { + switch (level.toUpperCase(Locale.getDefault()).trim()) { case "CRITICAL": case "ERROR": return Level.SEVERE; @@ -334,7 +339,7 @@ private SocketAddress parseHostPort(final String hostAddrStr) throws URISyntaxEx String host = uri.getHost(); int port = uri.getPort(); - if (uri.getHost() == null || uri.getPort() == -1) { + if (host == null || port == -1) { throw new URISyntaxException(uri.toString(), "URI must have host and port parts"); } @@ -352,28 +357,34 @@ public boolean isServer() { } /** Validate init parameters from env chaincode base. */ + @SuppressWarnings("PMD.CyclomaticComplexity") public void validateOptions() { if (this.id == null || this.id.isEmpty()) { - throw new IllegalArgumentException(format( + throw new IllegalArgumentException(String.format( "The chaincode id must be specified using either the -i or --i command line options or the %s environment variable.", CORE_CHAINCODE_ID_NAME)); } if (this.tlsEnabled) { if (tlsClientCertPath == null) { - throw new IllegalArgumentException( - format("Client key certificate chain (%s) was not specified.", ENV_TLS_CLIENT_CERT_PATH)); + throw new IllegalArgumentException(String.format( + "Client key certificate chain (%s) was not specified.", ENV_TLS_CLIENT_CERT_PATH)); } if (tlsClientKeyPath == null) { throw new IllegalArgumentException( - format("Client key (%s) was not specified.", ENV_TLS_CLIENT_KEY_PATH)); + String.format("Client key (%s) was not specified.", ENV_TLS_CLIENT_KEY_PATH)); } if (tlsClientRootCertPath == null) { - throw new IllegalArgumentException( - format("Peer certificate trust store (%s) was not specified.", CORE_PEER_TLS_ROOTCERT_FILE)); + throw new IllegalArgumentException(String.format( + "Peer certificate trust store (%s) was not specified.", CORE_PEER_TLS_ROOTCERT_FILE)); } } } + @SuppressWarnings({ + "PMD.AvoidLiteralsInIfCondition", + "PMD.AvoidCatchingGenericException", + "PMD.ExceptionAsFlowControl" + }) protected final void processCommandLineOptions(final String[] args) { final Options options = new Options(); options.addOption("a", "peer.address", true, "Address of peer to connect to"); @@ -391,7 +402,7 @@ protected final void processCommandLineOptions(final String[] args) { } final String[] hostArr = hostAddrStr.split(":"); if (hostArr.length == 2) { - port = Integer.valueOf(hostArr[1].trim()); + port = Integer.parseInt(hostArr[1].trim()); host = hostArr[0].trim(); } else { final String msg = String.format( @@ -407,12 +418,13 @@ protected final void processCommandLineOptions(final String[] args) { LOGGER.warning(() -> "cli parsing failed with exception" + Logging.formatError(e)); } - LOGGER.info("<<<<<<<<<<<<>>>>>>>>>>>"); - LOGGER.info("CORE_CHAINCODE_ID_NAME: " + this.id); - LOGGER.info("CORE_PEER_ADDRESS: " + this.host + ":" + this.port); + LOGGER.info(() -> "<<<<<<<<<<<<>>>>>>>>>>>" + "\nCORE_CHAINCODE_ID_NAME: " + + this.id + "\nCORE_PEER_ADDRESS: " + + this.host + ":" + this.port); } /** set fields from env. */ + @SuppressWarnings("PMD.AvoidLiteralsInIfCondition") public final void processEnvironmentOptions() { if (System.getenv().containsKey(CORE_CHAINCODE_ID_NAME)) { @@ -421,7 +433,7 @@ public final void processEnvironmentOptions() { if (System.getenv().containsKey(CORE_PEER_ADDRESS)) { final String[] hostArr = System.getenv(CORE_PEER_ADDRESS).split(":"); if (hostArr.length == 2) { - this.port = Integer.valueOf(hostArr[1].trim()); + this.port = Integer.parseInt(hostArr[1].trim()); this.host = hostArr[0].trim(); } else { final String msg = String.format( @@ -449,18 +461,20 @@ public final void processEnvironmentOptions() { this.tlsClientCertFile = System.getenv(ENV_TLS_CLIENT_CERT_FILE); } - LOGGER.info("<<<<<<<<<<<<>>>>>>>>>>>"); - LOGGER.info("CORE_CHAINCODE_ID_NAME: " + this.id); - LOGGER.info("CORE_PEER_ADDRESS: " + this.host); - LOGGER.info("CORE_PEER_TLS_ENABLED: " + this.tlsEnabled); - LOGGER.info("CORE_PEER_TLS_ROOTCERT_FILE: " + this.tlsClientRootCertPath); - LOGGER.info("CORE_TLS_CLIENT_KEY_PATH: " + this.tlsClientKeyPath); - LOGGER.info("CORE_TLS_CLIENT_CERT_PATH: " + this.tlsClientCertPath); - LOGGER.info("CORE_TLS_CLIENT_KEY_FILE: " + this.tlsClientKeyFile); - LOGGER.info("CORE_TLS_CLIENT_CERT_FILE: " + this.tlsClientCertFile); - LOGGER.info("CORE_PEER_LOCALMSPID: " + this.localMspId); - LOGGER.info("CHAINCODE_SERVER_ADDRESS: " + this.chaincodeServerAddress); - LOGGER.info("LOGLEVEL: " + this.logLevel); + if (LOGGER.isLoggable(Level.INFO)) { + LOGGER.info("<<<<<<<<<<<<>>>>>>>>>>>"); + LOGGER.info("CORE_CHAINCODE_ID_NAME: " + this.id); + LOGGER.info("CORE_PEER_ADDRESS: " + this.host); + LOGGER.info("CORE_PEER_TLS_ENABLED: " + this.tlsEnabled); + LOGGER.info("CORE_PEER_TLS_ROOTCERT_FILE: " + this.tlsClientRootCertPath); + LOGGER.info("CORE_TLS_CLIENT_KEY_PATH: " + this.tlsClientKeyPath); + LOGGER.info("CORE_TLS_CLIENT_CERT_PATH: " + this.tlsClientCertPath); + LOGGER.info("CORE_TLS_CLIENT_KEY_FILE: " + this.tlsClientKeyFile); + LOGGER.info("CORE_TLS_CLIENT_CERT_FILE: " + this.tlsClientCertFile); + LOGGER.info("CORE_PEER_LOCALMSPID: " + this.localMspId); + LOGGER.info("CHAINCODE_SERVER_ADDRESS: " + this.chaincodeServerAddress); + LOGGER.info("LOGLEVEL: " + this.logLevel); + } } /** @@ -490,7 +504,7 @@ public Properties getChaincodeConfig() { props.setProperty(CORE_PEER_ADDRESS, this.host); LOGGER.info("<<<<<<<<<<<<>>>>>>>>>>>"); - LOGGER.info(() -> this.props.toString()); + LOGGER.info(this.props::toString); } return this.props; @@ -646,8 +660,6 @@ public enum CCState { READY } - private CCState state = CCState.CREATED; - /** @return State */ public final CCState getState() { return this.state; diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeException.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeException.java index 7a4b5312..36ec21cf 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeException.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeException.java @@ -62,6 +62,7 @@ public ChaincodeException(final String message, final Throwable cause) { * @param message the detail message. * @param payload the response payload. */ + @SuppressWarnings("PMD.ArrayIsStoredDirectly") public ChaincodeException(final String message, final byte[] payload) { super(message); @@ -75,6 +76,7 @@ public ChaincodeException(final String message, final byte[] payload) { * @param payload the response payload. * @param cause the cause. */ + @SuppressWarnings("PMD.ArrayIsStoredDirectly") public ChaincodeException(final String message, final byte[] payload, final Throwable cause) { super(message, cause); @@ -115,6 +117,7 @@ public ChaincodeException(final String message, final String payload, final Thro * * @return the response payload or {@code null} if there is no response. */ + @SuppressWarnings("PMD.MethodReturnsInternalArray") public byte[] getPayload() { return payload; } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeServerProperties.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeServerProperties.java index af81bfb7..4352fe14 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeServerProperties.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeServerProperties.java @@ -9,21 +9,23 @@ public final class ChaincodeServerProperties { private SocketAddress serverAddress; - private int maxInboundMetadataSize = 100 * 1024 * 1024; // checkstyle:ignore-line:MagicNumber - private int maxInboundMessageSize = 100 * 1024 * 1024; // checkstyle:ignore-line:MagicNumber - private int maxConnectionAgeSeconds = 5; // checkstyle:ignore-line:MagicNumber - private int keepAliveTimeoutSeconds = 20; // checkstyle:ignore-line:MagicNumber - private int permitKeepAliveTimeMinutes = 1; // checkstyle:ignore-line:MagicNumber - private int keepAliveTimeMinutes = 1; // checkstyle:ignore-line:MagicNumber + private int maxInboundMetadataSize = 100 * 1024 * 1024; + private int maxInboundMessageSize = 100 * 1024 * 1024; + private int maxConnectionAgeSeconds = 5; + private int keepAliveTimeoutSeconds = 20; + private int permitKeepAliveTimeMinutes = 1; + private int keepAliveTimeMinutes = 1; private boolean permitKeepAliveWithoutCalls = true; private String keyPassword; private String keyCertChainFile; private String keyFile; private String trustCertCollectionFile; - private boolean tlsEnabled = false; + private boolean tlsEnabled; /** Constructor using default configuration. */ - public ChaincodeServerProperties() {} + public ChaincodeServerProperties() { + // Nothing to do + } /** * Constructor. @@ -39,7 +41,7 @@ public ChaincodeServerProperties() {} * @param permitKeepAliveWithoutCalls whether clients are allowed to send keep-alive HTTP/2 PINGs even if there are * no outstanding RPCs on the connection. */ - // checkstyle:ignore-next-line:ParameterNumber + @SuppressWarnings({"PMD.UnusedFormalParameter", "PMD.NullAssignment"}) public ChaincodeServerProperties( final int portChaincodeServer, final int maxInboundMetadataSize, @@ -174,6 +176,7 @@ public void setKeepAliveTimeMinutes(final int keepAliveTimeMinutes) { * * @return true if clients are allowed to send keep-alive requests without calls; otherwise false. */ + @SuppressWarnings("PMD.BooleanGetMethodName") public boolean getPermitKeepAliveWithoutCalls() { return permitKeepAliveWithoutCalls; } @@ -311,6 +314,7 @@ public void setTlsEnabled(final boolean tlsEnabled) { * * @throws IllegalArgumentException if any properties are not valid. */ + @SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity"}) public void validate() { if (this.getServerAddress() == null) { throw new IllegalArgumentException("chaincodeServerProperties.getServerAddress() must be set"); diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java index 8c223fc4..63a5fab4 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java @@ -26,6 +26,7 @@ * An object which manages the transaction context, provides access to state variables, and supports calls to other * chaincode implementations. */ +@SuppressWarnings("PMD.ExcessivePublicCount") public interface ChaincodeStub { /** diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChatChaincodeWithPeer.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChatChaincodeWithPeer.java index f53d0ad1..c6df3d0f 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChatChaincodeWithPeer.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChatChaincodeWithPeer.java @@ -15,9 +15,10 @@ public class ChatChaincodeWithPeer extends ChaincodeGrpc.ChaincodeImplBase { private static Logger logger = Logger.getLogger(ChatChaincodeWithPeer.class.getName()); - private ChaincodeBase chaincodeBase; + private final ChaincodeBase chaincodeBase; ChatChaincodeWithPeer(final ChaincodeBase chaincodeBase) throws IOException { + super(); if (chaincodeBase == null) { throw new IOException("chaincodeBase can't be null"); } @@ -34,6 +35,7 @@ public class ChatChaincodeWithPeer extends ChaincodeGrpc.ChaincodeImplBase { * @return */ @Override + @SuppressWarnings("PMD.AvoidCatchingGenericException") public StreamObserver connect(final StreamObserver responseObserver) { if (responseObserver == null) { return null; diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/NettyChaincodeServer.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/NettyChaincodeServer.java index df594ed2..14679384 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/NettyChaincodeServer.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/NettyChaincodeServer.java @@ -33,12 +33,14 @@ public NettyChaincodeServer( * @throws IOException problem while start grpc server * @throws InterruptedException thrown when block and awaiting shutdown gprc server */ + @Override public void start() throws IOException, InterruptedException { grpcServer.start(); grpcServer.blockUntilShutdown(); } /** shutdown now grpc server. */ + @Override public void stop() { grpcServer.stop(); } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/NettyGrpcServer.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/NettyGrpcServer.java index bb142337..00e7c2ac 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/NettyGrpcServer.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/NettyGrpcServer.java @@ -16,7 +16,9 @@ import java.io.IOException; import java.nio.file.Paths; import java.util.concurrent.TimeUnit; +import java.util.logging.Level; import java.util.logging.Logger; +import javax.net.ssl.SSLException; /** implementation grpc server with NettyGrpcServer. */ public final class NettyGrpcServer implements GrpcServer { @@ -54,67 +56,76 @@ public NettyGrpcServer(final ChaincodeBase chaincodeBase, final ChaincodeServerP .maxInboundMessageSize(chaincodeServerProperties.getMaxInboundMessageSize()); if (chaincodeServerProperties.isTlsEnabled()) { - final File keyCertChainFile = - Paths.get(chaincodeServerProperties.getKeyCertChainFile()).toFile(); - final File keyFile = - Paths.get(chaincodeServerProperties.getKeyFile()).toFile(); - - SslContextBuilder sslContextBuilder; - if (chaincodeServerProperties.getKeyPassword() == null - || chaincodeServerProperties.getKeyPassword().isEmpty()) { - sslContextBuilder = SslContextBuilder.forServer(keyCertChainFile, keyFile); - } else { - sslContextBuilder = SslContextBuilder.forServer( - keyCertChainFile, keyFile, chaincodeServerProperties.getKeyPassword()); - } - - ApplicationProtocolConfig apn = new ApplicationProtocolConfig( - ApplicationProtocolConfig.Protocol.ALPN, - ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE, - ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT, - ApplicationProtocolNames.HTTP_2); - sslContextBuilder.applicationProtocolConfig(apn); - - if (chaincodeServerProperties.getTrustCertCollectionFile() != null) { - final File trustCertCollectionFile = Paths.get(chaincodeServerProperties.getTrustCertCollectionFile()) - .toFile(); - sslContextBuilder.clientAuth(ClientAuth.REQUIRE); - sslContextBuilder.trustManager(trustCertCollectionFile); - } - - serverBuilder.sslContext(sslContextBuilder.build()); + configureTls(serverBuilder, chaincodeServerProperties); } - LOGGER.info("<<<<<<<<<<<<>>>>>>>>>>>:\n"); - LOGGER.info( - "ServerAddress:" + chaincodeServerProperties.getServerAddress().toString()); - LOGGER.info("MaxInboundMetadataSize:" + chaincodeServerProperties.getMaxInboundMetadataSize()); - LOGGER.info("MaxInboundMessageSize:" + chaincodeServerProperties.getMaxInboundMessageSize()); - LOGGER.info("MaxConnectionAgeSeconds:" + chaincodeServerProperties.getMaxConnectionAgeSeconds()); - LOGGER.info("KeepAliveTimeoutSeconds:" + chaincodeServerProperties.getKeepAliveTimeoutSeconds()); - LOGGER.info("PermitKeepAliveTimeMinutes:" + chaincodeServerProperties.getPermitKeepAliveTimeMinutes()); - LOGGER.info("KeepAliveTimeMinutes:" + chaincodeServerProperties.getKeepAliveTimeMinutes()); - LOGGER.info("PermitKeepAliveWithoutCalls:" + chaincodeServerProperties.getPermitKeepAliveWithoutCalls()); - LOGGER.info("KeyPassword:" + chaincodeServerProperties.getKeyPassword()); - LOGGER.info("KeyCertChainFile:" + chaincodeServerProperties.getKeyCertChainFile()); - LOGGER.info("KeyFile:" + chaincodeServerProperties.getKeyFile()); - LOGGER.info("isTlsEnabled:" + chaincodeServerProperties.isTlsEnabled()); - LOGGER.info("\n"); + if (LOGGER.isLoggable(Level.INFO)) { + LOGGER.info("<<<<<<<<<<<<>>>>>>>>>>>:\n"); + LOGGER.info("ServerAddress:" + + chaincodeServerProperties.getServerAddress().toString()); + LOGGER.info("MaxInboundMetadataSize:" + chaincodeServerProperties.getMaxInboundMetadataSize()); + LOGGER.info("MaxInboundMessageSize:" + chaincodeServerProperties.getMaxInboundMessageSize()); + LOGGER.info("MaxConnectionAgeSeconds:" + chaincodeServerProperties.getMaxConnectionAgeSeconds()); + LOGGER.info("KeepAliveTimeoutSeconds:" + chaincodeServerProperties.getKeepAliveTimeoutSeconds()); + LOGGER.info("PermitKeepAliveTimeMinutes:" + chaincodeServerProperties.getPermitKeepAliveTimeMinutes()); + LOGGER.info("KeepAliveTimeMinutes:" + chaincodeServerProperties.getKeepAliveTimeMinutes()); + LOGGER.info("PermitKeepAliveWithoutCalls:" + chaincodeServerProperties.getPermitKeepAliveWithoutCalls()); + LOGGER.info("KeyPassword:" + chaincodeServerProperties.getKeyPassword()); + LOGGER.info("KeyCertChainFile:" + chaincodeServerProperties.getKeyCertChainFile()); + LOGGER.info("KeyFile:" + chaincodeServerProperties.getKeyFile()); + LOGGER.info("isTlsEnabled:" + chaincodeServerProperties.isTlsEnabled()); + LOGGER.info("\n"); + } this.server = serverBuilder.build(); } + private static void configureTls( + final NettyServerBuilder serverBuilder, final ChaincodeServerProperties chaincodeServerProperties) + throws SSLException { + final File keyCertChainFile = + Paths.get(chaincodeServerProperties.getKeyCertChainFile()).toFile(); + final File keyFile = Paths.get(chaincodeServerProperties.getKeyFile()).toFile(); + + final SslContextBuilder sslContextBuilder; + if (chaincodeServerProperties.getKeyPassword() == null + || chaincodeServerProperties.getKeyPassword().isEmpty()) { + sslContextBuilder = SslContextBuilder.forServer(keyCertChainFile, keyFile); + } else { + sslContextBuilder = + SslContextBuilder.forServer(keyCertChainFile, keyFile, chaincodeServerProperties.getKeyPassword()); + } + + ApplicationProtocolConfig apn = new ApplicationProtocolConfig( + ApplicationProtocolConfig.Protocol.ALPN, + ApplicationProtocolConfig.SelectorFailureBehavior.NO_ADVERTISE, + ApplicationProtocolConfig.SelectedListenerFailureBehavior.ACCEPT, + ApplicationProtocolNames.HTTP_2); + sslContextBuilder.applicationProtocolConfig(apn); + + if (chaincodeServerProperties.getTrustCertCollectionFile() != null) { + final File trustCertCollectionFile = Paths.get(chaincodeServerProperties.getTrustCertCollectionFile()) + .toFile(); + sslContextBuilder.clientAuth(ClientAuth.REQUIRE); + sslContextBuilder.trustManager(trustCertCollectionFile); + } + + serverBuilder.sslContext(sslContextBuilder.build()); + } + /** * start grpc server. * * @throws IOException */ + @SuppressWarnings("PMD.SystemPrintln") + @Override public void start() throws IOException { LOGGER.info("start grpc server"); Runtime.getRuntime().addShutdownHook(new Thread(() -> { // Use stderr here since the logger may have been reset by its JVM shutdown hook. System.err.println("*** shutting down gRPC server since JVM is shutting down"); - NettyGrpcServer.this.stop(); + stop(); System.err.println("*** server shut down"); })); server.start(); @@ -125,12 +136,14 @@ public void start() throws IOException { * * @throws InterruptedException */ + @Override public void blockUntilShutdown() throws InterruptedException { LOGGER.info("Waits for the server to become terminated."); server.awaitTermination(); } /** shutdown now grpc server. */ + @Override public void stop() { LOGGER.info("shutdown now grpc server."); server.shutdownNow(); diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ResponseUtils.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ResponseUtils.java index bd6ee06e..ab7419fa 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ResponseUtils.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ResponseUtils.java @@ -85,15 +85,12 @@ public static Chaincode.Response newErrorResponse(final Throwable throwable) { // logged logger.error(() -> logger.formatError(throwable)); - String message = null; - byte[] payload = null; if (throwable instanceof ChaincodeException) { - message = throwable.getMessage(); - payload = ((ChaincodeException) throwable).getPayload(); + String message = throwable.getMessage(); + byte[] payload = ((ChaincodeException) throwable).getPayload(); return new Chaincode.Response(INTERNAL_SERVER_ERROR, message, payload); - } else { - message = "Unexpected error"; - return ResponseUtils.newErrorResponse(message, payload); } + + return newErrorResponse("Unexpected error", null); } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ext/sbe/StateBasedEndorsement.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ext/sbe/StateBasedEndorsement.java index c390ef90..df22a7b9 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ext/sbe/StateBasedEndorsement.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ext/sbe/StateBasedEndorsement.java @@ -48,12 +48,21 @@ public interface StateBasedEndorsement { List listOrgs(); /** RoleType of an endorsement policy's identity. */ + @SuppressWarnings("PMD.FieldNamingConventions") enum RoleType { /** RoleTypeMember identifies an org's member identity. */ RoleTypeMember("MEMBER"), /** RoleTypePeer identifies an org's peer identity. */ RoleTypePeer("PEER"); + private static final Map reverseLookup = new HashMap<>(); + + static { + for (final RoleType item : values()) { + reverseLookup.put(item.getVal(), item); + } + } + private final String val; RoleType(final String val) { @@ -65,14 +74,6 @@ public String getVal() { return val; } - private static Map reverseLookup = new HashMap<>(); - - static { - for (final RoleType item : RoleType.values()) { - reverseLookup.put(item.getVal(), item); - } - } - /** * @param val * @return RoleType diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementFactory.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementFactory.java index f93fc6ce..9a040a59 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementFactory.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementFactory.java @@ -9,14 +9,11 @@ /** Factory for {@link StateBasedEndorsement} objects. */ public class StateBasedEndorsementFactory { - private static StateBasedEndorsementFactory instance; + private static final StateBasedEndorsementFactory INSTANCE = new StateBasedEndorsementFactory(); /** @return Endorsement Factory */ - public static synchronized StateBasedEndorsementFactory getInstance() { - if (instance == null) { - instance = new StateBasedEndorsementFactory(); - } - return instance; + public static StateBasedEndorsementFactory getInstance() { + return INSTANCE; } /** diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementImpl.java index 68a67369..1226a3cb 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementImpl.java @@ -23,7 +23,8 @@ /** Implements {@link StateBasedEndorsement}. */ public final class StateBasedEndorsementImpl implements StateBasedEndorsement { - private static Log logger = LogFactory.getLog(StateBasedEndorsementImpl.class); + @SuppressWarnings("PMD.ProperLogger") // PMD 7.7.0 reports a false positive + private static final Log LOGGER = LogFactory.getLog(StateBasedEndorsementImpl.class); private final Map orgs = new HashMap<>(); @@ -86,7 +87,7 @@ private void addOrg(final MSPPrincipal identity) { final MSPRole mspRole = MSPRole.parseFrom(identity.getPrincipal()); orgs.put(mspRole.getMspIdentifier(), mspRole.getRole()); } catch (final InvalidProtocolBufferException e) { - logger.warn("error unmarshalling msp principal"); + LOGGER.warn("error unmarshalling msp principal"); throw new IllegalArgumentException("error unmarshalling msp principal", e); } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeInvocationTask.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeInvocationTask.java index b2c3c916..1f502b36 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeInvocationTask.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeInvocationTask.java @@ -13,11 +13,14 @@ import com.google.protobuf.InvalidProtocolBufferException; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.StatusCode; +import java.security.NoSuchAlgorithmException; import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; import java.util.function.Consumer; import java.util.logging.Logger; import org.hyperledger.fabric.Logging; +import org.hyperledger.fabric.contract.ContractRuntimeException; import org.hyperledger.fabric.protos.peer.ChaincodeMessage; import org.hyperledger.fabric.protos.peer.ChaincodeMessage.Type; import org.hyperledger.fabric.shim.Chaincode; @@ -25,10 +28,11 @@ import org.hyperledger.fabric.traces.Traces; /** A 'Callable' implementation the has the job of invoking the chaincode, and matching the response and requests. */ +@SuppressWarnings("PMD.MoreThanOneLogger") public class ChaincodeInvocationTask implements Callable { - private static Logger logger = Logger.getLogger(ChaincodeInvocationTask.class.getName()); - private static Logger perfLogger = Logger.getLogger(Logging.PERFLOGGER); + private static final Logger LOGGER = Logger.getLogger(ChaincodeInvocationTask.class.getName()); + private static final Logger PERFLOGGER = Logger.getLogger(Logging.PERFLOGGER); private final String key; private final Type type; @@ -40,7 +44,7 @@ public class ChaincodeInvocationTask implements Callable { // up if there's no body waiting. // // Usual case should be the main thread is waiting for something to come back - private final ArrayBlockingQueue postbox = new ArrayBlockingQueue<>(2, true); + private final BlockingQueue postbox = new ArrayBlockingQueue<>(2, true); private final ChaincodeMessage message; private final Chaincode chaincode; @@ -67,13 +71,14 @@ public ChaincodeInvocationTask( /** Main method to power the invocation of the chaincode. */ @Override + @SuppressWarnings("PMD.AvoidCatchingGenericException") public ChaincodeMessage call() { ChaincodeMessage finalResponseMessage; Span span = null; try { try { - perfLogger.fine(() -> "> task:start TX::" + this.txId); + PERFLOGGER.fine(() -> "> task:start TX::" + this.txId); // A key interface for the chaincode's invoke() method implementation // is the 'ChaincodeStub' interface. An instance of this is created @@ -87,7 +92,7 @@ public ChaincodeMessage call() { // result is what will be sent to the peer as a response to this invocation final Chaincode.Response result; - perfLogger.fine(() -> "> task:invoke TX::" + this.txId); + PERFLOGGER.fine(() -> "> task:invoke TX::" + this.txId); // Call chaincode's invoke // Note in Fabric v2, there won't be any INIT @@ -97,11 +102,11 @@ public ChaincodeMessage call() { result = chaincode.invoke(stub); } - perfLogger.fine(() -> "< task:invoke TX::" + this.txId); + PERFLOGGER.fine(() -> "< task:invoke TX::" + this.txId); if (result.getStatus().getCode() >= Chaincode.Response.Status.INTERNAL_SERVER_ERROR.getCode()) { // Send ERROR with entire result.Message as payload - logger.severe(() -> String.format( + LOGGER.severe(() -> String.format( "[%-8.8s] Invoke failed with error code %d. Sending %s", message.getTxid(), result.getStatus().getCode(), ERROR)); finalResponseMessage = ChaincodeMessageFactory.newCompletedEventMessage( @@ -111,14 +116,14 @@ public ChaincodeMessage call() { } } else { // Send COMPLETED with entire result as payload - logger.fine( + LOGGER.fine( () -> String.format("[%-8.8s] Invoke succeeded. Sending %s", message.getTxid(), COMPLETED)); finalResponseMessage = ChaincodeMessageFactory.newCompletedEventMessage( message.getChannelId(), message.getTxid(), result, stub.getEvent()); } - } catch (InvalidProtocolBufferException | RuntimeException e) { - logger.severe( + } catch (InvalidProtocolBufferException | NoSuchAlgorithmException | RuntimeException e) { + LOGGER.severe( () -> String.format("[%-8.8s] Invoke failed. Sending %s: %s", message.getTxid(), ERROR, e)); finalResponseMessage = ChaincodeMessageFactory.newErrorEventMessage(message.getChannelId(), message.getTxid(), e); @@ -129,7 +134,7 @@ public ChaincodeMessage call() { // send the final response message to the peer outgoingMessageConsumer.accept(finalResponseMessage); - perfLogger.fine(() -> "< task:end TX::" + this.txId); + PERFLOGGER.fine(() -> "< task:end TX::" + this.txId); } finally { if (span != null) { span.end(); @@ -151,11 +156,22 @@ public String getTxKey() { /** * Use the Key as to determine equality. * - * @param task + * @param other * @return equality */ - public boolean equals(final ChaincodeInvocationTask task) { - return key.equals(task.getTxKey()); + @Override + public boolean equals(final Object other) { + if (!(other instanceof ChaincodeInvocationTask)) { + return false; + } + + ChaincodeInvocationTask that = (ChaincodeInvocationTask) other; + return this.key.equals(that.getTxKey()); + } + + @Override + public int hashCode() { + return key.hashCode(); } /** @@ -190,33 +206,33 @@ public void postMessage(final ChaincodeMessage msg) throws InterruptedException protected ByteString invoke(final ChaincodeMessage message) { // send the message - logger.fine(() -> "Task Sending message to the peer " + message.getTxid()); + LOGGER.fine(() -> "Task Sending message to the peer " + message.getTxid()); outgoingMessageConsumer.accept(message); // wait for response ChaincodeMessage response; try { - perfLogger.fine(() -> "> task:answer TX::" + message.getTxid()); + PERFLOGGER.fine(() -> "> task:answer TX::" + message.getTxid()); response = postbox.take(); - perfLogger.fine(() -> "< task:answer TX::" + message.getTxid()); + PERFLOGGER.fine(() -> "< task:answer TX::" + message.getTxid()); } catch (final InterruptedException e) { - logger.severe(() -> "Interrupted exchanging messages "); - throw new RuntimeException(String.format("[%-8.8s]InterruptedException received.", txId), e); + LOGGER.severe(() -> "Interrupted exchanging messages "); + throw new ContractRuntimeException(String.format("[%-8.8s]InterruptedException received.", txId), e); } // handle response switch (response.getType()) { case RESPONSE: - logger.fine(() -> String.format("[%-8.8s] Successful response received.", txId)); + LOGGER.fine(() -> String.format("[%-8.8s] Successful response received.", txId)); return response.getPayload(); case ERROR: - logger.severe(() -> String.format("[%-8.8s] Unsuccessful response received.", txId)); - throw new RuntimeException(String.format("[%-8.8s]Unsuccessful response received.", txId)); + LOGGER.severe(() -> String.format("[%-8.8s] Unsuccessful response received.", txId)); + throw new ContractRuntimeException(String.format("[%-8.8s]Unsuccessful response received.", txId)); default: - logger.severe(() -> String.format( + LOGGER.severe(() -> String.format( "[%-8.8s] Unexpected %s response received. Expected %s or %s.", txId, response.getType(), RESPONSE, ERROR)); - throw new RuntimeException(String.format( + throw new IllegalStateException(String.format( "[%-8.8s] Unexpected %s response received. Expected %s or %s.", txId, response.getType(), RESPONSE, ERROR)); } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeMessageFactory.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeMessageFactory.java index 651bc81a..199a1b75 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeMessageFactory.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeMessageFactory.java @@ -37,7 +37,7 @@ public final class ChaincodeMessageFactory { private ChaincodeMessageFactory() {} - protected static ChaincodeMessage newGetPrivateDataHashEventMessage( + static ChaincodeMessage newGetPrivateDataHashEventMessage( final String channelId, final String txId, final String collection, final String key) { return newEventMessage( GET_PRIVATE_DATA_HASH, @@ -50,7 +50,7 @@ protected static ChaincodeMessage newGetPrivateDataHashEventMessage( .toByteString()); } - protected static ChaincodeMessage newGetStateEventMessage( + static ChaincodeMessage newGetStateEventMessage( final String channelId, final String txId, final String collection, final String key) { return newEventMessage( GET_STATE, @@ -63,7 +63,7 @@ protected static ChaincodeMessage newGetStateEventMessage( .toByteString()); } - protected static ChaincodeMessage newGetStateMetadataEventMessage( + static ChaincodeMessage newGetStateMetadataEventMessage( final String channelId, final String txId, final String collection, final String key) { return newEventMessage( GET_STATE_METADATA, @@ -76,7 +76,7 @@ protected static ChaincodeMessage newGetStateMetadataEventMessage( .toByteString()); } - protected static ChaincodeMessage newPutStateEventMessage( + static ChaincodeMessage newPutStateEventMessage( final String channelId, final String txId, final String collection, @@ -94,7 +94,7 @@ protected static ChaincodeMessage newPutStateEventMessage( .toByteString()); } - protected static ChaincodeMessage newPutStateMetadataEventMessage( + static ChaincodeMessage newPutStateMetadataEventMessage( final String channelId, final String txId, final String collection, @@ -116,7 +116,7 @@ protected static ChaincodeMessage newPutStateMetadataEventMessage( .toByteString()); } - protected static ChaincodeMessage newDeleteStateEventMessage( + static ChaincodeMessage newDeleteStateEventMessage( final String channelId, final String txId, final String collection, final String key) { return newEventMessage( DEL_STATE, @@ -129,7 +129,7 @@ protected static ChaincodeMessage newDeleteStateEventMessage( .toByteString()); } - protected static ChaincodeMessage newPurgeStateEventMessage( + static ChaincodeMessage newPurgeStateEventMessage( final String channelId, final String txId, final String collection, final String key) { return newEventMessage( Type.PURGE_PRIVATE_DATA, @@ -142,46 +142,43 @@ protected static ChaincodeMessage newPurgeStateEventMessage( .toByteString()); } - protected static ChaincodeMessage newErrorEventMessage( - final String channelId, final String txId, final Throwable throwable) { + static ChaincodeMessage newErrorEventMessage(final String channelId, final String txId, final Throwable throwable) { return newErrorEventMessage(channelId, txId, printStackTrace(throwable)); } - protected static ChaincodeMessage newErrorEventMessage( - final String channelId, final String txId, final String message) { + static ChaincodeMessage newErrorEventMessage(final String channelId, final String txId, final String message) { return newErrorEventMessage(channelId, txId, message, null); } - protected static ChaincodeMessage newErrorEventMessage( + static ChaincodeMessage newErrorEventMessage( final String channelId, final String txId, final String message, final ChaincodeEvent event) { return newEventMessage(ERROR, channelId, txId, ByteString.copyFromUtf8(message), event); } - protected static ChaincodeMessage newCompletedEventMessage( + static ChaincodeMessage newCompletedEventMessage( final String channelId, final String txId, final Chaincode.Response response, final ChaincodeEvent event) { - final ChaincodeMessage message = newEventMessage( + return newEventMessage( COMPLETED, channelId, txId, toProtoResponse(response).toByteString(), event); - return message; } - protected static ChaincodeMessage newInvokeChaincodeMessage( + static ChaincodeMessage newInvokeChaincodeMessage( final String channelId, final String txId, final ByteString payload) { return newEventMessage(INVOKE_CHAINCODE, channelId, txId, payload, null); } - protected static ChaincodeMessage newRegisterChaincodeMessage(final ChaincodeID chaincodeId) { + static ChaincodeMessage newRegisterChaincodeMessage(final ChaincodeID chaincodeId) { return ChaincodeMessage.newBuilder() .setType(REGISTER) .setPayload(chaincodeId.toByteString()) .build(); } - protected static ChaincodeMessage newEventMessage( + static ChaincodeMessage newEventMessage( final Type type, final String channelId, final String txId, final ByteString payload) { return newEventMessage(type, channelId, txId, payload, null); } - protected static ChaincodeMessage newEventMessage( + static ChaincodeMessage newEventMessage( final Type type, final String channelId, final String txId, diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeSupportClient.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeSupportClient.java index d3a6190d..b55a64f4 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeSupportClient.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeSupportClient.java @@ -22,8 +22,7 @@ public class ChaincodeSupportClient { private static final int DEFAULT_TIMEOUT = 5; - private static final Logger LOGGER = Logger.getLogger(ChaincodeSupportClient.class.getName()); - private static Logger perflogger = Logger.getLogger(Logging.PERFLOGGER); + private static final Logger PERFLOGGER = Logger.getLogger(Logging.PERFLOGGER); private final ManagedChannel channel; private final ChaincodeSupportStub stub; @@ -77,19 +76,14 @@ public void start(final InvocationTaskManager itm, final StreamObserver consumer = new Consumer() { - - // create a lock, with fair property - private final ReentrantLock lock = new ReentrantLock(true); - - @Override - public void accept(final ChaincodeMessage t) { - lock.lock(); - perflogger.fine(() -> "> sendToPeer TX::" + t.getTxid()); - requestObserver.onNext(t); - perflogger.fine(() -> "< sendToPeer TX::" + t.getTxid()); - lock.unlock(); - } + // create a lock, with fair property + final ReentrantLock lock = new ReentrantLock(true); + final Consumer consumer = t -> { + lock.lock(); + PERFLOGGER.fine(() -> "> sendToPeer TX::" + t.getTxid()); + requestObserver.onNext(t); + PERFLOGGER.fine(() -> "< sendToPeer TX::" + t.getTxid()); + lock.unlock(); }; // Pass a Consumer interface back to the the task manager. This is for tasks to @@ -97,7 +91,8 @@ public void accept(final ChaincodeMessage t) { // // NOTE the register() - very important - as this triggers the ITM to send the // first message to the peer; otherwise the both sides will sit there waiting - itm.setResponseConsumer(consumer).register(); + itm.setResponseConsumer(consumer); + itm.register(); } /** diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationStubImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationStubImpl.java index 809b36a5..ec0b1ffc 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationStubImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationStubImpl.java @@ -16,6 +16,7 @@ import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.Timestamp; +import java.io.UncheckedIOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.security.MessageDigest; @@ -25,6 +26,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.function.Function; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -57,6 +59,7 @@ import org.hyperledger.fabric.shim.ledger.QueryResultsIterator; import org.hyperledger.fabric.shim.ledger.QueryResultsIteratorWithMetadata; +@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.GodClass"}) class InvocationStubImpl implements ChaincodeStub { private static final String UNSPECIFIED_START_KEY = new String(Character.toChars(0x000001)); @@ -65,6 +68,24 @@ class InvocationStubImpl implements ChaincodeStub { public static final String MAX_UNICODE_RUNE = "\udbff\udfff"; private static final String CORE_PEER_LOCALMSPID = "CORE_PEER_LOCALMSPID"; + + private static final Function + QUERY_RESULT_BYTES_TO_KEY_MODIFICATION = queryResultBytes -> { + try { + return org.hyperledger.fabric.protos.ledger.queryresult.KeyModification.parseFrom( + queryResultBytes.getResultBytes()); + } catch (final InvalidProtocolBufferException e) { + throw new UncheckedIOException(e); + } + }; + private static final Function QUERY_RESULT_BYTES_TO_KV = queryResultBytes -> { + try { + return KV.parseFrom(queryResultBytes.getResultBytes()); + } catch (final InvalidProtocolBufferException e) { + throw new UncheckedIOException(e); + } + }; + private final String channelId; private final String txId; private final ChaincodeInvocationTask handler; @@ -82,7 +103,7 @@ class InvocationStubImpl implements ChaincodeStub { * @throws InvalidProtocolBufferException */ InvocationStubImpl(final ChaincodeMessage message, final ChaincodeInvocationTask handler) - throws InvalidProtocolBufferException { + throws InvalidProtocolBufferException, NoSuchAlgorithmException { this.channelId = message.getChannelId(); this.txId = message.getTxid(); this.handler = handler; @@ -90,31 +111,35 @@ class InvocationStubImpl implements ChaincodeStub { this.args = Collections.unmodifiableList(input.getArgsList()); this.signedProposal = message.getProposal(); - if (this.signedProposal == null - || this.signedProposal.getProposalBytes().isEmpty()) { + if (this.signedProposal.getProposalBytes().isEmpty()) { this.creator = null; this.txTimestamp = null; this.transientMap = Collections.emptyMap(); this.binding = null; } else { - try { - final Proposal proposal = Proposal.parseFrom(signedProposal.getProposalBytes()); - final Header header = Header.parseFrom(proposal.getHeader()); - final ChannelHeader channelHeader = ChannelHeader.parseFrom(header.getChannelHeader()); - validateProposalType(channelHeader); - final SignatureHeader signatureHeader = SignatureHeader.parseFrom(header.getSignatureHeader()); - final ChaincodeProposalPayload chaincodeProposalPayload = - ChaincodeProposalPayload.parseFrom(proposal.getPayload()); - final Timestamp timestamp = channelHeader.getTimestamp(); - - this.txTimestamp = Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos()); - this.creator = signatureHeader.getCreator(); - this.transientMap = chaincodeProposalPayload.getTransientMapMap(); - this.binding = computeBinding(channelHeader, signatureHeader); - } catch (InvalidProtocolBufferException | NoSuchAlgorithmException e) { - throw new RuntimeException(e); + final Proposal proposal = Proposal.parseFrom(signedProposal.getProposalBytes()); + final Header header = Header.parseFrom(proposal.getHeader()); + final ChannelHeader channelHeader = ChannelHeader.parseFrom(header.getChannelHeader()); + validateProposalType(channelHeader); + final SignatureHeader signatureHeader = SignatureHeader.parseFrom(header.getSignatureHeader()); + final ChaincodeProposalPayload chaincodeProposalPayload = + ChaincodeProposalPayload.parseFrom(proposal.getPayload()); + final Timestamp timestamp = channelHeader.getTimestamp(); + + this.txTimestamp = Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos()); + this.creator = signatureHeader.getCreator(); + this.transientMap = chaincodeProposalPayload.getTransientMapMap(); + this.binding = computeBinding(channelHeader, signatureHeader); + } + } + + private static boolean isEmptyString(final String str) { + for (int i = 0; i < str.length(); i++) { + if (!Character.isWhitespace(str.charAt(i))) { + return false; } } + return true; } private byte[] computeBinding(final ChannelHeader channelHeader, final SignatureHeader signatureHeader) @@ -135,24 +160,24 @@ private void validateProposalType(final ChannelHeader channelHeader) { case CONFIG: return; default: - throw new RuntimeException(String.format( + throw new IllegalArgumentException(String.format( "Unexpected transaction type: %s", HeaderType.forNumber(channelHeader.getType()))); } } @Override public List getArgs() { - return args.stream().map(x -> x.toByteArray()).collect(Collectors.toList()); + return args.stream().map(ByteString::toByteArray).collect(toList()); } @Override public List getStringArgs() { - return args.stream().map(x -> x.toStringUtf8()).collect(Collectors.toList()); + return args.stream().map(ByteString::toStringUtf8).collect(toList()); } @Override public String getFunction() { - return getStringArgs().size() > 0 ? getStringArgs().get(0) : null; + return getStringArgs().isEmpty() ? null : getStringArgs().get(0); } @Override @@ -162,7 +187,7 @@ public List getParameters() { @Override public void setEvent(final String name, final byte[] payload) { - if (name == null || name.trim().isEmpty()) { + if (null == name || isEmptyString(name)) { throw new IllegalArgumentException("event name can not be nil string"); } if (payload != null) { @@ -198,6 +223,7 @@ public byte[] getState(final String key) { } @Override + @SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull") public byte[] getStateValidationParameter(final String key) { final ByteString payload = @@ -215,8 +241,8 @@ public byte[] getStateValidationParameter(final String key) { .toByteArray(); } } catch (final InvalidProtocolBufferException e) { - LOGGER.severe(String.format("[%-8.8s] unmarshalling error", txId)); - throw new RuntimeException("Error unmarshalling StateMetadataResult.", e); + LOGGER.severe(() -> String.format("[%-8.8s] unmarshalling error", txId)); + throw new UncheckedIOException("Error unmarshalling StateMetadataResult.", e); } return null; @@ -273,21 +299,10 @@ private QueryResultsIterator executeGetStateByRange( ChaincodeMessageFactory.newEventMessage(GET_STATE_BY_RANGE, channelId, txId, requestPayload); final ByteString response = handler.invoke(requestMessage); - return new QueryResultsIteratorImpl( - this.handler, channelId, txId, response, queryResultBytesToKv.andThen(KeyValueImpl::new)); + return new QueryResultsIteratorImpl<>( + this.handler, channelId, txId, response, QUERY_RESULT_BYTES_TO_KV.andThen(KeyValueImpl::new)); } - private final Function queryResultBytesToKv = new Function() { - @Override - public KV apply(final QueryResultBytes queryResultBytes) { - try { - return KV.parseFrom(queryResultBytes.getResultBytes()); - } catch (final InvalidProtocolBufferException e) { - throw new RuntimeException(e); - } - } - }; - @Override public QueryResultsIteratorWithMetadata getStateByRangeWithPagination( final String startKey, final String endKey, final int pageSize, final String bookmark) { @@ -329,7 +344,7 @@ private QueryResultsIteratorWithMetadataImpl executeGetStateByRangeWit final ByteString response = this.handler.invoke(requestMessage); return new QueryResultsIteratorWithMetadataImpl<>( - this.handler, getChannelId(), getTxId(), response, queryResultBytesToKv.andThen(KeyValueImpl::new)); + this.handler, getChannelId(), getTxId(), response, QUERY_RESULT_BYTES_TO_KV.andThen(KeyValueImpl::new)); } @Override @@ -409,8 +424,8 @@ public QueryResultsIterator getQueryResult(final String query) { ChaincodeMessageFactory.newEventMessage(GET_QUERY_RESULT, channelId, txId, requestPayload); final ByteString response = handler.invoke(requestMessage); - return new QueryResultsIteratorImpl( - this.handler, channelId, txId, response, queryResultBytesToKv.andThen(KeyValueImpl::new)); + return new QueryResultsIteratorImpl<>( + this.handler, channelId, txId, response, QUERY_RESULT_BYTES_TO_KV.andThen(KeyValueImpl::new)); } @Override @@ -432,8 +447,8 @@ public QueryResultsIteratorWithMetadata getQueryResultWithPagination( ChaincodeMessageFactory.newEventMessage(GET_QUERY_RESULT, channelId, txId, requestPayload); final ByteString response = handler.invoke(requestMessage); - return new QueryResultsIteratorWithMetadataImpl( - this.handler, channelId, txId, response, queryResultBytesToKv.andThen(KeyValueImpl::new)); + return new QueryResultsIteratorWithMetadataImpl<>( + this.handler, channelId, txId, response, QUERY_RESULT_BYTES_TO_KV.andThen(KeyValueImpl::new)); } @Override @@ -448,29 +463,14 @@ public QueryResultsIterator getHistoryForKey(final String key) ChaincodeMessageFactory.newEventMessage(GET_HISTORY_FOR_KEY, channelId, txId, requestPayload); final ByteString response = handler.invoke(requestMessage); - return new QueryResultsIteratorImpl( + return new QueryResultsIteratorImpl<>( this.handler, channelId, txId, response, - queryResultBytesToKeyModification.andThen(KeyModificationImpl::new)); + QUERY_RESULT_BYTES_TO_KEY_MODIFICATION.andThen(KeyModificationImpl::new)); } - private final Function - queryResultBytesToKeyModification = - new Function() { - @Override - public org.hyperledger.fabric.protos.ledger.queryresult.KeyModification apply( - final QueryResultBytes queryResultBytes) { - try { - return org.hyperledger.fabric.protos.ledger.queryresult.KeyModification.parseFrom( - queryResultBytes.getResultBytes()); - } catch (final InvalidProtocolBufferException e) { - throw new RuntimeException(e); - } - } - }; - @Override public byte[] getPrivateData(final String collection, final String key) { validateCollection(collection); @@ -496,6 +496,7 @@ public byte[] getPrivateDataHash(final String collection, final String key) { } @Override + @SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull") public byte[] getPrivateDataValidationParameter(final String collection, final String key) { validateCollection(collection); @@ -514,8 +515,8 @@ public byte[] getPrivateDataValidationParameter(final String collection, final S .toByteArray(); } } catch (final InvalidProtocolBufferException e) { - LOGGER.severe(String.format("[%-8.8s] unmarshalling error", txId)); - throw new RuntimeException("Error unmarshalling StateMetadataResult.", e); + LOGGER.severe(() -> String.format("[%-8.8s] unmarshalling error", txId)); + throw new UncheckedIOException("Error unmarshalling StateMetadataResult.", e); } return null; @@ -625,8 +626,8 @@ public QueryResultsIterator getPrivateDataQueryResult(final String col ChaincodeMessageFactory.newEventMessage(GET_QUERY_RESULT, channelId, txId, requestPayload); final ByteString response = handler.invoke(requestMessage); - return new QueryResultsIteratorImpl( - this.handler, channelId, txId, response, queryResultBytesToKv.andThen(KeyValueImpl::new)); + return new QueryResultsIteratorImpl<>( + this.handler, channelId, txId, response, QUERY_RESULT_BYTES_TO_KV.andThen(KeyValueImpl::new)); } @Override @@ -634,7 +635,7 @@ public Chaincode.Response invokeChaincode( final String chaincodeName, final List args, final String channel) { // internally we handle chaincode name as a composite name final String compositeName; - if (channel != null && !channel.trim().isEmpty()) { + if (channel != null && !isEmptyString(channel)) { compositeName = chaincodeName + "/" + channel; } else { compositeName = chaincodeName; @@ -644,7 +645,7 @@ public Chaincode.Response invokeChaincode( final ByteString invocationSpecPayload = ChaincodeSpec.newBuilder() .setChaincodeId(ChaincodeID.newBuilder().setName(compositeName).build()) .setInput(ChaincodeInput.newBuilder() - .addAllArgs(args.stream().map(ByteString::copyFrom).collect(Collectors.toList())) + .addAllArgs(args.stream().map(ByteString::copyFrom).collect(toList())) .build()) .build() .toByteString(); @@ -659,7 +660,7 @@ public Chaincode.Response invokeChaincode( final ChaincodeMessage responseMessage = ChaincodeMessage.parseFrom(response); // the actual response message must be of type COMPLETED - LOGGER.fine(String.format( + LOGGER.fine(() -> String.format( "[%-8.8s] %s response received from other chaincode.", txId, responseMessage.getType())); if (responseMessage.getType() == COMPLETED) { @@ -668,14 +669,14 @@ public Chaincode.Response invokeChaincode( return new Chaincode.Response( Chaincode.Response.Status.forCode(r.getStatus()), r.getMessage(), - r.getPayload() == null ? null : r.getPayload().toByteArray()); + r.getPayload().toByteArray()); } else { // error final String message = responseMessage.getPayload().toStringUtf8(); return new Chaincode.Response(Chaincode.Response.Status.INTERNAL_SERVER_ERROR, message, null); } } catch (final InvalidProtocolBufferException e) { - throw new RuntimeException(e); + throw new UncheckedIOException(e); } } @@ -690,6 +691,7 @@ public Instant getTxTimestamp() { } @Override + @SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull") public byte[] getCreator() { if (creator == null) { return null; @@ -700,27 +702,24 @@ public byte[] getCreator() { @Override public Map getTransient() { return transientMap.entrySet().stream() - .collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue().toByteArray())); + .collect(Collectors.toMap(Map.Entry::getKey, x -> x.getValue().toByteArray())); } @Override + @SuppressWarnings("PMD.MethodReturnsInternalArray") public byte[] getBinding() { return this.binding; } private void validateKey(final String key) { - if (key == null) { - throw new NullPointerException("key cannot be null"); - } - if (key.length() == 0) { + Objects.requireNonNull(key, "key cannot be null"); + if (key.isEmpty()) { throw new IllegalArgumentException("key cannot not be an empty string"); } } private void validateCollection(final String collection) { - if (collection == null) { - throw new NullPointerException("collection cannot be null"); - } + Objects.requireNonNull(collection, "collection cannot be null"); if (collection.isEmpty()) { throw new IllegalArgumentException("collection must not be an empty string"); } @@ -731,6 +730,6 @@ public String getMspId() { if (System.getenv().containsKey(CORE_PEER_LOCALMSPID)) { return System.getenv(CORE_PEER_LOCALMSPID); } - throw new RuntimeException("CORE_PEER_LOCALMSPID is unset in chaincode process"); + throw new IllegalStateException("CORE_PEER_LOCALMSPID is unset in chaincode process"); } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationTaskExecutor.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationTaskExecutor.java index c2402e48..1267ac4f 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationTaskExecutor.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationTaskExecutor.java @@ -18,6 +18,8 @@ public final class InvocationTaskExecutor extends ThreadPoolExecutor implements TaskMetricsCollector { private static Logger logger = Logger.getLogger(InvocationTaskExecutor.class.getName()); + private final AtomicInteger count = new AtomicInteger(); + /** * @param corePoolSize * @param maximumPoolSize @@ -40,8 +42,6 @@ public InvocationTaskExecutor( logger.info("Thread pool created"); } - private final AtomicInteger count = new AtomicInteger(); - @Override protected void beforeExecute(final Thread thread, final Runnable task) { super.beforeExecute(thread, task); diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationTaskManager.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationTaskManager.java index 2de85597..aecc8e64 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationTaskManager.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationTaskManager.java @@ -8,6 +8,7 @@ import static org.hyperledger.fabric.protos.peer.ChaincodeMessage.Type.READY; import static org.hyperledger.fabric.protos.peer.ChaincodeMessage.Type.REGISTERED; +import java.util.Map; import java.util.Properties; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CompletableFuture; @@ -35,27 +36,19 @@ *

In the current 1.4 Fabric Protocol this is in practice a singleton - because the peer will ignore multiple * 'register' calls. And an instance of this will be created per register call for a given chaincodeID. */ +@SuppressWarnings("PMD.MoreThanOneLogger") public final class InvocationTaskManager { - private static Logger logger = Logger.getLogger(InvocationTaskManager.class.getName()); - private static Logger perflogger = Logger.getLogger(Logging.PERFLOGGER); - - /** - * Get an instance of the Invocation Task Manager. - * - * @param chaincode Chaincode Instance - * @param chaincodeId ID of the chaincode - * @return InvocationTaskManager - */ - public static InvocationTaskManager getManager(final ChaincodeBase chaincode, final ChaincodeID chaincodeId) { - return new InvocationTaskManager(chaincode, chaincodeId); - } + private static final Logger LOGGER = Logger.getLogger(InvocationTaskManager.class.getName()); + private static final Logger PERFLOGGER = Logger.getLogger(Logging.PERFLOGGER); + private static final String CANNOT_HANDLE_FORMAT = "[%-8.8s] Received %s: cannot handle"; + private static final int SHUTDOWN_TIMEOUT = 60; // Keeping a map here of the tasks that are currently ongoing, and the key // // Key = txid + channleid // One task = one transaction invocation - private final ConcurrentHashMap innvocationTasks = new ConcurrentHashMap<>(); + private final Map innvocationTasks = new ConcurrentHashMap<>(); // Way to send back the events and data that make up the requests private Consumer outgoingMessage; @@ -69,13 +62,14 @@ public static InvocationTaskManager getManager(final ChaincodeBase chaincode, fi private final int maximumPoolSize; private final int corePoolSize; private final long keepAliveTime; - private final TimeUnit unit = TimeUnit.MILLISECONDS; + private static final TimeUnit UNIT = TimeUnit.MILLISECONDS; private final BlockingQueue workQueue; // Minor customization of the ThreadFactory to give a more recognizable name to the threads private final ThreadFactory threadFactory = new ThreadFactory() { - private AtomicInteger next = new AtomicInteger(0); + private final AtomicInteger next = new AtomicInteger(0); + @Override public Thread newThread(final Runnable r) { Thread thread = Executors.defaultThreadFactory().newThread(r); thread.setName("fabric-txinvoke:" + next.incrementAndGet()); @@ -94,6 +88,17 @@ public Thread newThread(final Runnable r) { private final InvocationTaskExecutor taskService; + /** + * Get an instance of the Invocation Task Manager. + * + * @param chaincode Chaincode Instance + * @param chaincodeId ID of the chaincode + * @return InvocationTaskManager + */ + public static InvocationTaskManager getManager(final ChaincodeBase chaincode, final ChaincodeID chaincodeId) { + return new InvocationTaskManager(chaincode, chaincodeId); + } + /** * New InvocationTaskManager. * @@ -117,14 +122,14 @@ public InvocationTaskManager(final ChaincodeBase chaincode, final ChaincodeID ch corePoolSize = Integer.parseInt((String) props.getOrDefault("TP_CORE_POOL_SIZE", "5")); keepAliveTime = Long.parseLong((String) props.getOrDefault("TP_KEEP_ALIVE_MS", "5000")); - logger.info(() -> "Max Pool Size [TP_MAX_POOL_SIZE]" + maximumPoolSize); - logger.info(() -> "Queue Size [TP_CORE_POOL_SIZE]" + queueSize); - logger.info(() -> "Core Pool Size [TP_QUEUE_SIZE]" + corePoolSize); - logger.info(() -> "Keep Alive Time [TP_KEEP_ALIVE_MS]" + keepAliveTime); + LOGGER.info(() -> "Max Pool Size [TP_MAX_POOL_SIZE]" + maximumPoolSize); + LOGGER.info(() -> "Queue Size [TP_CORE_POOL_SIZE]" + queueSize); + LOGGER.info(() -> "Core Pool Size [TP_QUEUE_SIZE]" + corePoolSize); + LOGGER.info(() -> "Keep Alive Time [TP_KEEP_ALIVE_MS]" + keepAliveTime); - workQueue = new LinkedBlockingQueue(queueSize); + workQueue = new LinkedBlockingQueue<>(queueSize); taskService = new InvocationTaskExecutor( - corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); + corePoolSize, maximumPoolSize, keepAliveTime, UNIT, workQueue, threadFactory, handler); Metrics.getProvider().setTaskMetricsCollector(taskService); } @@ -135,45 +140,15 @@ public InvocationTaskManager(final ChaincodeBase chaincode, final ChaincodeID ch * @throws IllegalArgumentException validation fields and arguments * @param chaincodeMessage ChaincodeMessage */ - public void onChaincodeMessage(final ChaincodeMessage chaincodeMessage) throws IllegalArgumentException { - if (chaincodeMessage == null) { + @SuppressWarnings("PMD.AvoidCatchingGenericException") + public void onChaincodeMessage(final ChaincodeMessage chaincodeMessage) { + if (null == chaincodeMessage) { throw new IllegalArgumentException("chaincodeMessage is null"); } - logger.fine(() -> + LOGGER.fine(() -> String.format("[%-8.8s] %s", chaincodeMessage.getTxid(), ChaincodeBase.toJsonString(chaincodeMessage))); try { - final Type msgType = chaincodeMessage.getType(); - switch (chaincode.getState()) { - case CREATED: - if (msgType == REGISTERED) { - chaincode.setState(org.hyperledger.fabric.shim.ChaincodeBase.CCState.ESTABLISHED); - logger.fine(() -> String.format( - "[%-8.8s] Received REGISTERED: moving to established state", - chaincodeMessage.getTxid())); - } else { - logger.warning(() -> String.format( - "[%-8.8s] Received %s: cannot handle", chaincodeMessage.getTxid(), msgType)); - } - break; - case ESTABLISHED: - if (msgType == READY) { - chaincode.setState(org.hyperledger.fabric.shim.ChaincodeBase.CCState.READY); - logger.fine(() -> String.format( - "[%-8.8s] Received READY: ready for invocations", chaincodeMessage.getTxid())); - } else { - logger.warning(() -> String.format( - "[%-8.8s] Received %s: cannot handle", chaincodeMessage.getTxid(), msgType)); - } - break; - case READY: - handleMsg(chaincodeMessage, msgType); - break; - default: - logger.warning(() -> String.format( - "[%-8.8s] Received %s: cannot handle", - chaincodeMessage.getTxid(), chaincodeMessage.getType())); - break; - } + processChaincodeMessage(chaincodeMessage); } catch (final RuntimeException e) { // catch any issues with say the comms dropping or something else completely // unknown @@ -183,6 +158,37 @@ public void onChaincodeMessage(final ChaincodeMessage chaincodeMessage) throws I } } + private void processChaincodeMessage(final ChaincodeMessage chaincodeMessage) { + final Type msgType = chaincodeMessage.getType(); + + switch (chaincode.getState()) { + case CREATED: + if (msgType == REGISTERED) { + chaincode.setState(ChaincodeBase.CCState.ESTABLISHED); + LOGGER.fine(() -> String.format( + "[%-8.8s] Received REGISTERED: moving to established state", chaincodeMessage.getTxid())); + } else { + LOGGER.warning(() -> String.format(CANNOT_HANDLE_FORMAT, chaincodeMessage.getTxid(), msgType)); + } + break; + case ESTABLISHED: + if (msgType == READY) { + chaincode.setState(ChaincodeBase.CCState.READY); + LOGGER.fine(() -> String.format( + "[%-8.8s] Received READY: ready for invocations", chaincodeMessage.getTxid())); + } else { + LOGGER.warning(() -> String.format(CANNOT_HANDLE_FORMAT, chaincodeMessage.getTxid(), msgType)); + } + break; + case READY: + handleMsg(chaincodeMessage, msgType); + break; + default: + LOGGER.warning(() -> String.format(CANNOT_HANDLE_FORMAT, chaincodeMessage.getTxid(), msgType)); + break; + } + } + /** * Key method to take the message, determine if it is a new transaction or an answer (good or bad) to a stub api. * @@ -190,7 +196,7 @@ public void onChaincodeMessage(final ChaincodeMessage chaincodeMessage) throws I * @param msgType */ private void handleMsg(final ChaincodeMessage message, final Type msgType) { - logger.fine(() -> String.format("[%-8.8s] Received %s", message.getTxid(), msgType.toString())); + LOGGER.fine(() -> String.format("[%-8.8s] Received %s", message.getTxid(), msgType.toString())); switch (msgType) { case RESPONSE: case ERROR: @@ -201,8 +207,7 @@ private void handleMsg(final ChaincodeMessage message, final Type msgType) { newTask(message, msgType); break; default: - logger.warning(() -> - String.format("[%-8.8s] Received %s: cannot handle", message.getTxid(), message.getType())); + LOGGER.warning(() -> String.format(CANNOT_HANDLE_FORMAT, message.getTxid(), message.getType())); break; } } @@ -214,26 +219,29 @@ private void handleMsg(final ChaincodeMessage message, final Type msgType) { */ private void sendToTask(final ChaincodeMessage message) { try { - perflogger.fine(() -> "> sendToTask TX::" + message.getTxid()); + PERFLOGGER.fine(() -> "> sendToTask TX::" + message.getTxid()); final String key = message.getChannelId() + message.getTxid(); final ChaincodeInvocationTask task = this.innvocationTasks.get(key); if (task == null) { - throw new InterruptedException("Task hasmap missing entry"); + sendFailure(message, new InterruptedException("Task map missing entry: " + key)); + } else { + task.postMessage(message); + PERFLOGGER.fine(() -> "< sendToTask TX::" + message.getTxid()); } - task.postMessage(message); - - perflogger.fine(() -> "< sendToTask TX::" + message.getTxid()); } catch (final InterruptedException e) { - logger.severe( - () -> "Failed to send response to the task task " + message.getTxid() + Logging.formatError(e)); - - final ChaincodeMessage m = ChaincodeMessageFactory.newErrorEventMessage( - message.getChannelId(), message.getTxid(), "Failed to send response to task"); - this.outgoingMessage.accept(m); + sendFailure(message, e); } } + private void sendFailure(final ChaincodeMessage message, final InterruptedException e) { + LOGGER.severe(() -> "Failed to send response to the task task " + message.getTxid() + Logging.formatError(e)); + + final ChaincodeMessage m = ChaincodeMessageFactory.newErrorEventMessage( + message.getChannelId(), message.getTxid(), "Failed to send response to task"); + this.outgoingMessage.accept(m); + } + /** * Create a new task to handle this transaction function. * @@ -246,11 +254,11 @@ private void newTask(final ChaincodeMessage message, final Type type) { final ChaincodeInvocationTask task = new ChaincodeInvocationTask(message, type, this.outgoingMessage, this.chaincode); - perflogger.fine(() -> "> newTask:created TX::" + txid); + PERFLOGGER.fine(() -> "> newTask:created TX::" + txid); this.innvocationTasks.put(task.getTxKey(), task); try { - perflogger.fine(() -> "> newTask:submitting TX::" + txid); + PERFLOGGER.fine(() -> "> newTask:submitting TX::" + txid); // submit the task to run, with the taskService providing the // threading support. @@ -266,13 +274,13 @@ private void newTask(final ChaincodeMessage message, final Type type) { // list response.thenRun(() -> { innvocationTasks.remove(task.getTxKey()); - perflogger.fine(() -> "< newTask:completed TX::" + txid); + PERFLOGGER.fine(() -> "< newTask:completed TX::" + txid); }); - perflogger.fine(() -> "< newTask:submitted TX::" + txid); + PERFLOGGER.fine(() -> "< newTask:submitted TX::" + txid); } catch (final RejectedExecutionException e) { - logger.warning(() -> "Failed to submit task " + txid + Logging.formatError(e)); + LOGGER.warning(() -> "Failed to submit task " + txid + Logging.formatError(e)); // this means that there is no way that this can be handed off to another // thread for processing, and there's no space left in the queue to hold // it pending @@ -289,33 +297,27 @@ private void newTask(final ChaincodeMessage message, final Type type) { * @param outgoingMessage * @return InvocationTaskManager */ - public InvocationTaskManager setResponseConsumer(final Consumer outgoingMessage) { + public void setResponseConsumer(final Consumer outgoingMessage) { this.outgoingMessage = outgoingMessage; - - return this; } /** * Send the initial protocol message for the 'register' phase. * * @throws IllegalArgumentException validation fields and arguments - * @return InvocationTaskManager */ - public InvocationTaskManager register() throws IllegalArgumentException { + public void register() { if (outgoingMessage == null) { throw new IllegalArgumentException("outgoingMessage is null"); } - logger.info(() -> "Registering new chaincode " + this.chaincodeId); + LOGGER.info(() -> "Registering new chaincode " + this.chaincodeId); chaincode.setState(ChaincodeBase.CCState.CREATED); this.outgoingMessage.accept(ChaincodeMessageFactory.newRegisterChaincodeMessage(this.chaincodeId)); - - return this; } - private static final int SHUTDOWN_TIMEOUT = 60; - /** */ + @SuppressWarnings("PMD.SystemPrintln") public void shutdown() { // Recommended shutdown process from // https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/KeyModificationImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/KeyModificationImpl.java index 2cecbf97..2bbcca43 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/KeyModificationImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/KeyModificationImpl.java @@ -13,7 +13,7 @@ public final class KeyModificationImpl implements KeyModification { private final String txId; private final ByteString value; - private final java.time.Instant timestamp; + private final Instant timestamp; private final boolean deleted; KeyModificationImpl(final org.hyperledger.fabric.protos.ledger.queryresult.KeyModification km) { @@ -40,7 +40,7 @@ public String getStringValue() { } @Override - public java.time.Instant getTimestamp() { + public Instant getTimestamp() { return timestamp; } @@ -52,38 +52,29 @@ public boolean isDeleted() { @Override public int hashCode() { final int prime = 31; - int result = 1; - result = prime * result + (deleted ? 1231 : 1237); - result = prime * result + ((timestamp == null) ? 0 : timestamp.hashCode()); - result = prime * result + ((txId == null) ? 0 : txId.hashCode()); - result = prime * result + ((value == null) ? 0 : value.hashCode()); + int result = Boolean.hashCode(deleted); + result = prime * result + timestamp.hashCode(); + result = prime * result + txId.hashCode(); + result = prime * result + value.hashCode(); return result; } @Override - public boolean equals(final Object obj) { - if (this == obj) { + public boolean equals(final Object other) { + if (this == other) { return true; } - if (obj == null) { + if (other == null) { return false; } - if (getClass() != obj.getClass()) { + if (getClass() != other.getClass()) { return false; } - final KeyModificationImpl other = (KeyModificationImpl) obj; - if (deleted != other.deleted) { - return false; - } - if (!timestamp.equals(other.timestamp)) { - return false; - } - if (!txId.equals(other.txId)) { - return false; - } - if (!value.equals(other.value)) { - return false; - } - return true; + + final KeyModificationImpl that = (KeyModificationImpl) other; + return this.deleted == that.deleted + && this.timestamp.equals(that.timestamp) + && this.txId.equals(that.txId) + && this.value.equals(that.value); } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/KeyValueImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/KeyValueImpl.java index dbf1089f..e158fc96 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/KeyValueImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/KeyValueImpl.java @@ -37,30 +37,24 @@ public String getStringValue() { @Override public int hashCode() { final int prime = 31; - int result = 1; - result = prime * result + ((key == null) ? 0 : key.hashCode()); - result = prime * result + ((value == null) ? 0 : value.hashCode()); + int result = key.hashCode(); + result = prime * result + value.hashCode(); return result; } @Override - public boolean equals(final Object obj) { - if (this == obj) { + public boolean equals(final Object other) { + if (this == other) { return true; } - if (obj == null) { + if (other == null) { return false; } - if (getClass() != obj.getClass()) { + if (getClass() != other.getClass()) { return false; } - final KeyValueImpl other = (KeyValueImpl) obj; - if (!key.equals(other.key)) { - return false; - } - if (!value.equals(other.value)) { - return false; - } - return true; + + final KeyValueImpl that = (KeyValueImpl) other; + return this.key.equals(that.key) && this.value.equals(that.value); } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorImpl.java index 874ae4f7..89350204 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorImpl.java @@ -11,6 +11,7 @@ import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; +import java.io.UncheckedIOException; import java.util.Collections; import java.util.Iterator; import java.util.NoSuchElementException; @@ -39,7 +40,7 @@ class QueryResultsIteratorImpl implements QueryResultsIterator { private final String txId; private Iterator currentIterator; private QueryResponse currentQueryResponse; - private Function mapper; + private final Function mapper; QueryResultsIteratorImpl( final ChaincodeInvocationTask handler, @@ -56,13 +57,13 @@ class QueryResultsIteratorImpl implements QueryResultsIterator { this.currentIterator = currentQueryResponse.getResultsList().iterator(); this.mapper = mapper; } catch (final InvalidProtocolBufferException e) { - throw new RuntimeException(e); + throw new UncheckedIOException(e); } } @Override public Iterator iterator() { - return new Iterator() { + return new Iterator<>() { @Override public boolean hasNext() { @@ -95,7 +96,7 @@ public T next() { try { currentQueryResponse = QueryResponse.parseFrom(responseMessage); } catch (final InvalidProtocolBufferException e) { - throw new RuntimeException(e); + throw new UncheckedIOException(e); } currentIterator = currentQueryResponse.getResultsList().iterator(); diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorWithMetadataImpl.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorWithMetadataImpl.java index 26d24f55..25043547 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorWithMetadataImpl.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorWithMetadataImpl.java @@ -8,6 +8,7 @@ import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; +import java.io.UncheckedIOException; import java.util.function.Function; import java.util.logging.Logger; import org.hyperledger.fabric.protos.peer.QueryResponse; @@ -25,9 +26,9 @@ */ public final class QueryResultsIteratorWithMetadataImpl extends QueryResultsIteratorImpl implements QueryResultsIteratorWithMetadata { - private static Logger logger = Logger.getLogger(QueryResultsIteratorWithMetadataImpl.class.getName()); + private static final Logger LOGGER = Logger.getLogger(QueryResultsIteratorWithMetadataImpl.class.getName()); - private QueryResponseMetadata metadata; + private final QueryResponseMetadata metadata; /** * @param handler @@ -47,8 +48,8 @@ public QueryResultsIteratorWithMetadataImpl( final QueryResponse queryResponse = QueryResponse.parseFrom(responseBuffer); metadata = QueryResponseMetadata.parseFrom(queryResponse.getMetadata()); } catch (final InvalidProtocolBufferException e) { - logger.warning("can't parse response metadata"); - throw new RuntimeException(e); + LOGGER.warning("can't parse response metadata"); + throw new UncheckedIOException(e); } } diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ledger/CompositeKey.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ledger/CompositeKey.java index 32335315..4c88c237 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ledger/CompositeKey.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ledger/CompositeKey.java @@ -11,6 +11,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -26,7 +27,7 @@ public class CompositeKey { private final String objectType; private final List attributes; - private final String compositeKey; + private final String key; /** * @param objectType @@ -41,12 +42,10 @@ public CompositeKey(final String objectType, final String... attributes) { * @param attributes */ public CompositeKey(final String objectType, final List attributes) { - if (objectType == null) { - throw new NullPointerException("objectType cannot be null"); - } + Objects.requireNonNull(objectType, "objectType cannot be null"); this.objectType = objectType; this.attributes = attributes; - this.compositeKey = generateCompositeKeyString(objectType, attributes); + this.key = generateCompositeKeyString(objectType, attributes); } /** @return object type */ @@ -62,7 +61,7 @@ public List getAttributes() { /** */ @Override public String toString() { - return compositeKey; + return key; } /** diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/traces/Traces.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/traces/Traces.java index 28ba7b28..60f3407a 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/traces/Traces.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/traces/Traces.java @@ -5,7 +5,6 @@ */ package org.hyperledger.fabric.traces; -import java.lang.reflect.InvocationTargetException; import java.util.Properties; import java.util.logging.Logger; import org.hyperledger.fabric.traces.impl.DefaultTracesProvider; @@ -33,6 +32,7 @@ private Traces() {} * @param props the configuration of the chaincode * @return The traces provider */ + @SuppressWarnings("PMD.AvoidCatchingGenericException") public static TracesProvider initialize(final Properties props) { if (Boolean.parseBoolean((String) props.get(CHAINCODE_TRACES_ENABLED))) { try { @@ -47,14 +47,8 @@ public static TracesProvider initialize(final Properties props) { logger.info("Using default traces provider"); provider = new DefaultTracesProvider(); } - } catch (ClassNotFoundException - | InstantiationException - | IllegalAccessException - | IllegalArgumentException - | InvocationTargetException - | NoSuchMethodException - | SecurityException e) { - throw new RuntimeException("Unable to start traces", e); + } catch (Exception e) { + throw new IllegalStateException("Unable to start traces", e); } } else { // return a 'null' provider diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/traces/TracesProvider.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/traces/TracesProvider.java index 2083b4e9..40c49086 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/traces/TracesProvider.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/traces/TracesProvider.java @@ -31,8 +31,9 @@ public interface TracesProvider { * * @param props */ - default void initialize(final Properties props) {} - ; + default void initialize(final Properties props) { + // Do nothing by default + } /** * Creates a span with metadata of the current chaincode execution, possibly linked to the execution arguments. diff --git a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/traces/impl/OpenTelemetryProperties.java b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/traces/impl/OpenTelemetryProperties.java index 7afa5c48..da954aee 100644 --- a/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/traces/impl/OpenTelemetryProperties.java +++ b/fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/traces/impl/OpenTelemetryProperties.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import javax.annotation.Nullable; @@ -23,11 +24,12 @@ final class OpenTelemetryProperties implements ConfigProperties { private final Map config; - OpenTelemetryProperties(final Map... arrayOfProperties) { + @SafeVarargs + OpenTelemetryProperties(final Map... arrayOfProperties) { Map config = new HashMap<>(); - for (Map props : arrayOfProperties) { - props.forEach((key, value) -> - config.put(((String) key).toLowerCase(Locale.ROOT).replace('-', '.'), (String) value)); + for (Map props : arrayOfProperties) { + props.forEach( + (key, value) -> config.put(key.toLowerCase(Locale.ROOT).replace('-', '.'), value)); } this.config = config; } @@ -43,54 +45,50 @@ final class OpenTelemetryProperties implements ConfigProperties { if (value == null || value.isEmpty()) { return null; } - return Boolean.parseBoolean(value); + return Boolean.valueOf(value); } @Override - @Nullable @SuppressWarnings("UnusedException") - public Integer getInt(final String name) { + @Nullable public Integer getInt(final String name) { String value = config.get(name); if (value == null || value.isEmpty()) { return null; } try { - return Integer.parseInt(value); + return Integer.valueOf(value); } catch (NumberFormatException ex) { - throw newInvalidPropertyException(name, value, "integer"); + throw newInvalidPropertyException(name, value, "integer", ex); } } @Override - @Nullable @SuppressWarnings("UnusedException") - public Long getLong(final String name) { + @Nullable public Long getLong(final String name) { String value = config.get(name); if (value == null || value.isEmpty()) { return null; } try { - return Long.parseLong(value); + return Long.valueOf(value); } catch (NumberFormatException ex) { - throw newInvalidPropertyException(name, value, "long"); + throw newInvalidPropertyException(name, value, "long", ex); } } @Override - @Nullable @SuppressWarnings("UnusedException") - public Double getDouble(final String name) { + @Nullable public Double getDouble(final String name) { String value = config.get(name); if (value == null || value.isEmpty()) { return null; } try { - return Double.parseDouble(value); + return Double.valueOf(value); } catch (NumberFormatException ex) { - throw newInvalidPropertyException(name, value, "double"); + throw newInvalidPropertyException(name, value, "double", ex); } } @Override - @Nullable @SuppressWarnings("UnusedException") - public Duration getDuration(final String name) { + @Nullable public Duration getDuration(final String name) { String value = config.get(name); if (value == null || value.isEmpty()) { return null; @@ -99,14 +97,15 @@ public Duration getDuration(final String name) { String numberString = value.substring(0, value.length() - unitString.length()); try { long rawNumber = Long.parseLong(numberString.trim()); - TimeUnit unit = getDurationUnit(unitString.trim()); + TimeUnit unit = getDurationUnit(unitString.trim()) + .orElseThrow(() -> new ConfigurationException( + "Invalid duration property " + name + "=" + value + ". Invalid duration unit.")); return Duration.ofMillis(TimeUnit.MILLISECONDS.convert(rawNumber, unit)); } catch (NumberFormatException ex) { - throw new ConfigurationException( + var e = new ConfigurationException( "Invalid duration property " + name + "=" + value + ". Expected number, found: " + numberString); - } catch (ConfigurationException ex) { - throw new ConfigurationException( - "Invalid duration property " + name + "=" + value + ". " + ex.getMessage()); + e.addSuppressed(ex); + throw e; } } @@ -120,6 +119,7 @@ public List getList(final String name) { } @Override + @SuppressWarnings("PMD.AvoidLiteralsInIfCondition") public Map getMap(final String name) { return getList(name).stream() .map(keyValuePair -> filterBlanksAndNulls(keyValuePair.split("=", 2))) @@ -136,9 +136,11 @@ public Map getMap(final String name) { } private static ConfigurationException newInvalidPropertyException( - final String name, final String value, final String type) { - throw new ConfigurationException( + final String name, final String value, final String type, final Exception cause) { + var e = new ConfigurationException( "Invalid value for property " + name + "=" + value + ". Must be a " + type + "."); + e.addSuppressed(cause); + throw e; } private static List filterBlanksAndNulls(final String[] values) { @@ -151,21 +153,21 @@ private static List filterBlanksAndNulls(final String[] values) { * @param unitString the time unit as a string * @return the parsed TimeUnit */ - private static TimeUnit getDurationUnit(final String unitString) { + private static Optional getDurationUnit(final String unitString) { switch (unitString) { case "": // Fallthrough expected case "ms": - return TimeUnit.MILLISECONDS; + return Optional.of(TimeUnit.MILLISECONDS); case "s": - return TimeUnit.SECONDS; + return Optional.of(TimeUnit.SECONDS); case "m": - return TimeUnit.MINUTES; + return Optional.of(TimeUnit.MINUTES); case "h": - return TimeUnit.HOURS; + return Optional.of(TimeUnit.HOURS); case "d": - return TimeUnit.DAYS; + return Optional.of(TimeUnit.DAYS); default: - throw new ConfigurationException("Invalid duration string, found: " + unitString); + return Optional.empty(); } } diff --git a/fabric-chaincode-shim/src/test/java/ChaincodeWithoutPackageTest.java b/fabric-chaincode-shim/src/test/java/ChaincodeWithoutPackageTest.java index a97b3071..60d7c5ff 100644 --- a/fabric-chaincode-shim/src/test/java/ChaincodeWithoutPackageTest.java +++ b/fabric-chaincode-shim/src/test/java/ChaincodeWithoutPackageTest.java @@ -18,11 +18,11 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; -public final class ChaincodeWithoutPackageTest { +final class ChaincodeWithoutPackageTest { private ChaincodeMockPeer server; @AfterEach - public void afterTest() throws Exception { + void afterTest() throws Exception { if (server != null) { server.stop(); server = null; @@ -30,7 +30,7 @@ public void afterTest() throws Exception { } @Test - public void testRegisterChaincodeWithoutPackage() throws Exception { + void testRegisterChaincodeWithoutPackage() throws Exception { final ChaincodeBase cb = new EmptyChaincodeWithoutPackage(); final List scenario = new ArrayList<>(); diff --git a/fabric-chaincode-shim/src/test/java/contract/Greeting.java b/fabric-chaincode-shim/src/test/java/contract/Greeting.java index 7f2a16f5..ac49bf4f 100644 --- a/fabric-chaincode-shim/src/test/java/contract/Greeting.java +++ b/fabric-chaincode-shim/src/test/java/contract/Greeting.java @@ -5,6 +5,8 @@ */ package contract; +import static org.assertj.core.api.Assertions.assertThat; + import org.hyperledger.fabric.contract.annotation.DataType; import org.hyperledger.fabric.contract.annotation.Property; import org.json.JSONObject; @@ -53,13 +55,8 @@ public Greeting(final String text) { public static void validate(final Greeting greeting) { final String text = greeting.text; - if (text.length() != greeting.textLength) { - throw new Error("Length incorrectly set"); - } - - if (text.split(" ").length != greeting.wordCount) { - throw new Error("Word count incorrectly set"); - } + assertThat(text).as("greeting length").hasSize(greeting.textLength); + assertThat(text.split(" ")).as("word count").hasSize(greeting.wordCount); } public String toJSONString() { diff --git a/fabric-chaincode-shim/src/test/java/contract/SampleContract.java b/fabric-chaincode-shim/src/test/java/contract/SampleContract.java index a28a2924..426aab7c 100644 --- a/fabric-chaincode-shim/src/test/java/contract/SampleContract.java +++ b/fabric-chaincode-shim/src/test/java/contract/SampleContract.java @@ -24,6 +24,7 @@ license = @License(name = "fred", url = "http://fred.me"), version = "0.0.1", title = "samplecontract")) +@SuppressWarnings("PMD.SystemPrintln") @Default() public class SampleContract implements ContractInterface { public static int getBeforeInvoked() { @@ -109,7 +110,7 @@ public String t3(final Context ctx, final String exception, final String message throw new ChaincodeException(message, "T3ERR1"); } } else { - throw new RuntimeException(message); + throw new IllegalArgumentException(message); } } diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/LoggerTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/LoggerTest.java index 62751cef..90c327d6 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/LoggerTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/LoggerTest.java @@ -9,15 +9,15 @@ import org.hyperledger.fabric.contract.ContractRuntimeException; import org.junit.jupiter.api.Test; -public class LoggerTest { +class LoggerTest { @Test - public void logger() { + void logger() { Logger.getLogger(LoggerTest.class); Logger.getLogger(LoggerTest.class.getName()); } @Test - public void testContractException() { + void testContractException() { final Logger logger = Logger.getLogger(LoggerTest.class); final ContractRuntimeException cre1 = new ContractRuntimeException(""); @@ -32,7 +32,7 @@ public void testContractException() { } @Test - public void testDebug() { + void testDebug() { Logger.getLogger(LoggerTest.class).debug("debug message"); } } diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/LoggingTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/LoggingTest.java index c4b22d47..98c5dde8 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/LoggingTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/LoggingTest.java @@ -15,9 +15,9 @@ import org.hamcrest.CoreMatchers; import org.junit.jupiter.api.Test; -public final class LoggingTest { +final class LoggingTest { @Test - public void testMapLevel() { + void testMapLevel() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { assertEquals(Level.SEVERE, proxyMapLevel("ERROR"), "Error maps"); assertEquals(Level.SEVERE, proxyMapLevel("critical"), "Critical maps"); @@ -30,23 +30,15 @@ public void testMapLevel() { assertEquals(Level.INFO, proxyMapLevel(new Object[] {null}), "Info maps"); } - public Object proxyMapLevel(final Object... args) { - - try { - final Method m = Logging.class.getDeclaredMethod("mapLevel", String.class); - m.setAccessible(true); - return m.invoke(null, args); - } catch (NoSuchMethodException - | SecurityException - | IllegalAccessException - | IllegalArgumentException - | InvocationTargetException e) { - throw new RuntimeException(e); - } + private Object proxyMapLevel(final Object... args) + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + final Method m = Logging.class.getDeclaredMethod("mapLevel", String.class); + m.setAccessible(true); + return m.invoke(null, args); } @Test - public void testFormatError() { + void testFormatError() { final Exception e1 = new Exception("Computer says no"); assertThat(Logging.formatError(e1), containsString("Computer says no")); @@ -61,7 +53,7 @@ public void testFormatError() { } @Test - public void testSetLogLevel() { + void testSetLogLevel() { final java.util.logging.Logger l = java.util.logging.Logger.getLogger("org.hyperledger.fabric.test"); final java.util.logging.Logger another = java.util.logging.Logger.getLogger("acme.wibble"); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/TestUtil.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/TestUtil.java index 8c48b8b5..2aa32cfe 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/TestUtil.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/TestUtil.java @@ -108,7 +108,6 @@ public static String createCertWithIdentityAttributes(final String attributeValu final X509CertificateHolder builtCert = certBuilder.build(contentSigner); final X509Certificate certificate = (X509Certificate) CertificateFactory.getInstance("X509") .generateCertificate(new ByteArrayInputStream(builtCert.getEncoded())); - final String encodedCert = Base64.getEncoder().encodeToString(certificate.getEncoded()); - return encodedCert; + return Base64.getEncoder().encodeToString(certificate.getEncoded()); } } diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/AllTypesAsset.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/AllTypesAsset.java index 86cee5cf..4b4ed224 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/AllTypesAsset.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/AllTypesAsset.java @@ -122,7 +122,13 @@ public void setTheCustomObject(final MyType customObject) { this.theCustomObject = customObject; } - public boolean equals(final AllTypesAsset obj) { + @Override + public boolean equals(final Object other) { + if (!(other instanceof AllTypesAsset)) { + return false; + } + + AllTypesAsset obj = (AllTypesAsset) other; return theByte == obj.getTheByte() && theShort == obj.getTheShort() && theInt == obj.getTheInt() @@ -135,18 +141,15 @@ public boolean equals(final AllTypesAsset obj) { @Override public String toString() { - final StringBuilder builder = new StringBuilder(System.lineSeparator()); - builder.append("byte=" + theByte).append(System.lineSeparator()); - builder.append("short=" + theShort).append(System.lineSeparator()); - builder.append("int=" + theInt).append(System.lineSeparator()); - builder.append("long=" + theLong).append(System.lineSeparator()); - builder.append("float=" + theFloat).append(System.lineSeparator()); - builder.append("double=" + theDouble).append(System.lineSeparator()); - builder.append("boolean=" + theBoolean).append(System.lineSeparator()); - builder.append("char=" + theChar).append(System.lineSeparator()); - builder.append("String=" + theString).append(System.lineSeparator()); - builder.append("Mytype=" + theCustomObject).append(System.lineSeparator()); - - return builder.toString(); + return System.lineSeparator() + "byte=" + theByte + System.lineSeparator() + "short=" + + theShort + System.lineSeparator() + "int=" + + theInt + System.lineSeparator() + "long=" + + theLong + System.lineSeparator() + "float=" + + theFloat + System.lineSeparator() + "double=" + + theDouble + System.lineSeparator() + "boolean=" + + theBoolean + System.lineSeparator() + "char=" + + theChar + System.lineSeparator() + "String=" + + theString + System.lineSeparator() + "Mytype=" + + theCustomObject + System.lineSeparator(); } } diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ChaincodeStubNaiveImpl.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ChaincodeStubNaiveImpl.java index 0054c1d6..ad475730 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ChaincodeStubNaiveImpl.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ChaincodeStubNaiveImpl.java @@ -60,7 +60,9 @@ public ChaincodeStubNaiveImpl() { @Override public List getArgs() { if (argsAsByte == null) { - argsAsByte = args.stream().map(i -> i.getBytes()).collect(Collectors.toList()); + argsAsByte = args.stream() + .map(arg -> arg.getBytes(StandardCharsets.UTF_8)) + .collect(Collectors.toList()); } return argsAsByte; } @@ -259,7 +261,7 @@ public byte[] getCreator() { @Override public Map getTransient() { - return null; + return new HashMap<>(); } @Override @@ -269,7 +271,7 @@ public byte[] getBinding() { void setStringArgs(final List args) { this.args = args; - this.argsAsByte = args.stream().map(i -> i.getBytes()).collect(Collectors.toList()); + this.argsAsByte = args.stream().map(String::getBytes).collect(Collectors.toList()); } public byte[] buildSerializedIdentity() { diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ClientIdentityTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ClientIdentityTest.java index 9f85095d..f1da45ed 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ClientIdentityTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ClientIdentityTest.java @@ -16,10 +16,10 @@ import org.hyperledger.fabric.shim.ChaincodeStub; import org.junit.jupiter.api.Test; -public class ClientIdentityTest { +final class ClientIdentityTest { /** Test client identity can be created using certificate without attributes */ @Test - public void clientIdentityWithoutAttributes() throws Exception { + void clientIdentityWithoutAttributes() throws Exception { final ChaincodeStub stub = new ChaincodeStubNaiveImpl(); final ClientIdentity identity = new ClientIdentity(stub); assertEquals(identity.getMSPID(), "testMSPID"); @@ -39,7 +39,7 @@ public void clientIdentityWithoutAttributes() throws Exception { /** Test client identity can be created using certificate with attributes */ @Test - public void clientIdentityWithAttributes() throws Exception { + void clientIdentityWithAttributes() throws Exception { final ChaincodeStub stub = new ChaincodeStubNaiveImpl(); ((ChaincodeStubNaiveImpl) stub).setCertificate(TestUtil.CERT_WITH_ATTRS); final ClientIdentity identity = new ClientIdentity(stub); @@ -58,7 +58,7 @@ public void clientIdentityWithAttributes() throws Exception { /** Test client identity can be created using certificate with multiple attributes */ @Test - public void clientIdentityWithMultipleAttributes() throws Exception { + void clientIdentityWithMultipleAttributes() throws Exception { final ChaincodeStub stub = new ChaincodeStubNaiveImpl(); ((ChaincodeStubNaiveImpl) stub).setCertificate(TestUtil.CERT_MULTIPLE_ATTRIBUTES); final ClientIdentity identity = new ClientIdentity(stub); @@ -81,7 +81,7 @@ public void clientIdentityWithMultipleAttributes() throws Exception { /** Test client identity can be created using certificate with long distinguished name */ @Test - public void clientIdentityWithLongDNs() throws Exception { + void clientIdentityWithLongDNs() throws Exception { final ChaincodeStub stub = new ChaincodeStubNaiveImpl(); ((ChaincodeStubNaiveImpl) stub).setCertificate(TestUtil.CERT_WITH_DNS); final ClientIdentity identity = new ClientIdentity(stub); @@ -100,7 +100,7 @@ public void clientIdentityWithLongDNs() throws Exception { /** Test client identity throws a ContractRuntimeException when creating a serialized identity fails */ @Test - public void catchInvalidProtocolBufferException() { + void catchInvalidProtocolBufferException() { final ChaincodeStub stub = mock(ChaincodeStub.class); when(stub.getCreator()).thenReturn("somethingInvalid".getBytes()); @@ -111,7 +111,7 @@ public void catchInvalidProtocolBufferException() { /** Test client identity attributes are empty when using a certificate with dummy attributes */ @Test - public void createClientIdentityWithDummyAttributesCert() throws Exception { + void createClientIdentityWithDummyAttributesCert() throws Exception { final ChaincodeStub stub = new ChaincodeStubNaiveImpl(); // Create a certificate with rubbish attributes final String certWithDummyAttrs = diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContextFactoryTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContextFactoryTest.java index d0c6c393..4686080b 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContextFactoryTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContextFactoryTest.java @@ -14,17 +14,17 @@ import org.hyperledger.fabric.shim.ChaincodeStub; import org.junit.jupiter.api.Test; -public class ContextFactoryTest { +final class ContextFactoryTest { @Test - public void getInstance() { + void getInstance() { final ContextFactory f1 = ContextFactory.getInstance(); final ContextFactory f2 = ContextFactory.getInstance(); assertThat(f1, sameInstance(f2)); } @Test - public void createContext() { + void createContext() { final ChaincodeStub stub = new ChaincodeStubNaiveImpl(); final Context ctx = ContextFactory.getInstance().createContext(stub); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContextTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContextTest.java index 405e68ff..85aa8b75 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContextTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContextTest.java @@ -11,11 +11,11 @@ import org.hyperledger.fabric.shim.ChaincodeStub; import org.junit.jupiter.api.Test; -public class ContextTest { +final class ContextTest { /** Test creating a new context returns what we expect */ @Test - public void getInstance() { + void getInstance() { final ChaincodeStub stub = new ChaincodeStubNaiveImpl(); final Context context1 = new Context(stub); final Context context2 = new Context(stub); @@ -24,7 +24,7 @@ public void getInstance() { /** Test identity created in Context constructor matches getClientIdentity */ @Test - public void getSetClientIdentity() { + void getSetClientIdentity() { final ChaincodeStub stub = new ChaincodeStubNaiveImpl(); final Context context = ContextFactory.getInstance().createContext(stub); assertThat(context.getClientIdentity(), sameInstance(context.clientIdentity)); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContractInterfaceTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContractInterfaceTest.java index e3fcdd6a..7df683c4 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContractInterfaceTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContractInterfaceTest.java @@ -13,16 +13,15 @@ import org.hyperledger.fabric.shim.ChaincodeException; import org.junit.jupiter.api.Test; -public class ContractInterfaceTest { +final class ContractInterfaceTest { @Test - public void createContext() { + void createContext() { assertThat( - (new ContractInterface() {}).createContext(new ChaincodeStubNaiveImpl()), - is(instanceOf(Context.class))); + new ContractInterface() {}.createContext(new ChaincodeStubNaiveImpl()), is(instanceOf(Context.class))); } @Test - public void unknownTransaction() { + void unknownTransaction() { final ContractInterface c = new ContractInterface() {}; assertThatThrownBy(() -> c.unknownTransaction(c.createContext(new ChaincodeStubNaiveImpl()))) @@ -31,14 +30,14 @@ public void unknownTransaction() { } @Test - public void beforeTransaction() { + void beforeTransaction() { final ContractInterface c = new ContractInterface() {}; c.beforeTransaction(c.createContext(new ChaincodeStubNaiveImpl())); } @Test - public void afterTransaction() { + void afterTransaction() { final ContractInterface c = new ContractInterface() {}; c.afterTransaction(c.createContext(new ChaincodeStubNaiveImpl()), "ReturnValue"); } diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContractRouterTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContractRouterTest.java index b5692cc2..210321be 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContractRouterTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/ContractRouterTest.java @@ -28,9 +28,9 @@ import org.hyperledger.fabric.shim.NettyChaincodeServer; import org.junit.jupiter.api.Test; -public class ContractRouterTest { +final class ContractRouterTest { @Test - public void testCreateFailsWithoutValidOptions() { + void testCreateFailsWithoutValidOptions() { assertThatThrownBy(() -> new ContractRouter(new String[] {})) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("The chaincode id must be specified using either the -i or --i command " @@ -38,7 +38,7 @@ public void testCreateFailsWithoutValidOptions() { } @Test - public void testCreateAndScan() { + void testCreateAndScan() { final ContractRouter r = new ContractRouter(new String[] {"-a", "127.0.0.1:7052", "-i", "testId"}); r.findAllContracts(); final ChaincodeStub s = new ChaincodeStubNaiveImpl(); @@ -58,7 +58,7 @@ public void testCreateAndScan() { } @Test - public void testInit() { + void testInit() { final ContractRouter r = new ContractRouter(new String[] {"-a", "127.0.0.1:7052", "-i", "testId"}); r.findAllContracts(); final ChaincodeStub s = new ChaincodeStubNaiveImpl(); @@ -86,7 +86,7 @@ public void testInit() { /** Test invoking two transaction functions in a contract via fully qualified name */ @Test - public void testInvokeTwoTxnsThatExist() { + void testInvokeTwoTxnsThatExist() { final ContractRouter r = new ContractRouter(new String[] {"-a", "127.0.0.1:7052", "-i", "testId"}); r.findAllContracts(); final ChaincodeStub s = new ChaincodeStubNaiveImpl(); @@ -133,7 +133,7 @@ public void testInvokeTwoTxnsThatExist() { } @Test - public void testInvokeTxnWithDefinedName() { + void testInvokeTxnWithDefinedName() { final ContractRouter r = new ContractRouter(new String[] {"-a", "127.0.0.1:7052", "-i", "testId"}); r.findAllContracts(); final ChaincodeStub s = new ChaincodeStubNaiveImpl(); @@ -161,7 +161,7 @@ public void testInvokeTxnWithDefinedName() { /** Test invoking two transaction functions in a contract via default name name */ @Test - public void testInvokeTwoTxnsWithDefaultNamespace() { + void testInvokeTwoTxnsWithDefaultNamespace() { final ContractRouter r = new ContractRouter(new String[] {"-a", "127.0.0.1:7052", "-i", "testId"}); r.findAllContracts(); final ChaincodeStub s = new ChaincodeStubNaiveImpl(); @@ -208,7 +208,7 @@ public void testInvokeTwoTxnsWithDefaultNamespace() { } @Test - public void testInvokeTxnWithDefinedNameUsingMethodName() { + void testInvokeTxnWithDefinedNameUsingMethodName() { final ContractRouter r = new ContractRouter(new String[] {"-a", "127.0.0.1:7052", "-i", "testId"}); r.findAllContracts(); final ChaincodeStub s = new ChaincodeStubNaiveImpl(); @@ -235,7 +235,7 @@ public void testInvokeTxnWithDefinedNameUsingMethodName() { } @Test - public void testInvokeContractThatDoesNotExist() { + void testInvokeContractThatDoesNotExist() { final ContractRouter r = new ContractRouter(new String[] {"-a", "127.0.0.1:7052", "-i", "testId"}); r.findAllContracts(); final ChaincodeStub s = new ChaincodeStubNaiveImpl(); @@ -262,7 +262,7 @@ public void testInvokeContractThatDoesNotExist() { } @Test - public void testInvokeTxnThatDoesNotExist() { + void testInvokeTxnThatDoesNotExist() { final ContractRouter r = new ContractRouter(new String[] {"-a", "127.0.0.1:7052", "-i", "testId"}); r.findAllContracts(); final ChaincodeStub s = new ChaincodeStubNaiveImpl(); @@ -289,7 +289,7 @@ public void testInvokeTxnThatDoesNotExist() { } @Test - public void testInvokeTxnThatReturnsNullString() { + void testInvokeTxnThatReturnsNullString() { final ContractRouter r = new ContractRouter(new String[] {"-a", "127.0.0.1:7052", "-i", "testId"}); r.findAllContracts(); final ChaincodeStub s = new ChaincodeStubNaiveImpl(); @@ -316,7 +316,7 @@ public void testInvokeTxnThatReturnsNullString() { } @Test - public void testInvokeTxnThatThrowsAnException() { + void testInvokeTxnThatThrowsAnException() { final ContractRouter r = new ContractRouter(new String[] {"-a", "127.0.0.1:7052", "-i", "testId"}); r.findAllContracts(); final ChaincodeStub s = new ChaincodeStubNaiveImpl(); @@ -342,7 +342,7 @@ public void testInvokeTxnThatThrowsAnException() { } @Test - public void testInvokeTxnThatThrowsAChaincodeException() { + void testInvokeTxnThatThrowsAChaincodeException() { final ContractRouter r = new ContractRouter(new String[] {"-a", "127.0.0.1:7052", "-i", "testId"}); r.findAllContracts(); final ChaincodeStub s = new ChaincodeStubNaiveImpl(); @@ -369,14 +369,14 @@ public void testInvokeTxnThatThrowsAChaincodeException() { /** Test confirming ContractRuntimeExceptions can be created. */ @Test - public void createContractRuntimeExceptions() { + void createContractRuntimeExceptions() { final ContractRuntimeException cre1 = new ContractRuntimeException("failure"); new ContractRuntimeException("another failure", cre1); new ContractRuntimeException(new Exception("cause")); } @Test - public void testStartingContractRouterWithStartingAChaincodeServer() throws IOException { + void testStartingContractRouterWithStartingAChaincodeServer() throws IOException { ChaincodeServerProperties chaincodeServerProperties = new ChaincodeServerProperties(); chaincodeServerProperties.setServerAddress(new InetSocketAddress("0.0.0.0", 9999)); final ContractRouter r = new ContractRouter(new String[] {"-i", "testId"}); @@ -397,12 +397,12 @@ public void testStartingContractRouterWithStartingAChaincodeServer() throws IOEx e.printStackTrace(); } - final ChaincodeStub s = new ChaincodeStubNaiveImpl(); + final ChaincodeStubNaiveImpl s = new ChaincodeStubNaiveImpl(); final List args = new ArrayList<>(); args.add("samplecontract:t1"); args.add("asdf"); - ((ChaincodeStubNaiveImpl) s).setStringArgs(args); + s.setStringArgs(args); SampleContract.setBeforeInvoked(0); SampleContract.setAfterInvoked(0); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/MyType.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/MyType.java index 893aa81e..6cc39f0e 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/MyType.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/MyType.java @@ -27,12 +27,12 @@ public void setState(final String state) { @JSONPropertyIgnore() public boolean isStarted() { - return state.equals(STARTED); + return STARTED.equals(state); } @JSONPropertyIgnore() public boolean isStopped() { - return state.equals(STARTED); + return STOPPED.equals(state); } public MyType setValue(final String value) { diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/TransactionExceptionTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/TransactionExceptionTest.java index 3cc4df28..dcf619ad 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/TransactionExceptionTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/TransactionExceptionTest.java @@ -12,7 +12,7 @@ import org.hyperledger.fabric.shim.ChaincodeException; import org.junit.jupiter.api.Test; -public class TransactionExceptionTest { +final class TransactionExceptionTest { class MyTransactionException extends ChaincodeException { @@ -33,21 +33,21 @@ public byte[] getPayload() { } @Test - public void testNoArgConstructor() { + void testNoArgConstructor() { final ChaincodeException e = new ChaincodeException(); assertThat(e.getMessage(), is(nullValue())); assertThat(e.getPayload(), is(nullValue())); } @Test - public void testMessageArgConstructor() { + void testMessageArgConstructor() { final ChaincodeException e = new ChaincodeException("Failure"); assertThat(e.getMessage(), is("Failure")); assertThat(e.getPayload(), is(nullValue())); } @Test - public void testCauseArgConstructor() { + void testCauseArgConstructor() { final ChaincodeException e = new ChaincodeException(new Error("Cause")); assertThat(e.getMessage(), is("java.lang.Error: Cause")); assertThat(e.getPayload(), is(nullValue())); @@ -55,7 +55,7 @@ public void testCauseArgConstructor() { } @Test - public void testMessageAndCauseArgConstructor() { + void testMessageAndCauseArgConstructor() { final ChaincodeException e = new ChaincodeException("Failure", new Error("Cause")); assertThat(e.getMessage(), is("Failure")); assertThat(e.getPayload(), is(nullValue())); @@ -63,14 +63,14 @@ public void testMessageAndCauseArgConstructor() { } @Test - public void testMessageAndPayloadArgConstructor() { + void testMessageAndPayloadArgConstructor() { final ChaincodeException e = new ChaincodeException("Failure", new byte[] {'P', 'a', 'y', 'l', 'o', 'a', 'd'}); assertThat(e.getMessage(), is("Failure")); assertThat(e.getPayload(), is(new byte[] {'P', 'a', 'y', 'l', 'o', 'a', 'd'})); } @Test - public void testMessagePayloadAndCauseArgConstructor() { + void testMessagePayloadAndCauseArgConstructor() { final ChaincodeException e = new ChaincodeException("Failure", new byte[] {'P', 'a', 'y', 'l', 'o', 'a', 'd'}, new Error("Cause")); assertThat(e.getMessage(), is("Failure")); @@ -79,14 +79,14 @@ public void testMessagePayloadAndCauseArgConstructor() { } @Test - public void testMessageAndStringPayloadArgConstructor() { + void testMessageAndStringPayloadArgConstructor() { final ChaincodeException e = new ChaincodeException("Failure", "Payload"); assertThat(e.getMessage(), is("Failure")); assertThat(e.getPayload(), is(new byte[] {'P', 'a', 'y', 'l', 'o', 'a', 'd'})); } @Test - public void testMessageStringPayloadAndCauseArgConstructor() { + void testMessageStringPayloadAndCauseArgConstructor() { final ChaincodeException e = new ChaincodeException("Failure", "Payload", new Error("Cause")); assertThat(e.getMessage(), is("Failure")); assertThat(e.getPayload(), is(new byte[] {'P', 'a', 'y', 'l', 'o', 'a', 'd'})); @@ -94,7 +94,7 @@ public void testMessageStringPayloadAndCauseArgConstructor() { } @Test - public void testSubclass() { + void testSubclass() { final ChaincodeException e = new MyTransactionException(1); assertThat(e.getMessage(), is("MyTransactionException")); assertThat(e.getPayload(), is(new byte[] {'E', '0', '0', '1'})); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/execution/ContractExecutionServiceTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/execution/ContractExecutionServiceTest.java index 67d006d3..124ccbf9 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/execution/ContractExecutionServiceTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/execution/ContractExecutionServiceTest.java @@ -33,9 +33,9 @@ import org.hyperledger.fabric.shim.ChaincodeStub; import org.junit.jupiter.api.Test; -public final class ContractExecutionServiceTest { +final class ContractExecutionServiceTest { @Test - public void noReturnValue() + void noReturnValue() throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException { JSONTransactionSerializer jts = new JSONTransactionSerializer(); @@ -50,7 +50,7 @@ public void noReturnValue() ChaincodeStub stub = new ChaincodeStubNaiveImpl(); when(txFn.getRouting()).thenReturn(routing); - when(req.getArgs()).thenReturn(new ArrayList()); + when(req.getArgs()).thenReturn(new ArrayList<>()); when(routing.getMethod()) .thenReturn(SampleContract.class.getMethod("noReturn", new Class[] {Context.class})); when(routing.getContractInstance()).thenReturn(contract); @@ -61,7 +61,7 @@ public void noReturnValue() } @Test() - public void failureToInvoke() + void failureToInvoke() throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException { JSONTransactionSerializer jts = new JSONTransactionSerializer(); @@ -76,7 +76,7 @@ public void failureToInvoke() ChaincodeStub stub = mock(ChaincodeStub.class); when(txFn.getRouting()).thenReturn(routing); - when(req.getArgs()).thenReturn(new ArrayList() {}); + when(req.getArgs()).thenReturn(new ArrayList<>() {}); when(routing.getContractInstance()).thenThrow(IllegalAccessException.class); when(routing.toString()).thenReturn("MockMethodName:MockClassName"); @@ -88,7 +88,7 @@ public void failureToInvoke() } @Test() - public void invokeWithDifferentSerializers() + void invokeWithDifferentSerializers() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { JSONTransactionSerializer defaultSerializer = spy(new JSONTransactionSerializer()); SerializerInterface customSerializer = mock(SerializerInterface.class); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/execution/JSONTransactionSerializerTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/execution/JSONTransactionSerializerTest.java index eb15c8c9..15d0a52a 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/execution/JSONTransactionSerializerTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/execution/JSONTransactionSerializerTest.java @@ -21,9 +21,9 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -public class JSONTransactionSerializerTest { +final class JSONTransactionSerializerTest { @Test - public void toBuffer() { + void toBuffer() { final TypeRegistry tr = TypeRegistry.getRegistry(); tr.addDataType(MyType.class); @@ -53,14 +53,12 @@ public void toBuffer() { final byte[] buffer = "[{\"value\":\"hello\"},{\"value\":\"world\"}]".getBytes(StandardCharsets.UTF_8); - System.out.println(new String(buffer, StandardCharsets.UTF_8)); - System.out.println(new String(bytes, StandardCharsets.UTF_8)); assertThat(bytes, equalTo(buffer)); } @Nested @DisplayName("Complex Data types") - class ComplexDataTypes { + final class ComplexDataTypes { @Test public void alltypes() { @@ -74,22 +72,18 @@ public void alltypes() { final AllTypesAsset all = new AllTypesAsset(); final TypeSchema ts = TypeSchema.typeConvert(AllTypesAsset.class); - System.out.println("TS = " + ts); final byte[] bytes = serializer.toBuffer(all, ts); - System.out.println("Data as toBuffer-ed " + new String(bytes, StandardCharsets.UTF_8)); final AllTypesAsset returned = (AllTypesAsset) serializer.fromBuffer(bytes, ts); - System.out.println("Start object = " + all); - System.out.println("Returned object = " + returned); assertTrue(all.equals(returned)); } } @Nested @DisplayName("Primitive Arrays") - class PrimitiveArrays { + final class PrimitiveArrays { @Test - public void ints() { + void ints() { final JSONTransactionSerializer serializer = new JSONTransactionSerializer(); // convert array of primitive final int[] intarray = new int[] {42, 83}; @@ -101,7 +95,7 @@ public void ints() { } @Test - public void bytes() { + void bytes() { final JSONTransactionSerializer serializer = new JSONTransactionSerializer(); // convert array of primitive final byte[] array = new byte[] {42, 83}; @@ -113,7 +107,7 @@ public void bytes() { } @Test - public void floats() { + void floats() { final JSONTransactionSerializer serializer = new JSONTransactionSerializer(); // convert array of primitive final float[] array = new float[] {42.5F, 83.5F}; @@ -125,7 +119,7 @@ public void floats() { } @Test - public void booleans() { + void booleans() { final JSONTransactionSerializer serializer = new JSONTransactionSerializer(); // convert array of primitive final boolean[] array = new boolean[] {true, false, true}; @@ -137,7 +131,7 @@ public void booleans() { } @Test - public void chars() { + void chars() { final JSONTransactionSerializer serializer = new JSONTransactionSerializer(); // convert array of primitive final char[] array = new char[] {'a', 'b', 'c'}; @@ -151,9 +145,9 @@ public void chars() { @Nested @DisplayName("Nested Arrays") - class NestedArrays { + final class NestedArrays { @Test - public void ints() { + void ints() { final JSONTransactionSerializer serializer = new JSONTransactionSerializer(); final int[][] array = new int[][] {{42, 83}, {83, 42}}; final byte[] bytes = serializer.toBuffer(array, TypeSchema.typeConvert(int[][].class)); @@ -164,7 +158,7 @@ public void ints() { } @Test - public void longs() { + void longs() { final JSONTransactionSerializer serializer = new JSONTransactionSerializer(); final long[][] array = new long[][] {{42L, 83L}, {83L, 42L}}; final byte[] bytes = serializer.toBuffer(array, TypeSchema.typeConvert(long[][].class)); @@ -175,7 +169,7 @@ public void longs() { } @Test - public void doubles() { + void doubles() { final JSONTransactionSerializer serializer = new JSONTransactionSerializer(); final double[][] array = new double[][] {{42.42d, 83.83d}, {83.23d, 42.33d}}; final byte[] bytes = serializer.toBuffer(array, TypeSchema.typeConvert(double[][].class)); @@ -186,7 +180,7 @@ public void doubles() { } @Test - public void bytes() { + void bytes() { final JSONTransactionSerializer serializer = new JSONTransactionSerializer(); final byte[][] array = new byte[][] {{42, 83}, {83, 42}}; final byte[] bytes = serializer.toBuffer(array, TypeSchema.typeConvert(byte[][].class)); @@ -197,7 +191,7 @@ public void bytes() { } @Test - public void shorts() { + void shorts() { final JSONTransactionSerializer serializer = new JSONTransactionSerializer(); final short[][] array = new short[][] {{42, 83}, {83, 42}}; final byte[] bytes = serializer.toBuffer(array, TypeSchema.typeConvert(short[][].class)); @@ -209,7 +203,7 @@ public void shorts() { } @Test - public void fromBufferObject() { + void fromBufferObject() { final byte[] buffer = "[{\"value\":\"hello\"},{\"value\":\"world\"}]".getBytes(StandardCharsets.UTF_8); final TypeRegistry tr = TypeRegistry.getRegistry(); @@ -226,7 +220,7 @@ public void fromBufferObject() { } @Test - public void toBufferPrimitive() { + void toBufferPrimitive() { final TypeRegistry tr = TypeRegistry.getRegistry(); final JSONTransactionSerializer serializer = new JSONTransactionSerializer(); @@ -267,7 +261,7 @@ public void toBufferPrimitive() { } @Test - public void fromBufferErrors() { + void fromBufferErrors() { final TypeRegistry tr = new TypeRegistryImpl(); tr.addDataType(MyType.class); MetadataBuilder.addComponent(tr.getDataType("MyType")); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/metadata/MetadataBuilderTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/metadata/MetadataBuilderTest.java index 903a287f..2c55d199 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/metadata/MetadataBuilderTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/metadata/MetadataBuilderTest.java @@ -7,7 +7,6 @@ import contract.SampleContract; import java.io.InputStream; -import java.io.Serializable; import java.lang.reflect.Field; import java.util.HashMap; import org.everit.json.schema.loader.SchemaClient; @@ -22,46 +21,27 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public final class MetadataBuilderTest { - private final String expectedMetadataString = " {\n" + " \"components\": {\"schemas\": {}},\n" - + " \"$schema\": \"https://fabric-shim.github.io/contract-schema.json\",\n" - + " \"contracts\": {\"SampleContract\": {\n" - + " \"name\": \"SampleContract\",\n" + " \"transactions\": [],\n" - + " \"info\": {\n" - + " \"license\": {\"name\": \"\"},\n" + " \"description\": \"\",\n" - + " \"termsOfService\": \"\",\n" - + " \"title\": \"\",\n" + " \"version\": \"\",\n" - + " \"contact\": {\"email\": \"fred@example.com\"}\n" - + " }\n" + " }},\n" + " \"info\": {\n" + " \"license\": {\"name\": \"\"},\n" - + " \"description\": \"\",\n" - + " \"termsOfService\": \"\",\n" + " \"title\": \"\",\n" - + " \"version\": \"\",\n" - + " \"contact\": {\"email\": \"fred@example.com\"}\n" + " }\n" + " }\n" + ""; - +final class MetadataBuilderTest { // fields are private, so use reflection to bypass this for unit testing - private void setMetadataBuilderField(final String name, final Object value) { - try { - final Field f = MetadataBuilder.class.getDeclaredField(name); - f.setAccessible(true); - f.set(null, value); - } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { - e.printStackTrace(); - throw new RuntimeException("Unable to set field " + e.getMessage()); - } + private void setMetadataBuilderField(final String name, final Object value) + throws NoSuchFieldException, IllegalAccessException { + final Field f = MetadataBuilder.class.getDeclaredField(name); + f.setAccessible(true); + f.set(null, value); } @BeforeEach @AfterEach - public void beforeAndAfterEach() { + void beforeAndAfterEach() throws NoSuchFieldException, IllegalAccessException { - setMetadataBuilderField("componentMap", new HashMap()); - setMetadataBuilderField("contractMap", new HashMap>()); - setMetadataBuilderField("overallInfoMap", new HashMap()); + setMetadataBuilderField("componentMap", new HashMap<>()); + setMetadataBuilderField("contractMap", new HashMap<>()); + setMetadataBuilderField("overallInfoMap", new HashMap<>()); setMetadataBuilderField("schemaClient", new DefaultSchemaClient()); } @Test - public void systemContract() { + void systemContract() { final SystemContract system = new SystemContract(); final ChaincodeStub stub = new ChaincodeStubNaiveImpl(); @@ -69,14 +49,14 @@ public void systemContract() { } @Test - public void defaultSchemasNotLoadedFromNetwork() { + void defaultSchemasNotLoadedFromNetwork() throws NoSuchFieldException, IllegalAccessException { final ContractDefinition contractDefinition = new ContractDefinitionImpl(SampleContract.class); MetadataBuilder.addContract(contractDefinition); setMetadataBuilderField("schemaClient", new SchemaClient() { @Override public InputStream get(final String uri) { - throw new RuntimeException("Refusing to load schema: " + uri); + throw new IllegalStateException("Refusing to load schema: " + uri); } }); MetadataBuilder.validate(); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/metadata/TypeSchemaTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/metadata/TypeSchemaTest.java index 4139c165..7274e654 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/metadata/TypeSchemaTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/metadata/TypeSchemaTest.java @@ -18,32 +18,29 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -public class TypeSchemaTest { +final class TypeSchemaTest { @BeforeEach - public void beforeEach() {} + void beforeEach() {} @Test - public void putIfNotNull() { + void putIfNotNull() { final TypeSchema ts = new TypeSchema(); - System.out.println("Key - value"); ts.putIfNotNull("Key", "value"); - System.out.println("Key - null"); final String nullstr = null; ts.putIfNotNull("Key", nullstr); assertThat(ts.get("Key"), equalTo("value")); - System.out.println("Key - "); ts.putIfNotNull("Key", ""); assertThat(ts.get("Key"), equalTo("value")); } @Test - public void getType() { + void getType() { final TypeSchema ts = new TypeSchema(); ts.put("type", "MyType"); assertThat(ts.getType(), equalTo("MyType")); @@ -54,7 +51,7 @@ public void getType() { } @Test - public void getFormat() { + void getFormat() { final TypeSchema ts = new TypeSchema(); ts.put("format", "MyFormat"); assertThat(ts.getFormat(), equalTo("MyFormat")); @@ -65,7 +62,7 @@ public void getFormat() { } @Test - public void getRef() { + void getRef() { final TypeSchema ts = new TypeSchema(); ts.put("$ref", "#/ref/to/MyType"); assertThat(ts.getRef(), equalTo("#/ref/to/MyType")); @@ -76,7 +73,7 @@ public void getRef() { } @Test - public void getItems() { + void getItems() { final TypeSchema ts1 = new TypeSchema(); final TypeSchema ts = new TypeSchema(); @@ -92,7 +89,7 @@ public void getItems() { class MyType {} @Test - public void getTypeClass() { + void getTypeClass() { final TypeSchema ts = new TypeSchema(); ts.put("type", "string"); @@ -139,7 +136,7 @@ public void getTypeClass() { } @Test - public void unknownConversions() { + void unknownConversions() { assertThrows(RuntimeException.class, () -> { final TypeSchema ts = new TypeSchema(); final TypeRegistry mockRegistry = new TypeRegistryImpl(); @@ -158,7 +155,7 @@ public void unknownConversions() { } @Test - public void typeConvertPrimitives() { + void typeConvertPrimitives() { TypeSchema rts; final String[] array = new String[] {}; @@ -188,7 +185,7 @@ public void typeConvertPrimitives() { } @Test - public void typeConvertObjects() { + void typeConvertObjects() { TypeSchema rts; rts = TypeSchema.typeConvert(String.class); assertThat(rts.getType(), equalTo("string")); @@ -223,7 +220,7 @@ public void typeConvertObjects() { } @Test - public void validate() { + void validate() { final TypeSchema ts = TypeSchema.typeConvert(org.hyperledger.fabric.contract.MyType.class); final DataTypeDefinition dtd = new DataTypeDefinitionImpl(org.hyperledger.fabric.contract.MyType.class); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/ContractDefinitionTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/ContractDefinitionTest.java index a948720c..7ba81940 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/ContractDefinitionTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/ContractDefinitionTest.java @@ -21,9 +21,9 @@ import org.hyperledger.fabric.contract.routing.impl.ContractDefinitionImpl; import org.junit.jupiter.api.Test; -public class ContractDefinitionTest { +final class ContractDefinitionTest { @Test - public void constructor() throws NoSuchMethodException, SecurityException { + void constructor() throws NoSuchMethodException, SecurityException { final ContractDefinition cf = new ContractDefinitionImpl(SampleContract.class); assertThat(cf.toString(), startsWith("samplecontract:")); @@ -33,10 +33,10 @@ public void constructor() throws NoSuchMethodException, SecurityException { public class FailureTestObject {} private boolean fail; - private final int step = 1; + private static final int STEP = 1; @Test - public void unknownRoute() { + void unknownRoute() { final SecurityManager tmp = new SecurityManager() { private int count = 0; @@ -45,7 +45,7 @@ public void unknownRoute() { public void checkPackageAccess(final String pkg) { if (pkg.startsWith("org.hyperledger.fabric.contract")) { - if (count >= step) { + if (count >= STEP) { throw new SecurityException("Sorry I can't do that"); } count++; @@ -54,9 +54,7 @@ public void checkPackageAccess(final String pkg) { } @Override - public void checkPermission(final Permission perm) { - return; - } + public void checkPermission(final Permission perm) {} }; try { @@ -73,7 +71,7 @@ public void checkPermission(final Permission perm) { } @Test - public void duplicateTransaction() throws NoSuchMethodException, SecurityException { + void duplicateTransaction() throws NoSuchMethodException, SecurityException { final ContractDefinition cf = new ContractDefinitionImpl(SampleContract.class); final ContractInterface contract = new SampleContract(); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/DataTypeDefinitionTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/DataTypeDefinitionTest.java index f08e3bd4..84ca808c 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/DataTypeDefinitionTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/DataTypeDefinitionTest.java @@ -5,37 +5,38 @@ */ package org.hyperledger.fabric.contract.routing; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasEntry; -import static org.hamcrest.Matchers.hasKey; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; import java.util.Map; import org.hyperledger.fabric.contract.MyType2; +import org.hyperledger.fabric.contract.metadata.TypeSchema; import org.hyperledger.fabric.contract.routing.impl.DataTypeDefinitionImpl; import org.junit.jupiter.api.Test; -public class DataTypeDefinitionTest { +final class DataTypeDefinitionTest { @Test - public void constructor() { + void constructor() { final DataTypeDefinitionImpl dtd = new DataTypeDefinitionImpl(MyType2.class); - assertThat(dtd.getTypeClass(), equalTo(MyType2.class)); - assertThat(dtd.getName(), equalTo("org.hyperledger.fabric.contract.MyType2")); - assertThat(dtd.getSimpleName(), equalTo("MyType2")); + assertThat(dtd.getTypeClass()).isEqualTo(MyType2.class); + assertThat(dtd.getName()).isEqualTo("org.hyperledger.fabric.contract.MyType2"); + assertThat(dtd.getSimpleName()).isEqualTo("MyType2"); final Map properties = dtd.getProperties(); - assertThat(properties.size(), equalTo(2)); - assertThat(properties, hasKey("value")); - assertThat(properties, hasKey("constrainedValue")); + assertThat(properties.size()).isEqualTo(2); + assertThat(properties).containsKey("value"); + assertThat(properties).containsKey("constrainedValue"); final PropertyDefinition pd = properties.get("constrainedValue"); - final Map ts = pd.getSchema(); + final TypeSchema ts = pd.getSchema(); - assertThat(ts, hasEntry("title", "MrProperty")); - assertThat(ts, hasEntry("Pattern", "[a-z]")); - assertThat(ts, hasEntry("uniqueItems", false)); - assertThat(ts, hasEntry("required", new String[] {"true", "false"})); - assertThat(ts, hasEntry("enum", new String[] {"a", "bee", "cee", "dee"})); - assertThat(ts, hasEntry("minimum", 42)); + assertThat(ts) + .contains( + entry("title", "MrProperty"), + entry("Pattern", "[a-z]"), + entry("uniqueItems", false), + entry("required", new String[] {"true", "false"}), + entry("enum", new String[] {"a", "bee", "cee", "dee"}), + entry("minimum", 42)); } } diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/ParameterDefinitionTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/ParameterDefinitionTest.java index 84524443..8349527b 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/ParameterDefinitionTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/ParameterDefinitionTest.java @@ -13,9 +13,9 @@ import org.hyperledger.fabric.contract.routing.impl.ParameterDefinitionImpl; import org.junit.jupiter.api.Test; -public class ParameterDefinitionTest { +final class ParameterDefinitionTest { @Test - public void constructor() throws NoSuchMethodException, SecurityException { + void constructor() throws NoSuchMethodException, SecurityException { final Parameter[] params = String.class.getMethod("concat", String.class).getParameters(); final ParameterDefinition pd = new ParameterDefinitionImpl("test", String.class, new TypeSchema(), params[0]); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/PropertyDefinitionTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/PropertyDefinitionTest.java index 9ec68a68..cb73dbca 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/PropertyDefinitionTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/PropertyDefinitionTest.java @@ -13,9 +13,9 @@ import org.hyperledger.fabric.contract.routing.impl.PropertyDefinitionImpl; import org.junit.jupiter.api.Test; -public class PropertyDefinitionTest { +final class PropertyDefinitionTest { @Test - public void constructor() throws NoSuchMethodException, SecurityException { + void constructor() throws NoSuchMethodException, SecurityException { final Field[] props = String.class.getFields(); final TypeSchema ts = new TypeSchema(); final PropertyDefinition pd = new PropertyDefinitionImpl("test", String.class, ts, props[0]); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/TxFunctionTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/TxFunctionTest.java index 428a9483..d110dfa9 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/TxFunctionTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/TxFunctionTest.java @@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; -public class TxFunctionTest { +final class TxFunctionTest { @Contract() class TestObject implements ContractInterface { @@ -39,7 +39,7 @@ public void wibble(final String arg1) {} } @Test - public void constructor() throws NoSuchMethodException, SecurityException { + void constructor() throws NoSuchMethodException, SecurityException { final TestObject test = new TestObject(); final ContractDefinition cd = mock(ContractDefinition.class); Mockito.when(cd.getAnnotation()).thenReturn(test.getClass().getAnnotation(Contract.class)); @@ -53,7 +53,7 @@ public void constructor() throws NoSuchMethodException, SecurityException { } @Test - public void property() throws NoSuchMethodException, SecurityException { + void property() throws NoSuchMethodException, SecurityException { final TestObject test = new TestObject(); final ContractDefinition cd = mock(ContractDefinition.class); Mockito.when(cd.getAnnotation()).thenReturn(test.getClass().getAnnotation(Contract.class)); @@ -70,12 +70,11 @@ public void property() throws NoSuchMethodException, SecurityException { final TypeSchema ts = new TypeSchema(); txfn.setReturnSchema(ts); final TypeSchema rts = txfn.getReturnSchema(); - System.out.println(ts); assertEquals(ts, rts); } @Test - public void invaldtxfn() throws NoSuchMethodException, SecurityException { + void invaldtxfn() throws NoSuchMethodException, SecurityException { final TestObject test = new TestObject(); final ContractDefinition cd = mock(ContractDefinition.class); Mockito.when(cd.getAnnotation()).thenReturn(test.getClass().getAnnotation(Contract.class)); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/TypeRegistryTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/TypeRegistryTest.java index 16c59648..7c38c01b 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/TypeRegistryTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/routing/TypeRegistryTest.java @@ -13,9 +13,9 @@ import org.hyperledger.fabric.contract.routing.impl.TypeRegistryImpl; import org.junit.jupiter.api.Test; -public class TypeRegistryTest { +final class TypeRegistryTest { @Test - public void addDataType() { + void addDataType() { final TypeRegistryImpl tr = new TypeRegistryImpl(); tr.addDataType(String.class); @@ -24,7 +24,7 @@ public void addDataType() { } @Test - public void addDataTypeDefinition() { + void addDataTypeDefinition() { final DataTypeDefinitionImpl dtd = new DataTypeDefinitionImpl(String.class); final TypeRegistryImpl tr = new TypeRegistryImpl(); tr.addDataType(dtd); @@ -34,7 +34,7 @@ public void addDataTypeDefinition() { } @Test - public void getAllDataTypes() { + void getAllDataTypes() { final TypeRegistryImpl tr = new TypeRegistryImpl(); tr.addDataType(String.class); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/simplepath/ContractSimplePathTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/simplepath/ContractSimplePathTest.java index 489919d4..1b292661 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/simplepath/ContractSimplePathTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/contract/simplepath/ContractSimplePathTest.java @@ -31,14 +31,14 @@ import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; @ExtendWith(SystemStubsExtension.class) -public final class ContractSimplePathTest { +final class ContractSimplePathTest { @SystemStub private final EnvironmentVariables environmentVariables = new EnvironmentVariables(); private ChaincodeMockPeer server; @AfterEach - public void afterTest() throws Exception { + void afterTest() throws Exception { if (server != null) { server.stop(); server = null; @@ -51,7 +51,7 @@ public void afterTest() throws Exception { * @throws Exception */ @Test - public void testContract() throws Exception { + void testContract() throws Exception { final List scenario = new ArrayList<>(); scenario.add(new RegisterStep()); @@ -66,7 +66,7 @@ public void testContract() throws Exception { setLogLevel("INFO"); } - public ChaincodeMessage newInvokeFn(final String[] args) { + private ChaincodeMessage newInvokeFn(final String[] args) { final Builder invokePayload = ChaincodeInput.newBuilder(); for (final String arg : args) { invokePayload.addArgs(ByteString.copyFromUtf8(arg)); @@ -76,12 +76,12 @@ public ChaincodeMessage newInvokeFn(final String[] args) { TRANSACTION, "testChannel", "0", invokePayload.build().toByteString(), null); } - public String getLastReturnString() throws Exception { + private String getLastReturnString() throws Exception { final Response resp = Response.parseFrom(server.getLastMessageRcvd().getPayload()); - return (resp.getPayload().toStringUtf8()); + return resp.getPayload().toStringUtf8(); } - public void setLogLevel(final String logLevel) { + private void setLogLevel(final String logLevel) { environmentVariables.set("CORE_CHAINCODE_LOGGING_SHIM", logLevel); environmentVariables.set("CORE_CHAINCODE_LOGGING_LEVEL", logLevel); } diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/ledger/LedgerTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/ledger/LedgerTest.java index 42800291..fac77201 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/ledger/LedgerTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/ledger/LedgerTest.java @@ -12,10 +12,10 @@ import org.hyperledger.fabric.contract.Context; import org.junit.jupiter.api.Test; -public class LedgerTest { +final class LedgerTest { @Test - public void getLedger() { + void getLedger() { final Context ctx = mock(Context.class); final Ledger ledger = Ledger.getLedger(ctx); @@ -29,7 +29,7 @@ public void getLedger() { } @Test - public void getCollection() { + void getCollection() { final Context ctx = mock(Context.class); final Ledger ledger = Ledger.getLedger(ctx); @@ -44,7 +44,7 @@ public void getCollection() { } @Test - public void getNamedCollection() { + void getNamedCollection() { final Context ctx = mock(Context.class); final Ledger ledger = Ledger.getLedger(ctx); @@ -57,7 +57,7 @@ public void getNamedCollection() { } @Test - public void getOrganizationCollection() { + void getOrganizationCollection() { final Context ctx = mock(Context.class); final Ledger ledger = Ledger.getLedger(ctx); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/metrics/MetricsTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/metrics/MetricsTest.java index eb562ade..8da2694f 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/metrics/MetricsTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/metrics/MetricsTest.java @@ -16,9 +16,9 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -public class MetricsTest { +final class MetricsTest { - public static class TestProvider implements MetricsProvider { + private static final class TestProvider implements MetricsProvider { public TestProvider() {} @@ -31,16 +31,16 @@ public void initialize(final Properties props) {} @Nested @DisplayName("Metrics initialize") - class Initialize { + final class Initialize { @Test - public void metricsDisabled() { + void metricsDisabled() { final MetricsProvider provider = Metrics.initialize(new Properties()); assertThat(provider).isExactlyInstanceOf(NullProvider.class); } @Test - public void metricsEnabledUnknownProvider() { + void metricsEnabledUnknownProvider() { final Properties props = new Properties(); props.put("CHAINCODE_METRICS_PROVIDER", "org.example.metrics.provider"); props.put("CHAINCODE_METRICS_ENABLED", "true"); @@ -54,7 +54,7 @@ public void metricsEnabledUnknownProvider() { } @Test - public void metricsNoProvider() { + void metricsNoProvider() { final Properties props = new Properties(); props.put("CHAINCODE_METRICS_ENABLED", "true"); @@ -63,7 +63,7 @@ public void metricsNoProvider() { } @Test - public void metricsValid() { + void metricsValid() { final Properties props = new Properties(); props.put("CHAINCODE_METRICS_PROVIDER", MetricsTest.TestProvider.class.getName()); props.put("CHAINCODE_METRICS_ENABLED", "true"); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/metrics/impl/DefaultProviderTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/metrics/impl/DefaultProviderTest.java index 0da931aa..a3fecb8d 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/metrics/impl/DefaultProviderTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/metrics/impl/DefaultProviderTest.java @@ -13,17 +13,16 @@ import java.util.logging.LogManager; import java.util.logging.LogRecord; import java.util.logging.Logger; -import org.hyperledger.fabric.metrics.MetricsProvider; import org.hyperledger.fabric.metrics.TaskMetricsCollector; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; -public class DefaultProviderTest { +final class DefaultProviderTest { @Test - public void allMethods() { - MetricsProvider provider = new DefaultProvider(); + void allMethods() throws InterruptedException { + DefaultProvider provider = new DefaultProvider(); provider.setTaskMetricsCollector(new TaskMetricsCollector() { @Override @@ -73,13 +72,8 @@ public int getActiveCount() { perfLogger.addHandler(mockHandler); provider.initialize(new Properties()); - ((DefaultProvider) provider).logMetrics(); - try { - Thread.sleep(6000); - } catch (InterruptedException e) { - e.printStackTrace(); - throw new RuntimeException(e); - } + provider.logMetrics(); + Thread.sleep(6000); Mockito.verify(mockHandler, Mockito.atLeast(1)).publish(argumentCaptor.capture()); LogRecord lr = argumentCaptor.getValue(); String msg = lr.getMessage(); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeBaseTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeBaseTest.java index 7cd97a1a..894c60fe 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeBaseTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeBaseTest.java @@ -34,12 +34,12 @@ import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; @ExtendWith(SystemStubsExtension.class) -public class ChaincodeBaseTest { +final class ChaincodeBaseTest { @SystemStub private final EnvironmentVariables environmentVariables = new EnvironmentVariables(); @Test - public void testNewSuccessResponseEmpty() { + void testNewSuccessResponseEmpty() { final org.hyperledger.fabric.shim.Chaincode.Response response = ResponseUtils.newSuccessResponse(); assertThat(response.getStatus()) .as("Response status") @@ -49,7 +49,7 @@ public void testNewSuccessResponseEmpty() { } @Test - public void testNewSuccessResponseWithMessage() { + void testNewSuccessResponseWithMessage() { final org.hyperledger.fabric.shim.Chaincode.Response response = ResponseUtils.newSuccessResponse("Simple message"); assertThat(response.getStatus()) @@ -60,7 +60,7 @@ public void testNewSuccessResponseWithMessage() { } @Test - public void testNewSuccessResponseWithPayload() { + void testNewSuccessResponseWithPayload() { final org.hyperledger.fabric.shim.Chaincode.Response response = ResponseUtils.newSuccessResponse("Simple payload".getBytes(Charset.defaultCharset())); assertThat(response.getStatus()) @@ -73,7 +73,7 @@ public void testNewSuccessResponseWithPayload() { } @Test - public void testNewSuccessResponseWithMessageAndPayload() { + void testNewSuccessResponseWithMessageAndPayload() { final org.hyperledger.fabric.shim.Chaincode.Response response = ResponseUtils.newSuccessResponse("Simple message", "Simple payload".getBytes(Charset.defaultCharset())); assertThat(response.getStatus()) @@ -86,7 +86,7 @@ public void testNewSuccessResponseWithMessageAndPayload() { } @Test - public void testNewErrorResponseEmpty() { + void testNewErrorResponseEmpty() { final org.hyperledger.fabric.shim.Chaincode.Response response = ResponseUtils.newErrorResponse(); assertThat(response.getStatus()) .as("Response status") @@ -96,7 +96,7 @@ public void testNewErrorResponseEmpty() { } @Test - public void testNewErrorResponseWithMessage() { + void testNewErrorResponseWithMessage() { final org.hyperledger.fabric.shim.Chaincode.Response response = ResponseUtils.newErrorResponse("Simple message"); assertThat(response.getStatus()) @@ -107,7 +107,7 @@ public void testNewErrorResponseWithMessage() { } @Test - public void testNewErrorResponseWithPayload() { + void testNewErrorResponseWithPayload() { final org.hyperledger.fabric.shim.Chaincode.Response response = ResponseUtils.newErrorResponse("Simple payload".getBytes(Charset.defaultCharset())); assertThat(response.getStatus()) @@ -120,7 +120,7 @@ public void testNewErrorResponseWithPayload() { } @Test - public void testNewErrorResponseWithMessageAndPayload() { + void testNewErrorResponseWithMessageAndPayload() { final org.hyperledger.fabric.shim.Chaincode.Response response = ResponseUtils.newErrorResponse("Simple message", "Simple payload".getBytes(Charset.defaultCharset())); assertThat(response.getStatus()) @@ -133,7 +133,7 @@ public void testNewErrorResponseWithMessageAndPayload() { } @Test - public void testNewErrorResponseWithException() { + void testNewErrorResponseWithException() { final org.hyperledger.fabric.shim.Chaincode.Response response = ResponseUtils.newErrorResponse(new Exception("Simple exception")); assertThat(response.getStatus()) @@ -144,7 +144,7 @@ public void testNewErrorResponseWithException() { } @Test - public void testNewErrorResponseWithChaincodeException() { + void testNewErrorResponseWithChaincodeException() { final org.hyperledger.fabric.shim.Chaincode.Response response = ResponseUtils.newErrorResponse(new ChaincodeException("Chaincode exception")); assertThat(response.getStatus()) @@ -155,7 +155,7 @@ public void testNewErrorResponseWithChaincodeException() { } @Test - public void testOptions() throws Exception { + void testOptions() throws Exception { final ChaincodeBase cb = new EmptyChaincode(); assertThat(cb.getHost()).as("Host incorrect").isEqualTo(ChaincodeBase.DEFAULT_HOST); @@ -197,7 +197,7 @@ public void testOptions() throws Exception { } @Test - public void testUnsetOptionId() { + void testUnsetOptionId() { final ChaincodeBase cb = new EmptyChaincode(); assertThatThrownBy(cb::validateOptions) .isInstanceOf(IllegalArgumentException.class) @@ -205,7 +205,7 @@ public void testUnsetOptionId() { } @Test - public void testUnsetOptionClientCertPath() { + void testUnsetOptionClientCertPath() { final ChaincodeBase cb = new EmptyChaincode(); environmentVariables.set("CORE_CHAINCODE_ID_NAME", "mycc"); environmentVariables.set("CORE_PEER_TLS_ENABLED", "true"); @@ -216,7 +216,7 @@ public void testUnsetOptionClientCertPath() { } @Test - public void testUnsetOptionClientKeyPath() { + void testUnsetOptionClientKeyPath() { final ChaincodeBase cb = new EmptyChaincode(); environmentVariables.set("CORE_CHAINCODE_ID_NAME", "mycc"); environmentVariables.set("CORE_PEER_TLS_ENABLED", "true"); @@ -229,7 +229,7 @@ public void testUnsetOptionClientKeyPath() { @Test @Disabled - public void testNewChannelBuilder() throws Exception { + void testNewChannelBuilder() throws Exception { final ChaincodeBase cb = new EmptyChaincode(); environmentVariables.set("CORE_CHAINCODE_ID_NAME", "mycc"); @@ -245,7 +245,7 @@ public void testNewChannelBuilder() throws Exception { } @Test - public void testInitializeLogging() { + void testInitializeLogging() { final ChaincodeBase cb = new EmptyChaincode(); cb.processEnvironmentOptions(); @@ -291,7 +291,7 @@ public void testInitializeLogging() { } @Test - public void testStartFailsWithoutValidOptions() { + void testStartFailsWithoutValidOptions() { final String[] args = new String[0]; final ChaincodeBase cb = new EmptyChaincode(); @@ -311,7 +311,7 @@ public void testStartFailsWithoutValidOptions() { "The chaincode id must be specified using either the -i or --i command line options or the CORE_CHAINCODE_ID_NAME environment variable."); } - public static void setLogLevelForChaincode( + private static void setLogLevelForChaincode( final EnvironmentVariables environmentVariables, final ChaincodeBase cb, final String shimLevel, @@ -323,7 +323,7 @@ public static void setLogLevelForChaincode( } @Test - public void connectChaincodeBase() throws IOException { + void connectChaincodeBase() throws IOException { final ChaincodeBase cb = new EmptyChaincode(); environmentVariables.set("CORE_CHAINCODE_ID_NAME", "mycc"); @@ -337,7 +337,7 @@ public void connectChaincodeBase() throws IOException { Metrics.initialize(props); Traces.initialize(props); - cb.connectToPeer(new StreamObserver() { + cb.connectToPeer(new StreamObserver<>() { @Override public void onNext(final ChaincodeMessage value) {} @@ -354,7 +354,7 @@ public void onCompleted() {} } @Test - public void connectChaincodeBaseNull() { + void connectChaincodeBaseNull() { Assertions.assertThrows(IllegalArgumentException.class, () -> { final ChaincodeBase cb = new EmptyChaincode(); cb.connectToPeer(null); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeServerImplTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeServerImplTest.java index 3ab40706..3d143458 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeServerImplTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeServerImplTest.java @@ -18,7 +18,7 @@ import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; @ExtendWith(SystemStubsExtension.class) -class ChaincodeServerImplTest { +final class ChaincodeServerImplTest { @SystemStub private final EnvironmentVariables environmentVariables = new EnvironmentVariables(); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeStubTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeStubTest.java index ccf3185a..0a583dea 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeStubTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeStubTest.java @@ -19,9 +19,10 @@ import org.hyperledger.fabric.shim.ledger.QueryResultsIteratorWithMetadata; import org.junit.jupiter.api.Test; -public class ChaincodeStubTest { +final class ChaincodeStubTest { - class FakeStub implements ChaincodeStub { + @SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull") + private static final class FakeStub implements ChaincodeStub { @Override public List getArgs() { @@ -289,13 +290,13 @@ public String getMspId() { } @Test - public void testDefaultMethods() { + void testDefaultMethods() { final ChaincodeStub stub = new FakeStub(); final String chaincodeName = "ACME_SHIPPING"; - stub.invokeChaincode(chaincodeName, new ArrayList()); - stub.invokeChaincodeWithStringArgs(chaincodeName, new ArrayList(), "channel"); - stub.invokeChaincodeWithStringArgs(chaincodeName, new ArrayList()); + stub.invokeChaincode(chaincodeName, new ArrayList<>()); + stub.invokeChaincodeWithStringArgs(chaincodeName, new ArrayList<>(), "channel"); + stub.invokeChaincodeWithStringArgs(chaincodeName, new ArrayList<>()); stub.invokeChaincodeWithStringArgs(chaincodeName, "anvil", "tnt"); stub.getStringState("key"); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeTest.java index 7b70b388..2eaa38ca 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChaincodeTest.java @@ -11,9 +11,9 @@ import java.nio.charset.StandardCharsets; import org.junit.jupiter.api.Test; -public class ChaincodeTest { +final class ChaincodeTest { @Test - public void testResponse() { + void testResponse() { final Chaincode.Response resp = new Chaincode.Response( Chaincode.Response.Status.SUCCESS, "No message", "no payload".getBytes(StandardCharsets.UTF_8)); assertThat(Chaincode.Response.Status.SUCCESS).as("Incorrect status").isEqualTo(resp.getStatus()); @@ -22,7 +22,7 @@ public void testResponse() { } @Test - public void testResponseWithCode() { + void testResponseWithCode() { Chaincode.Response resp = new Chaincode.Response(200, "No message", "no payload".getBytes(StandardCharsets.UTF_8)); assertThat(Chaincode.Response.Status.SUCCESS).as("Incorrect status").isEqualTo(resp.getStatus()); @@ -46,7 +46,7 @@ public void testResponseWithCode() { } @Test - public void testStatus() { + void testStatus() { assertThat(Chaincode.Response.Status.SUCCESS) .as("Wrong status") .isEqualTo(Chaincode.Response.Status.forCode(200)); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChatChaincodeWithPeerTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChatChaincodeWithPeerTest.java index d3832502..d6093a5d 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChatChaincodeWithPeerTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ChatChaincodeWithPeerTest.java @@ -22,7 +22,6 @@ import java.io.IOException; import java.util.List; import java.util.Properties; -import java.util.stream.Collectors; import java.util.stream.Stream; import org.hyperledger.fabric.metrics.Metrics; import org.hyperledger.fabric.protos.peer.ChaincodeID; @@ -42,7 +41,7 @@ import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; @ExtendWith(SystemStubsExtension.class) -class ChatChaincodeWithPeerTest { +final class ChatChaincodeWithPeerTest { private static final String TEST_CHANNEL = "testChannel"; @SystemStub @@ -138,6 +137,7 @@ void connectNull() throws IOException { } @Test + @SuppressWarnings("PMD.SystemPrintln") void connectAndReceiveRegister() throws IOException { environmentVariables.set("CORE_CHAINCODE_ID_NAME", "mycc"); ChaincodeBase chaincodeBase = new EmptyChaincode(); @@ -149,32 +149,31 @@ void connectAndReceiveRegister() throws IOException { Metrics.initialize(props); ChatChaincodeWithPeer chatChaincodeWithPeer = new ChatChaincodeWithPeer(chaincodeBase); - final StreamObserver connect = - chatChaincodeWithPeer.connect(new StreamObserver() { - @Override - public void onNext(final ChaincodeMessage value) { - assertEquals(ChaincodeMessage.Type.REGISTER, value.getType()); - assertEquals("\u0012\u0004mycc", value.getPayload().toStringUtf8()); - } - - @Override - public void onError(final Throwable t) { - assertNull(t); - } - - @Override - public void onCompleted() {} - }); + final StreamObserver connect = chatChaincodeWithPeer.connect(new StreamObserver<>() { + @Override + public void onNext(final ChaincodeMessage value) { + assertEquals(ChaincodeMessage.Type.REGISTER, value.getType()); + assertEquals("\u0012\u0004mycc", value.getPayload().toStringUtf8()); + } + + @Override + public void onError(final Throwable t) { + assertNull(t); + } + + @Override + public void onCompleted() {} + }); assertNotNull(connect); - final ByteString payload = org.hyperledger.fabric.protos.peer.ChaincodeInput.newBuilder() + final ByteString payload = ChaincodeInput.newBuilder() .addArgs(ByteString.copyFromUtf8("")) .build() .toByteString(); final ChaincodeMessage initMsg = MessageUtil.newEventMessage(INIT, TEST_CHANNEL, "0", payload, null); connect.onNext(initMsg); - try { + { final List args = Stream.of("invoke", "a", "1").map(x -> x.getBytes(UTF_8)).collect(toList()); final ByteString invocationSpecPayload = ChaincodeSpec.newBuilder() @@ -182,7 +181,7 @@ public void onCompleted() {} .setName(chaincodeBase.getId()) .build()) .setInput(ChaincodeInput.newBuilder() - .addAllArgs(args.stream().map(ByteString::copyFrom).collect(Collectors.toList())) + .addAllArgs(args.stream().map(ByteString::copyFrom).collect(toList())) .build()) .build() .toByteString(); @@ -195,11 +194,9 @@ public void onCompleted() {} .build(); connect.onNext(invokeChaincodeMessage); System.out.println(invokeChaincodeMessage.getPayload().toStringUtf8()); - } catch (Exception e) { - } - try { + { final List args = Stream.of("invoke", "a", "1").map(x -> x.getBytes(UTF_8)).collect(toList()); final ByteString invocationSpecPayload = ChaincodeSpec.newBuilder() @@ -207,7 +204,7 @@ public void onCompleted() {} .setName(chaincodeBase.getId()) .build()) .setInput(ChaincodeInput.newBuilder() - .addAllArgs(args.stream().map(ByteString::copyFrom).collect(Collectors.toList())) + .addAllArgs(args.stream().map(ByteString::copyFrom).collect(toList())) .build()) .build() .toByteString(); @@ -220,8 +217,6 @@ public void onCompleted() {} .build(); connect.onNext(invokeChaincodeMessage); System.out.println(invokeChaincodeMessage.getPayload().toStringUtf8()); - } catch (Exception e) { - } } @@ -237,22 +232,21 @@ void connectAndReceiveRegisterComplete() throws IOException { Metrics.initialize(props); ChatChaincodeWithPeer chatChaincodeWithPeer = new ChatChaincodeWithPeer(chaincodeBase); - final StreamObserver connect = - chatChaincodeWithPeer.connect(new StreamObserver() { - @Override - public void onNext(final ChaincodeMessage value) { - assertEquals(ChaincodeMessage.Type.REGISTER, value.getType()); - assertEquals("\u0012\u0004mycc", value.getPayload().toStringUtf8()); - } - - @Override - public void onError(final Throwable t) { - assertNull(t); - } - - @Override - public void onCompleted() {} - }); + final StreamObserver connect = chatChaincodeWithPeer.connect(new StreamObserver<>() { + @Override + public void onNext(final ChaincodeMessage value) { + assertEquals(ChaincodeMessage.Type.REGISTER, value.getType()); + assertEquals("\u0012\u0004mycc", value.getPayload().toStringUtf8()); + } + + @Override + public void onError(final Throwable t) { + assertNull(t); + } + + @Override + public void onCompleted() {} + }); connect.onCompleted(); } @@ -268,21 +262,21 @@ void connectAndReceiveRegisterException() throws IOException { Metrics.initialize(props); ChatChaincodeWithPeer chatChaincodeWithPeer = new ChatChaincodeWithPeer(chaincodeBase); - final StreamObserver connect = - chatChaincodeWithPeer.connect(new StreamObserver() { - @Override - public void onNext(final ChaincodeMessage value) {} + final StreamObserver connect = chatChaincodeWithPeer.connect(new StreamObserver<>() { + @Override + public void onNext(final ChaincodeMessage value) {} - @Override - public void onError(final Throwable t) {} + @Override + public void onError(final Throwable t) {} - @Override - public void onCompleted() {} - }); + @Override + public void onCompleted() {} + }); connect.onError(new Exception("some_error")); } @Test + @SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes") void connectOnCompletedException() throws IOException { environmentVariables.set("CORE_CHAINCODE_ID_NAME", "mycc"); ChaincodeBase chaincodeBase = new EmptyChaincode(); @@ -297,19 +291,18 @@ void connectOnCompletedException() throws IOException { Assertions.assertDoesNotThrow( () -> { - final StreamObserver connect = - chatChaincodeWithPeer.connect(new StreamObserver() { - @Override - public void onNext(final ChaincodeMessage value) {} - - @Override - public void onError(final Throwable t) {} - - @Override - public void onCompleted() { - throw new RuntimeException("some_error"); - } - }); + chatChaincodeWithPeer.connect(new StreamObserver<>() { + @Override + public void onNext(final ChaincodeMessage value) {} + + @Override + public void onError(final Throwable t) {} + + @Override + public void onCompleted() { + throw new RuntimeException("some_error"); + } + }); }, "some_error"); } @@ -323,7 +316,7 @@ void testMockChaincodeBase() throws IOException { ChatChaincodeWithPeer chatChaincodeWithPeer = new ChatChaincodeWithPeer(mockChaincodeBase); assertNotNull(chatChaincodeWithPeer); - assertNull(chatChaincodeWithPeer.connect(new StreamObserver() { + assertNull(chatChaincodeWithPeer.connect(new StreamObserver<>() { @Override public void onNext(final ChaincodeMessage value) {} @@ -346,7 +339,7 @@ void testMockChaincodeBaseThrowIOException() throws IOException { ChatChaincodeWithPeer chatChaincodeWithPeer = new ChatChaincodeWithPeer(mockChaincodeBase); assertNotNull(chatChaincodeWithPeer); - assertNull(chatChaincodeWithPeer.connect(new StreamObserver() { + assertNull(chatChaincodeWithPeer.connect(new StreamObserver<>() { @Override public void onNext(final ChaincodeMessage value) {} diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/NettyGrpcServerTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/NettyGrpcServerTest.java index 87e8474e..05bfb3ff 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/NettyGrpcServerTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/NettyGrpcServerTest.java @@ -21,7 +21,7 @@ import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; @ExtendWith(SystemStubsExtension.class) -class NettyGrpcServerTest { +final class NettyGrpcServerTest { @SystemStub private final EnvironmentVariables environmentVariables = new EnvironmentVariables(); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ext/sbe/StateBasedEndorsementTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ext/sbe/StateBasedEndorsementTest.java index 3983ce57..3ab30011 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ext/sbe/StateBasedEndorsementTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ext/sbe/StateBasedEndorsementTest.java @@ -10,9 +10,9 @@ import org.junit.jupiter.api.Test; -public class StateBasedEndorsementTest { +final class StateBasedEndorsementTest { @Test - public void testRoleType() { + void testRoleType() { assertThat(StateBasedEndorsement.RoleType.forVal("MEMBER")) .isEqualTo(StateBasedEndorsement.RoleType.RoleTypeMember); assertThat(StateBasedEndorsement.RoleType.forVal("PEER")) diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementFactoryTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementFactoryTest.java index 750e24f0..c980b5a2 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementFactoryTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementFactoryTest.java @@ -11,15 +11,15 @@ import org.junit.jupiter.api.Test; -public class StateBasedEndorsementFactoryTest { +final class StateBasedEndorsementFactoryTest { @Test - public void getInstance() { + void getInstance() { assertNotNull(StateBasedEndorsementFactory.getInstance()); assertInstanceOf(StateBasedEndorsementFactory.class, StateBasedEndorsementFactory.getInstance()); } @Test - public void newStateBasedEndorsement() { + void newStateBasedEndorsement() { assertNotNull(StateBasedEndorsementFactory.getInstance().newStateBasedEndorsement(new byte[] {})); assertThatThrownBy(() -> StateBasedEndorsementFactory.getInstance().newStateBasedEndorsement(new byte[] {0})) .isInstanceOf(IllegalArgumentException.class); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementImplTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementImplTest.java index 195a672e..fe6482fb 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementImplTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ext/sbe/impl/StateBasedEndorsementImplTest.java @@ -20,10 +20,10 @@ import org.hyperledger.fabric.shim.ext.sbe.StateBasedEndorsement.RoleType; import org.junit.jupiter.api.Test; -public class StateBasedEndorsementImplTest { +final class StateBasedEndorsementImplTest { @Test - public void addOrgs() { + void addOrgs() { // add an org final StateBasedEndorsement ep = StateBasedEndorsementFactory.getInstance().newStateBasedEndorsement(null); @@ -39,7 +39,7 @@ public void addOrgs() { } @Test - public void delOrgs() { + void delOrgs() { final byte[] initEPBytes = StateBasedEndorsementUtils.signedByFabricEntity("Org1", MSPRoleType.PEER) .toByteString() @@ -66,7 +66,7 @@ public void delOrgs() { } @Test - public void listOrgs() { + void listOrgs() { final byte[] initEPBytes = StateBasedEndorsementUtils.signedByFabricEntity("Org1", MSPRoleType.PEER) .toByteString() .toByteArray(); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/fvt/ChaincodeFVTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/fvt/ChaincodeFVTest.java index 490be49b..0148486a 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/fvt/ChaincodeFVTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/fvt/ChaincodeFVTest.java @@ -61,7 +61,7 @@ import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; @ExtendWith(SystemStubsExtension.class) -public final class ChaincodeFVTest { +final class ChaincodeFVTest { @SystemStub private final EnvironmentVariables environmentVariables = new EnvironmentVariables(); @@ -69,7 +69,7 @@ public final class ChaincodeFVTest { private ChaincodeMockPeer server; @AfterEach - public void afterTest() throws Exception { + void afterTest() throws Exception { if (server != null) { server.stop(); server = null; @@ -77,7 +77,7 @@ public void afterTest() throws Exception { } @Test - public void testRegister() throws Exception { + void testRegister() throws Exception { final ChaincodeBase cb = new EmptyChaincode(); final List scenario = new ArrayList<>(); @@ -94,7 +94,7 @@ public void testRegister() throws Exception { } @Test - public void testRegisterAndEmptyInit() throws Exception { + void testRegisterAndEmptyInit() throws Exception { final ChaincodeBase cb = new ChaincodeBase() { @Override public Response init(final ChaincodeStub stub) { @@ -107,7 +107,7 @@ public Response invoke(final ChaincodeStub stub) { } }; - final ByteString payload = org.hyperledger.fabric.protos.peer.ChaincodeInput.newBuilder() + final ByteString payload = ChaincodeInput.newBuilder() .addArgs(ByteString.copyFromUtf8("")) .build() .toByteString(); @@ -130,7 +130,7 @@ public Response invoke(final ChaincodeStub stub) { } @Test - public void testInitAndInvoke() throws Exception { + void testInitAndInvoke() throws Exception { final ChaincodeBase cb = new ChaincodeBase() { @Override public Response init(final ChaincodeStub stub) { @@ -204,7 +204,7 @@ public Response invoke(final ChaincodeStub stub) { } @Test - public void testStateValidationParameter() throws Exception { + void testStateValidationParameter() throws Exception { final ChaincodeBase cb = new ChaincodeBase() { @Override public Response init(final ChaincodeStub stub) { @@ -272,7 +272,7 @@ public Response invoke(final ChaincodeStub stub) { } @Test - public void testInvokeRangeQ() throws Exception { + void testInvokeRangeQ() throws Exception { final ChaincodeBase cb = new ChaincodeBase() { @Override public Response init(final ChaincodeStub stub) { @@ -351,7 +351,7 @@ public Response invoke(final ChaincodeStub stub) { } @Test - public void testGetQueryResult() throws Exception { + void testGetQueryResult() throws Exception { final ChaincodeBase cb = new ChaincodeBase() { @Override public Response init(final ChaincodeStub stub) { @@ -426,7 +426,7 @@ public Response invoke(final ChaincodeStub stub) { } @Test - public void testGetHistoryForKey() throws Exception { + void testGetHistoryForKey() throws Exception { final ChaincodeBase cb = new ChaincodeBase() { @Override public Response init(final ChaincodeStub stub) { @@ -490,7 +490,7 @@ public Response invoke(final ChaincodeStub stub) { } @Test - public void testInvokeChaincode() throws Exception { + void testInvokeChaincode() throws Exception { final ChaincodeBase cb = new ChaincodeBase() { @Override public Response init(final ChaincodeStub stub) { @@ -540,7 +540,7 @@ public Response invoke(final ChaincodeStub stub) { } @Test - public void testErrorInitInvoke() throws Exception { + void testErrorInitInvoke() throws Exception { final ChaincodeBase cb = new ChaincodeBase() { @Override public Response init(final ChaincodeStub stub) { @@ -553,7 +553,7 @@ public Response invoke(final ChaincodeStub stub) { } }; - final ByteString payload = org.hyperledger.fabric.protos.peer.ChaincodeInput.newBuilder() + final ByteString payload = ChaincodeInput.newBuilder() .addArgs(ByteString.copyFromUtf8("")) .build() .toByteString(); @@ -575,9 +575,9 @@ public Response invoke(final ChaincodeStub stub) { assertThat(server.getLastMessageSend().getType(), is(INIT)); assertThat(server.getLastMessageRcvd().getType(), is(COMPLETED)); - String resp1 = (Response.parseFrom(server.getLastMessageRcvd().getPayload()) + String resp1 = Response.parseFrom(server.getLastMessageRcvd().getPayload()) .getPayload() - .toStringUtf8()); + .toStringUtf8(); assertThat(resp1, is("Wrong response1")); final ByteString invokePayload = ChaincodeInput.newBuilder().build().toByteString(); @@ -596,13 +596,13 @@ public Response invoke(final ChaincodeStub stub) { } @Test - public void testStreamShutdown() throws Exception { + void testStreamShutdown() throws Exception { final ChaincodeBase cb = new ChaincodeBase() { @Override public Response init(final ChaincodeStub stub) { try { Thread.sleep(10); - } catch (final InterruptedException e) { + } catch (final InterruptedException ignored) { } return ResponseUtils.newSuccessResponse(); } @@ -613,7 +613,7 @@ public Response invoke(final ChaincodeStub stub) { } }; - final ByteString payload = org.hyperledger.fabric.protos.peer.ChaincodeInput.newBuilder() + final ByteString payload = ChaincodeInput.newBuilder() .addArgs(ByteString.copyFromUtf8("")) .build() .toByteString(); @@ -634,7 +634,7 @@ public Response invoke(final ChaincodeStub stub) { } @Test - public void testChaincodeLogLevel() throws Exception { + void testChaincodeLogLevel() throws Exception { final ChaincodeBase cb = new EmptyChaincode(); final List scenario = new ArrayList<>(); @@ -652,7 +652,7 @@ public void testChaincodeLogLevel() throws Exception { "Wrong debug level for " + cb.getClass().getPackage().getName()); } - public void setLogLevel(final String logLevel) { + private void setLogLevel(final String logLevel) { environmentVariables.set("CORE_CHAINCODE_LOGGING_SHIM", logLevel); environmentVariables.set("CORE_CHAINCODE_LOGGING_LEVEL", logLevel); } diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/ChaincodeMessageFactoryTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/ChaincodeMessageFactoryTest.java index 2a7c561e..ca152f9a 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/ChaincodeMessageFactoryTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/ChaincodeMessageFactoryTest.java @@ -15,7 +15,7 @@ import org.hyperledger.fabric.shim.ResponseUtils; import org.junit.jupiter.api.Test; -class ChaincodeMessageFactoryTest { +final class ChaincodeMessageFactoryTest { private final String txId = "txid"; private final String key = "key"; diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/ChaincodeSupportClientTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/ChaincodeSupportClientTest.java index 2bdf9c87..369f22cc 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/ChaincodeSupportClientTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/ChaincodeSupportClientTest.java @@ -24,7 +24,7 @@ import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; @ExtendWith(SystemStubsExtension.class) -class ChaincodeSupportClientTest { +final class ChaincodeSupportClientTest { @SystemStub private final EnvironmentVariables environmentVariables = new EnvironmentVariables(); @@ -74,7 +74,7 @@ void testStartInvocationTaskManagerNullAndRequestObserver() throws IOException { assertThatThrownBy( () -> { - chaincodeSupportClient.start(null, new StreamObserver() { + chaincodeSupportClient.start(null, new StreamObserver<>() { @Override public void onNext(final ChaincodeMessage value) {} diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InnvocationTaskManagerTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InnvocationTaskManagerTest.java index fd51e718..a7f1348a 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InnvocationTaskManagerTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InnvocationTaskManagerTest.java @@ -28,7 +28,7 @@ import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension; @ExtendWith(SystemStubsExtension.class) -class InnvocationTaskManagerTest { +final class InnvocationTaskManagerTest { @SystemStub private final EnvironmentVariables environmentVariables = new EnvironmentVariables(); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InvocationStubImplTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InvocationStubImplTest.java index b053a869..63aa35f0 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InvocationStubImplTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InvocationStubImplTest.java @@ -25,21 +25,21 @@ import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; -public class InvocationStubImplTest { +final class InvocationStubImplTest { private final String channelId = "mychannel"; private final String txId = "0xCAFEBABE"; private final String simpleKeyStartNamespace = new String(Character.toChars(0x000001)); @Nested - class GetStateByRangeTests { + final class GetStateByRangeTests { private InvocationStubImpl stubImpl; private ArgumentCaptor chaincodeMessageCaptor; private ChaincodeInvocationTask mockHandler; @BeforeEach - public void beforeEach() throws Exception { + void beforeEach() throws Exception { final ChaincodeMessage mockMessage = ChaincodeMessageFactory.newGetStateEventMessage(channelId, txId, "", "key"); mockHandler = mock(ChaincodeInvocationTask.class); @@ -52,7 +52,7 @@ public void beforeEach() throws Exception { } @Test - public void regular() throws InvalidProtocolBufferException { + void regular() throws InvalidProtocolBufferException { final QueryResultsIterator qri = stubImpl.getStateByRange("Aardvark", "Zebra"); verify(mockHandler).invoke(chaincodeMessageCaptor.capture()); @@ -68,7 +68,7 @@ public void regular() throws InvalidProtocolBufferException { } @Test - public void nullvalues() throws InvalidProtocolBufferException { + void nullvalues() throws InvalidProtocolBufferException { final QueryResultsIterator qri = stubImpl.getStateByRange(null, null); verify(mockHandler).invoke(chaincodeMessageCaptor.capture()); @@ -85,7 +85,7 @@ public void nullvalues() throws InvalidProtocolBufferException { } @Test - public void unbounded() throws InvalidProtocolBufferException { + void unbounded() throws InvalidProtocolBufferException { final QueryResultsIterator qri = stubImpl.getStateByRange("", ""); verify(mockHandler).invoke(chaincodeMessageCaptor.capture()); @@ -102,7 +102,7 @@ public void unbounded() throws InvalidProtocolBufferException { } @Test - public void simplekeys() { + void simplekeys() { assertThatThrownBy(() -> { final QueryResultsIterator qri = stubImpl.getStateByRange(new String(Character.toChars(Character.MIN_CODE_POINT)), ""); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InvocationTaskManagerTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InvocationTaskManagerTest.java index 7fd6b760..a7299c53 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InvocationTaskManagerTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InvocationTaskManagerTest.java @@ -23,14 +23,14 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; -public final class InvocationTaskManagerTest { +final class InvocationTaskManagerTest { private InvocationTaskManager itm; private ChaincodeBase chaincode; private Logger perfLogger; @BeforeEach - public void setup() { + void setup() { Metrics.initialize(new Properties()); Traces.initialize(new Properties()); @@ -45,19 +45,19 @@ public void setup() { } @AfterEach - public void teardown() { + void teardown() { itm.shutdown(); perfLogger.setLevel(Level.INFO); } @Test - public void register() throws UnsupportedEncodingException { + void register() throws UnsupportedEncodingException { itm.register(); } @Test - public void onMessageTestTx() throws UnsupportedEncodingException { + void onMessageTestTx() throws UnsupportedEncodingException { final ChaincodeMessage msg = ChaincodeMessageFactory.newEventMessage( ChaincodeMessage.Type.TRANSACTION, "mychannel", "txid", ByteString.copyFrom("Hello", "UTF-8")); @@ -68,7 +68,7 @@ public void onMessageTestTx() throws UnsupportedEncodingException { } @Test - public void onWrongCreatedState() throws UnsupportedEncodingException { + void onWrongCreatedState() throws UnsupportedEncodingException { perfLogger.setLevel(Level.ALL); final ChaincodeMessage msg = ChaincodeMessageFactory.newEventMessage( @@ -80,7 +80,7 @@ public void onWrongCreatedState() throws UnsupportedEncodingException { } @Test - public void onWrongEstablishedState() throws UnsupportedEncodingException { + void onWrongEstablishedState() throws UnsupportedEncodingException { final ChaincodeMessage msg = ChaincodeMessageFactory.newEventMessage( ChaincodeMessage.Type.TRANSACTION, "mychannel", "txid", ByteString.copyFrom("Hello", "UTF-8")); @@ -93,7 +93,7 @@ public void onWrongEstablishedState() throws UnsupportedEncodingException { } @Test - public void onErrorResponse() throws UnsupportedEncodingException { + void onErrorResponse() throws UnsupportedEncodingException { final ChaincodeMessage msg = ChaincodeMessageFactory.newEventMessage( ChaincodeMessage.Type.ERROR, "mychannel", "txid", ByteString.copyFrom("Hello", "UTF-8")); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/KeyModificationImplTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/KeyModificationImplTest.java index 2dc72dc9..28527224 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/KeyModificationImplTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/KeyModificationImplTest.java @@ -7,25 +7,18 @@ package org.hyperledger.fabric.shim.impl; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasProperty; -import static org.hamcrest.Matchers.is; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import com.google.protobuf.ByteString; import com.google.protobuf.Timestamp; -import java.time.Instant; import java.util.stream.Stream; import org.hyperledger.fabric.shim.ledger.KeyModification; import org.junit.jupiter.api.Test; -public class KeyModificationImplTest { +final class KeyModificationImplTest { @Test - public void testKeyModificationImpl() { + void testKeyModificationImpl() { new KeyModificationImpl(org.hyperledger.fabric.protos.ledger.queryresult.KeyModification.newBuilder() .setTxId("txid") .setValue(ByteString.copyFromUtf8("value")) @@ -35,72 +28,70 @@ public void testKeyModificationImpl() { } @Test - public void testGetTxId() { + void testGetTxId() { final KeyModification km = new KeyModificationImpl(org.hyperledger.fabric.protos.ledger.queryresult.KeyModification.newBuilder() .setTxId("txid") .build()); - assertThat(km.getTxId(), is(equalTo("txid"))); + assertThat(km.getTxId()).isEqualTo("txid"); } @Test - public void testGetValue() { + void testGetValue() { final KeyModification km = new KeyModificationImpl(org.hyperledger.fabric.protos.ledger.queryresult.KeyModification.newBuilder() .setValue(ByteString.copyFromUtf8("value")) .build()); - assertThat(km.getValue(), is(equalTo("value".getBytes(UTF_8)))); + assertThat(km.getValue()).isEqualTo("value".getBytes(UTF_8)); } @Test - public void testGetStringValue() { + void testGetStringValue() { final KeyModification km = new KeyModificationImpl(org.hyperledger.fabric.protos.ledger.queryresult.KeyModification.newBuilder() .setValue(ByteString.copyFromUtf8("value")) .build()); - assertThat(km.getStringValue(), is(equalTo("value"))); + assertThat(km.getStringValue()).isEqualTo("value"); } @Test - public void testGetTimestamp() { + void testGetTimestamp() { final KeyModification km = new KeyModificationImpl(org.hyperledger.fabric.protos.ledger.queryresult.KeyModification.newBuilder() .setTimestamp( Timestamp.newBuilder().setSeconds(1234567890L).setNanos(123456789)) .build()); - assertThat(km.getTimestamp(), hasProperty("epochSecond", equalTo(1234567890L))); - assertThat(km.getTimestamp(), hasProperty("nano", equalTo(123456789))); + assertThat(km.getTimestamp().getEpochSecond()).isEqualTo(1234567890L); + assertThat(km.getTimestamp().getNano()).isEqualTo(123456789); } @Test - public void testIsDeleted() { + void testIsDeleted() { Stream.of(true, false).forEach(b -> { final KeyModification km = new KeyModificationImpl( org.hyperledger.fabric.protos.ledger.queryresult.KeyModification.newBuilder() .setIsDelete(b) .build()); - assertThat(km.isDeleted(), is(b)); + assertThat(km.isDeleted()).isEqualTo(b); }); } @Test - public void testHashCode() { - final KeyModification km = + void testHashCode() { + final KeyModification km1 = + new KeyModificationImpl(org.hyperledger.fabric.protos.ledger.queryresult.KeyModification.newBuilder() + .setIsDelete(false) + .build()); + final KeyModification km2 = new KeyModificationImpl(org.hyperledger.fabric.protos.ledger.queryresult.KeyModification.newBuilder() .setIsDelete(false) .build()); - int expectedHashCode = 31; - expectedHashCode = expectedHashCode + 1237; - expectedHashCode = expectedHashCode * 31 + Instant.EPOCH.hashCode(); - expectedHashCode = expectedHashCode * 31 + "".hashCode(); - expectedHashCode = expectedHashCode * 31 + ByteString.copyFromUtf8("").hashCode(); - - assertEquals(expectedHashCode, km.hashCode(), "Wrong hash code"); + assertThat(km1.hashCode()).isEqualTo(km2.hashCode()); } @Test - public void testEquals() { + void testEquals() { final KeyModification km1 = new KeyModificationImpl(org.hyperledger.fabric.protos.ledger.queryresult.KeyModification.newBuilder() .setIsDelete(false) @@ -115,7 +106,7 @@ public void testEquals() { .setIsDelete(false) .build()); - assertFalse(km1.equals(km2)); - assertTrue(km1.equals(km3)); + assertThat(km1).isNotEqualTo(km2); + assertThat(km1).isEqualTo(km3); } } diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/KeyValueImplTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/KeyValueImplTest.java index 3b8bbf43..219aaf46 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/KeyValueImplTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/KeyValueImplTest.java @@ -7,21 +7,16 @@ package org.hyperledger.fabric.shim.impl; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import com.google.protobuf.ByteString; import org.hyperledger.fabric.protos.ledger.queryresult.KV; import org.junit.jupiter.api.Test; -public class KeyValueImplTest { +final class KeyValueImplTest { @Test - public void testKeyValueImpl() { + void testKeyValueImpl() { new KeyValueImpl(KV.newBuilder() .setKey("key") .setValue(ByteString.copyFromUtf8("value")) @@ -29,45 +24,42 @@ public void testKeyValueImpl() { } @Test - public void testGetKey() { + void testGetKey() { final KeyValueImpl kv = new KeyValueImpl(KV.newBuilder() .setKey("key") .setValue(ByteString.copyFromUtf8("value")) .build()); - assertThat(kv.getKey(), is(equalTo("key"))); + assertThat(kv.getKey()).isEqualTo("key"); } @Test - public void testGetValue() { + void testGetValue() { final KeyValueImpl kv = new KeyValueImpl(KV.newBuilder() .setKey("key") .setValue(ByteString.copyFromUtf8("value")) .build()); - assertThat(kv.getValue(), is(equalTo("value".getBytes(UTF_8)))); + assertThat(kv.getValue()).isEqualTo("value".getBytes(UTF_8)); } @Test - public void testGetStringValue() { + void testGetStringValue() { final KeyValueImpl kv = new KeyValueImpl(KV.newBuilder() .setKey("key") .setValue(ByteString.copyFromUtf8("value")) .build()); - assertThat(kv.getStringValue(), is(equalTo("value"))); + assertThat(kv.getStringValue()).isEqualTo("value"); } @Test - public void testHashCode() { - final KeyValueImpl kv = new KeyValueImpl(KV.newBuilder().build()); + void testHashCode() { + final KeyValueImpl kv1 = new KeyValueImpl(KV.newBuilder().build()); + final KeyValueImpl kv2 = new KeyValueImpl(KV.newBuilder().build()); - int expectedHashCode = 31; - expectedHashCode = expectedHashCode + "".hashCode(); - expectedHashCode = expectedHashCode * 31 + ByteString.copyFromUtf8("").hashCode(); - - assertEquals(expectedHashCode, kv.hashCode(), "Wrong hashcode"); + assertThat(kv1.hashCode()).isEqualTo(kv2.hashCode()); } @Test - public void testEquals() { + void testEquals() { final KeyValueImpl kv1 = new KeyValueImpl(KV.newBuilder() .setKey("a") .setValue(ByteString.copyFromUtf8("valueA")) @@ -88,8 +80,8 @@ public void testEquals() { .setValue(ByteString.copyFromUtf8("valueA")) .build()); - assertFalse(kv1.equals(kv2)); - assertFalse(kv1.equals(kv3)); - assertTrue(kv1.equals(kv4)); + assertThat(kv1).isNotEqualTo(kv2); + assertThat(kv1).isNotEqualTo(kv3); + assertThat(kv1).isEqualTo(kv4); } } diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorWithMetadataImplTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorWithMetadataImplTest.java index 3d1b299f..73cc0079 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorWithMetadataImplTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/QueryResultsIteratorWithMetadataImplTest.java @@ -6,9 +6,8 @@ package org.hyperledger.fabric.shim.impl; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.junit.jupiter.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import com.google.protobuf.ByteString; import java.util.function.Function; @@ -17,33 +16,24 @@ import org.hyperledger.fabric.protos.peer.QueryResultBytes; import org.junit.jupiter.api.Test; -public class QueryResultsIteratorWithMetadataImplTest { +final class QueryResultsIteratorWithMetadataImplTest { + private static final Function QUERY_RESULT_BYTES_TO_KV = queryResultBytes -> 0; @Test - public void getMetadata() { + void getMetadata() { final QueryResultsIteratorWithMetadataImpl testIter = new QueryResultsIteratorWithMetadataImpl<>( - null, "", "", prepareQueryResponse().toByteString(), queryResultBytesToKv); - assertThat(testIter.getMetadata().getBookmark(), is("asdf")); - assertThat(testIter.getMetadata().getFetchedRecordsCount(), is(2)); + null, "", "", prepareQueryResponse().toByteString(), QUERY_RESULT_BYTES_TO_KV); + assertThat(testIter.getMetadata().getBookmark()).isEqualTo("asdf"); + assertThat(testIter.getMetadata().getFetchedRecordsCount()).isEqualTo(2); } @Test - public void getInvalidMetadata() { - try { - new QueryResultsIteratorWithMetadataImpl<>( - null, "", "", prepareQueryResponseWrongMeta().toByteString(), queryResultBytesToKv); - fail(); - } catch (final RuntimeException e) { - } + void getInvalidMetadata() { + assertThatThrownBy(() -> new QueryResultsIteratorWithMetadataImpl<>( + null, "", "", prepareQueryResponseWrongMeta().toByteString(), QUERY_RESULT_BYTES_TO_KV)) + .isInstanceOf(RuntimeException.class); } - private final Function queryResultBytesToKv = new Function() { - @Override - public Integer apply(final QueryResultBytes queryResultBytes) { - return 0; - } - }; - private QueryResponse prepareQueryResponse() { final QueryResponseMetadata qrm = QueryResponseMetadata.newBuilder() .setBookmark("asdf") diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ledger/CompositeKeyTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ledger/CompositeKeyTest.java index d2bac2f8..21293a01 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ledger/CompositeKeyTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/ledger/CompositeKeyTest.java @@ -16,20 +16,20 @@ import java.util.Arrays; import org.junit.jupiter.api.Test; -public class CompositeKeyTest { +final class CompositeKeyTest { @Test - public void testValidateSimpleKeys() { + void testValidateSimpleKeys() { CompositeKey.validateSimpleKeys("abc", "def", "ghi"); } @Test - public void testValidateSimpleKeysException() { + void testValidateSimpleKeysException() { assertThatThrownBy(() -> CompositeKey.validateSimpleKeys("\u0000abc")) .isInstanceOf(CompositeKeyFormatException.class); } @Test - public void testCompositeKeyStringStringArray() { + void testCompositeKeyStringStringArray() { final CompositeKey key = new CompositeKey("abc", "def", "ghi", "jkl", "mno"); assertThat(key.getObjectType(), is(equalTo("abc"))); assertThat(key.getAttributes(), hasSize(4)); @@ -37,7 +37,7 @@ public void testCompositeKeyStringStringArray() { } @Test - public void testCompositeKeyStringListOfString() { + void testCompositeKeyStringListOfString() { final CompositeKey key = new CompositeKey("abc", Arrays.asList("def", "ghi", "jkl", "mno")); assertThat(key.getObjectType(), is(equalTo("abc"))); assertThat(key.getAttributes(), hasSize(4)); @@ -45,7 +45,7 @@ public void testCompositeKeyStringListOfString() { } @Test - public void testEmptyAttributes() { + void testEmptyAttributes() { final CompositeKey key = new CompositeKey("abc"); assertThat(key.getObjectType(), is(equalTo("abc"))); assertThat(key.getAttributes(), hasSize(0)); @@ -53,37 +53,37 @@ public void testEmptyAttributes() { } @Test - public void testCompositeKeyWithInvalidObjectTypeDelimiter() { + void testCompositeKeyWithInvalidObjectTypeDelimiter() { assertThatThrownBy(() -> new CompositeKey("ab\u0000c", Arrays.asList("def", "ghi", "jkl", "mno"))) .isInstanceOf(CompositeKeyFormatException.class); } @Test - public void testCompositeKeyWithInvalidAttributeDelimiter() { + void testCompositeKeyWithInvalidAttributeDelimiter() { assertThatThrownBy(() -> new CompositeKey("abc", Arrays.asList("def", "ghi", "j\u0000kl", "mno"))) .isInstanceOf(CompositeKeyFormatException.class); } @Test - public void testCompositeKeyWithInvalidObjectTypeMaxCodePoint() { + void testCompositeKeyWithInvalidObjectTypeMaxCodePoint() { assertThatThrownBy(() -> new CompositeKey("ab\udbff\udfffc", Arrays.asList("def", "ghi", "jkl", "mno"))) .isInstanceOf(CompositeKeyFormatException.class); } @Test - public void testCompositeKeyWithInvalidAttributeMaxCodePoint() { + void testCompositeKeyWithInvalidAttributeMaxCodePoint() { assertThatThrownBy(() -> new CompositeKey("abc", Arrays.asList("def", "ghi", "jk\udbff\udfffl", "mno"))) .isInstanceOf(CompositeKeyFormatException.class); } @Test - public void testGetObjectType() { + void testGetObjectType() { final CompositeKey key = new CompositeKey("abc", Arrays.asList("def", "ghi", "jkl", "mno")); assertThat(key.getObjectType(), is(equalTo("abc"))); } @Test - public void testGetAttributes() { + void testGetAttributes() { final CompositeKey key = new CompositeKey("abc", Arrays.asList("def", "ghi", "jkl", "mno")); assertThat(key.getObjectType(), is(equalTo("abc"))); assertThat(key.getAttributes(), hasSize(4)); @@ -91,13 +91,13 @@ public void testGetAttributes() { } @Test - public void testToString() { + void testToString() { final CompositeKey key = new CompositeKey("abc", Arrays.asList("def", "ghi", "jkl", "mno")); assertThat(key.toString(), is(equalTo("\u0000abc\u0000def\u0000ghi\u0000jkl\u0000mno\u0000"))); } @Test - public void testParseCompositeKey() { + void testParseCompositeKey() { final CompositeKey key = CompositeKey.parseCompositeKey("\u0000abc\u0000def\u0000ghi\u0000jkl\u0000mno\u0000"); assertThat(key.getObjectType(), is(equalTo("abc"))); assertThat(key.getAttributes(), hasSize(4)); @@ -106,14 +106,14 @@ public void testParseCompositeKey() { } @Test - public void testParseCompositeKeyInvalidObjectType() { + void testParseCompositeKeyInvalidObjectType() { assertThatThrownBy(() -> CompositeKey.parseCompositeKey("ab\udbff\udfffc\u0000def\u0000ghi\u0000jkl\u0000mno\u0000")) .isInstanceOf(CompositeKeyFormatException.class); } @Test - public void testParseCompositeKeyInvalidAttribute() { + void testParseCompositeKeyInvalidAttribute() { assertThatThrownBy(() -> CompositeKey.parseCompositeKey("abc\u0000def\u0000ghi\u0000jk\udbff\udfffl\u0000mno\u0000")) .isInstanceOf(CompositeKeyFormatException.class); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/mock/peer/ChaincodeMockPeer.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/mock/peer/ChaincodeMockPeer.java index 710b9eb0..cf89c888 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/mock/peer/ChaincodeMockPeer.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/mock/peer/ChaincodeMockPeer.java @@ -45,13 +45,14 @@ public ChaincodeMockPeer(final List scenario, final int port) { } /** Start serving requests. */ + @SuppressWarnings("PMD.SystemPrintln") public void start() throws IOException { server.start(); - LOGGER.info("Server started, listening on " + port); + LOGGER.info(() -> "Server started, listening on " + port); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { - // Use stderr here since the logger may has been reset by its JVM shutdown hook. + // Use stderr here since the logger may have been reset by its JVM shutdown hook. System.err.println("*** shutting down gRPC server since JVM is shutting down"); ChaincodeMockPeer.this.stop(); System.err.println("*** server shut down"); @@ -65,7 +66,7 @@ public void stop() { server.shutdownNow(); try { server.awaitTermination(); - } catch (final InterruptedException e) { + } catch (final InterruptedException ignored) { } } } @@ -78,7 +79,7 @@ public void stop() { public void send(final ChaincodeMessage msg) { this.service.lastMessageSend = msg; - LOGGER.info("Mock peer => Sending message: " + msg); + LOGGER.info(() -> "Mock peer => Sending message: " + msg); this.service.send(msg); } @@ -96,7 +97,7 @@ public ChaincodeMessage getLastMessageRcvd() { return this.service.lastMessageRcvd; } - public ArrayList getAllReceivedMessages() { + public List getAllReceivedMessages() { return this.service.allMessages; } @@ -123,7 +124,7 @@ private static class ChaincodeMockPeerService extends ChaincodeSupportGrpc.Chain private int lastExecutedStepNumber; private ChaincodeMessage lastMessageRcvd; private ChaincodeMessage lastMessageSend; - private final ArrayList allMessages = new ArrayList<>(); + private final List allMessages = new ArrayList<>(); private StreamObserver observer; // create a lock, with fair property @@ -150,7 +151,7 @@ public void send(final ChaincodeMessage msg) { @Override public StreamObserver register(final StreamObserver responseObserver) { observer = responseObserver; - return new StreamObserver() { + return new StreamObserver<>() { /** * Handling incoming messages @@ -158,9 +159,10 @@ public StreamObserver register(final StreamObserver Got message: " + chaincodeMessage); + LOGGER.info(() -> "Mock peer => Got message: " + chaincodeMessage); ChaincodeMockPeerService.this.lastMessageRcvd = chaincodeMessage; ChaincodeMockPeerService.this.allMessages.add(chaincodeMessage); if (chaincodeMessage.getType().equals(PUT_STATE)) { @@ -178,11 +180,11 @@ public void onNext(final ChaincodeMessage chaincodeMessage) { final List nextSteps = step.next(); for (final ChaincodeMessage m : nextSteps) { ChaincodeMockPeerService.this.lastMessageSend = m; - LOGGER.info("Mock peer => Sending response message: " + m); + LOGGER.info(() -> "Mock peer => Sending response message: " + m); ChaincodeMockPeerService.this.send(m); } } else { - LOGGER.warning("Non expected message rcvd in step " + LOGGER.warning(() -> "Non expected message rcvd in step " + step.getClass().getSimpleName()); } ChaincodeMockPeerService.this.lastExecutedStepNumber++; @@ -194,7 +196,7 @@ public void onNext(final ChaincodeMessage chaincodeMessage) { @Override public void onError(final Throwable throwable) { - System.out.println(throwable); + throwable.printStackTrace(); } @Override @@ -203,6 +205,7 @@ public void onCompleted() {} } } + @SuppressWarnings("PMD.SystemPrintln") public static void checkScenarioStepEnded( final ChaincodeMockPeer s, final int step, final int timeout, final TimeUnit units) throws Exception { try { @@ -214,7 +217,7 @@ public static void checkScenarioStepEnded( } try { Thread.sleep(500); - } catch (final InterruptedException e) { + } catch (final InterruptedException ignored) { } } }), diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/mock/peer/GetHistoryForKeyStep.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/mock/peer/GetHistoryForKeyStep.java index 96e2fcc0..c307d6ae 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/mock/peer/GetHistoryForKeyStep.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/mock/peer/GetHistoryForKeyStep.java @@ -28,7 +28,7 @@ public final class GetHistoryForKeyStep implements ScenarioStep { * @param vals list of keys to generate ("key" => "key Value") pairs */ public GetHistoryForKeyStep(final boolean hasNext, final String... vals) { - this.values = vals; + this.values = Arrays.copyOf(vals, vals.length); this.hasNext = hasNext; } diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/mock/peer/QueryResultStep.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/mock/peer/QueryResultStep.java index 26c52724..69712719 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/mock/peer/QueryResultStep.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/mock/peer/QueryResultStep.java @@ -29,7 +29,7 @@ public abstract class QueryResultStep implements ScenarioStep { * @param vals list of keys to generate ("key" => "key Value") pairs */ QueryResultStep(final boolean hasNext, final String... vals) { - this.values = vals; + this.values = Arrays.copyOf(vals, vals.length); this.hasNext = hasNext; } diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/TracesTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/TracesTest.java index 51e0a1bf..f175484c 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/TracesTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/TracesTest.java @@ -19,9 +19,9 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -public class TracesTest { +final class TracesTest { - public static final class TestProvider implements TracesProvider { + private static final class TestProvider implements TracesProvider { public TestProvider() {} @@ -36,16 +36,16 @@ public Span createSpan(final ChaincodeStub stub) { @Nested @DisplayName("Traces initialize") - class Initialize { + final class Initialize { @Test - public void tracesDisabled() { + void tracesDisabled() { final TracesProvider provider = Traces.initialize(new Properties()); assertThat(provider).isExactlyInstanceOf(NullProvider.class); } @Test - public void tracesEnabledUnknownProvider() { + void tracesEnabledUnknownProvider() { final Properties props = new Properties(); props.put("CHAINCODE_TRACES_PROVIDER", "org.example.traces.provider"); props.put("CHAINCODE_TRACES_ENABLED", "true"); @@ -59,7 +59,7 @@ public void tracesEnabledUnknownProvider() { } @Test - public void tracesNoProvider() { + void tracesNoProvider() { final Properties props = new Properties(); props.put("CHAINCODE_TRACES_ENABLED", "true"); @@ -68,7 +68,7 @@ public void tracesNoProvider() { } @Test - public void tracesOpenTelemetryProvider() { + void tracesOpenTelemetryProvider() { final Properties props = new Properties(); props.put("CHAINCODE_TRACES_PROVIDER", "org.hyperledger.fabric.traces.impl.OpenTelemetryTracesProvider"); props.put("CHAINCODE_TRACES_ENABLED", "true"); @@ -78,7 +78,7 @@ public void tracesOpenTelemetryProvider() { } @Test - public void tracesValid() { + void tracesValid() { final Properties props = new Properties(); props.put("CHAINCODE_TRACES_PROVIDER", TracesTest.TestProvider.class.getName()); props.put("CHAINCODE_TRACES_ENABLED", "true"); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/DefaultProviderTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/DefaultProviderTest.java index e36075fd..2c07a3d4 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/DefaultProviderTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/DefaultProviderTest.java @@ -12,10 +12,10 @@ import org.hyperledger.fabric.shim.ChaincodeStub; import org.junit.jupiter.api.Test; -public class DefaultProviderTest { +final class DefaultProviderTest { @Test - public void testDefaultProvider() { + void testDefaultProvider() { DefaultTracesProvider provider = new DefaultTracesProvider(); ChaincodeStub stub = new ChaincodeStubNaiveImpl(); Span span = provider.createSpan(stub); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/OpenTelemetryPropertiesTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/OpenTelemetryPropertiesTest.java index 9c9278be..96d89537 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/OpenTelemetryPropertiesTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/OpenTelemetryPropertiesTest.java @@ -21,94 +21,94 @@ import java.util.Map; import org.junit.jupiter.api.Test; -public class OpenTelemetryPropertiesTest { +final class OpenTelemetryPropertiesTest { @Test - public void testOverrideValue() { + void testOverrideValue() { OpenTelemetryProperties props = new OpenTelemetryProperties( Collections.singletonMap("foo", "bar"), Collections.singletonMap("foo", "foobar")); assertThat(props.getString("foo")).isEqualTo("foobar"); } @Test - public void testCanGetDurationDays() { + void testCanGetDurationDays() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5d")); assertThat(props.getDuration("foo")).isEqualTo(Duration.of(5, DAYS)); } @Test - public void testCanGetDurationHours() { + void testCanGetDurationHours() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5h")); assertThat(props.getDuration("foo")).isEqualTo(Duration.of(5, HOURS)); } @Test - public void testCanGetDurationMinutes() { + void testCanGetDurationMinutes() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5m")); assertThat(props.getDuration("foo")).isEqualTo(Duration.of(5, MINUTES)); } @Test - public void testCanGetDurationSeconds() { + void testCanGetDurationSeconds() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5s")); assertThat(props.getDuration("foo")).isEqualTo(Duration.of(5, SECONDS)); } @Test - public void testCanGetDurationMilliSeconds() { + void testCanGetDurationMilliSeconds() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5ms")); assertThat(props.getDuration("foo")).isEqualTo(Duration.of(5, MILLIS)); } @Test - public void testCanGetDurationInvalid() { + void testCanGetDurationInvalid() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5foo")); assertThatThrownBy(() -> props.getDuration("foo")).isInstanceOf(ConfigurationException.class); } @Test - public void testGetDouble() { + void testGetDouble() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5.23")); assertThat(props.getDouble("foo")).isEqualTo(5.23d); assertThat(props.getDouble("bar")).isNull(); } @Test - public void testGetDoubleInvalid() { + void testGetDoubleInvalid() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "5foo")); assertThatThrownBy(() -> props.getDouble("foo")).isInstanceOf(ConfigurationException.class); } @Test - public void testGetLong() { + void testGetLong() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "500003")); assertThat(props.getLong("foo")).isEqualTo(500003L); assertThat(props.getLong("bar")).isNull(); } @Test - public void testGetInt() { + void testGetInt() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "500003")); assertThat(props.getInt("foo")).isEqualTo(500003); assertThat(props.getInt("bar")).isNull(); } @Test - public void testGetBoolean() { + void testGetBoolean() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "true")); assertThat(props.getBoolean("foo")).isTrue(); assertThat(props.getBoolean("bar")).isNull(); } @Test - public void testGetList() { + void testGetList() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "foo,bar,foobar")); assertThat(props.getList("foo")).isEqualTo(Arrays.asList("foo", "bar", "foobar")); assertThat(props.getList("bar")).isEqualTo(Collections.emptyList()); } @Test - public void testGetMap() { + void testGetMap() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "foo=bar,foobar=noes")); Map expected = new HashMap<>(); @@ -118,7 +118,7 @@ public void testGetMap() { } @Test - public void testGetMapInvalid() { + void testGetMapInvalid() { OpenTelemetryProperties props = new OpenTelemetryProperties(Collections.singletonMap("foo", "foo/bar,foobar/noes")); Map expected = new HashMap<>(); diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/OpenTelemetryTracesProviderTest.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/OpenTelemetryTracesProviderTest.java index 6aeda3e8..af8689ef 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/OpenTelemetryTracesProviderTest.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/OpenTelemetryTracesProviderTest.java @@ -37,7 +37,7 @@ import org.hyperledger.fabric.traces.Traces; import org.junit.jupiter.api.Test; -public final class OpenTelemetryTracesProviderTest { +final class OpenTelemetryTracesProviderTest { private final class ContextGetterChaincode extends ChaincodeBase { @@ -58,7 +58,7 @@ public Properties getChaincodeConfig() { } @Test - public void testProvider() { + void testProvider() { OpenTelemetryTracesProvider provider = new OpenTelemetryTracesProvider(); provider.initialize(new Properties()); ChaincodeStub stub = new ChaincodeStubNaiveImpl(); @@ -68,7 +68,7 @@ public void testProvider() { } @Test - public void testTracing() throws Exception { + void testTracing() throws Exception { Properties props = new Properties(); props.put("CHAINCODE_TRACES_ENABLED", "true"); @@ -82,7 +82,7 @@ public void testTracing() throws Exception { // set up a grpc server in process ServerCallHandler handler = (call, headers) -> { call.close(Status.OK, headers); - return new ServerCall.Listener() {}; + return new ServerCall.Listener<>() {}; }; ServerServiceDefinition.Builder builder = ServerServiceDefinition.builder(ChaincodeGrpc.getServiceDescriptor()) @@ -110,7 +110,7 @@ public void testTracing() throws Exception { CompletableFuture wait = new CompletableFuture<>(); StreamObserver requestObserver = chaincodeSupportClient .getStub() - .register(new StreamObserver() { + .register(new StreamObserver<>() { @Override public void onNext(final ChaincodeMessage chaincodeMessage) { // message off to the ITM... diff --git a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/TestSpanExporterProvider.java b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/TestSpanExporterProvider.java index 5baa2ff9..95891c67 100644 --- a/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/TestSpanExporterProvider.java +++ b/fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/traces/impl/TestSpanExporterProvider.java @@ -21,7 +21,7 @@ public final class TestSpanExporterProvider implements ConfigurableSpanExporterP @Override public CompletableResultCode export(final Collection spans) { - TestSpanExporterProvider.SPANS.addAll(spans); + SPANS.addAll(spans); return CompletableResultCode.ofSuccess(); } diff --git a/pmd-ruleset.xml b/pmd-ruleset.xml new file mode 100644 index 00000000..3c886141 --- /dev/null +++ b/pmd-ruleset.xml @@ -0,0 +1,54 @@ + + + Custom PMD ruleset + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +