diff --git a/src/main/java/zowe/client/sdk/parse/JsonParseFactory.java b/src/main/java/zowe/client/sdk/parse/JsonParseFactory.java index 667aed406..75dc243f2 100644 --- a/src/main/java/zowe/client/sdk/parse/JsonParseFactory.java +++ b/src/main/java/zowe/client/sdk/parse/JsonParseFactory.java @@ -53,9 +53,6 @@ public static JsonParse buildParser(final ParseType type) { case MEMBER: parseResponse = MemberJsonParse.getInstance(); break; - case MVS_CONSOLE: - parseResponse = MvsConsoleJsonParse.getInstance(); - break; case PROPS: parseResponse = PropsJsonParse.getInstance(); break; diff --git a/src/main/java/zowe/client/sdk/parse/MvsConsoleJsonParse.java b/src/main/java/zowe/client/sdk/parse/MvsConsoleJsonParse.java deleted file mode 100644 index 14eb96732..000000000 --- a/src/main/java/zowe/client/sdk/parse/MvsConsoleJsonParse.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * This program and the accompanying materials are made available under the terms of the - * Eclipse Public License v2.0 which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-v20.html - * - * SPDX-License-Identifier: EPL-2.0 - * - * Copyright Contributors to the Zowe Project. - */ -package zowe.client.sdk.parse; - -import org.json.simple.JSONObject; -import zowe.client.sdk.utility.ValidateUtils; -import zowe.client.sdk.zosconsole.response.ZosmfIssueResponse; - -/** - * Parse JSON response from MVS console request - * - * @author Frank Giordano - * @version 5.0 - */ -public final class MvsConsoleJsonParse implements JsonParse { - - private static class Holder { - - /** - * Represents one singleton instance - */ - private static final MvsConsoleJsonParse instance = new MvsConsoleJsonParse(); - - } - - /** - * Private constructor defined to avoid public instantiation of class - * - * @author Frank Giordano - */ - private MvsConsoleJsonParse() { - } - - /** - * Get a singleton instance - * - * @return MvsConsoleJsonParse object - * @author Frank Giordano - */ - public static MvsConsoleJsonParse getInstance() { - return MvsConsoleJsonParse.Holder.instance; - } - - /** - * Transform data into ZosmfIssueResponse object - * - * @param args json data to parse - * @return ZosmfIssueResponse object - * @author Frank Giordano - */ - @Override - public synchronized Object parseResponse(final Object... args) { - ValidateUtils.checkNullParameter(args[0] == null, ParseConstants.DATA_NULL_MSG); - final JSONObject data = (JSONObject) args[0]; - final ZosmfIssueResponse zosmfIssueResponse = new ZosmfIssueResponse(); - zosmfIssueResponse.setCmdResponseKey( - data.get("cmd-response-key") != null ? (String) data.get("cmd-response-key") : null); - zosmfIssueResponse.setCmdResponseUrl( - data.get("cmd-response-url") != null ? (String) data.get("cmd-response-url") : null); - zosmfIssueResponse.setCmdResponseUri( - data.get("cmd-response-uri") != null ? (String) data.get("cmd-response-uri") : null); - zosmfIssueResponse.setCmdResponse( - data.get("cmd-response") != null ? (String) data.get("cmd-response") : null); - zosmfIssueResponse.setSolKeyDetected( - data.get("sol-key-detected") != null ? (String) data.get("sol-key-detected") : null); - return zosmfIssueResponse; - } - -} diff --git a/src/main/java/zowe/client/sdk/parse/type/ParseType.java b/src/main/java/zowe/client/sdk/parse/type/ParseType.java index 9fd41592b..2f6e5fa76 100644 --- a/src/main/java/zowe/client/sdk/parse/type/ParseType.java +++ b/src/main/java/zowe/client/sdk/parse/type/ParseType.java @@ -33,10 +33,6 @@ public enum ParseType { * member type */ MEMBER, - /** - * mvs console type - */ - MVS_CONSOLE, /** * property type */ diff --git a/src/main/java/zowe/client/sdk/utility/ConsoleUtils.java b/src/main/java/zowe/client/sdk/utility/ConsoleUtils.java new file mode 100644 index 000000000..953505c68 --- /dev/null +++ b/src/main/java/zowe/client/sdk/utility/ConsoleUtils.java @@ -0,0 +1,41 @@ +/* + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright Contributors to the Zowe Project. + */ +package zowe.client.sdk.utility; + +/** + * Utility class contains helper methods for console processing. + * + * @author Frank Giordano + * @version 5.0 + */ +public final class ConsoleUtils { + + /** + * Private constructor defined to avoid instantiation of class + */ + private ConsoleUtils() { + throw new IllegalStateException("Utility class"); + } + + /** + * Process response from console command. Add a line break if needed. + * + * @param response string value of response from console command + * @return string value of response with line break added if needed + */ + public static String processCmdResponse(String response) { + response = response.replace('\r', '\n'); + if (!response.isBlank() && response.charAt(response.length() - 1) != '\n') { + response = response + "\n"; + } + return response; + } + +} diff --git a/src/main/java/zowe/client/sdk/utility/EncodeUtils.java b/src/main/java/zowe/client/sdk/utility/EncodeUtils.java index 40a9cc63b..17412974b 100644 --- a/src/main/java/zowe/client/sdk/utility/EncodeUtils.java +++ b/src/main/java/zowe/client/sdk/utility/EncodeUtils.java @@ -16,7 +16,7 @@ import java.util.Base64; /** - * Utility class contains helper methods for encoding processing + * Utility class contains helper methods for encoding processing. * * @author Frank Giordano * @version 5.0 diff --git a/src/main/java/zowe/client/sdk/utility/JsonParserUtil.java b/src/main/java/zowe/client/sdk/utility/JsonParserUtils.java similarity index 96% rename from src/main/java/zowe/client/sdk/utility/JsonParserUtil.java rename to src/main/java/zowe/client/sdk/utility/JsonParserUtils.java index e8ee40109..66eb9b9fd 100644 --- a/src/main/java/zowe/client/sdk/utility/JsonParserUtil.java +++ b/src/main/java/zowe/client/sdk/utility/JsonParserUtils.java @@ -18,21 +18,21 @@ import zowe.client.sdk.rest.exception.ZosmfRequestException; /** - * Utility class contains helper methods for JSON parse processing + * Utility class contains helper methods for JSON parse processing. * * @author Frank Giordano * @version 5.0 */ -public final class JsonParserUtil { +public final class JsonParserUtils { - private static final Logger LOG = LoggerFactory.getLogger(JsonParserUtil.class); + private static final Logger LOG = LoggerFactory.getLogger(JsonParserUtils.class); private static final String PARSE_ERROR_MSG = "json response parse error"; /** * Private constructor defined to avoid instantiation of class */ - private JsonParserUtil() { + private JsonParserUtils() { throw new IllegalStateException("Utility class"); } diff --git a/src/main/java/zowe/client/sdk/utility/TsoUtil.java b/src/main/java/zowe/client/sdk/utility/TsoUtils.java similarity index 95% rename from src/main/java/zowe/client/sdk/utility/TsoUtil.java rename to src/main/java/zowe/client/sdk/utility/TsoUtils.java index 193d6ce5a..82df5f559 100644 --- a/src/main/java/zowe/client/sdk/utility/TsoUtil.java +++ b/src/main/java/zowe/client/sdk/utility/TsoUtils.java @@ -19,17 +19,17 @@ import java.util.concurrent.atomic.AtomicInteger; /** - * Utility class contains helper methods for response processing + * Utility class contains helper methods for TSO response processing. * * @author Frank Giordano * @version 5.0 */ -public class TsoUtil { +public final class TsoUtils { /** * Private constructor defined to avoid instantiation of class */ - private TsoUtil() { + private TsoUtils() { throw new IllegalStateException("Utility class"); } diff --git a/src/main/java/zowe/client/sdk/utility/ValidateUtils.java b/src/main/java/zowe/client/sdk/utility/ValidateUtils.java index 795b3efd1..daa2cf4d0 100644 --- a/src/main/java/zowe/client/sdk/utility/ValidateUtils.java +++ b/src/main/java/zowe/client/sdk/utility/ValidateUtils.java @@ -14,7 +14,7 @@ import java.util.Optional; /** - * Utility class contains helper methods for validation processing + * Utility class contains helper methods for validation processing. * * @author Frank Giordano * @version 5.0 diff --git a/src/main/java/zowe/client/sdk/zosconsole/README.md b/src/main/java/zowe/client/sdk/zosconsole/README.md index 23e9beba0..9f0a03e40 100644 --- a/src/main/java/zowe/client/sdk/zosconsole/README.md +++ b/src/main/java/zowe/client/sdk/zosconsole/README.md @@ -16,8 +16,9 @@ import zowe.client.sdk.core.ZosConnectionFactory; import zowe.client.sdk.examples.TstZosConnection; import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.zosconsole.ConsoleConstants; -import zowe.client.sdk.zosconsole.input.IssueConsoleInputData; -import zowe.client.sdk.zosconsole.method.IssueConsole; +import zowe.client.sdk.zosconsole.input.ConsoleCmdInputData; +import zowe.client.sdk.zosconsole.method.ConsoleCmd; +import zowe.client.sdk.zosconsole.response.ConsoleGetResponse; import zowe.client.sdk.zosconsole.response.ConsoleResponse; /** @@ -50,10 +51,10 @@ public class IssueConsoleExp extends TstZosConnection { * @author Frank Giordano */ public static void issueCommand(ZosConnection connection, String cmd) { - ConsoleResponse response; + ConsoleGetResponse response; try { - IssueConsole issueConsole = new IssueConsole(connection); - response = issueConsole.issueCommand(cmd); + ConsoleCmd consoleCmd = new ConsoleCmd(connection); + response = consoleCmd.issueCommand(cmd); } catch (ZosmfRequestException e) { String errMsg = (String) e.getResponse().getResponsePhrase().orElse(e.getMessage()); throw new RuntimeException(errMsg); @@ -70,12 +71,12 @@ public class IssueConsoleExp extends TstZosConnection { * @author Frank Giordano */ public static void issueCommandCommon(ZosConnection connection, String cmd) { - ConsoleResponse response; + ConsoleGetResponse response; try { - IssueConsole issueConsole = new IssueConsole(connection); - IssueConsoleInputData consoleInputData = new IssueConsoleInputData(cmd); + ConsoleCmd consoleCmd = new ConsoleCmd(connection); + ConsoleCmdInputData consoleInputData = new ConsoleCmdInputData(cmd); consoleInputData.setProcessResponse(); - response = issueConsole.issueCommandCommon(ConsoleConstants.RES_DEF_CN, consoleInputData); + response = consoleCmd.issueCommandCommon(ConsoleConstants.RES_DEF_CN, consoleInputData); } catch (ZosmfRequestException e) { String errMsg = (String) e.getResponse().getResponsePhrase().orElse(e.getMessage()); throw new RuntimeException(errMsg); diff --git a/src/main/java/zowe/client/sdk/zosconsole/input/IssueConsoleInputData.java b/src/main/java/zowe/client/sdk/zosconsole/input/ConsoleCmdInputData.java similarity index 86% rename from src/main/java/zowe/client/sdk/zosconsole/input/IssueConsoleInputData.java rename to src/main/java/zowe/client/sdk/zosconsole/input/ConsoleCmdInputData.java index 295708967..56ff5e059 100644 --- a/src/main/java/zowe/client/sdk/zosconsole/input/IssueConsoleInputData.java +++ b/src/main/java/zowe/client/sdk/zosconsole/input/ConsoleCmdInputData.java @@ -14,12 +14,12 @@ import java.util.Optional; /** - * The z/OSMF console API parameters. See the z/OSMF REST API documentation for full details. + * The z/OSMF console command API parameters. See the z/OSMF REST API documentation for full details. * * @author Frank Giordano * @version 5.0 */ -public class IssueConsoleInputData { +public class ConsoleCmdInputData { /** * The z/OS console command to issue. @@ -28,7 +28,7 @@ public class IssueConsoleInputData { /** * The solicited keyword to check for in the response. Causes the API to return immediately when the keyword - * is found, however, it may include solicited command response messages beyond the keyword itself. + * is found; however, it may include solicited command response messages beyond the keyword itself. */ private String solKey; @@ -38,8 +38,10 @@ public class IssueConsoleInputData { private String system; /** - * The z/OSMF Console API returns '\r' or '\r\n' where line-breaks. Can attempt to replace these - * sequences with '\n', but there may be cases where that are not preferable. Specify false to prevent processing. + * The z/OSMF Console API returns '\r' or '\r\n' where line-breaks. + * You can use the following flag variable to replace these sequences with '\n'. + * But there may be cases where this may not be preferable. + * Specify false to prevent processing. */ private boolean processResponse = false; @@ -49,7 +51,7 @@ public class IssueConsoleInputData { * @param command console command to issue * @author Frank Giordano */ - public IssueConsoleInputData(final String command) { + public ConsoleCmdInputData(final String command) { ValidateUtils.checkIllegalParameter(command, "command"); this.cmd = command; } diff --git a/src/main/java/zowe/client/sdk/zosconsole/method/IssueConsole.java b/src/main/java/zowe/client/sdk/zosconsole/method/ConsoleCmd.java similarity index 61% rename from src/main/java/zowe/client/sdk/zosconsole/method/IssueConsole.java rename to src/main/java/zowe/client/sdk/zosconsole/method/ConsoleCmd.java index 59d9dcfd8..c5d216942 100644 --- a/src/main/java/zowe/client/sdk/zosconsole/method/IssueConsole.java +++ b/src/main/java/zowe/client/sdk/zosconsole/method/ConsoleCmd.java @@ -9,56 +9,69 @@ */ package zowe.client.sdk.zosconsole.method; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import org.json.simple.JSONObject; import zowe.client.sdk.core.ZosConnection; -import zowe.client.sdk.parse.JsonParseFactory; -import zowe.client.sdk.parse.type.ParseType; import zowe.client.sdk.rest.PutJsonZosmfRequest; import zowe.client.sdk.rest.ZosmfRequest; import zowe.client.sdk.rest.ZosmfRequestFactory; import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; +import zowe.client.sdk.utility.ConsoleUtils; import zowe.client.sdk.utility.EncodeUtils; -import zowe.client.sdk.utility.JsonParserUtil; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zosconsole.ConsoleConstants; -import zowe.client.sdk.zosconsole.input.IssueConsoleInputData; -import zowe.client.sdk.zosconsole.response.ConsoleResponse; -import zowe.client.sdk.zosconsole.response.ZosmfIssueResponse; -import zowe.client.sdk.zosconsole.service.ConsoleResponseService; +import zowe.client.sdk.zosconsole.input.ConsoleCmdInputData; +import zowe.client.sdk.zosconsole.response.ConsoleCmdResponse; import java.util.HashMap; import java.util.Map; /** - * Issue MVS Console commands by using a system console + * Issue a MVS console command. + *

+ * This operation issues a command, based on the properties that are specified in the request body. + * On successful completion, HTTP status code 200 is returned. A JSON object typically contains the + * command response. + *

+ * When a command is issued synchronously, the console API attempts to get the solicited messages + * immediately after the command is issued. If there are no messages available within a certain time + * interval, approximately 3 seconds when your system workload is not high, the API returns + * "cmd-response": "" in the response body. + *

+ * A value for cmd-response of the empty string, "", usually means that there is no command response. + * However, it is also possible that the command response arrived after 3 seconds. If that is the case, + * you can use the cmd-response-url field in the response body to retrieve the command response. + * You might do this several times to ensure that all messages that are related to the command + * are retrieved. This is prebuilt for you via the ConsoleGet class in this package. * * @author Frank Giordano * @version 5.0 */ -public class IssueConsole { +public class ConsoleCmd { private static final String CMD = "cmd"; private static final String SOL_KEY = "sol-key"; private static final String SYSTEM = "system"; - + private final ObjectMapper objectMapper = new ObjectMapper(); private final ZosConnection connection; - private ZosmfRequest request; /** - * IssueCommand constructor + * ConsoleCmd constructor * * @param connection for connection information, see ZosConnection object * @author Frank Giordano */ - public IssueConsole(final ZosConnection connection) { + public ConsoleCmd(final ZosConnection connection) { ValidateUtils.checkNullParameter(connection == null, "connection is null"); this.connection = connection; } /** - * Alternative IssueCommand constructor with ZoweRequest object. This is mainly used for internal code unit testing + * Alternative ConsoleCmd constructor with ZoweRequest object. This is mainly used for internal code unit testing * with mockito, and it is not recommended to be used by the larger community. *

* This constructor is package-private @@ -67,7 +80,7 @@ public IssueConsole(final ZosConnection connection) { * @param request any compatible ZoweRequest Interface object * @author Frank Giordano */ - IssueConsole(final ZosConnection connection, final ZosmfRequest request) { + ConsoleCmd(final ZosConnection connection, final ZosmfRequest request) { ValidateUtils.checkNullParameter(connection == null, "connection is null"); ValidateUtils.checkNullParameter(request == null, "request is null"); this.connection = connection; @@ -84,12 +97,12 @@ public IssueConsole(final ZosConnection connection) { * will be returned. * * @param command string value that represents command to issue - * @return ConsoleResponse object + * @return IssueCommandResponse object * @throws ZosmfRequestException request error state * @author Frank Giordano */ - public ConsoleResponse issueCommand(final String command) throws ZosmfRequestException { - return issueCommandCommon(ConsoleConstants.RES_DEF_CN, new IssueConsoleInputData(command)); + public ConsoleCmdResponse issueCommand(final String command) throws ZosmfRequestException { + return issueCommandCommon(ConsoleConstants.RES_DEF_CN, new ConsoleCmdInputData(command)); } /** @@ -100,24 +113,24 @@ public ConsoleResponse issueCommand(final String command) throws ZosmfRequestExc * * @param command string value representing console command to issue * @param consoleName name of the console that is used to issue the command - * @return ConsoleResponse object + * @return IssueCommandResponse object * @throws ZosmfRequestException request error state * @author Frank Giordano */ - public ConsoleResponse issueCommand(final String command, final String consoleName) throws ZosmfRequestException { - return issueCommandCommon(consoleName, new IssueConsoleInputData(command)); + public ConsoleCmdResponse issueCommand(final String command, final String consoleName) throws ZosmfRequestException { + return issueCommandCommon(consoleName, new ConsoleCmdInputData(command)); } /** - * Issue an MVS console command on a given console name driven by IssueConsoleInputData settings done synchronously + * Issue an MVS console command on a given console name driven by ConsoleCmdInputData settings done synchronously. * * @param consoleName name of the console that is used to issue the command - * @param consoleInputData synchronous console issue parameters, see IssueConsoleInputData object - * @return command response on resolve, see ZosmfIssueResponse object + * @param consoleInputData synchronous console issue parameters, see ConsoleCmdInputData object + * @return IssueCommandResponse object * @throws ZosmfRequestException request error state * @author Frank Giordano */ - public ConsoleResponse issueCommandCommon(final String consoleName, final IssueConsoleInputData consoleInputData) + public ConsoleCmdResponse issueCommandCommon(final String consoleName, final ConsoleCmdInputData consoleInputData) throws ZosmfRequestException { ValidateUtils.checkIllegalParameter(consoleName, "consoleName"); ValidateUtils.checkNullParameter(consoleInputData == null, "consoleInputData is null"); @@ -135,20 +148,29 @@ public ConsoleResponse issueCommandCommon(final String consoleName, final IssueC final String jsonStr = request.executeRequest().getResponsePhrase() .orElseThrow(() -> new IllegalStateException("no issue console response phrase")).toString(); - final JSONObject jsonObject = JsonParserUtil.parse(jsonStr); - return ConsoleResponseService.getInstance().buildConsoleResponse( - (ZosmfIssueResponse) JsonParseFactory.buildParser(ParseType.MVS_CONSOLE).parseResponse(jsonObject), - consoleInputData.isProcessResponse()); + + final JsonNode jsonNode; + try { + jsonNode = objectMapper.readTree(jsonStr); + } catch (JsonProcessingException e) { + throw new ZosmfRequestException(e.getMessage()); + } + + ConsoleCmdResponse response = objectMapper.convertValue(jsonNode, ConsoleCmdResponse.class); + if (consoleInputData.isProcessResponse()) { + response.setCmdResponse(ConsoleUtils.processCmdResponse(response.getCmdResponse().orElse(""))); + } + return response; } /** - * Generate a map of values for JSON body payload for request + * Generate a map of values for JSON body payload for request. * - * @param consoleInputData IssueConsoleInputData object + * @param consoleInputData ConsoleCmdInputData object * @return Map for JSON body * @author Shabaz Kowthalam */ - private Map getIssueMap(IssueConsoleInputData consoleInputData) { + private Map getIssueMap(ConsoleCmdInputData consoleInputData) { final Map issueMap = new HashMap<>(); issueMap.put(CMD, consoleInputData.getCmd()); consoleInputData.getSolKey().ifPresent(solKey -> issueMap.put(SOL_KEY, solKey)); diff --git a/src/main/java/zowe/client/sdk/zosconsole/method/ConsoleGet.java b/src/main/java/zowe/client/sdk/zosconsole/method/ConsoleGet.java new file mode 100644 index 000000000..88e369187 --- /dev/null +++ b/src/main/java/zowe/client/sdk/zosconsole/method/ConsoleGet.java @@ -0,0 +1,135 @@ +package zowe.client.sdk.zosconsole.method; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import zowe.client.sdk.core.ZosConnection; +import zowe.client.sdk.rest.GetJsonZosmfRequest; +import zowe.client.sdk.rest.ZosmfRequest; +import zowe.client.sdk.rest.ZosmfRequestFactory; +import zowe.client.sdk.rest.exception.ZosmfRequestException; +import zowe.client.sdk.rest.type.ZosmfRequestType; +import zowe.client.sdk.utility.ConsoleUtils; +import zowe.client.sdk.utility.EncodeUtils; +import zowe.client.sdk.utility.ValidateUtils; +import zowe.client.sdk.zosconsole.ConsoleConstants; +import zowe.client.sdk.zosconsole.response.ConsoleGetResponse; + +/** + * Get synchronous z/OS console response messages from a console issue command request (see ConsoleCmd class). + *

+ * This class is used to retrieve the console response messages not provided to the console issue command request + * within a certain time interval of approximately 3 seconds. It is possible that the command responses exist after + * the time interval and can be retrieved. + *

+ * Response messages are stored in the response key of the console issue command request. + * + * @author Frank Giordano + * @version 5.0 + */ +public class ConsoleGet { + + private final ObjectMapper objectMapper = new ObjectMapper(); + private final ZosConnection connection; + private ZosmfRequest request; + + /** + * ConsoleGet constructor + * + * @param connection for connection information, see ZosConnection object + * @author Frank Giordano + */ + public ConsoleGet(final ZosConnection connection) { + ValidateUtils.checkNullParameter(connection == null, "connection is null"); + this.connection = connection; + } + + /** + * Alternative ConsoleGet constructor with ZoweRequest object. This is mainly used for internal code unit testing + * with mockito, and it is not recommended to be used by the larger community. + *

+ * This constructor is package-private + * + * @param connection for connection information, see ZosConnection object + * @param request any compatible ZoweRequest Interface object + * @author Frank Giordano + */ + ConsoleGet(final ZosConnection connection, final ZosmfRequest request) { + ValidateUtils.checkNullParameter(connection == null, "connection is null"); + ValidateUtils.checkNullParameter(request == null, "request is null"); + this.connection = connection; + if (!(request instanceof GetJsonZosmfRequest)) { + throw new IllegalStateException("GET_JSON request type required"); + } + this.request = request; + } + + /** + * Retrieve synchronous z/OS console response messages from a console issue command request's response value. + * + * @param responseKey response key from the issue console command request + * @return ConsoleResponse object + * @throws ZosmfRequestException request error state + * @author Frank Giordano + */ + public ConsoleGetResponse getResponse(final String responseKey) throws ZosmfRequestException { + return getResponseCommon(responseKey, "", true); + } + + /** + * Retrieve synchronous z/OS console response messages from a console issue command request's + * response value and console name. + * + * @param responseKey response key from the issue console command request + * @param consoleName name of the console that is used to issue the command + * @return ConsoleResponse object + * @throws ZosmfRequestException request error state + * @author Frank Giordano + */ + public ConsoleGetResponse getResponse(final String responseKey, final String consoleName) + throws ZosmfRequestException { + return getResponseCommon(responseKey, consoleName, true); + } + + /** + * Common method with all inputs to retrieve any outstanding synchronous z/OS console response messages + * from a console issue command request. + * + * @param responseKey response key from the issue console command request + * @param consoleName name of the console that is used to issue the command + * @param processResponse process console command response + * @return ConsoleResponse object + * @throws ZosmfRequestException request error state + * @author Frank Giordano + */ + public ConsoleGetResponse getResponseCommon(final String responseKey, final String consoleName, + boolean processResponse) throws ZosmfRequestException { + ValidateUtils.checkIllegalParameter(responseKey, "responseKey"); + + final String url = connection.getZosmfUrl() + ConsoleConstants.RESOURCE + "/" + + EncodeUtils.encodeURIComponent(consoleName.isBlank() ? ConsoleConstants.RES_DEF_CN : consoleName) + + "/solmsgs/" + responseKey; + + if (request == null) { + request = ZosmfRequestFactory.buildRequest(connection, ZosmfRequestType.GET_JSON); + } + request.setUrl(url); + + final String jsonStr = request.executeRequest().getResponsePhrase() + .orElseThrow(() -> new IllegalStateException("no issue console response phrase")).toString(); + + final JsonNode jsonNode; + try { + jsonNode = objectMapper.readTree(jsonStr); + } catch (JsonProcessingException e) { + throw new ZosmfRequestException(e.getMessage()); + } + + ConsoleGetResponse response = objectMapper.convertValue(jsonNode, ConsoleGetResponse.class); + if (processResponse) { + response.setCmdResponse(ConsoleUtils.processCmdResponse(response.getCmdResponse().orElse(""))); + } + return response; + } + +} diff --git a/src/main/java/zowe/client/sdk/zosconsole/response/ZosmfIssueResponse.java b/src/main/java/zowe/client/sdk/zosconsole/response/ConsoleCmdResponse.java similarity index 65% rename from src/main/java/zowe/client/sdk/zosconsole/response/ZosmfIssueResponse.java rename to src/main/java/zowe/client/sdk/zosconsole/response/ConsoleCmdResponse.java index 1215e6a06..d8ad77c78 100644 --- a/src/main/java/zowe/client/sdk/zosconsole/response/ZosmfIssueResponse.java +++ b/src/main/java/zowe/client/sdk/zosconsole/response/ConsoleCmdResponse.java @@ -1,14 +1,8 @@ -/* - * This program and the accompanying materials are made available under the terms of the - * Eclipse Public License v2.0 which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-v20.html - * - * SPDX-License-Identifier: EPL-2.0 - * - * Copyright Contributors to the Zowe Project. - */ package zowe.client.sdk.zosconsole.response; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + import java.util.Optional; /** @@ -17,67 +11,77 @@ * @author Frank Giordano * @version 5.0 */ -public class ZosmfIssueResponse { +@JsonIgnoreProperties +public class ConsoleCmdResponse { /** - * Follow-up response URL. + * Key that can be used to retrieve the command response. */ - private String cmdResponseUrl; + @JsonProperty("cmd-response-key") + private String cmdResponseKey; /** - * Command response text. + * URL that can be used to retrieve the command response later + * when the value for cmd-response is empty. */ - private String cmdResponse; + @JsonProperty("cmd-response-url") + private String cmdResponseUrl; /** - * The follow-up response URI. + * URI that can be used to retrieve the command response later + * when the value for cmd-response is empty. The URI starts with /zosmf. */ + @JsonProperty("cmd-response-uri") private String cmdResponseUri; /** - * The command response key used for follow-up requests. + * Command response text. */ - private String cmdResponseKey; + @JsonProperty("cmd-response") + private String cmdResponse; /** - * True if the solicited keyword requested is present. + * Returned when a sol-key was specified, and unsol-detect-sync was specified + * as N or not specified. If the keyword was detected in the command response, + * the value is true. Otherwise, the value is false. */ + @JsonProperty("sol-key-detected") private String solKeyDetected; /** - * Retrieve cmdResponse value + * Retrieve cmdResponseKey value * - * @return cmdResponse value + * @return cmdResponseKey value */ - public Optional getCmdResponse() { - return Optional.ofNullable(cmdResponse); + public Optional getCmdResponseKey() { + return Optional.ofNullable(cmdResponseKey); } /** - * Assign cmdResponse value + * Assign cmdResponseKey value * - * @param cmdResponse value + * @param cmdResponseKey value */ - public void setCmdResponse(final String cmdResponse) { - this.cmdResponse = cmdResponse; + public void setCmdResponseKey(final String cmdResponseKey) { + this.cmdResponseKey = cmdResponseKey; } /** - * Retrieve cmdResponseKey value + * Retrieve cmdResponseUrl value * - * @return cmdResponseKey value + * @return cmdResponseUrl value */ - public Optional getCmdResponseKey() { - return Optional.ofNullable(cmdResponseKey); + public Optional getCmdResponseUrl() { + return Optional.ofNullable(cmdResponseUrl); } /** - * Assign cmdResponseKey value + * Assign cmdResponseUrl value * - * @param cmdResponseKey value + * @param cmdResponseUrl value */ - public void setCmdResponseKey(final String cmdResponseKey) { - this.cmdResponseKey = cmdResponseKey; + public void setCmdResponseUrl(final String cmdResponseUrl) { + this.cmdResponseUrl = cmdResponseUrl; } /** @@ -99,21 +103,21 @@ public void setCmdResponseUri(final String cmdResponseUri) { } /** - * Retrieve cmdResponseUrl value + * Retrieve cmdResponse value * - * @return cmdResponseUrl value + * @return cmdResponse value */ - public Optional getCmdResponseUrl() { - return Optional.ofNullable(cmdResponseUrl); + public Optional getCmdResponse() { + return Optional.ofNullable(cmdResponse); } /** - * Assign cmdResponseUrl value + * Assign cmdResponse value * - * @param cmdResponseUrl value + * @param cmdResponse value */ - public void setCmdResponseUrl(final String cmdResponseUrl) { - this.cmdResponseUrl = cmdResponseUrl; + public void setCmdResponse(final String cmdResponse) { + this.cmdResponse = cmdResponse; } /** @@ -135,18 +139,18 @@ public void setSolKeyDetected(final String solKeyDetected) { } /** - * Return string value representing ZosmfIssueResponse object + * Return string value representing IssueCommandResponse object * - * @return string representation of ZosmfIssueResponse + * @return string representation of IssueCommandResponse */ @Override public String toString() { - return "ZosmfIssueResponse{" + - "cmdResponseUrl=" + cmdResponseUrl + - ", cmdResponse=" + cmdResponse + - ", cmdResponseUri=" + cmdResponseUri + - ", cmdResponseKey=" + cmdResponseKey + - ", solKeyDetected=" + solKeyDetected + + return "IssueCommandResponse{" + + "cmdResponseKey='" + cmdResponseKey + '\'' + + ", cmdResponseUrl='" + cmdResponseUrl + '\'' + + ", cmdResponseUri='" + cmdResponseUri + '\'' + + ", cmdResponse='" + cmdResponse + '\'' + + ", solKeyDetected='" + solKeyDetected + '\'' + '}'; } diff --git a/src/main/java/zowe/client/sdk/zosconsole/response/ConsoleGetResponse.java b/src/main/java/zowe/client/sdk/zosconsole/response/ConsoleGetResponse.java new file mode 100644 index 000000000..74db52bb7 --- /dev/null +++ b/src/main/java/zowe/client/sdk/zosconsole/response/ConsoleGetResponse.java @@ -0,0 +1,82 @@ +/* + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright Contributors to the Zowe Project. + */ +package zowe.client.sdk.zosconsole.response; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Optional; + +/** + * The console response for a z/OSMF synchronous issue console command request. + * + * @author Frank Giordano + * @version 5.0 + */ +@JsonIgnoreProperties +public class ConsoleGetResponse { + + /** + * Command response text. + */ + @JsonProperty("cmd-response") + private String cmdResponse; + + /** + * If the solicited keyword is specified, indicates that the keyword was detected. + */ + @JsonProperty("sol_key_detected") + private boolean keywordDetected = false; + + /** + * Retrieve cmdResponse specified + * + * @return cmdResponse value + */ + public Optional getCmdResponse() { + return Optional.ofNullable(cmdResponse); + } + + /** + * Assign cmdResponse value + * + * @param cmdResponse value + */ + public void setCmdResponse(final String cmdResponse) { + this.cmdResponse = cmdResponse; + } + + /** + * Retrieve the keywordDetected specified + * + * @return keywordDetected true or false is keywordDetected seen + */ + public boolean isKeywordDetected() { + return keywordDetected; + } + + /** + * Assign keywordDetected value + * + * @param keywordDetected value + */ + public void setKeywordDetected(boolean keywordDetected) { + this.keywordDetected = keywordDetected; + } + + @Override + public String toString() { + return "ConsoleResponse{" + + "cmdResponse='" + cmdResponse + '\'' + + ", keywordDetected=" + keywordDetected + + '}'; + } + +} diff --git a/src/main/java/zowe/client/sdk/zosconsole/response/ConsoleResponse.java b/src/main/java/zowe/client/sdk/zosconsole/response/ConsoleResponse.java deleted file mode 100644 index 75af16b5f..000000000 --- a/src/main/java/zowe/client/sdk/zosconsole/response/ConsoleResponse.java +++ /dev/null @@ -1,203 +0,0 @@ -/* - * This program and the accompanying materials are made available under the terms of the - * Eclipse Public License v2.0 which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-v20.html - * - * SPDX-License-Identifier: EPL-2.0 - * - * Copyright Contributors to the Zowe Project. - */ -package zowe.client.sdk.zosconsole.response; - -import java.util.Optional; - -/** - * The Console API response. - * - * @author Frank Giordano - * @version 5.0 - */ -public class ConsoleResponse { - - /** - * True if the command was issued and the responses were collected. - */ - private boolean success = false; - - /** - * The list of zOSMF console API responses. May issue multiple requests (because of a user request) or - * to ensure that all messages are collected. Each response is placed here. - */ - private ZosmfIssueResponse zosmfResponse; - - /** - * If an error occurs, returns the ImperativeError, which contains a case error. - */ - private String failureResponse; - - /** - * The command response text. - */ - private String commandResponse; - - /** - * The final command response key - used to "follow up" and check for additional response messages for the command. - */ - private String lastResponseKey; - - /** - * If the solicited keyword is specified, indicates that the keyword was detected. - */ - private boolean keywordDetected = false; - - /** - * The "follow-up" command response URL - you can paste this in the browser to do a "GET" using the command - * response key provided in the URI route. - */ - private String cmdResponseUrl; - - /** - * Retrieve cmdResponseUrl specified - * - * @return cmdResponseUrl value - */ - public Optional getCmdResponseUrl() { - return Optional.ofNullable(cmdResponseUrl); - } - - /** - * Assign cmdResponseUrl value - * - * @param cmdResponseUrl value - */ - public void setCmdResponseUrl(final String cmdResponseUrl) { - this.cmdResponseUrl = cmdResponseUrl; - } - - /** - * Retrieve commandResponse specified - * - * @return commandResponse value - */ - public Optional getCommandResponse() { - return Optional.ofNullable(commandResponse); - } - - /** - * Assign commandResponse value - * - * @param commandResponse value - */ - public void setCommandResponse(final String commandResponse) { - this.commandResponse = commandResponse; - } - - /** - * Retrieve failureResponse specified - * - * @return failureResponse value - */ - public Optional getFailureResponse() { - return Optional.ofNullable(failureResponse); - } - - /** - * Assign failureResponse value - * - * @param failureResponse value - */ - public void setFailureResponse(final String failureResponse) { - this.failureResponse = failureResponse; - } - - /** - * Retrieve the keywordDetected specified - * - * @return keywordDetected true or false is keywordDetected seen - */ - public boolean isKeywordDetected() { - return keywordDetected; - } - - /** - * Assign keywordDetected value - * - * @param keywordDetected value - */ - public void setKeywordDetected(boolean keywordDetected) { - this.keywordDetected = keywordDetected; - } - - /** - * Retrieve lastResponseKey specified - * - * @return lastResponseKey value - */ - public Optional getLastResponseKey() { - return Optional.ofNullable(lastResponseKey); - } - - /** - * Assign lastResponseKey value - * - * @param lastResponseKey value - */ - public void setLastResponseKey(final String lastResponseKey) { - this.lastResponseKey = lastResponseKey; - } - - /** - * Retrieve is success - * - * @return boolean true or false - */ - public boolean isSuccess() { - return success; - } - - /** - * Assign success value - * - * @param success true or false is the response seen - */ - public void setSuccess(boolean success) { - this.success = success; - } - - /** - * Retrieve zosmfResponse specified - * - * @return zosmfResponse value - */ - public Optional getZosmfResponse() { - return Optional.ofNullable(zosmfResponse); - } - - /** - * Assign zosmfResponse value - * - * @param zosmfResponse value - */ - public void setZosmfResponse(final ZosmfIssueResponse zosmfResponse) { - this.zosmfResponse = zosmfResponse; - } - - /** - * Return string value representing ConsoleResponse object - * - * @return string representation of ConsoleResponse - */ - @Override - public String toString() { - return "ConsoleResponse{" + - "success=" + success + - ", zosmfResponse=" + zosmfResponse + - ", failureResponse=" + failureResponse + - ", commandResponse=" + commandResponse + - ", lastResponseKey=" + lastResponseKey + - ", keywordDetected=" + keywordDetected + - ", cmdResponseUrl=" + cmdResponseUrl + - '}'; - } - -} diff --git a/src/main/java/zowe/client/sdk/zosconsole/service/ConsoleResponseService.java b/src/main/java/zowe/client/sdk/zosconsole/service/ConsoleResponseService.java deleted file mode 100644 index 246841a87..000000000 --- a/src/main/java/zowe/client/sdk/zosconsole/service/ConsoleResponseService.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * This program and the accompanying materials are made available under the terms of the - * Eclipse Public License v2.0 which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-v20.html - * - * SPDX-License-Identifier: EPL-2.0 - * - * Copyright Contributors to the Zowe Project. - */ -package zowe.client.sdk.zosconsole.service; - -import zowe.client.sdk.utility.ValidateUtils; -import zowe.client.sdk.zosconsole.response.ConsoleResponse; -import zowe.client.sdk.zosconsole.response.ZosmfIssueResponse; - -/** - * ConsoleResponseService singleton class service. - * - * @author Frank Giordano - * @version 5.0 - */ -public final class ConsoleResponseService { - - private static ConsoleResponseService INSTANCE; - - /** - * Private constructor defined to avoid public instantiation of class - * - * @author Frank Giordano - */ - private ConsoleResponseService() { - } - - /** - * Get singleton instance - * - * @return ConsoleResponseService object - * @author Frank Giordano - */ - public synchronized static ConsoleResponseService getInstance() { - if (INSTANCE == null) { - INSTANCE = new ConsoleResponseService(); - } - return INSTANCE; - } - - /** - * Populate the console response with the details returned from the z/OSMF console API. - * Method adds a response to a collection of z/OSMF responses, marks the response as "succeeded" - * (response.success = true) and populates other fields of the response with values from z/OSMF response. - * - * @param zosmfResponse zosmf request response object - * @param processResponses boolean if set to true, append a command response string to the console API response - * @return response console response to be populated, see ConsoleResponse object - * @author Frank Giordano - */ - public ConsoleResponse buildConsoleResponse(final ZosmfIssueResponse zosmfResponse, boolean processResponses) { - ValidateUtils.checkNullParameter(zosmfResponse == null, "zosmfResponse is null"); - ConsoleResponse consoleResponse = new ConsoleResponse(); - consoleResponse.setZosmfResponse(zosmfResponse); - consoleResponse.setSuccess(true); - - if (zosmfResponse.getSolKeyDetected().isPresent()) { - consoleResponse.setKeywordDetected(true); - } - - // Append the command response string to the console response. - if (zosmfResponse.getCmdResponse().isPresent()) { - String responseStr = zosmfResponse.getCmdResponse().get(); - consoleResponse.setCommandResponse(responseStr); - if (processResponses && !responseStr.isBlank()) { - // the IBM responses sometimes have \r and \r\n, we will process them here and return them with just \n. - responseStr = responseStr.replace('\r', '\n'); - consoleResponse.setCommandResponse(responseStr); - // Append a line-break to ensure that additional messages collected are displayed properly. - if (responseStr.charAt(responseStr.length() - 1) != '\n') { - consoleResponse.setCommandResponse(responseStr + "\n"); - } - } - } - - // If the response key is present, set the last response key value in the response. - zosmfResponse.getCmdResponseKey().ifPresent(consoleResponse::setLastResponseKey); - - // Collect the response url. - zosmfResponse.getCmdResponseUrl().ifPresent(consoleResponse::setCmdResponseUrl); - return consoleResponse; - } - -} - diff --git a/src/main/java/zowe/client/sdk/zosconsole/service/package-info.java b/src/main/java/zowe/client/sdk/zosconsole/service/package-info.java deleted file mode 100644 index 53a60aabb..000000000 --- a/src/main/java/zowe/client/sdk/zosconsole/service/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * z/OS mvs console service package - */ -package zowe.client.sdk.zosconsole.service; \ No newline at end of file diff --git a/src/main/java/zowe/client/sdk/zosfiles/dsn/methods/DsnList.java b/src/main/java/zowe/client/sdk/zosfiles/dsn/methods/DsnList.java index f25f29fbc..911cfa7e5 100644 --- a/src/main/java/zowe/client/sdk/zosfiles/dsn/methods/DsnList.java +++ b/src/main/java/zowe/client/sdk/zosfiles/dsn/methods/DsnList.java @@ -20,7 +20,7 @@ import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; import zowe.client.sdk.utility.EncodeUtils; -import zowe.client.sdk.utility.JsonParserUtil; +import zowe.client.sdk.utility.JsonParserUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zosfiles.ZosFilesConstants; import zowe.client.sdk.zosfiles.dsn.input.DsnListInputData; @@ -184,7 +184,7 @@ private List getResult(final Response response, final List datasetLst, } final String jsonStr = response.getResponsePhrase().get().toString(); - final JSONObject jsonObject = JsonParserUtil.parse(jsonStr); + final JSONObject jsonObject = JsonParserUtils.parse(jsonStr); if (jsonObject.isEmpty()) { if (datasetLst == null) { return memberLst; diff --git a/src/main/java/zowe/client/sdk/zosfiles/uss/methods/UssExtAttr.java b/src/main/java/zowe/client/sdk/zosfiles/uss/methods/UssExtAttr.java index 31e2819a1..60432d970 100644 --- a/src/main/java/zowe/client/sdk/zosfiles/uss/methods/UssExtAttr.java +++ b/src/main/java/zowe/client/sdk/zosfiles/uss/methods/UssExtAttr.java @@ -20,7 +20,7 @@ import zowe.client.sdk.rest.type.ZosmfRequestType; import zowe.client.sdk.utility.EncodeUtils; import zowe.client.sdk.utility.FileUtils; -import zowe.client.sdk.utility.JsonParserUtil; +import zowe.client.sdk.utility.JsonParserUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zosfiles.ZosFilesConstants; @@ -81,7 +81,7 @@ public String display(final String targetPath) throws ZosmfRequestException { final Map requestMap = new HashMap<>(); requestMap.put("request", "extattr"); final Response response = executeRequest(targetPath, requestMap); - final JSONObject json = JsonParserUtil.parse(response.getResponsePhrase() + final JSONObject json = JsonParserUtils.parse(response.getResponsePhrase() .orElseThrow(() -> new IllegalStateException(ZosFilesConstants.RESPONSE_PHRASE_ERROR)).toString()); final StringBuilder str = new StringBuilder(); ((JSONArray) json.get("stdout")).forEach(item -> str.append(item.toString()).append("\n")); diff --git a/src/main/java/zowe/client/sdk/zosfiles/uss/methods/UssGetAcl.java b/src/main/java/zowe/client/sdk/zosfiles/uss/methods/UssGetAcl.java index be6123a36..83e6d3922 100644 --- a/src/main/java/zowe/client/sdk/zosfiles/uss/methods/UssGetAcl.java +++ b/src/main/java/zowe/client/sdk/zosfiles/uss/methods/UssGetAcl.java @@ -20,7 +20,7 @@ import zowe.client.sdk.rest.type.ZosmfRequestType; import zowe.client.sdk.utility.EncodeUtils; import zowe.client.sdk.utility.FileUtils; -import zowe.client.sdk.utility.JsonParserUtil; +import zowe.client.sdk.utility.JsonParserUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zosfiles.ZosFilesConstants; import zowe.client.sdk.zosfiles.uss.input.UssGetAclInputData; @@ -84,7 +84,7 @@ public String get(final String targetPath, final boolean useCommas) throws Zosmf final Response response = useCommas ? getAclCommon(targetPath, new UssGetAclInputData.Builder().usecommas(true).build()) : getAclCommon(targetPath, new UssGetAclInputData.Builder().build()); - final JSONObject json = JsonParserUtil.parse(response.getResponsePhrase() + final JSONObject json = JsonParserUtils.parse(response.getResponsePhrase() .orElseThrow(() -> new IllegalStateException(ZosFilesConstants.RESPONSE_PHRASE_ERROR)).toString()); final StringBuilder str = new StringBuilder(); if (useCommas) { diff --git a/src/main/java/zowe/client/sdk/zosfiles/uss/methods/UssList.java b/src/main/java/zowe/client/sdk/zosfiles/uss/methods/UssList.java index c7e9b8d48..4d428e2f4 100644 --- a/src/main/java/zowe/client/sdk/zosfiles/uss/methods/UssList.java +++ b/src/main/java/zowe/client/sdk/zosfiles/uss/methods/UssList.java @@ -23,7 +23,7 @@ import zowe.client.sdk.rest.type.ZosmfRequestType; import zowe.client.sdk.utility.EncodeUtils; import zowe.client.sdk.utility.FileUtils; -import zowe.client.sdk.utility.JsonParserUtil; +import zowe.client.sdk.utility.JsonParserUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zosfiles.ZosFilesConstants; import zowe.client.sdk.zosfiles.uss.input.UssListInputData; @@ -129,7 +129,7 @@ public List getFiles(final UssListInputData listInputData) throws Zosm final Response response = request.executeRequest(); final List items = new ArrayList<>(); - final JSONObject jsonObject = JsonParserUtil.parse(String.valueOf(response.getResponsePhrase() + final JSONObject jsonObject = JsonParserUtils.parse(String.valueOf(response.getResponsePhrase() .orElseThrow(() -> new IllegalStateException(ZosFilesConstants.RESPONSE_PHRASE_ERROR)))); final JSONArray jsonArray = (JSONArray) jsonObject.get("items"); if (jsonArray != null) { @@ -174,7 +174,7 @@ public List getZfsSystems(final UssListZfsInputData listZfsInputData) t final Response response = request.executeRequest(); final List items = new ArrayList<>(); - final JSONObject jsonObject = JsonParserUtil.parse(String.valueOf(response.getResponsePhrase() + final JSONObject jsonObject = JsonParserUtils.parse(String.valueOf(response.getResponsePhrase() .orElseThrow(() -> new IllegalStateException(ZosFilesConstants.RESPONSE_PHRASE_ERROR)))); final JSONArray jsonArray = (JSONArray) jsonObject.get("items"); if (jsonArray != null) { diff --git a/src/main/java/zowe/client/sdk/zosjobs/methods/JobGet.java b/src/main/java/zowe/client/sdk/zosjobs/methods/JobGet.java index d016df02c..a3d9ac079 100644 --- a/src/main/java/zowe/client/sdk/zosjobs/methods/JobGet.java +++ b/src/main/java/zowe/client/sdk/zosjobs/methods/JobGet.java @@ -18,7 +18,7 @@ import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; import zowe.client.sdk.utility.EncodeUtils; -import zowe.client.sdk.utility.JsonParserUtil; +import zowe.client.sdk.utility.JsonParserUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zosjobs.JobsConstants; import zowe.client.sdk.zosjobs.input.CommonJobInputData; @@ -258,7 +258,7 @@ public List getCommon(final JobGetInputData getInputData) throws ZosmfReque final String jsonStr = request.executeRequest().getResponsePhrase() .orElseThrow(() -> new IllegalStateException("no get job response phrase")).toString(); - final JSONArray results = JsonParserUtil.parseArray(jsonStr); + final JSONArray results = JsonParserUtils.parseArray(jsonStr); for (final Object jsonObj : results) { jobs.add((Job) JsonParseFactory.buildParser(ParseType.JOB).parseResponse(jsonObj)); } @@ -385,7 +385,7 @@ public List getSpoolFilesCommon(final CommonJobInputData commonInputDat return files; } - final JSONArray results = JsonParserUtil.parseArray(jsonStr); + final JSONArray results = JsonParserUtils.parseArray(jsonStr); for (final Object obj : results) { final JSONObject jsonObj = (JSONObject) obj; files.add((JobFile) JsonParseFactory.buildParser(ParseType.JOB_FILE).parseResponse(jsonObj)); @@ -454,7 +454,7 @@ public Job getStatusCommon(final CommonJobInputData commonInputData) throws Zosm final String jsonStr = request.executeRequest() .getResponsePhrase().orElseThrow(() -> new IllegalStateException("no job get response phrase")) .toString(); - final JSONObject jsonObject = JsonParserUtil.parse(jsonStr); + final JSONObject jsonObject = JsonParserUtils.parse(jsonStr); return (Job) JsonParseFactory.buildParser(ParseType.JOB).parseResponse(jsonObject); } diff --git a/src/main/java/zowe/client/sdk/zosjobs/methods/JobSubmit.java b/src/main/java/zowe/client/sdk/zosjobs/methods/JobSubmit.java index 09c04658d..07a7d943a 100644 --- a/src/main/java/zowe/client/sdk/zosjobs/methods/JobSubmit.java +++ b/src/main/java/zowe/client/sdk/zosjobs/methods/JobSubmit.java @@ -19,7 +19,7 @@ import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; import zowe.client.sdk.utility.EncodeUtils; -import zowe.client.sdk.utility.JsonParserUtil; +import zowe.client.sdk.utility.JsonParserUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zosjobs.JobsConstants; import zowe.client.sdk.zosjobs.input.JobSubmitInputData; @@ -144,7 +144,7 @@ public Job submitJclCommon(final JobSubmitJclInputData submitJclInputData) throw final String jsonStr = request.executeRequest().getResponsePhrase() .orElseThrow(() -> new IllegalStateException("no job jcl submit response phrase")).toString(); - final JSONObject jsonObject = JsonParserUtil.parse(jsonStr); + final JSONObject jsonObject = JsonParserUtils.parse(jsonStr); return (Job) JsonParseFactory.buildParser(ParseType.JOB).parseResponse(jsonObject); } @@ -190,7 +190,7 @@ public Job submitCommon(final JobSubmitInputData submitInputData) throws ZosmfRe final String jsonStr = request.executeRequest().getResponsePhrase() .orElseThrow(() -> new IllegalStateException("no job submit response phrase")).toString(); - final JSONObject jsonObject = JsonParserUtil.parse(jsonStr); + final JSONObject jsonObject = JsonParserUtils.parse(jsonStr); return (Job) JsonParseFactory.buildParser(ParseType.JOB).parseResponse(jsonObject); } diff --git a/src/main/java/zowe/client/sdk/zoslogs/method/ZosLog.java b/src/main/java/zowe/client/sdk/zoslogs/method/ZosLog.java index 7ea8e6f69..35fa0a083 100644 --- a/src/main/java/zowe/client/sdk/zoslogs/method/ZosLog.java +++ b/src/main/java/zowe/client/sdk/zoslogs/method/ZosLog.java @@ -22,7 +22,7 @@ import zowe.client.sdk.rest.ZosmfRequestFactory; import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; -import zowe.client.sdk.utility.JsonParserUtil; +import zowe.client.sdk.utility.JsonParserUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zoslogs.input.ZosLogInputData; import zowe.client.sdk.zoslogs.model.ZosLogItem; @@ -127,7 +127,7 @@ public ZosLogResponse issueCommand(final ZosLogInputData logInputData) throws Zo final String jsonStr = request.executeRequest().getResponsePhrase() .orElseThrow(() -> new IllegalStateException("no zos log response phrase")).toString(); - final JSONObject jsonObject = JsonParserUtil.parse(jsonStr); + final JSONObject jsonObject = JsonParserUtils.parse(jsonStr); JSONArray jsonArray = new JSONArray(); if (jsonObject.get("items") != null) { jsonArray = (JSONArray) jsonObject.get("items"); diff --git a/src/main/java/zowe/client/sdk/zosmfinfo/methods/ZosmfStatus.java b/src/main/java/zowe/client/sdk/zosmfinfo/methods/ZosmfStatus.java index 598350f33..c47829669 100644 --- a/src/main/java/zowe/client/sdk/zosmfinfo/methods/ZosmfStatus.java +++ b/src/main/java/zowe/client/sdk/zosmfinfo/methods/ZosmfStatus.java @@ -18,7 +18,7 @@ import zowe.client.sdk.rest.ZosmfRequestFactory; import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; -import zowe.client.sdk.utility.JsonParserUtil; +import zowe.client.sdk.utility.JsonParserUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zosmfinfo.ZosmfConstants; import zowe.client.sdk.zosmfinfo.response.ZosmfInfoResponse; @@ -83,7 +83,7 @@ public ZosmfInfoResponse get() throws ZosmfRequestException { final String jsonStr = request.executeRequest().getResponsePhrase() .orElseThrow(() -> new IllegalStateException("no z/osmf status response phrase")).toString(); - final JSONObject jsonObject = JsonParserUtil.parse(jsonStr); + final JSONObject jsonObject = JsonParserUtils.parse(jsonStr); return (ZosmfInfoResponse) JsonParseFactory.buildParser(ParseType.ZOSMF_INFO).parseResponse(jsonObject); } diff --git a/src/main/java/zowe/client/sdk/zosmfinfo/methods/ZosmfSystems.java b/src/main/java/zowe/client/sdk/zosmfinfo/methods/ZosmfSystems.java index b7f39b1fe..f12d4b259 100644 --- a/src/main/java/zowe/client/sdk/zosmfinfo/methods/ZosmfSystems.java +++ b/src/main/java/zowe/client/sdk/zosmfinfo/methods/ZosmfSystems.java @@ -18,7 +18,7 @@ import zowe.client.sdk.rest.ZosmfRequestFactory; import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; -import zowe.client.sdk.utility.JsonParserUtil; +import zowe.client.sdk.utility.JsonParserUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zosmfinfo.ZosmfConstants; import zowe.client.sdk.zosmfinfo.response.ZosmfSystemsResponse; @@ -83,7 +83,7 @@ public ZosmfSystemsResponse get() throws ZosmfRequestException { final String jsonStr = request.executeRequest().getResponsePhrase() .orElseThrow(() -> new IllegalStateException("no z/osmf info response phrase")).toString(); - final JSONObject jsonObject = JsonParserUtil.parse(jsonStr); + final JSONObject jsonObject = JsonParserUtils.parse(jsonStr); return (ZosmfSystemsResponse) JsonParseFactory.buildParser(ParseType.ZOSMF_SYSTEMS).parseResponse(jsonObject); } diff --git a/src/main/java/zowe/client/sdk/zostso/methods/TsoPing.java b/src/main/java/zowe/client/sdk/zostso/methods/TsoPing.java index 8238ebfec..a51e5f92d 100644 --- a/src/main/java/zowe/client/sdk/zostso/methods/TsoPing.java +++ b/src/main/java/zowe/client/sdk/zostso/methods/TsoPing.java @@ -17,7 +17,7 @@ import zowe.client.sdk.rest.ZosmfRequestFactory; import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; -import zowe.client.sdk.utility.TsoUtil; +import zowe.client.sdk.utility.TsoUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zostso.TsoConstants; import zowe.client.sdk.zostso.response.TsoCommonResponse; @@ -83,14 +83,14 @@ public TsoCommonResponse ping(final String sessionId) throws ZosmfRequestExcepti request.setUrl(url); request.setBody(""); - final String responseStr = TsoUtil.getResponseStr(request); + final String responseStr = TsoUtils.getResponseStr(request); TsoCommonResponse tsoCommonResponse; try { tsoCommonResponse = objectMapper.readValue(responseStr, TsoCommonResponse.class); } catch (JsonProcessingException e) { // check for msdData error message if it exists - final String errMsg = TsoUtil.getMsgDataText(responseStr); + final String errMsg = TsoUtils.getMsgDataText(responseStr); throw new ZosmfRequestException(errMsg.isBlank() ? e.getMessage() : errMsg); } diff --git a/src/main/java/zowe/client/sdk/zostso/methods/TsoReply.java b/src/main/java/zowe/client/sdk/zostso/methods/TsoReply.java index 6dc89e813..30af3ceb8 100644 --- a/src/main/java/zowe/client/sdk/zostso/methods/TsoReply.java +++ b/src/main/java/zowe/client/sdk/zostso/methods/TsoReply.java @@ -15,7 +15,7 @@ import zowe.client.sdk.rest.ZosmfRequestFactory; import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; -import zowe.client.sdk.utility.TsoUtil; +import zowe.client.sdk.utility.TsoUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zostso.TsoConstants; @@ -80,9 +80,9 @@ public String reply(final String sessionId) throws ZosmfRequestException { request.setUrl(url); request.setBody(""); - final String responseStr = TsoUtil.getResponseStr(request); + final String responseStr = TsoUtils.getResponseStr(request); if (responseStr.contains("msgData")) { - final String errMsg = TsoUtil.getMsgDataText(responseStr); + final String errMsg = TsoUtils.getMsgDataText(responseStr); throw new ZosmfRequestException(errMsg); } return responseStr; diff --git a/src/main/java/zowe/client/sdk/zostso/methods/TsoSend.java b/src/main/java/zowe/client/sdk/zostso/methods/TsoSend.java index 3fd99f9a5..2c7150756 100644 --- a/src/main/java/zowe/client/sdk/zostso/methods/TsoSend.java +++ b/src/main/java/zowe/client/sdk/zostso/methods/TsoSend.java @@ -15,7 +15,7 @@ import zowe.client.sdk.rest.ZosmfRequestFactory; import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; -import zowe.client.sdk.utility.TsoUtil; +import zowe.client.sdk.utility.TsoUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zostso.TsoConstants; @@ -83,7 +83,7 @@ public String sendCommand(final String sessionId, final String command) throws Z request.setUrl(url); request.setBody(body); - return TsoUtil.getResponseStr(request); + return TsoUtils.getResponseStr(request); } } diff --git a/src/main/java/zowe/client/sdk/zostso/methods/TsoStart.java b/src/main/java/zowe/client/sdk/zostso/methods/TsoStart.java index 24e59dd95..8e44ece20 100644 --- a/src/main/java/zowe/client/sdk/zostso/methods/TsoStart.java +++ b/src/main/java/zowe/client/sdk/zostso/methods/TsoStart.java @@ -19,7 +19,7 @@ import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; import zowe.client.sdk.utility.EncodeUtils; -import zowe.client.sdk.utility.TsoUtil; +import zowe.client.sdk.utility.TsoUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zostso.TsoConstants; import zowe.client.sdk.zostso.input.StartTsoInputData; @@ -94,7 +94,7 @@ public TsoStartResponse start(final StartTsoInputData inputData) throws ZosmfReq request.setUrl(url); request.setBody(""); - final String responseStr = TsoUtil.getResponseStr(request); + final String responseStr = TsoUtils.getResponseStr(request); final JsonNode rootNode; try { diff --git a/src/main/java/zowe/client/sdk/zostso/methods/TsoStop.java b/src/main/java/zowe/client/sdk/zostso/methods/TsoStop.java index 93e219f51..ab9c8929a 100644 --- a/src/main/java/zowe/client/sdk/zostso/methods/TsoStop.java +++ b/src/main/java/zowe/client/sdk/zostso/methods/TsoStop.java @@ -17,7 +17,7 @@ import zowe.client.sdk.rest.ZosmfRequestFactory; import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; -import zowe.client.sdk.utility.TsoUtil; +import zowe.client.sdk.utility.TsoUtils; import zowe.client.sdk.utility.ValidateUtils; import zowe.client.sdk.zostso.TsoConstants; import zowe.client.sdk.zostso.response.TsoCommonResponse; @@ -83,14 +83,14 @@ public TsoCommonResponse stop(final String sessionId) throws ZosmfRequestExcepti } request.setUrl(url); - final String responseStr = TsoUtil.getResponseStr(request); + final String responseStr = TsoUtils.getResponseStr(request); TsoCommonResponse tsoCommonResponse; try { tsoCommonResponse = objectMapper.readValue(responseStr, TsoCommonResponse.class); } catch (JsonProcessingException e) { // check for msdData error message if it exists - final String errMsg = TsoUtil.getMsgDataText(responseStr); + final String errMsg = TsoUtils.getMsgDataText(responseStr); throw new ZosmfRequestException(errMsg.isBlank() ? e.getMessage() : errMsg); } diff --git a/src/test/java/zowe/client/sdk/parse/MvsConsoleParseResponseTest.java b/src/test/java/zowe/client/sdk/parse/MvsConsoleParseResponseTest.java deleted file mode 100644 index e59e47c7a..000000000 --- a/src/test/java/zowe/client/sdk/parse/MvsConsoleParseResponseTest.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * This program and the accompanying materials are made available under the terms of the - * Eclipse Public License v2.0 which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-v20.html - * - * SPDX-License-Identifier: EPL-2.0 - * - * Copyright Contributors to the Zowe Project. - */ -package zowe.client.sdk.parse; - -import org.json.simple.JSONObject; -import org.junit.jupiter.api.Test; -import zowe.client.sdk.parse.type.ParseType; -import zowe.client.sdk.zosconsole.response.ZosmfIssueResponse; - -import java.util.HashMap; -import java.util.Map; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertSame; - -/** - * Class containing unit tests for MvsConsoleParseResponse. - * - * @author Frank Giordano - * @version 5.0 - */ -public class MvsConsoleParseResponseTest { - - @Test - public void tstMvsConsoleParseResponseNullFail() { - String msg = ""; - try { - JsonParseFactory.buildParser(ParseType.MVS_CONSOLE).parseResponse((Object) null); - } catch (NullPointerException e) { - msg = e.getMessage(); - } - assertEquals("data is null", msg); - } - - @Test - public void tstMvsConsoleParseResponseSingletonSuccess() { - final JsonParse parser = JsonParseFactory.buildParser(ParseType.MVS_CONSOLE); - final JsonParse parser2 = JsonParseFactory.buildParser(ParseType.MVS_CONSOLE); - assertSame(parser, parser2); - } - - @Test - public void tstMvsConsoleParseResponseSuccess() { - final Map jsonMap = new HashMap<>(); - jsonMap.put("cmd-response", "ver"); - final JSONObject json = new JSONObject(jsonMap); - - final ZosmfIssueResponse response = (ZosmfIssueResponse) JsonParseFactory - .buildParser(ParseType.MVS_CONSOLE).parseResponse(json); - assertEquals("ver", response.getCmdResponse().orElse("n\\a")); - } - -} diff --git a/src/test/java/zowe/client/sdk/rest/QueryConstantsTest.java b/src/test/java/zowe/client/sdk/rest/QueryConstantsTest.java index 8aae39a4b..420f18f4e 100644 --- a/src/test/java/zowe/client/sdk/rest/QueryConstantsTest.java +++ b/src/test/java/zowe/client/sdk/rest/QueryConstantsTest.java @@ -10,7 +10,7 @@ package zowe.client.sdk.rest; import org.junit.jupiter.api.Test; -import zowe.client.sdk.utility.Utils; +import zowe.client.sdk.utility.UtilsTestHelper; /** * Class containing unit test for QueryConstantsTest. @@ -26,7 +26,7 @@ public class QueryConstantsTest { @Test public void tstQueryConstantsClassStructureSuccess() { final String privateConstructorExceptionMsg = "Constants class"; - Utils.validateClass(QueryConstants.class, privateConstructorExceptionMsg); + UtilsTestHelper.validateClass(QueryConstants.class, privateConstructorExceptionMsg); } } diff --git a/src/test/java/zowe/client/sdk/rest/ZosmfHeadersTest.java b/src/test/java/zowe/client/sdk/rest/ZosmfHeadersTest.java index 5516e7861..301c5470e 100644 --- a/src/test/java/zowe/client/sdk/rest/ZosmfHeadersTest.java +++ b/src/test/java/zowe/client/sdk/rest/ZosmfHeadersTest.java @@ -10,7 +10,7 @@ package zowe.client.sdk.rest; import org.junit.jupiter.api.Test; -import zowe.client.sdk.utility.Utils; +import zowe.client.sdk.utility.UtilsTestHelper; /** * Class containing unit test for ZosmfHeaders. @@ -26,7 +26,7 @@ public class ZosmfHeadersTest { @Test public void tstZosmfHeadersClassStructureSuccess() { final String privateConstructorExceptionMsg = "Constants class"; - Utils.validateClass(ZosmfHeaders.class, privateConstructorExceptionMsg); + UtilsTestHelper.validateClass(ZosmfHeaders.class, privateConstructorExceptionMsg); } } diff --git a/src/test/java/zowe/client/sdk/rest/ZoweRequestFactoryTest.java b/src/test/java/zowe/client/sdk/rest/ZoweRequestFactoryTest.java index 6d06b0e18..2a9fd09d9 100644 --- a/src/test/java/zowe/client/sdk/rest/ZoweRequestFactoryTest.java +++ b/src/test/java/zowe/client/sdk/rest/ZoweRequestFactoryTest.java @@ -10,7 +10,7 @@ package zowe.client.sdk.rest; import org.junit.jupiter.api.Test; -import zowe.client.sdk.utility.Utils; +import zowe.client.sdk.utility.UtilsTestHelper; /** * Class containing unit tests for ZoweRequestFactory. @@ -26,7 +26,7 @@ public class ZoweRequestFactoryTest { @Test public void tstZoweRequestFactoryClassStructureSuccess() { final String privateConstructorExceptionMsg = "Factory class"; - Utils.validateClass(ZosmfRequestFactory.class, privateConstructorExceptionMsg); + UtilsTestHelper.validateClass(ZosmfRequestFactory.class, privateConstructorExceptionMsg); } } diff --git a/src/test/java/zowe/client/sdk/utility/ConsoleUtilsTest.java b/src/test/java/zowe/client/sdk/utility/ConsoleUtilsTest.java new file mode 100644 index 000000000..61a925a20 --- /dev/null +++ b/src/test/java/zowe/client/sdk/utility/ConsoleUtilsTest.java @@ -0,0 +1,80 @@ +/* + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright Contributors to the Zowe Project. + */ +package zowe.client.sdk.utility; + +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Class containing unit test for ConsoleUtils. + * + * @author Frank Giordano + * @version 5.0 + */ +public class ConsoleUtilsTest { + + @Test + public void tstConsoleUtilsClassStructureSuccess() { + final String privateConstructorExceptionMsg = "Utility class"; + UtilsTestHelper.validateClass(ConsoleUtils.class, privateConstructorExceptionMsg); + } + + @Test + public void tstConsoleUtilPrivateConstructorThrowsException() { + Constructor constructor = ConsoleUtils.class.getDeclaredConstructors()[0]; + constructor.setAccessible(true); + InvocationTargetException exception = assertThrows( + InvocationTargetException.class, + () -> constructor.newInstance() + ); + assertEquals("java.lang.IllegalStateException: Utility class", exception.getCause().toString()); + } + + @Test + public void tstProcessCmdResponseConvertsCRToLF() { + String input = "LINE1\rLINE2"; + String output = ConsoleUtils.processCmdResponse(input); + assertEquals("LINE1\nLINE2\n", output); + } + + @Test + public void tstProcessCmdResponseAddsTrailingNewline() { + String input = "LINE1"; + String output = ConsoleUtils.processCmdResponse(input); + assertEquals("LINE1\n", output); + } + + @Test + public void tstProcessCmdResponseKeepsExistingNewline() { + String input = "LINE1\n"; + String output = ConsoleUtils.processCmdResponse(input); + assertEquals("LINE1\n", output); + } + + @Test + public void tstProcessCmdResponseBlankString() { + String input = ""; + String output = ConsoleUtils.processCmdResponse(input); + assertEquals("", output); + } + + @Test + public void tstProcessCmdResponseWhitespaceOnly() { + String input = " "; + String output = ConsoleUtils.processCmdResponse(input); + assertEquals(" ", output); // Space is considered blank; no newline appended + } + +} diff --git a/src/test/java/zowe/client/sdk/utility/EncodeUtilsTest.java b/src/test/java/zowe/client/sdk/utility/EncodeUtilsTest.java index 5b41fefcc..21f85c249 100644 --- a/src/test/java/zowe/client/sdk/utility/EncodeUtilsTest.java +++ b/src/test/java/zowe/client/sdk/utility/EncodeUtilsTest.java @@ -25,7 +25,7 @@ public class EncodeUtilsTest { @Test public void tstEncodeUtilsClassStructureSuccess() { final String privateConstructorExceptionMsg = "Utility class"; - Utils.validateClass(EncodeUtils.class, privateConstructorExceptionMsg); + UtilsTestHelper.validateClass(EncodeUtils.class, privateConstructorExceptionMsg); } } diff --git a/src/test/java/zowe/client/sdk/utility/FileUtilsTest.java b/src/test/java/zowe/client/sdk/utility/FileUtilsTest.java index 363071b92..21a6e7800 100644 --- a/src/test/java/zowe/client/sdk/utility/FileUtilsTest.java +++ b/src/test/java/zowe/client/sdk/utility/FileUtilsTest.java @@ -28,7 +28,7 @@ public class FileUtilsTest { @Test public void tstFileUtilsClassStructureSuccess() { final String privateConstructorExceptionMsg = "Utility class"; - Utils.validateClass(FileUtils.class, privateConstructorExceptionMsg); + UtilsTestHelper.validateClass(FileUtils.class, privateConstructorExceptionMsg); } @Test diff --git a/src/test/java/zowe/client/sdk/utility/JsonParserUtilTest.java b/src/test/java/zowe/client/sdk/utility/JsonParserUtilsTest.java similarity index 70% rename from src/test/java/zowe/client/sdk/utility/JsonParserUtilTest.java rename to src/test/java/zowe/client/sdk/utility/JsonParserUtilsTest.java index fee6fbcf6..36edd47be 100644 --- a/src/test/java/zowe/client/sdk/utility/JsonParserUtilTest.java +++ b/src/test/java/zowe/client/sdk/utility/JsonParserUtilsTest.java @@ -12,20 +12,20 @@ import org.junit.jupiter.api.Test; /** - * Class containing unit test for JsonParserUtil. + * Class containing unit test for JsonParserUtils. * * @author Frank Giordano * @version 5.0 */ -public class JsonParserUtilTest { +public class JsonParserUtilsTest { /** * Validate class structure */ @Test - public void tstEncodeUtilsClassStructureSuccess() { + public void tstJsonParserUtilsClassStructureSuccess() { final String privateConstructorExceptionMsg = "Utility class"; - Utils.validateClass(JsonParserUtil.class, privateConstructorExceptionMsg); + UtilsTestHelper.validateClass(JsonParserUtils.class, privateConstructorExceptionMsg); } } diff --git a/src/test/java/zowe/client/sdk/utility/TsoUtilsTest.java b/src/test/java/zowe/client/sdk/utility/TsoUtilsTest.java new file mode 100644 index 000000000..a2f5fcd14 --- /dev/null +++ b/src/test/java/zowe/client/sdk/utility/TsoUtilsTest.java @@ -0,0 +1,108 @@ +/* + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright Contributors to the Zowe Project. + */ +package zowe.client.sdk.utility; + +import org.junit.jupiter.api.Test; +import zowe.client.sdk.rest.Response; +import zowe.client.sdk.rest.ZosmfRequest; +import zowe.client.sdk.rest.exception.ZosmfRequestException; + +import java.util.Optional; +import java.util.OptionalInt; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Class containing unit test for TsoUtils. + * + * @author Frank Giordano + * @version 5.0 + */ +class TsoUtilsTest { + + /** + * Validate class structure + */ + @Test + public void tstTsoUtilsClassStructureSuccess() { + final String privateConstructorExceptionMsg = "Utility class"; + UtilsTestHelper.validateClass(TsoUtils.class, privateConstructorExceptionMsg); + } + + @Test + void tstGetResponseStrSuccess() throws Exception { + // Mock Response + Response mockResponse = mock(Response.class); + when(mockResponse.getResponsePhrase()).thenReturn(Optional.of("OK")); + when(mockResponse.getStatusCode()).thenReturn(OptionalInt.of(200)); + + // Mock Request + ZosmfRequest mockRequest = mock(ZosmfRequest.class); + when(mockRequest.executeRequest()).thenReturn(mockResponse); + + String result = TsoUtils.getResponseStr(mockRequest); + assertEquals("OK", result); + } + + @Test + void tstGetResponseStrMissingResponsePhraseFailure() throws Exception { + Response mockResponse = mock(Response.class); + when(mockResponse.getResponsePhrase()).thenReturn(Optional.empty()); + when(mockResponse.getStatusCode()).thenReturn(OptionalInt.of(200)); + + ZosmfRequest mockRequest = mock(ZosmfRequest.class); + when(mockRequest.executeRequest()).thenReturn(mockResponse); + + assertThrows(ZosmfRequestException.class, () -> TsoUtils.getResponseStr(mockRequest)); + } + + @Test + void tstGetResponseStrErrorStatusCodeFailure() throws Exception { + Response mockResponse = mock(Response.class); + when(mockResponse.getResponsePhrase()).thenReturn(Optional.of("ERROR")); + when(mockResponse.getStatusCode()).thenReturn(OptionalInt.of(500)); + + ZosmfRequest mockRequest = mock(ZosmfRequest.class); + when(mockRequest.executeRequest()).thenReturn(mockResponse); + + assertThrows(ZosmfRequestException.class, () -> TsoUtils.getResponseStr(mockRequest)); + } + + @Test + void testGetMsgDataTextMessageSuccess() { + String json = "{ \"msgData\": [{ \"messageText\": \"Error occurred\" }] }"; + String result = TsoUtils.getMsgDataText(json); + assertEquals("Error occurred", result); + } + + @Test + void tstGetMsgDataTextMultipleMessagesSuccess() { + String json = "{ \"msgData\": [{ \"messageText\": \"One\" }, { \"messageText\": \"Two\" }] }"; + String result = TsoUtils.getMsgDataText(json); + assertTrue(result.contains("One")); + assertTrue(result.contains("Two")); + } + + @Test + void tstGetMsgDataTextCustomMessageSuccess() { + String json = "{ \"msgData\": { \"custom\": \"data\" } }"; + String result = TsoUtils.getMsgDataText(json); + assertTrue(result.contains("custom")); + } + + @Test + void tstGetMsgDataTextInvalidJsonFailure() { + String result = TsoUtils.getMsgDataText("Not a JSON"); + assertEquals("", result); + } + +} diff --git a/src/test/java/zowe/client/sdk/utility/Utils.java b/src/test/java/zowe/client/sdk/utility/UtilsTestHelper.java similarity index 86% rename from src/test/java/zowe/client/sdk/utility/Utils.java rename to src/test/java/zowe/client/sdk/utility/UtilsTestHelper.java index 8a18eca91..7ba90819b 100644 --- a/src/test/java/zowe/client/sdk/utility/Utils.java +++ b/src/test/java/zowe/client/sdk/utility/UtilsTestHelper.java @@ -23,20 +23,39 @@ /** * Utility class for the test package. * - * @author James Kostrewski + * @author Frank Giordano * @version 5.0 */ -public final class Utils { +public final class UtilsTestHelper { - private static final Logger LOG = LoggerFactory.getLogger(Utils.class); + private static final Logger LOG = LoggerFactory.getLogger(UtilsTestHelper.class); /** * Private constructor defined to avoid instantiation of class */ - private Utils() { + private UtilsTestHelper() { throw new IllegalStateException("Utility class"); } + /** + * Assertion checks class conforms to being set as final, one private constructor, and with all static methods. + * + * @param name class object + */ + public static void validateClass(Class name, String privateConstructorMsg) { + try { + UtilsTestHelper.assertUtilityClassWellDefined(name); + } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | IllegalStateException e) { + markFailed("error " + e); + } catch (InvocationTargetException e) { + if (e.getTargetException() instanceof IllegalStateException) { + assertEquals(privateConstructorMsg, e.getTargetException().getMessage()); + } else { + markFailed("error " + e); + } + } + } + /** * Verifies that a class defined as final and uses all static methods is well-defined. * @@ -73,24 +92,13 @@ private static void assertUtilityClassWellDefined(final Class clazz) throws N } /** - * Assertion checks class conforms to being set as final, one private constructor, and with all static methods. + * Helper method to mark the test as failed. * - * @param name class object + * @param error error message */ - public static void validateClass(Class name, String privateConstructorMsg) { - try { - Utils.assertUtilityClassWellDefined(name); - } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | IllegalStateException e) { - LOG.error("error " + e); - fail(); - } catch (InvocationTargetException e) { - if (e.getTargetException() instanceof IllegalStateException) { - assertEquals(privateConstructorMsg, e.getTargetException().getMessage()); - } else { - LOG.error("error " + e); - fail(); - } - } + private static void markFailed(String error) { + LOG.error(error); + fail(); } } diff --git a/src/test/java/zowe/client/sdk/utility/ValidateUtilsTest.java b/src/test/java/zowe/client/sdk/utility/ValidateUtilsTest.java index 374e0e068..d283b3475 100644 --- a/src/test/java/zowe/client/sdk/utility/ValidateUtilsTest.java +++ b/src/test/java/zowe/client/sdk/utility/ValidateUtilsTest.java @@ -28,7 +28,7 @@ public class ValidateUtilsTest { @Test public void tstValidateUtilsClassStructureSuccess() { final String privateConstructorExceptionMsg = "Utility class"; - Utils.validateClass(ValidateUtils.class, privateConstructorExceptionMsg); + UtilsTestHelper.validateClass(ValidateUtils.class, privateConstructorExceptionMsg); } @Test diff --git a/src/test/java/zowe/client/sdk/zosconsole/ConsoleConstantsTest.java b/src/test/java/zowe/client/sdk/zosconsole/ConsoleConstantsTest.java index 964838c35..5c0ee6e43 100644 --- a/src/test/java/zowe/client/sdk/zosconsole/ConsoleConstantsTest.java +++ b/src/test/java/zowe/client/sdk/zosconsole/ConsoleConstantsTest.java @@ -10,7 +10,7 @@ package zowe.client.sdk.zosconsole; import org.junit.jupiter.api.Test; -import zowe.client.sdk.utility.Utils; +import zowe.client.sdk.utility.UtilsTestHelper; /** * Class containing unit test for ConsoleConstantsTest. @@ -26,7 +26,7 @@ public class ConsoleConstantsTest { @Test public void tstConsoleConstantsClassStructureSuccess() { final String privateConstructorExceptionMsg = "Constants class"; - Utils.validateClass(ConsoleConstants.class, privateConstructorExceptionMsg); + UtilsTestHelper.validateClass(ConsoleConstants.class, privateConstructorExceptionMsg); } } diff --git a/src/test/java/zowe/client/sdk/zosconsole/method/IssueConsoleTest.java b/src/test/java/zowe/client/sdk/zosconsole/method/ConsoleCmdTest.java similarity index 79% rename from src/test/java/zowe/client/sdk/zosconsole/method/IssueConsoleTest.java rename to src/test/java/zowe/client/sdk/zosconsole/method/ConsoleCmdTest.java index c994e0703..60babb8d8 100644 --- a/src/test/java/zowe/client/sdk/zosconsole/method/IssueConsoleTest.java +++ b/src/test/java/zowe/client/sdk/zosconsole/method/ConsoleCmdTest.java @@ -24,8 +24,8 @@ import zowe.client.sdk.rest.ZosmfRequestFactory; import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; -import zowe.client.sdk.zosconsole.input.IssueConsoleInputData; -import zowe.client.sdk.zosconsole.response.ConsoleResponse; +import zowe.client.sdk.zosconsole.input.ConsoleCmdInputData; +import zowe.client.sdk.zosconsole.response.ConsoleCmdResponse; import java.util.HashMap; import java.util.Map; @@ -37,12 +37,12 @@ import static org.mockito.Mockito.withSettings; /** - * Class containing unit tests for IssueCommand. + * Class containing unit tests for ConsoleCmd class. * * @author Frank Giordano * @version 5.0 */ -public class IssueConsoleTest { +public class ConsoleCmdTest { private final ZosConnection connection = ZosConnectionFactory .createBasicConnection("1", "1", "1", "1"); @@ -62,9 +62,9 @@ public void tstIssueConsoleIssueCommandCmdResponseSuccess() throws ZosmfRequestE final JSONObject json = new JSONObject(jsonMap); Mockito.when(mockJsonGetRequest.executeRequest()).thenReturn( new Response(json, 200, "success")); - final IssueConsole issueCommand = new IssueConsole(connection, mockJsonGetRequest); - final ConsoleResponse response = issueCommand.issueCommand("command"); - assertEquals("student", response.getCommandResponse().orElse("n/a")); + final ConsoleCmd consoleCmd = new ConsoleCmd(connection, mockJsonGetRequest); + final ConsoleCmdResponse response = consoleCmd.issueCommand("command"); + assertEquals("student", response.getCmdResponse().orElse("n/a")); } @Test @@ -82,12 +82,12 @@ public void tstIssueConsoleIssueCommandCmdResponseToggleTokenSuccess() throws Zo doCallRealMethod().when(mockJsonGetRequestAuth).setUrl(any()); doCallRealMethod().when(mockJsonGetRequestAuth).getHeaders(); - final IssueConsole issueCommand = new IssueConsole(tokenConnection, mockJsonGetRequestAuth); + final ConsoleCmd consoleCmd = new ConsoleCmd(tokenConnection, mockJsonGetRequestAuth); - ConsoleResponse response = issueCommand.issueCommand("command"); + ConsoleCmdResponse response = consoleCmd.issueCommand("command"); assertEquals("{X-CSRF-ZOSMF-HEADER=true, Content-Type=application/json}", mockJsonGetRequestAuth.getHeaders().toString()); - assertEquals("student", response.getCommandResponse().orElse("n/a")); + assertEquals("student", response.getCmdResponse().orElse("n/a")); } @Test @@ -96,7 +96,7 @@ public void tstIssueConsoleIssueCommandCmdResponseWithInvalidBasePathFailure() { .createBasicConnection("1", "1", "1", "1", "consoles//"); // Create a mock request to verify URL final ZosmfRequest request = ZosmfRequestFactory.buildRequest(connection, ZosmfRequestType.PUT_JSON); - final IssueConsole issueCommand = new IssueConsole(connection, request); + final ConsoleCmd issueCommand = new ConsoleCmd(connection, request); assertThrows(IllegalArgumentException.class, () -> issueCommand.issueCommand("command")); } @@ -107,9 +107,9 @@ public void tstIssueConsoleIssueCommandCmdResponseWithEmptyStringSuccess() throw final JSONObject json = new JSONObject(jsonMap); Mockito.when(mockJsonGetRequest.executeRequest()).thenReturn( new Response(json, 200, "success")); - final IssueConsole issueCommand = new IssueConsole(connection, mockJsonGetRequest); - final ConsoleResponse response = issueCommand.issueCommand("command"); - assertEquals("", response.getCommandResponse().orElse("n/a")); + final ConsoleCmd consoleCmd = new ConsoleCmd(connection, mockJsonGetRequest); + final ConsoleCmdResponse response = consoleCmd.issueCommand("command"); + assertEquals("", response.getCmdResponse().orElse("n/a")); } @Test @@ -119,12 +119,12 @@ public void tstIssueConsoleIssueCommandCmdResponseWithEmptyStringAndIsProcessReq final JSONObject json = new JSONObject(jsonMap); Mockito.when(mockJsonGetRequest.executeRequest()).thenReturn( new Response(json, 200, "success")); - final IssueConsole issueCommand = new IssueConsole(connection, mockJsonGetRequest); - final IssueConsoleInputData consoleInputData = new IssueConsoleInputData("command"); + final ConsoleCmd consoleCmd = new ConsoleCmd(connection, mockJsonGetRequest); + final ConsoleCmdInputData consoleInputData = new ConsoleCmdInputData("command"); consoleInputData.setProcessResponse(); consoleInputData.setSolKey("foo"); - final ConsoleResponse response = issueCommand.issueCommandCommon("consolename", consoleInputData); - assertEquals("", response.getCommandResponse().orElse("n/a")); + final ConsoleCmdResponse response = consoleCmd.issueCommandCommon("consolename", consoleInputData); + assertEquals("", response.getCmdResponse().orElse("n/a")); } @Test @@ -134,8 +134,8 @@ public void tstIssueConsoleIssueCommandCmdResponseUrlSuccess() throws ZosmfReque final JSONObject json = new JSONObject(jsonMap); Mockito.when(mockJsonGetRequest.executeRequest()).thenReturn( new Response(json, 200, "success")); - IssueConsole issueCommand = new IssueConsole(connection, mockJsonGetRequest); - ConsoleResponse response = issueCommand.issueCommand("command"); + ConsoleCmd consoleCmd = new ConsoleCmd(connection, mockJsonGetRequest); + ConsoleCmdResponse response = consoleCmd.issueCommand("command"); assertEquals("student", response.getCmdResponseUrl().orElse("n/a")); } @@ -158,7 +158,7 @@ public void tstIssueConsoleIssueCommandHttpErrorFailure() throws ZosmfRequestExc Mockito.when(mockJsonGetRequest.executeRequest()).thenThrow(new IllegalStateException(errorMsg)); - final IssueConsole issueCommand = new IssueConsole(connection, mockJsonGetRequest); + final ConsoleCmd issueCommand = new ConsoleCmd(connection, mockJsonGetRequest); try { issueCommand.issueCommand("test"); } catch (IllegalStateException e) { @@ -172,7 +172,7 @@ public void tstIssueConsoleIssueCommandHttpErrorFailure() throws ZosmfRequestExc @Test public void tstIssueConsoleNullConnectionFailure() { try { - new IssueConsole(null); + new ConsoleCmd(null); } catch (NullPointerException e) { assertEquals("connection is null", e.getMessage()); } @@ -182,8 +182,8 @@ public void tstIssueConsoleNullConnectionFailure() { public void tstIssueConsoleSecondaryConstructorWithValidRequestType() { ZosConnection connection = Mockito.mock(ZosConnection.class); ZosmfRequest request = Mockito.mock(PutJsonZosmfRequest.class); - IssueConsole issueConsole = new IssueConsole(connection, request); - assertNotNull(issueConsole); + ConsoleCmd consoleCmd = new ConsoleCmd(connection, request); + assertNotNull(consoleCmd); } @Test @@ -191,7 +191,7 @@ public void tstIssueConsoleSecondaryConstructorWithNullConnection() { ZosmfRequest request = Mockito.mock(PutJsonZosmfRequest.class); NullPointerException exception = assertThrows( NullPointerException.class, - () -> new IssueConsole(null, request) + () -> new ConsoleCmd(null, request) ); assertEquals("connection is null", exception.getMessage()); } @@ -201,7 +201,7 @@ public void tstIssueConsoleSecondaryConstructorWithNullRequest() { ZosConnection connection = Mockito.mock(ZosConnection.class); NullPointerException exception = assertThrows( NullPointerException.class, - () -> new IssueConsole(connection, null) + () -> new ConsoleCmd(connection, null) ); assertEquals("request is null", exception.getMessage()); } @@ -212,7 +212,7 @@ public void tstIssueConsoleSecondaryConstructorWithInvalidRequestType() { ZosmfRequest request = Mockito.mock(ZosmfRequest.class); // Not a PutJsonZosmfRequest IllegalStateException exception = assertThrows( IllegalStateException.class, - () -> new IssueConsole(connection, request) + () -> new ConsoleCmd(connection, request) ); assertEquals("PUT_JSON request type required", exception.getMessage()); } @@ -220,8 +220,8 @@ public void tstIssueConsoleSecondaryConstructorWithInvalidRequestType() { @Test public void tstIssueConsolePrimaryConstructorWithValidConnection() { ZosConnection connection = Mockito.mock(ZosConnection.class); - IssueConsole issueConsole = new IssueConsole(connection); - assertNotNull(issueConsole); + ConsoleCmd consoleCmd = new ConsoleCmd(connection); + assertNotNull(consoleCmd); } @Test @@ -229,7 +229,7 @@ public void tstDsnCopyPrimaryConstructorWithNullConnection() { // When/Then NullPointerException exception = assertThrows( NullPointerException.class, - () -> new IssueConsole(null) + () -> new ConsoleCmd(null) ); assertEquals("connection is null", exception.getMessage()); } diff --git a/src/test/java/zowe/client/sdk/zosconsole/method/ConsoleGetTest.java b/src/test/java/zowe/client/sdk/zosconsole/method/ConsoleGetTest.java new file mode 100644 index 000000000..e06e40f0e --- /dev/null +++ b/src/test/java/zowe/client/sdk/zosconsole/method/ConsoleGetTest.java @@ -0,0 +1,205 @@ +/* + * This program and the accompanying materials are made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-v20.html + * + * SPDX-License-Identifier: EPL-2.0 + * + * Copyright Contributors to the Zowe Project. + */ +package zowe.client.sdk.zosconsole.method; + +import kong.unirest.core.Cookie; +import org.json.simple.JSONObject; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import zowe.client.sdk.core.ZosConnection; +import zowe.client.sdk.core.ZosConnectionFactory; +import zowe.client.sdk.rest.GetJsonZosmfRequest; +import zowe.client.sdk.rest.Response; +import zowe.client.sdk.rest.ZosmfRequest; +import zowe.client.sdk.rest.exception.ZosmfRequestException; +import zowe.client.sdk.zosconsole.response.ConsoleGetResponse; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.withSettings; + +/** + * Class containing unit tests for ConsoleGet class. + * + * @author Frank Giordano + * @version 5.0 + */ +public class ConsoleGetTest { + + private final ZosConnection connection = + ZosConnectionFactory.createBasicConnection("1", "1", "1", "1"); + private final ZosConnection tokenConnection = + ZosConnectionFactory.createTokenConnection("1", "1", new Cookie("hello=hello")); + private GetJsonZosmfRequest mockJsonGetRequest; + + @BeforeEach + public void init() { + mockJsonGetRequest = Mockito.mock(GetJsonZosmfRequest.class, withSettings().useConstructor(connection)); + } + + @Test + public void tstConsoleGetPrimaryConstructorSuccess() { + ConsoleGet consoleGet = new ConsoleGet(connection); + assertNotNull(consoleGet); + } + + @Test + public void tstConsoleGetPrimaryConstructorNullConnectionFailure() { + NullPointerException exception = assertThrows( + NullPointerException.class, + () -> new ConsoleGet(null) + ); + assertEquals("connection is null", exception.getMessage()); + } + + @Test + public void tstConsoleGetSecondaryConstructorSuccess() { + ZosConnection connection = Mockito.mock(ZosConnection.class); + ZosmfRequest request = Mockito.mock(GetJsonZosmfRequest.class); + ConsoleGet consoleGet = new ConsoleGet(connection, request); + assertNotNull(consoleGet); + } + + @Test + public void tstConsoleGetSecondaryConstructorNullConnectionFailure() { + ZosmfRequest request = Mockito.mock(GetJsonZosmfRequest.class); + NullPointerException exception = assertThrows( + NullPointerException.class, + () -> new ConsoleGet(null, request) + ); + assertEquals("connection is null", exception.getMessage()); + } + + @Test + public void tstConsoleGetSecondaryConstructorNullRequestFailure() { + ZosConnection connection = Mockito.mock(ZosConnection.class); + NullPointerException exception = assertThrows( + NullPointerException.class, + () -> new ConsoleGet(connection, null) + ); + assertEquals("request is null", exception.getMessage()); + } + + @Test + public void tstConsoleGetSecondaryConstructorInvalidRequestTypeFailure() { + ZosConnection connection = Mockito.mock(ZosConnection.class); + ZosmfRequest request = Mockito.mock(ZosmfRequest.class); + IllegalStateException exception = assertThrows( + IllegalStateException.class, + () -> new ConsoleGet(connection, request) + ); + assertEquals("GET_JSON request type required", exception.getMessage()); + } + + @Test + public void tstConsoleGetToggleTokenSuccess() throws Exception { + final Map jsonMap = new HashMap<>(); + jsonMap.put("cmd-response", "LINE1\rLINE2"); + final JSONObject json = new JSONObject(jsonMap); + + // Create mock with token constructor + GetJsonZosmfRequest mockJsonGetRequestAuth = Mockito.mock( + GetJsonZosmfRequest.class, + withSettings().useConstructor(tokenConnection) + ); + + Mockito.when(mockJsonGetRequestAuth.executeRequest()).thenReturn( + new Response(json, 200, "{ \"cmd-response\": \"LINE1\\rLINE2\" }") + ); + + // Enable real header and URL handling + doCallRealMethod().when(mockJsonGetRequestAuth).setHeaders(anyMap()); + doCallRealMethod().when(mockJsonGetRequestAuth).setStandardHeaders(); + doCallRealMethod().when(mockJsonGetRequestAuth).setUrl(any()); + doCallRealMethod().when(mockJsonGetRequestAuth).getHeaders(); + + // Execute ConsoleGet with token + final ConsoleGet consoleGet = new ConsoleGet(tokenConnection, mockJsonGetRequestAuth); + ConsoleGetResponse response = consoleGet.getResponse("respKey"); + + // 🔍 Validate token header injection + assertEquals("{X-CSRF-ZOSMF-HEADER=true, Content-Type=application/json}", + mockJsonGetRequestAuth.getHeaders().toString()); + + // 🔍 Validate response was processed + assertEquals("LINE1\nLINE2\n", response.getCmdResponse().orElse("n/a")); + } + + + @Test + public void tstConsoleGetResponseSuccess() throws ZosmfRequestException { + final Map jsonMap = new HashMap<>(); + jsonMap.put("cmd-response", "LINE1\rLINE2"); + final JSONObject json = new JSONObject(jsonMap); + Mockito.when(mockJsonGetRequest.executeRequest()).thenReturn( + new Response(json, 200, "{ \"cmd-response\": \"LINE1\\rLINE2\" }") + ); + ConsoleGet consoleGet = new ConsoleGet(connection, mockJsonGetRequest); + ConsoleGetResponse response = consoleGet.getResponse("respKey"); + assertEquals("LINE1\nLINE2\n", response.getCmdResponse().orElse("n/a")); + } + + @Test + public void tstConsoleGetResponseBlankContentSuccess() throws ZosmfRequestException { + final Map jsonMap = new HashMap<>(); + jsonMap.put("cmd-response", ""); + final JSONObject json = new JSONObject(jsonMap); + Mockito.when(mockJsonGetRequest.executeRequest()).thenReturn( + new Response(json, 200, "{ \"cmd-response\": \"\" }") + ); + ConsoleGet consoleGet = new ConsoleGet(connection, mockJsonGetRequest); + ConsoleGetResponse response = consoleGet.getResponse("respKey"); + assertEquals("", response.getCmdResponse().orElse("n/a")); + } + + @Test + public void tstConsoleGetMissingResponsePhraseFailure() { + ConsoleGet consoleGet = new ConsoleGet(connection, mockJsonGetRequest); + try { + Mockito.when(mockJsonGetRequest.executeRequest()).thenReturn( + new Response(null, 200, null) + ); + consoleGet.getResponse("respKey"); + } catch (Exception e) { + assertEquals("java.lang.IllegalStateException: no issue console response phrase", e.toString()); + } + } + + @Test + public void tstConsoleGetJsonParseFailure() throws Exception { + Mockito.when(mockJsonGetRequest.executeRequest()).thenReturn( + new Response(null, 200, "{ invalid json }") + ); + ConsoleGet consoleGet = new ConsoleGet(connection, mockJsonGetRequest); + assertThrows(IllegalStateException.class, () -> consoleGet.getResponse("respKey")); + } + + @Test + public void tstConsoleGetUsesZosmfRequestFactoryWhenRequestNull() { + ZosConnection mockConn = Mockito.mock(ZosConnection.class); + Mockito.when(mockConn.getZosmfUrl()).thenReturn("http://test"); + ConsoleGet consoleGet = new ConsoleGet(mockConn); + try { + consoleGet.getResponse("respKey"); + } catch (Exception e) { + // expected due to no response phrase + } + // No crash = success + assertTrue(true); + } + +} + diff --git a/src/test/java/zowe/client/sdk/zosfiles/uss/methods/UssListTest.java b/src/test/java/zowe/client/sdk/zosfiles/uss/methods/UssListTest.java index 36a80a090..fcdf40b37 100644 --- a/src/test/java/zowe/client/sdk/zosfiles/uss/methods/UssListTest.java +++ b/src/test/java/zowe/client/sdk/zosfiles/uss/methods/UssListTest.java @@ -20,7 +20,7 @@ import zowe.client.sdk.rest.Response; import zowe.client.sdk.rest.ZosmfRequest; import zowe.client.sdk.rest.exception.ZosmfRequestException; -import zowe.client.sdk.utility.JsonParserUtil; +import zowe.client.sdk.utility.JsonParserUtils; import zowe.client.sdk.zosfiles.ZosFilesConstants; import zowe.client.sdk.zosfiles.uss.input.UssListInputData; import zowe.client.sdk.zosfiles.uss.input.UssListZfsInputData; @@ -179,7 +179,7 @@ public void tstUssListFileListNullResponseFailure() throws ZosmfRequestException @Test public void tstUssListFileListSuccess() throws ZosmfRequestException { - final JSONObject json = JsonParserUtil.parse(dataForFileList); + final JSONObject json = JsonParserUtils.parse(dataForFileList); Mockito.when(mockJsonGetRequest.executeRequest()).thenReturn( new Response(json, 200, "success")); final UssList ussList = new UssList(connection, mockJsonGetRequest); @@ -211,7 +211,7 @@ public void tstUssListFileListSuccess() throws ZosmfRequestException { @Test public void tstUssListFileListPartialSuccess() throws ZosmfRequestException { - final JSONObject json = JsonParserUtil.parse(partialDataForFileList); + final JSONObject json = JsonParserUtils.parse(partialDataForFileList); Mockito.when(mockJsonGetRequest.executeRequest()).thenReturn( new Response(json, 200, "success")); final UssList ussList = new UssList(connection, mockJsonGetRequest); @@ -243,7 +243,7 @@ public void tstUssListFileListPartialSuccess() throws ZosmfRequestException { @Test public void tstUssListEmptyFileListSuccess() throws ZosmfRequestException { - final JSONObject json = JsonParserUtil.parse("{}"); + final JSONObject json = JsonParserUtils.parse("{}"); Mockito.when(mockJsonGetRequest.executeRequest()).thenReturn( new Response(json, 200, "success")); final UssList ussList = new UssList(connection, mockJsonGetRequest); @@ -264,7 +264,7 @@ public void tstUssListEmptyFileListWithJsonObjectSuccess() throws ZosmfRequestEx @Test public void tstUssListZfsListSuccess() throws ZosmfRequestException { - final JSONObject json = JsonParserUtil.parse(dataForZfsList); + final JSONObject json = JsonParserUtils.parse(dataForZfsList); Mockito.when(mockJsonGetRequest.executeRequest()).thenReturn( new Response(json, 200, "success")); final UssList ussList = new UssList(connection, mockJsonGetRequest); diff --git a/src/test/java/zowe/client/sdk/zosjobs/JobsConstantsTest.java b/src/test/java/zowe/client/sdk/zosjobs/JobsConstantsTest.java index 305d84c39..cead5f7cd 100644 --- a/src/test/java/zowe/client/sdk/zosjobs/JobsConstantsTest.java +++ b/src/test/java/zowe/client/sdk/zosjobs/JobsConstantsTest.java @@ -10,7 +10,7 @@ package zowe.client.sdk.zosjobs; import org.junit.jupiter.api.Test; -import zowe.client.sdk.utility.Utils; +import zowe.client.sdk.utility.UtilsTestHelper; /** * Class containing unit test for JobsConstantsTest. @@ -26,7 +26,7 @@ public class JobsConstantsTest { @Test public void tstJobsConstantsClassStructureSuccess() { final String privateConstructorExceptionMsg = "Constants class"; - Utils.validateClass(JobsConstants.class, privateConstructorExceptionMsg); + UtilsTestHelper.validateClass(JobsConstants.class, privateConstructorExceptionMsg); } } diff --git a/src/test/java/zowe/client/sdk/zosmfinfo/ZosmfConstantsTest.java b/src/test/java/zowe/client/sdk/zosmfinfo/ZosmfConstantsTest.java index 48c3d5769..e3594d084 100644 --- a/src/test/java/zowe/client/sdk/zosmfinfo/ZosmfConstantsTest.java +++ b/src/test/java/zowe/client/sdk/zosmfinfo/ZosmfConstantsTest.java @@ -10,7 +10,7 @@ package zowe.client.sdk.zosmfinfo; import org.junit.jupiter.api.Test; -import zowe.client.sdk.utility.Utils; +import zowe.client.sdk.utility.UtilsTestHelper; /** * Class containing unit test for ZosmfConstantsTest. @@ -26,7 +26,7 @@ public class ZosmfConstantsTest { @Test public void tstZosmfConstantsClassStructureSuccess() { final String privateConstructorExceptionMsg = "Constants class"; - Utils.validateClass(ZosmfConstants.class, privateConstructorExceptionMsg); + UtilsTestHelper.validateClass(ZosmfConstants.class, privateConstructorExceptionMsg); } } diff --git a/src/test/java/zowe/client/sdk/zostso/TsoConstantsTest.java b/src/test/java/zowe/client/sdk/zostso/TsoConstantsTest.java index ecc820b3d..9a7e6ba4e 100644 --- a/src/test/java/zowe/client/sdk/zostso/TsoConstantsTest.java +++ b/src/test/java/zowe/client/sdk/zostso/TsoConstantsTest.java @@ -10,7 +10,7 @@ package zowe.client.sdk.zostso; import org.junit.jupiter.api.Test; -import zowe.client.sdk.utility.Utils; +import zowe.client.sdk.utility.UtilsTestHelper; /** * Class containing unit test for TsoConstantsTest. @@ -26,7 +26,7 @@ public class TsoConstantsTest { @Test public void tstTsoConstantsClassStructureSuccess() { final String privateConstructorExceptionMsg = "Constants class"; - Utils.validateClass(TsoConstants.class, privateConstructorExceptionMsg); + UtilsTestHelper.validateClass(TsoConstants.class, privateConstructorExceptionMsg); } } diff --git a/src/test/java/zowe/client/sdk/zostso/methods/TsoPingTest.java b/src/test/java/zowe/client/sdk/zostso/methods/TsoPingTest.java index aee36b455..696aee7f0 100644 --- a/src/test/java/zowe/client/sdk/zostso/methods/TsoPingTest.java +++ b/src/test/java/zowe/client/sdk/zostso/methods/TsoPingTest.java @@ -18,7 +18,7 @@ import zowe.client.sdk.rest.PutJsonZosmfRequest; import zowe.client.sdk.rest.ZosmfRequest; import zowe.client.sdk.rest.exception.ZosmfRequestException; -import zowe.client.sdk.utility.TsoUtil; +import zowe.client.sdk.utility.TsoUtils; import zowe.client.sdk.zostso.response.TsoCommonResponse; import java.util.Map; @@ -56,8 +56,8 @@ public void tstTsoPingSetsCorrectUrlSuccess() throws Exception { doCallRealMethod().when(mockPutRequest).setUrl(any()); doCallRealMethod().when(mockPutRequest).getUrl(); - try (MockedStatic mockResponseUtil = mockStatic(TsoUtil.class)) { - mockResponseUtil.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic mockResponseUtil = mockStatic(TsoUtils.class)) { + mockResponseUtil.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{}"); final TsoPing tsoPing = new TsoPing(mockConnection, mockPutRequest); @@ -81,8 +81,8 @@ public void tstTsoPingSetsCorrectHeadersSuccess() throws Exception { doCallRealMethod().when(putJsonZosmfRequest).setHeaders(anyMap()); doCallRealMethod().when(putJsonZosmfRequest).getHeaders(); - try (MockedStatic mockResponseUtil = mockStatic(TsoUtil.class)) { - mockResponseUtil.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic mockResponseUtil = mockStatic(TsoUtils.class)) { + mockResponseUtil.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{}"); final TsoPing tsoPing = new TsoPing(mockConnection, putJsonZosmfRequest); @@ -103,8 +103,8 @@ public void tstTsoPingSetsCorrectHeadersSuccess() throws Exception { @Test public void tstTsoPingSuccess() throws ZosmfRequestException { final String payload = "{\"servletKey\":\"ZOSMFAD-71-xyz\",\"ver\":\"0100\",\"timeout\":false,\"reused\":true}"; - try (MockedStatic mockResponseUtil = mockStatic(TsoUtil.class)) { - mockResponseUtil.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic mockResponseUtil = mockStatic(TsoUtils.class)) { + mockResponseUtil.when(() -> TsoUtils.getResponseStr(any())) .thenReturn(payload); final TsoPing tsoPing = new TsoPing(mockConnection, mockPutRequest); diff --git a/src/test/java/zowe/client/sdk/zostso/methods/TsoReplyTest.java b/src/test/java/zowe/client/sdk/zostso/methods/TsoReplyTest.java index bd88984ef..40f5f1451 100644 --- a/src/test/java/zowe/client/sdk/zostso/methods/TsoReplyTest.java +++ b/src/test/java/zowe/client/sdk/zostso/methods/TsoReplyTest.java @@ -20,7 +20,7 @@ import zowe.client.sdk.rest.ZosmfRequestFactory; import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; -import zowe.client.sdk.utility.TsoUtil; +import zowe.client.sdk.utility.TsoUtils; import java.util.Map; @@ -57,8 +57,8 @@ public void tstTsoReplySetsCorrectUrlSuccess() throws Exception { doCallRealMethod().when(mockPutRequest).setUrl(any()); doCallRealMethod().when(mockPutRequest).getUrl(); - try (MockedStatic mockResponseUtil = mockStatic(TsoUtil.class)) { - mockResponseUtil.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic mockResponseUtil = mockStatic(TsoUtils.class)) { + mockResponseUtil.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{}"); final TsoReply tsoReply = new TsoReply(mockConnection, mockPutRequest); @@ -82,8 +82,8 @@ public void tstTsoReplySetsCorrectHeadersSuccess() throws Exception { doCallRealMethod().when(putJsonZosmfRequest).setHeaders(anyMap()); doCallRealMethod().when(putJsonZosmfRequest).getHeaders(); - try (MockedStatic mockResponseUtil = mockStatic(TsoUtil.class)) { - mockResponseUtil.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic mockResponseUtil = mockStatic(TsoUtils.class)) { + mockResponseUtil.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{}"); final TsoReply tsoReply = new TsoReply(mockConnection, putJsonZosmfRequest); @@ -103,8 +103,8 @@ public void tstTsoReplySetsCorrectHeadersSuccess() throws Exception { */ @Test public void tstTsoReplyThrowsZosmfRequestExceptionOnResponseErrorFailure() { - try (MockedStatic mockResponseUtil = mockStatic(TsoUtil.class)) { - mockResponseUtil.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic mockResponseUtil = mockStatic(TsoUtils.class)) { + mockResponseUtil.when(() -> TsoUtils.getResponseStr(any())) .thenThrow(new ZosmfRequestException("Reply failed")); final TsoReply tsoReply = new TsoReply(mockConnection, mockPutRequest); @@ -123,8 +123,8 @@ public void tstTsoReplyThrowsZosmfRequestExceptionOnResponseErrorFailure() { */ @Test public void tstTsoReplySuccess() { - try (MockedStatic mockResponseUtil = mockStatic(TsoUtil.class)) { - mockResponseUtil.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic mockResponseUtil = mockStatic(TsoUtils.class)) { + mockResponseUtil.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{\"status\":\"ok\"}"); final TsoReply tsoReply = new TsoReply(mockConnection, mockPutRequest); @@ -181,11 +181,11 @@ public void tstAlternativeConstructorWrongRequestTypeFailure() { @Test public void tstTsoReplyBuildsNewRequestWhenRequestIsNullSuccess() throws Exception { try (MockedStatic factoryMock = mockStatic(ZosmfRequestFactory.class); - MockedStatic responseMock = mockStatic(TsoUtil.class)) { + MockedStatic responseMock = mockStatic(TsoUtils.class)) { factoryMock.when(() -> ZosmfRequestFactory.buildRequest(mockConnection, ZosmfRequestType.PUT_JSON)) .thenReturn(mockPutRequest); - responseMock.when(() -> TsoUtil.getResponseStr(mockPutRequest)) + responseMock.when(() -> TsoUtils.getResponseStr(mockPutRequest)) .thenReturn("{\"status\":\"ok\"}"); final TsoReply tsoReply = new TsoReply(mockConnection); diff --git a/src/test/java/zowe/client/sdk/zostso/methods/TsoSendTest.java b/src/test/java/zowe/client/sdk/zostso/methods/TsoSendTest.java index 1cda858cb..d052d0ca1 100644 --- a/src/test/java/zowe/client/sdk/zostso/methods/TsoSendTest.java +++ b/src/test/java/zowe/client/sdk/zostso/methods/TsoSendTest.java @@ -20,7 +20,7 @@ import zowe.client.sdk.rest.ZosmfRequestFactory; import zowe.client.sdk.rest.exception.ZosmfRequestException; import zowe.client.sdk.rest.type.ZosmfRequestType; -import zowe.client.sdk.utility.TsoUtil; +import zowe.client.sdk.utility.TsoUtils; import java.util.Map; @@ -65,8 +65,8 @@ public void tstTsoSendCommandSetsCorrectHeadersSuccess() throws Exception { doCallRealMethod().when(putJsonZosmfRequest).setHeaders(anyMap()); doCallRealMethod().when(putJsonZosmfRequest).getHeaders(); - try (MockedStatic mockResponseUtil = mockStatic(TsoUtil.class)) { - mockResponseUtil.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic mockResponseUtil = mockStatic(TsoUtils.class)) { + mockResponseUtil.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{}"); final TsoSend tsoSend = new TsoSend(mockConnection, putJsonZosmfRequest); @@ -91,8 +91,8 @@ public void tstTsoSendCommandSetsCorrectUrlAndBodySuccess() throws Exception { doCallRealMethod().when(mockPutRequest).setBody(any()); doCallRealMethod().when(mockPutRequest).getUrl(); - try (MockedStatic mockResponseUtil = mockStatic(TsoUtil.class)) { - mockResponseUtil.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic mockResponseUtil = mockStatic(TsoUtils.class)) { + mockResponseUtil.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{}"); final TsoSend tsoSend = new TsoSend(mockConnection, mockPutRequest); @@ -111,11 +111,11 @@ public void tstTsoSendCommandSetsCorrectUrlAndBodySuccess() throws Exception { @Test public void tstTsoSendCommandBuildsNewRequestWhenRequestIsNullSuccess() throws Exception { try (MockedStatic factoryMock = mockStatic(ZosmfRequestFactory.class); - MockedStatic responseMock = mockStatic(TsoUtil.class)) { + MockedStatic responseMock = mockStatic(TsoUtils.class)) { factoryMock.when(() -> ZosmfRequestFactory.buildRequest(mockConnection, ZosmfRequestType.PUT_JSON)) .thenReturn(mockPutRequest); - responseMock.when(() -> TsoUtil.getResponseStr(eq(mockPutRequest))) + responseMock.when(() -> TsoUtils.getResponseStr(eq(mockPutRequest))) .thenReturn("{\"status\":\"ok\"}"); final TsoSend tsoSend = new TsoSend(mockConnection); @@ -132,8 +132,8 @@ public void tstTsoSendCommandBuildsNewRequestWhenRequestIsNullSuccess() throws E */ @Test public void tstTsoSendCommandThrowsZosmfRequestExceptionOnResponseErrorFailure() { - try (MockedStatic responseMock = mockStatic(TsoUtil.class)) { - responseMock.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic responseMock = mockStatic(TsoUtils.class)) { + responseMock.when(() -> TsoUtils.getResponseStr(any())) .thenThrow(new ZosmfRequestException("Send failed")); final TsoSend tsoSend = new TsoSend(mockConnection, mockPutRequest); @@ -152,8 +152,8 @@ public void tstTsoSendCommandThrowsZosmfRequestExceptionOnResponseErrorFailure() */ @Test public void tstTsoSendCommandSuccess() throws Exception { - try (MockedStatic responseMock = mockStatic(TsoUtil.class)) { - responseMock.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic responseMock = mockStatic(TsoUtils.class)) { + responseMock.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{\"status\":\"ok\"}"); final TsoSend tsoSend = new TsoSend(mockConnection, mockPutRequest); diff --git a/src/test/java/zowe/client/sdk/zostso/methods/TsoStartTest.java b/src/test/java/zowe/client/sdk/zostso/methods/TsoStartTest.java index 4e974ddb1..c4ca5bf0c 100644 --- a/src/test/java/zowe/client/sdk/zostso/methods/TsoStartTest.java +++ b/src/test/java/zowe/client/sdk/zostso/methods/TsoStartTest.java @@ -20,7 +20,7 @@ import zowe.client.sdk.rest.Response; import zowe.client.sdk.rest.ZosmfRequest; import zowe.client.sdk.rest.exception.ZosmfRequestException; -import zowe.client.sdk.utility.TsoUtil; +import zowe.client.sdk.utility.TsoUtils; import zowe.client.sdk.zostso.input.StartTsoInputData; import zowe.client.sdk.zostso.response.TsoStartResponse; @@ -71,8 +71,8 @@ public void tstTsoStartSuccess() throws Exception { "1047", "24", "80", "ACCT123", "4096"); // Mock ResponseUtil static call - try (var mocked = Mockito.mockStatic(TsoUtil.class)) { - mocked.when(() -> TsoUtil.getResponseStr(any())) + try (var mocked = Mockito.mockStatic(TsoUtils.class)) { + mocked.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{\"servletKey\":\"mySession123\"}"); final TsoStartResponse result = tsoStart.start(input); @@ -92,8 +92,8 @@ public void tstTsoStartInvalidJsonThrowsZosmfRequestExceptionWithCorrectMessageF StartTsoInputData input = new StartTsoInputData("LOGONPROC", "UTF-8", "1047", "24", "80", "ACCT123", "4096"); - try (var mocked = Mockito.mockStatic(TsoUtil.class)) { - mocked.when(() -> TsoUtil.getResponseStr(any())) + try (var mocked = Mockito.mockStatic(TsoUtils.class)) { + mocked.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("invalid json response"); ZosmfRequestException ex = assertThrows(ZosmfRequestException.class, @@ -111,8 +111,8 @@ public void tstTsoStartNullServletKeyThrowsZosmfRequestExceptionWithCorrectMessa StartTsoInputData input = new StartTsoInputData("LOGONPROC", "UTF-8", "1047", "24", "80", "ACCT123", "4096"); - try (var mocked = Mockito.mockStatic(TsoUtil.class)) { - mocked.when(() -> TsoUtil.getResponseStr(any())) + try (var mocked = Mockito.mockStatic(TsoUtils.class)) { + mocked.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{\"servletKey\":\"null\"}"); ZosmfRequestException ex = assertThrows(ZosmfRequestException.class, @@ -129,8 +129,8 @@ public void tstTsoStartMissingServletKeyThrowsZosmfRequestExceptionFailure() { StartTsoInputData input = new StartTsoInputData("LOGONPROC", "UTF-8", "1047", "24", "80", "ACCT123", "4096"); - try (var mocked = Mockito.mockStatic(TsoUtil.class)) { - mocked.when(() -> TsoUtil.getResponseStr(any())) + try (var mocked = Mockito.mockStatic(TsoUtils.class)) { + mocked.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{}\n"); ZosmfRequestException ex = assertThrows(ZosmfRequestException.class, @@ -147,8 +147,8 @@ public void tstTsoStartEmptyServletKeyThrowsZosmfRequestExceptionFailure() { StartTsoInputData input = new StartTsoInputData("LOGONPROC", "UTF-8", "1047", "24", "80", "ACCT123", "4096"); - try (var mocked = Mockito.mockStatic(TsoUtil.class)) { - mocked.when(() -> TsoUtil.getResponseStr(any())) + try (var mocked = Mockito.mockStatic(TsoUtils.class)) { + mocked.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{\"servletKey\":\"\"}"); ZosmfRequestException ex = assertThrows(ZosmfRequestException.class, @@ -198,8 +198,8 @@ public void tstTsoStartWithStartTsoInputDataSettersSuccess() throws Exception { inputData.setRegionSize("4096"); final String jsonResponse = "{\"servletKey\":\"TSO12345\"}"; - mockStatic(TsoUtil.class); - when(TsoUtil.getResponseStr(eq(mockRequest))).thenReturn(jsonResponse); + mockStatic(TsoUtils.class); + when(TsoUtils.getResponseStr(eq(mockRequest))).thenReturn(jsonResponse); final TsoStartResponse result = tsoStart.start(inputData); assertEquals("TSO12345", result.getSessionId()); @@ -250,8 +250,8 @@ public void tstTsoStartSetsCorrectHeadersSuccess() throws Exception { doCallRealMethod().when(postJsonZosmfRequest).getHeaders(); // Mock response to return servletKey - try (var mocked = Mockito.mockStatic(TsoUtil.class)) { - mocked.when(() -> TsoUtil.getResponseStr(any())) + try (var mocked = Mockito.mockStatic(TsoUtils.class)) { + mocked.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{\"servletKey\":\"SERVKEY123\"}"); tsoStart = new TsoStart(mockConnection, postJsonZosmfRequest); diff --git a/src/test/java/zowe/client/sdk/zostso/methods/TsoStopTest.java b/src/test/java/zowe/client/sdk/zostso/methods/TsoStopTest.java index 7cb753c09..febc96124 100644 --- a/src/test/java/zowe/client/sdk/zostso/methods/TsoStopTest.java +++ b/src/test/java/zowe/client/sdk/zostso/methods/TsoStopTest.java @@ -18,7 +18,7 @@ import zowe.client.sdk.rest.DeleteJsonZosmfRequest; import zowe.client.sdk.rest.ZosmfRequest; import zowe.client.sdk.rest.exception.ZosmfRequestException; -import zowe.client.sdk.utility.TsoUtil; +import zowe.client.sdk.utility.TsoUtils; import zowe.client.sdk.zostso.response.TsoCommonResponse; import java.util.Map; @@ -56,8 +56,8 @@ public void tstTsoStopSetsCorrectUrlSuccess() throws Exception { doCallRealMethod().when(mockDeleteRequest).setUrl(any()); doCallRealMethod().when(mockDeleteRequest).getUrl(); - try (MockedStatic mockResponseUtil = mockStatic(TsoUtil.class)) { - mockResponseUtil.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic mockResponseUtil = mockStatic(TsoUtils.class)) { + mockResponseUtil.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{}"); final TsoStop tsoStop = new TsoStop(mockConnection, mockDeleteRequest); @@ -81,8 +81,8 @@ public void tstTsoStopSetsCorrectHeadersSuccess() throws Exception { doCallRealMethod().when(deleteJsonZosmfRequest).setHeaders(anyMap()); doCallRealMethod().when(deleteJsonZosmfRequest).getHeaders(); - try (MockedStatic mockResponseUtil = mockStatic(TsoUtil.class)) { - mockResponseUtil.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic mockResponseUtil = mockStatic(TsoUtils.class)) { + mockResponseUtil.when(() -> TsoUtils.getResponseStr(any())) .thenReturn("{}"); final TsoStop tsoStop = new TsoStop(mockConnection, deleteJsonZosmfRequest); @@ -103,8 +103,8 @@ public void tstTsoStopSetsCorrectHeadersSuccess() throws Exception { @Test public void tstTsoStopSuccess() throws ZosmfRequestException { final String payload = "{\"servletKey\":\"ZOSMFAD-71-aabcaaaf\",\"ver\":\"0100\",\"timeout\":false,\"reused\":true}"; - try (MockedStatic mockResponseUtil = mockStatic(TsoUtil.class)) { - mockResponseUtil.when(() -> TsoUtil.getResponseStr(any())) + try (MockedStatic mockResponseUtil = mockStatic(TsoUtils.class)) { + mockResponseUtil.when(() -> TsoUtils.getResponseStr(any())) .thenReturn(payload); final TsoStop tsoStop = new TsoStop(mockConnection, mockDeleteRequest);