Skip to content

Commit

Permalink
Endret response til json for initialize og upload (#547)
Browse files Browse the repository at this point in the history
* Endret response til json for initialize og upload

* Rettet opp feil i tester

* Endret variabelnavn

* Fiks for MalwareScanTest

---------

Co-authored-by: Martin Todorov <>
  • Loading branch information
mSunberg authored Oct 7, 2024
1 parent ab0a740 commit 89c48d9
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 60 deletions.
24 changes: 18 additions & 6 deletions altinn-broker-postman-collection.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"info": {
"_postman_id": "eeea7c88-1d7d-47f3-8148-1f33b641af3e",
"_postman_id": "eb3386d2-82bf-4c01-aeac-9c965f0b0e88",
"name": "Altinn.Broker",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "1775784"
"_exporter_id": "38611559"
},
"item": [
{
Expand Down Expand Up @@ -453,11 +453,11 @@
"listen": "test",
"script": {
"exec": [
"var fileTransferId = pm.response.text().replace(/\\\"/g, \"\")\r",
"pm.collectionVariables.set(\"fileTransferId\", fileTransferId);\r",
""
"var fileTransferId = pm.response.json()\r",
"pm.collectionVariables.set(\"fileTransferId\", fileTransferId.fileTransferId);"
],
"type": "text/javascript"
"type": "text/javascript",
"packages": {}
}
}
],
Expand Down Expand Up @@ -1366,6 +1366,18 @@
{
"key": "platform_subscription_key",
"value": "Contact us to get a subscription key for use with Altinn APIs",
"type": "string",
"disabled": true
},
{
"key": "local_testing",
"value": "http://localhost:5096",
"type": "string",
"disabled": true
},
{
"key": "fileTransferId",
"value": "",
"type": "string"
}
]
Expand Down
13 changes: 10 additions & 3 deletions src/Altinn.Broker.API/Controllers/FileTransferController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Altinn.Broker.API.Configuration;
using Altinn.Broker.API.Models;
using Altinn.Broker.Application;
using Altinn.Broker.Application.ConfirmDownload;
using Altinn.Broker.Application.DownloadFile;
Expand Down Expand Up @@ -46,8 +47,11 @@ public async Task<ActionResult<Guid>> InitializeFileTransfer(FileTransferInitali

var commandResult = await handler.Process(commandRequest, cancellationToken);
return commandResult.Match(
fileTransferId => Ok(fileTransferId.ToString()),
Problem
fileTransferId => Ok(new FileTransferInitializeResponseExt()
{
FileTransferId = fileTransferId
}),
Problem
);
}

