From a23bd8c218d7044e1d79e240fdb74f00947a3108 Mon Sep 17 00:00:00 2001 From: "adheli.tavares" Date: Tue, 18 Jun 2024 15:12:56 +0100 Subject: Convert junit4 to junit5 - clean up any references to junit4 - clean up some sonar complaints - remove not used integration tests Issue-ID: POLICY-5041 Change-Id: I67e7a8f00df5b6c5ba514f4ea2ecd96bf942d4c7 Signed-off-by: adheli.tavares --- .../apex/auth/clieditor/ApexModelHandler.java | 41 +++++++++++----------- .../apex/auth/clieditor/CommandLineParser.java | 34 +++++++++--------- .../policy/apex/auth/clieditor/FileMacroTest.java | 8 +++-- .../policy/apex/auth/clieditor/LogicBlockTest.java | 2 +- 4 files changed, 45 insertions(+), 40 deletions(-) (limited to 'auth/cli-editor') diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexModelHandler.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexModelHandler.java index dfb3a3865..23820207f 100644 --- a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexModelHandler.java +++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/ApexModelHandler.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2021-2022 Nordix Foundation. + * Modifications Copyright (C) 2021-2022, 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,7 +53,7 @@ public class ApexModelHandler { /** * Create the Apex Model with the properties specified and load it from a file. * - * @param properties The properties of the Apex model + * @param properties The properties of the Apex model * @param modelFileName The name of the model file to edit */ public ApexModelHandler(final Properties properties, final String modelFileName) { @@ -72,13 +72,14 @@ public class ApexModelHandler { /** * Execute a command on the Apex model. * - * @param command The command to execute + * @param command The command to execute * @param argumentValues Arguments of the command - * @param writer A writer to which to write output + * @param writer A writer to which to write output * @return the result of the executed command */ public Result executeCommand(final CommandLineCommand command, - final SortedMap argumentValues, final PrintWriter writer) { + final SortedMap argumentValues, + final PrintWriter writer) { // Get the method final var apiMethod = getCommandMethod(command); @@ -88,27 +89,26 @@ public class ApexModelHandler { try { final var returnObject = apiMethod.invoke(apexModel, parameterArray); - if (returnObject instanceof ApexApiResult) { - final ApexApiResult result = (ApexApiResult) returnObject; + if (returnObject instanceof ApexApiResult result) { writer.println(result); return result.getResult(); } else { throw new CommandLineException(INVOCATION_OF_SPECIFIED_METHOD + command.getApiMethod() - + FAILED_FOR_COMMAND + command.getName() - + "\" the returned object is not an instance of ApexAPIResult"); + + FAILED_FOR_COMMAND + command.getName() + + "\" the returned object is not an instance of ApexAPIResult"); } } catch (IllegalAccessException | IllegalArgumentException e) { writer.println(INVOCATION_OF_SPECIFIED_METHOD + command.getApiMethod() + FAILED_FOR_COMMAND - + command.getName() + "\""); + + command.getName() + "\""); e.printStackTrace(writer); throw new CommandLineException(INVOCATION_OF_SPECIFIED_METHOD + command.getApiMethod() + FAILED_FOR_COMMAND - + command.getName() + "\"", e); + + command.getName() + "\"", e); } catch (final InvocationTargetException e) { writer.println(INVOCATION_OF_SPECIFIED_METHOD + command.getApiMethod() + FAILED_FOR_COMMAND - + command.getName() + "\""); + + command.getName() + "\""); e.getCause().printStackTrace(writer); throw new CommandLineException(INVOCATION_OF_SPECIFIED_METHOD + command.getApiMethod() + FAILED_FOR_COMMAND - + command.getName() + "\"", e); + + command.getName() + "\"", e); } } @@ -130,30 +130,31 @@ public class ApexModelHandler { } } throw new CommandLineException("specified method \"" + command.getApiMethod() - + "\" not found for command \"" + command.getName() + "\""); + + "\" not found for command \"" + command.getName() + "\""); } catch (final ClassNotFoundException e) { throw new CommandLineException("specified class \"" + command.getApiMethod() + "\" not found for command \"" - + command.getName() + "\"", e); + + command.getName() + "\"", e); } } /** * Get the arguments of the command as an ordered array of objects ready for the method. * - * @param command the command that invoked the method + * @param command the command that invoked the method * @param argumentValues the argument values for the method - * @param apiMethod the method itself + * @param apiMethod the method itself * @return the argument list */ private Object[] getParameterArray(final CommandLineCommand command, - final SortedMap argumentValues, final Method apiMethod) { + final SortedMap argumentValues, + final Method apiMethod) { final var parameterArray = new Object[argumentValues.size()]; var item = 0; try { for (final Class parametertype : apiMethod.getParameterTypes()) { final String parameterValue = argumentValues.get(command.getArgumentList().get(item).getArgumentName()) - .getValue(); + .getValue(); if (parametertype.equals(boolean.class)) { parameterArray[item] = Boolean.valueOf(parameterValue); @@ -164,7 +165,7 @@ public class ApexModelHandler { } } catch (final Exception e) { throw new CommandLineException("number of argument mismatch on method \"" + command.getApiMethod() - + "\" for command \"" + command.getName() + "\"", e); + + "\" for command \"" + command.getName() + "\"", e); } return parameterArray; diff --git a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CommandLineParser.java b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CommandLineParser.java index fcbf571ca..2910c041e 100644 --- a/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CommandLineParser.java +++ b/auth/cli-editor/src/main/java/org/onap/policy/apex/auth/clieditor/CommandLineParser.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020-2021 Nordix Foundation. + * Modifications Copyright (C) 2020-2021, 2024 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,15 +42,15 @@ public class CommandLineParser { * *

Examples entity create name=hello description="description of hello" help entity list * - * @param line The line to parse + * @param line The line to parse * @param logicBlock A block of logic code to be taken literally * @return the string array list */ public List parse(final String line, final String logicBlock) { return checkFormat( - mergeArguments(mergeEquals(splitOnEquals( - stripAndSplitWords(mergeQuotes(splitOnChar(stripComments(line), '\"')))))), - logicBlock); + mergeArguments(mergeEquals(splitOnEquals( + stripAndSplitWords(mergeQuotes(splitOnChar(stripComments(line), '\"')))))), + logicBlock); } /** @@ -90,13 +90,13 @@ public class CommandLineParser { /** * This method merges the next set of quotes. * - * @param wordsSplitOnQuotes the words split on quotes + * @param wordsSplitOnQuotes the words split on quotes * @param wordsWithQuotesMerged the merged words - * @param wordIndex the current word index + * @param wordIndex the current word index * @return the next word index */ private int mergeQuote(final ArrayList wordsSplitOnQuotes, final ArrayList wordsWithQuotesMerged, - int wordIndex) { + int wordIndex) { if ("\"".equals(wordsSplitOnQuotes.get(wordIndex))) { var quotedWord = new StringBuilder(wordsSplitOnQuotes.get(wordIndex++)); @@ -167,9 +167,9 @@ public class CommandLineParser { if ("=".equals(wordsSplitOnEquals.get(wordIndex))) { if (wordIndex < wordsSplitOnEquals.size() - 1 - && !wordsSplitOnEquals.get(wordIndex + 1).startsWith("=")) { + && !wordsSplitOnEquals.get(wordIndex + 1).startsWith("=")) { wordsWithEqualsMerged.add( - wordsSplitOnEquals.get(wordIndex) + wordsSplitOnEquals.get(wordIndex + 1)); + wordsSplitOnEquals.get(wordIndex) + wordsSplitOnEquals.get(wordIndex + 1)); wordIndex += 2; } else { wordsWithEqualsMerged.add(wordsSplitOnEquals.get(wordIndex++)); @@ -243,14 +243,14 @@ public class CommandLineParser { * @param word the word to split * @return the array of split words */ - private Collection stripAndSplitWord(final String word) { + private Collection stripAndSplitWord(final String word) { final ArrayList strippedAndSplitWords = new ArrayList<>(); // Strip white space by replacing all white space with blanks and then removing leading // and trailing blanks String singleSpaceWord = word.replaceAll("\\s+", " ").trim(); - if (singleSpaceWord.length() == 0) { + if (singleSpaceWord.isEmpty()) { return strippedAndSplitWords; } @@ -267,7 +267,7 @@ public class CommandLineParser { * Dumpty had ""a "great" fall becomes [Humpty ],["],[Dumpty sat on the wall],["],[, Humpty Dumpty had ],["],["],a * ["],[great],["],[ fall]. * - * @param line the input line + * @param line the input line * @param splitChar the split char * @return the split array list */ @@ -300,7 +300,7 @@ public class CommandLineParser { * This method checks that an array list containing a command is in the correct format. * * @param commandWords the command words - * @param logicBlock A block of logic code to be taken literally + * @param logicBlock A block of logic code to be taken literally * @return the checked array list */ private ArrayList checkFormat(final ArrayList commandWords, final String logicBlock) { @@ -312,7 +312,7 @@ public class CommandLineParser { // The first word must be alphanumeric, that is a command if (!commandWords.get(0).matches("^[a-zA-Z0-9]*$")) { throw new CommandLineException( - "first command word is not alphanumeric or is not a command: " + commandWords.get(0)); + "first command word is not alphanumeric or is not a command: " + commandWords.get(0)); } // Now check that we have a sequence of commands at the beginning @@ -330,12 +330,12 @@ public class CommandLineParser { commandWords.set(currentWordPos, commandWords.get(currentWordPos) + logicBlock); } else { throw new CommandLineException( - "command argument is not properly formed: " + commandWords.get(currentWordPos)); + "command argument is not properly formed: " + commandWords.get(currentWordPos)); } } else if (!commandWords.get(currentWordPos).matches("^[a-zA-Z0-9]+=[a-zA-Z0-9/\"].*$")) { // Not a last command, or the last command, but there is no logic block - wrong pattern throw new CommandLineException( - "command argument is not properly formed: " + commandWords.get(currentWordPos)); + "command argument is not properly formed: " + commandWords.get(currentWordPos)); } } diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/FileMacroTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/FileMacroTest.java index d407e5a4e..443727236 100644 --- a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/FileMacroTest.java +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/FileMacroTest.java @@ -46,6 +46,8 @@ class FileMacroTest { private File tempModelFile; private File tempLogFile; + private static final String WHITESPACE_REGEX = "(\\s+){1,4}"; + /** * Creates the temp files. * @@ -101,11 +103,13 @@ class FileMacroTest { final File outputLogFile = new File(tempLogFile.getCanonicalPath()); final String outputLogString = TextFileUtils.getTextFileAsString(outputLogFile.getCanonicalPath()) - .replace(Paths.get("").toAbsolutePath() + File.separator, "").replaceAll("\\s+", ""); + .replace(Paths.get("").toAbsolutePath() + File.separator, "") + .replaceAll(WHITESPACE_REGEX, ""); // We compare the log to what we expect to get final String outputLogCompareString = TextFileUtils - .getTextFileAsString("src/test/resources/compare/FileMacro_Compare.log").replaceAll("\\s+", ""); + .getTextFileAsString("src/test/resources/compare/FileMacro_Compare.log") + .replaceAll(WHITESPACE_REGEX, ""); // Check what we got is what we expected to get assertEquals(outputLogCompareString, outputLogString); diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/LogicBlockTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/LogicBlockTest.java index e30780ca0..982d5195e 100644 --- a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/LogicBlockTest.java +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/LogicBlockTest.java @@ -96,7 +96,7 @@ class LogicBlockTest { writtenModel.getKeyInformation().getKeyInfoMap().clear(); compareModel.getKeyInformation().getKeyInfoMap().clear(); - assertEquals(writtenModel, compareModel); + assertEquals(compareModel, writtenModel); } /** -- cgit 1.2.3-korg