Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize response #285

Merged
merged 8 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ All notable changes to the Zowe Client Python SDK will be documented in this fil

- Added logger class to core SDK [#185](https://github.com/zowe/zowe-client-python-sdk/issues/185)
- Added classes for handling `Datasets`, `USSFiles`, and `FileSystems` in favor of the single Files class. [#264](https://github.com/zowe/zowe-client-python-sdk/issues/264)
- Refactored tests into proper folders and files [#265](https://github.com/zowe/zowe-client-python-sdk/issues/265)
- Refactored tests into proper folders and files and add more tests [#265](https://github.com/zowe/zowe-client-python-sdk/issues/265)
- **Breaking:** Standardized `response` outputs based on `Content-Type`. [#266](https://github.com/zowe/zowe-client-python-sdk/issues/266)

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion samples/SampleFiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
# -----------------------------------------------------
print("...content of a file\n")
my_file_content = my_files.get_file_content("/z/tm891807/file.txt")
print(my_file_content["response"])
print(my_file_content)
16 changes: 9 additions & 7 deletions src/core/zowe/core_for_zowe_sdk/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,15 @@ def __normalize_response(self):

Returns
-------
A bytes object if the response content type is application/octet-stream,
a normalized JSON for the request response otherwise
Response object at the format based on Content-Type header:
- `bytes` when the response is binary data
- object when the response is JSON text
- `str` when the response is plain text
"""
if self.response.headers.get("Content-Type") == "application/octet-stream":
contentType = self.response.headers.get("Content-Type")
if contentType == "application/octet-stream":
return self.response.content
elif contentType and contentType.startswith("application/json"):
return "" if self.response.text == "" else self.response.json()
else:
try:
return self.response.json()
except:
return {"response": self.response.text}
return self.response.text
4 changes: 2 additions & 2 deletions src/zos_jobs/zowe/zos_jobs_for_zowe_sdk/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@
os.makedirs(output_dir, exist_ok=True)
output_file = os.path.join(output_dir, job_name, job_id, "jcl.txt")
data_spool_file = self.get_jcl_text(job_correlator)
dataset_content = data_spool_file["response"]
dataset_content = data_spool_file

Check warning on line 405 in src/zos_jobs/zowe/zos_jobs_for_zowe_sdk/jobs.py

View check run for this annotation

Codecov / codecov/patch

src/zos_jobs/zowe/zos_jobs_for_zowe_sdk/jobs.py#L405

Added line #L405 was not covered by tests
with open(output_file, "w", encoding="utf-8") as out_file:
out_file.write(dataset_content)

Expand All @@ -416,7 +416,7 @@

output_file = os.path.join(output_dir, job_name, job_id, stepname, ddname)
data_spool_file = self.get_spool_file_contents(job_correlator, spoolfile_id)
dataset_content = data_spool_file["response"]
dataset_content = data_spool_file

Check warning on line 419 in src/zos_jobs/zowe/zos_jobs_for_zowe_sdk/jobs.py

View check run for this annotation

Codecov / codecov/patch

src/zos_jobs/zowe/zos_jobs_for_zowe_sdk/jobs.py#L419

Added line #L419 was not covered by tests
with open(output_file, "w", encoding="utf-8") as out_file:
out_file.write(dataset_content)

Expand Down
15 changes: 8 additions & 7 deletions tests/integration/test_zos_files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Integration tests for the Zowe Python SDK z/OS Files package."""

import json
import os
import unittest
Expand Down Expand Up @@ -66,7 +67,7 @@ def test_list_members_should_return_a_list_of_members(self):
def test_get_dsn_content_should_return_content_from_dataset(self):
"""Executing get_dsn_content should return content from dataset."""
command_output = self.files.get_dsn_content(self.test_member_jcl)
self.assertIsInstance(command_output["response"], str)
self.assertIsInstance(command_output, str)

def test_get_dsn_content_streamed_should_return_response_content(self):
"""Executing get_dsn_content_streamed should return response object from the server."""
Expand All @@ -86,14 +87,14 @@ def test_get_file_content_streamed_should_return_response_content(self):
def test_write_to_dsn_should_be_possible(self):
"""Executing write_to_dsn should be possible."""
command_output = self.files.write_to_dsn(self.test_member_generic, "HELLO WORLD")
self.assertTrue(command_output["response"] == "")
self.assertTrue(command_output == "")

def test_copy_uss_to_dataset_should_be_possible(self):
"""Executing copy_uss_to_dataset should be possible."""
command_output = self.files.copy_uss_to_dataset(
self.files_fixtures["TEST_USS"], self.files_fixtures["TEST_PDS"] + "(TEST2)", replace=True
)
self.assertTrue(command_output["response"] == "")
self.assertTrue(command_output == "")

def test_copy_dataset_or_member_should_be_possible(self):
"""Executing copy_dataset_or_member should be possible."""
Expand All @@ -105,7 +106,7 @@ def test_copy_dataset_or_member_should_be_possible(self):
"replace": True,
}
command_output = self.files.copy_dataset_or_member(**test_case)
self.assertTrue(command_output["response"] == "")
self.assertTrue(command_output == "")

def test_mount_unmount_zfs_file_system(self):
"""Mounting a zfs filesystem should be possible"""
Expand All @@ -119,19 +120,19 @@ def test_mount_unmount_zfs_file_system(self):
command_output = self.files.mount_file_system(
self.test2_zfs_file_system, mount_point, self.mount_zfs_file_system_options
)
self.assertTrue(command_output["response"] == "")
self.assertTrue(command_output == "")

# List a zfs file system
command_output = self.files.list_unix_file_systems(file_system_name=self.test2_zfs_file_system.upper())
self.assertTrue(len(command_output["items"]) > 0)

# Unmount file system
command_output = self.files.unmount_file_system(self.test2_zfs_file_system)
self.assertTrue(command_output["response"] == "")
self.assertTrue(command_output == "")

# Delete file system
command_output = self.files.delete_zFS_file_system(self.test2_zfs_file_system)
self.assertTrue(command_output["response"] == "")
self.assertTrue(command_output == "")

def test_upload_download_delete_dataset(self):
self.files.upload_file_to_dsn(SAMPLE_JCL_FIXTURE_PATH, self.test_ds_upload)
Expand Down
32 changes: 20 additions & 12 deletions tests/unit/core/test_request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import unittest
from unittest import mock

from zowe.core_for_zowe_sdk import (
RequestHandler,
exceptions
)
from zowe.core_for_zowe_sdk import RequestHandler, exceptions


class TestRequestHandlerClass(unittest.TestCase):
"""RequestHandler class unit tests."""
Expand All @@ -24,35 +22,36 @@ def test_object_should_be_instance_of_class(self):
@mock.patch("logging.Logger.debug")
@mock.patch("logging.Logger.error")
@mock.patch("requests.Session.send")
def test_perform_streamed_request(self, mock_send_request, mock_logger_error: mock.MagicMock, mock_logger_debug: mock.MagicMock):
def test_perform_streamed_request(
self, mock_send_request, mock_logger_error: mock.MagicMock, mock_logger_debug: mock.MagicMock
):
"""Performing a streamed request should call 'send_request' method"""
mock_send_request.return_value = mock.Mock(status_code=200)
request_handler = RequestHandler(self.session_arguments)
request_handler.perform_request("GET", {"url": "https://www.zowe.org"}, stream = True)
request_handler.perform_request("GET", {"url": "https://www.zowe.org"}, stream=True)

mock_logger_error.assert_not_called()
mock_logger_debug.assert_called()
self.assertIn("Request method: GET", mock_logger_debug.call_args[0][0])
mock_send_request.assert_called_once()
self.assertTrue(mock_send_request.call_args[1]["stream"])


@mock.patch("logging.Logger.error")
def test_logger_unmatched_status_code(self, mock_logger_error: mock.MagicMock):
"""Test logger with unexpected status code"""
request_handler = RequestHandler(self.session_arguments)
try:
request_handler.perform_request("GET", {"url": "https://www.zowe.org"}, expected_code= [0], stream = True)
request_handler.perform_request("GET", {"url": "https://www.zowe.org"}, expected_code=[0], stream=True)
except exceptions.UnexpectedStatus:
mock_logger_error.assert_called_once()
self.assertIn("The status code", mock_logger_error.call_args[0][0])

@mock.patch("logging.Logger.error")
def test_logger_perform_request_invalid_method(self, mock_logger_error: mock.MagicMock):
"""Test logger with invalid request method"""
request_handler = RequestHandler(self.session_arguments)
try:
request_handler.perform_request("Invalid method", {"url": "https://www.zowe.org"}, stream = True)
request_handler.perform_request("Invalid method", {"url": "https://www.zowe.org"}, stream=True)
except exceptions.InvalidRequestMethod:
mock_logger_error.assert_called_once()
self.assertIn("Invalid HTTP method input", mock_logger_error.call_args[0][0])
Expand All @@ -63,8 +62,17 @@ def test_logger_invalid_status_code(self, mock_send_request, mock_logger_error:
mock_send_request.return_value = mock.Mock(ok=False)
request_handler = RequestHandler(self.session_arguments)
try:
request_handler.perform_request("GET", {"url": "https://www.zowe.org"}, stream = True)
request_handler.perform_request("GET", {"url": "https://www.zowe.org"}, stream=True)
except exceptions.RequestFailed:
mock_logger_error.assert_called_once()
self.assertIn("HTTP Request has failed", mock_logger_error.call_args[0][0])
mock_logger_error.assert_called_once
mock_logger_error.assert_called_once

@mock.patch("requests.Session.send")
def test_empty_text(self, mock_send_request):
mock_send_request.return_value = mock.Mock(
headers={"Content-Type": "application/json"}, text="", status_code=200
)
request_handler = RequestHandler(self.session_arguments)
response = request_handler.perform_request("GET", {"url": "https://www.zowe.org"})
self.assertTrue(response == "")
Loading