Expand Down Expand Up @@ -78,7 +82,10 @@ CancellationToken cancellationToken
ContentLength = Request.ContentLength ?? Request.Body.Length
}, cancellationToken);
return commandResult.Match(
fileId => Ok(fileId.ToString()),
fileTransferId => Ok(new FileTransferUploadResponseExt()
{
FileTransferId = fileTransferId
}),
Problem
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Altinn.Broker.API.Models;

public class FileTransferInitializeResponseExt
{
public Guid FileTransferId { get; set; }
}
6 changes: 6 additions & 0 deletions src/Altinn.Broker.API/Models/FileTransferUploadResponseExt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Altinn.Broker.API.Models;

public class FileTransferUploadResponseExt
{
public Guid FileTransferId { get; set; }
}
16 changes: 11 additions & 5 deletions tests/Altinn.Broker.Tests/FileTransferControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.Json;
using System.Xml;

using Altinn.Broker.API.Models;
using Altinn.Broker.Application;
using Altinn.Broker.Application.ExpireFileTransfer;
using Altinn.Broker.Core.Models;
Expand Down Expand Up @@ -107,7 +108,8 @@ public async Task NormalFlow_With10Properties_Success()

var initializeFileTransferResponse = await _senderClient.PostAsJsonAsync("broker/api/v1/filetransfer", initializeRequestBody);
Assert.True(initializeFileTransferResponse.IsSuccessStatusCode, await initializeFileTransferResponse.Content.ReadAsStringAsync());
var fileTransferId = await initializeFileTransferResponse.Content.ReadAsStringAsync();
var fileTransferResponse = await initializeFileTransferResponse.Content.ReadFromJsonAsync<FileTransferInitializeResponseExt>();
var fileTransferId = fileTransferResponse.FileTransferId;

var fileTransferAfterInitialize = await _senderClient.GetFromJsonAsync<FileTransferOverviewExt>($"broker/api/v1/filetransfer/{fileTransferId}", _responseSerializerOptions);
Assert.NotNull(fileTransferAfterInitialize);
Expand Down Expand Up @@ -359,7 +361,8 @@ public async Task UploadFileTransfer_ChecksumCorrect_Succeeds()

// Act
var initializeFileTransferResponse = await _senderClient.PostAsJsonAsync("broker/api/v1/filetransfer", fileTransfer);
var fileTransferId = await initializeFileTransferResponse.Content.ReadAsStringAsync();
var fileTransferResponse = await initializeFileTransferResponse.Content.ReadFromJsonAsync<FileTransferInitializeResponseExt>();
var fileTransferId = fileTransferResponse.FileTransferId.ToString();
Assert.True(initializeFileTransferResponse.IsSuccessStatusCode, fileTransferId);
var uploadResponse = await UploadTextFileTransfer(fileTransferId, fileContent);

Expand Down Expand Up @@ -428,7 +431,8 @@ public async Task UploadFileTransfer_ChecksumSetWhenInitialized_SameChecksumSetA
// Act
var initializeFileTransferResponse = await _senderClient.PostAsJsonAsync("broker/api/v1/filetransfer", file);
Assert.True(initializeFileTransferResponse.IsSuccessStatusCode, await initializeFileTransferResponse.Content.ReadAsStringAsync());
var fileTransferId = await initializeFileTransferResponse.Content.ReadAsStringAsync();
var fileTransferResponse = await initializeFileTransferResponse.Content.ReadFromJsonAsync<FileTransferInitializeResponseExt>();
var fileTransferId = fileTransferResponse.FileTransferId.ToString();
var uploadResponse = await UploadTextFileTransfer(fileTransferId, fileContent);

// Assert
Expand Down Expand Up @@ -509,7 +513,8 @@ private async Task<string> InitializeAndAssertBasicFileTransfer()
var initializeFileTransferResponse = await _senderClient.PostAsJsonAsync("broker/api/v1/filetransfer", FileTransferInitializeExtTestFactory.BasicFileTransfer());
Assert.True(initializeFileTransferResponse.IsSuccessStatusCode, await initializeFileTransferResponse.Content.ReadAsStringAsync());

return await initializeFileTransferResponse.Content.ReadAsStringAsync();
var fileTransferResponse = await initializeFileTransferResponse.Content.ReadFromJsonAsync<API.Models.FileTransferInitializeResponseExt>();
return fileTransferResponse?.FileTransferId.ToString() ?? string.Empty;
}

private string CalculateChecksum(byte[] data)
Expand All @@ -535,7 +540,8 @@ private async Task Test_Graceful_purge_changes_purge_time(string time = "PT12H")
var file = FileTransferInitializeExtTestFactory.BasicFileTransfer();
var initializeFileTransferResponse = await _senderClient.PostAsJsonAsync("broker/api/v1/filetransfer", file);
Assert.True(initializeFileTransferResponse.IsSuccessStatusCode, await initializeFileTransferResponse.Content.ReadAsStringAsync());
var fileTransferId = await initializeFileTransferResponse.Content.ReadAsStringAsync();
var fileTransferResponse = await initializeFileTransferResponse.Content.ReadFromJsonAsync<FileTransferInitializeResponseExt>();
var fileTransferId = fileTransferResponse.FileTransferId.ToString();

var jobstorage = _factory.Services.GetService(typeof(JobStorage)) as JobStorage;
var uploadResponse = await UploadTextFileTransfer(fileTransferId, fileContent);
Expand Down
Loading

0 comments on commit 89c48d9

Please sign in to comment.