2023-10-10
(C) Questetra, Inc. (MIT License)
3
2
This item downloads the specified file in OneDrive.
この工程は、OneDrive 内の指定ファイルをダウンロードします。
https://support.questetra.com/bpmn-icons/service-task-onedrive-file-download/
https://support.questetra.com/ja/bpmn-icons/service-task-onedrive-file-download/
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAADXUlEQVRYR8WXW0hUURSG/33GbC5e
8pJJF6nM6YKUmsFAIAZSONOM4FtqL/mQVg9GhXR5iCIiEAqF9KEMsvKpi06OZoRPGaRmVoSGU4om
aqaMzeXYOLPjTJ7R41w8k8bZLwNn1r/Wt9dee+29CSQeRHR8vVEpd7vzGTAHKKXpADYDiJnTTwEY
IIR0u+FuYxnmCYx6uxjfSwKotE2JbuoqB0EJALkYpwBYUNQwRHbDZtKNBtMEBVBqG0ooSAUAlcjA
i81sBPSs3ZRXE0gfEECuM94mlJb+Y2CBjBJSzTbpT/jz5RdAoTU+BGjBSgSf90EeOUz6wsU+fQBW
cuaLg/nLhABgbs2rV3bmQm8EtHRhTXgBPNUOV/8yCk4st42BbBu/O7wAitzGmyAoE+tlWXYUtxzN
htOcj78AeqNS4aI/Q9jneH5Ng0x1DCLkMlhZFz59m0ZZ9UfPr4jBOmQkjmtWHgC5rqGIUFIXTHih
QI3UzVHoH7HhsCYROzZF+JgPjNqxq/iViPgAJfQo25T3wAOg1BnvUkqP+VNGKsLQXpmF5PXCXmR1
zKJvyAqL3Yn4qHCoN0ZCHs6gvm0YxRXdS0IQQmrtTfpiD4BC2/gOANfffUbjVQ1yMtYKvnNZGJlg
kZESDeVqGSy2WbzoHPdkpdtswcnKniUBAHQ7TIYMHmBywcEiEPfey0FSgsL7zWJzosc8jazdcQI7
l5tiwvIbUaowTE47UfXMjMqnX4OBTDlMhlgegC62TNmgwp0z6UhLjsaqMMbzN5f25o4x5O5bhwhF
WNBZ2mdcyDn3Gu/NloB2DpOBBAToqs7GzqRIr/j7BIu+4V9I2xqN2KhwMSlGbcsgTlV9EAXgswSW
Bp135pQCrV3jOJSZICowb2RjXWjtHEfh9U5/OsES+BThj8daqOQyb+q/DFuRkbImJADOmKuNg+Xt
ePOZm6NgzBehv21YfzETek0iGIZgxun2OMjeEx8yACfIv/wWLR1jAq1gGwZqROePqLE/NRZjUzOo
ezmE++V7ER8tbv35aFzm0o63+YALGlEorfhS0XZsSVSKysTgmANX6nr92QpbMWch7WHEncFSH8dc
FiS9kPALJemVjIeQ9FL6PzIR8rWch5D0YcJDSPo0E3QQqR6nolreMoz+ACVfiTBvFfnjAAAAAElF
TkSuQmCC
{
const oauth2 = httpClient.createAuthSettingOAuth2(
'OneDrive',
'https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize',
'https://login.microsoftonline.com/organizations/oauth2/v2.0/token',
'https://graph.microsoft.com/Files.ReadWrite.All offline_access',
'client_id',
'client_secret',
'access_token'
);
configs.putObject('conf_OAuth2', oauth2);
if (isFileUrlFixed) {
configs.put('conf_FileUrl', fileUrl);
} else {
const fileUrlDef = engine.createDataDefinition('ファイル URL', 1, 'q_fileUrl', 'STRING_TEXTFIELD');
engine.setData(fileUrlDef, fileUrl);
configs.putObject('conf_FileUrl', fileUrlDef);
}
configs.put('conf_SaveAs', saveAs);
const filesDef = engine.createDataDefinition('ファイル', 2, 'q_files', 'FILE');
engine.setData(filesDef, files);
configs.putObject('conf_Files', filesDef);
return filesDef;
};
/**
* 異常系のテスト
* @param func
* @param errorMsg
*/
const assertError = (func, errorMsg) => {
try {
func();
fail();
} catch (e) {
expect(e.toString()).toEqual(errorMsg);
}
};
/**
* ファイル URL を文字型データ項目で指定し、値が空でエラー
*/
test('File URL is blank', () => {
prepareConfigs(null, false, '', null);
assertError(main, 'File URL is blank.');
});
/**
* ドライブアイテム取得の GET リクエストのテスト
* @param {Object} request
* @param request.url
* @param request.method
* @param sharingUrl
*/
const assertGetDriveItemRequest = ({ url, method }, sharingUrl) => {
const encodedUrl = encodeSharingUrl(sharingUrl);
const expectedUrl = `${GRAPH_URI}shares/${encodedUrl}/driveItem`
+ `?select=${encodeURIComponent('name,@microsoft.graph.downloadUrl')}`;
expect(url).toEqual(expectedUrl);
expect(method).toEqual('GET');
};
/**
* ドライブアイテム取得の GET リクエストでエラー
*/
test('Fail in 1st GET request', () => {
const fileUrl = 'https://test-my.sharepoint.com/personal/aaa/Documents/test1.txt';
prepareConfigs(fileUrl, false, '', null);
httpClient.setRequestHandler((request) => {
assertGetDriveItemRequest(request, fileUrl);
return httpClient.createHttpResponse(400, 'application/json', '{}');
});
assertError(main, 'Failed to get drive item. status: 400');
});
/**
* ダウンロード URL の取得に失敗 (フォルダ URL を指定した場合など)
*/
test('Fail to get download url', () => {
const fileUrl = 'https://test-my.sharepoint.com/personal/aaa/Documents/folder1';
prepareConfigs(fileUrl, false, '', null);
httpClient.setRequestHandler((request) => {
assertGetDriveItemRequest(request, fileUrl);
return httpClient.createHttpResponse(200, 'application/json', '{"name": "folder1"}');
});
assertError(main, 'Failed to get download url.');
});
/**
* ドライブアイテム取得の GET リクエストのレスポンスを準備
* @param fileName
* @param downloadUrl
* @return {Object} responseObj
*/
const prepareDriveItemResponse = (fileName, downloadUrl) => {
return JSON.stringify(
{
"name": fileName,
"@microsoft.graph.downloadUrl": downloadUrl
}
);
};
/**
* ファイルダウンロードの GET リクエストのテスト
* @param {Object} request
* @param request.url
* @param request.method
* @param downloadUrl
*/
const assertDownloadFileRequest = ({ url, method }, downloadUrl) => {
expect(url).toEqual(downloadUrl);
expect(method).toEqual('GET');
};
/**
* ファイルダウンロードの GET リクエストでエラー
*/
test('Fail in 2nd GET request', () => {
const fileUrl = 'https://test-my.sharepoint.com/personal/aaa/Documents/test1.txt';
prepareConfigs(fileUrl, false, '', null);
let reqCount = 0;
const fileName = 'test1.txt';
const downloadUrl = 'https://test-my.sharepoint.com/personal/downloadUrlWithTempAuth1'
httpClient.setRequestHandler((request) => {
if (reqCount === 0) {
assertGetDriveItemRequest(request, fileUrl);
reqCount++;
return httpClient.createHttpResponse(200, 'application/json', prepareDriveItemResponse(fileName, downloadUrl));
}
assertDownloadFileRequest(request, downloadUrl);
return httpClient.createHttpResponse(400, 'application/json', '{}');
});
assertError(main, 'Failed to download. status: 400');
});
/**
* ファイルのテスト
* @param file
* @param name
* @param contentType
* @param encoding
* @param body
*/
const assertFile = (file, name, contentType, encoding, body) => {
expect(file.getName()).toEqual(name);
expect(file.getContentType()).toEqual(contentType);
let text = '';
fileRepository.readFile(file, encoding, line => text += line + '\n');
expect(text).toEqual(body);
};
/**
* 成功
* ファイル URL は文字型データ項目
* 保存ファイル名の指定なし
* ファイル型データ項目に事前ファイルの添付なし
*/
test('Succeed - set file URL by data item, no saveAs, no existing files', () => {
const fileUrl = 'https://test-my.sharepoint.com/personal/aaa/Documents/test1.txt';
const filesDef = prepareConfigs(fileUrl, false, '', null);
let reqCount = 0;
const fileName = 'test1.txt';
const downloadUrl = 'https://test-my.sharepoint.com/personal/downloadUrlWithTempAuth1'
httpClient.setRequestHandler((request) => {
if (reqCount === 0) {
assertGetDriveItemRequest(request, fileUrl);
reqCount++;
return httpClient.createHttpResponse(200, 'application/json', prepareDriveItemResponse(fileName, downloadUrl));
}
assertDownloadFileRequest(request, downloadUrl);
return httpClient.createHttpResponse(200, 'text/plain; charset=UTF-8', 'こんにちは');
});
expect(main()).toEqual(undefined);
const files = engine.findData(filesDef);
expect(files.size()).toEqual(1);
assertFile(files.get(0), fileName, 'text/plain', 'UTF-8', 'こんにちは\n');
});
/**
* 成功
* ファイル URL は固定値で指定
* 保存ファイル名の指定あり
* ファイル型データ項目に事前ファイルの添付あり
*/
test('Succeed - set file URL as fixes value, with saveAs, with existing files', () => {
const fileUrl = 'https://test-my.sharepoint.com/personal/aaa/Documents/test2.txt';
const saveAs = '新しいファイル.txt';
let files = new java.util.ArrayList();
files.add(engine.createQfile('test.html', 'text/html; charset=UTF-8', ''));
files.add(engine.createQfile('test.xml', 'text/xml; charset=UTF-16', 'さようなら'));
const filesDef = prepareConfigs(fileUrl, true, saveAs, files);
let reqCount = 0;
const fileName = 'test1.txt';
const downloadUrl = 'https://test-my.sharepoint.com/personal/downloadUrlWithTempAuth2'
httpClient.setRequestHandler((request) => {
if (reqCount === 0) {
assertGetDriveItemRequest(request, fileUrl);
reqCount++;
return httpClient.createHttpResponse(200, 'application/json', prepareDriveItemResponse(fileName, downloadUrl));
}
assertDownloadFileRequest(request, downloadUrl);
return httpClient.createHttpResponse(200, 'text/plain; charset=EUC-JP', 'さようなら');
});
expect(main()).toEqual(undefined);
files = engine.findData(filesDef);
expect(files.size()).toEqual(3);
assertFile(files.get(2), saveAs, 'text/plain', 'EUC-JP', 'さようなら\n');
});
]]>