From de048b68425a5e142cac8a1242dc84c1d4ed26e8 Mon Sep 17 00:00:00 2001 From: Mike Phares Date: Wed, 8 Mar 2023 10:04:15 -0700 Subject: [PATCH] Removed FromBody --- .vscode/thunder-tests/thunderActivity.json | 40 ++++++++++++++- Server/ApiControllers/ExportController.cs | 51 ++++++++++++++++--- Server/ApiControllers/InboundController.cs | 30 ++++++----- Server/Repositories/ExportRepository.cs | 1 - Shared/Models/Stateless/IExportController.cs | 14 ++--- Shared/Models/Stateless/IInboundController.cs | 4 +- Tests/UnitTestExportController.cs | 50 ++++++++++++++++-- 7 files changed, 155 insertions(+), 35 deletions(-) diff --git a/.vscode/thunder-tests/thunderActivity.json b/.vscode/thunder-tests/thunderActivity.json index 51f4a32..d846c32 100644 --- a/.vscode/thunder-tests/thunderActivity.json +++ b/.vscode/thunder-tests/thunderActivity.json @@ -147,7 +147,45 @@ "method": "GET", "sortNum": 0, "created": "2023-03-07T17:21:05.219Z", - "modified": "2023-03-07T17:28:47.716Z", + "modified": "2023-03-07T19:35:11.146Z", + "headers": [], + "params": [], + "body": { + "type": "json", + "raw": "{\n \"PSN\": \"5296\",\n \"RDS\": \"578280\",\n \"Reactor\": \"37\",\n \"ID\": 1678216576\n}", + "form": [] + }, + "tests": [] + }, + { + "_id": "6d8098aa-eb50-422e-b38d-32709d985e8e", + "colId": "history", + "containerId": "", + "name": "GetHeaders-Dev", + "url": "http://mestsa008/api/export/headers", + "method": "POST", + "sortNum": 0, + "created": "2023-03-07T17:21:05.219Z", + "modified": "2023-03-08T16:39:11.451Z", + "headers": [], + "params": [], + "body": { + "type": "json", + "raw": "{\n \"MesEntity\": \"CDE5\",\n \"Employee\": \"Operator\",\n \"Layer\": \"-\",\n \"PSN\": \"5008\",\n \"RDS\": \"579487\",\n \"Reactor\": \"61\",\n \"Recipe\": \"LSL8IN \\\\ 10PT_5mm\",\n \"Zone\": \"-\",\n \"Ticks\": 638137811504314166,\n \"ID\": 1678209360\n}", + "form": [] + }, + "tests": [] + }, + { + "_id": "61383ad6-ceb4-4d98-86c1-bf00c0e4204d", + "colId": "history", + "containerId": "", + "name": "GetHeaders-localhost", + "url": "http://localhost:5126/api/export/headers", + "method": "GET", + "sortNum": 0, + "created": "2023-03-07T17:21:05.219Z", + "modified": "2023-03-08T16:05:18.727Z", "headers": [], "params": [], "body": { diff --git a/Server/ApiControllers/ExportController.cs b/Server/ApiControllers/ExportController.cs index 3c296b9..4bb0ee3 100644 --- a/Server/ApiControllers/ExportController.cs +++ b/Server/ApiControllers/ExportController.cs @@ -15,25 +15,60 @@ public class ExportController : Controller, IExportController public ExportController(IExportRepository exportRepository) => _ExportRepository = exportRepository; + private static HeaderCommon GetHeaderCommon(Stream stream) + { + HeaderCommon? result; + if (!stream.CanRead) + result = new(); + else + { + Task task = new StreamReader(stream).ReadToEndAsync(); + result = string.IsNullOrEmpty(task.Result) ? null : JsonSerializer.Deserialize(task.Result); + result ??= new(); + } + return result; + } + [HttpGet] [Route("export")] - public IActionResult GetExport(HeaderCommon headerCommon) => - Content(_ExportRepository.GetExport(headerCommon)); + public IActionResult GetExport() => + Content(_ExportRepository.GetExport(GetHeaderCommon(Request.Body))); [HttpGet] [Route("headers")] - public IActionResult GetHeaders(HeaderCommon headerCommon) => - Json(_ExportRepository.GetHeaders(headerCommon), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true }); + public IActionResult GetHeaders() => + Json(_ExportRepository.GetHeaders(GetHeaderCommon(Request.Body)), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true }); [HttpGet] [Route("logistics")] - public IActionResult GetLogistics(HeaderCommon headerCommon) => - Json(_ExportRepository.GetLogistics(headerCommon), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true }); + public IActionResult GetLogistics() => + Json(_ExportRepository.GetLogistics(GetHeaderCommon(Request.Body)), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true }); [HttpGet] [Route("pdsf")] [Route("processDataStandardFormat")] - public IActionResult GetProcessDataStandardFormat(HeaderCommon headerCommon) => - Content(_ExportRepository.GetProcessDataStandardFormat(headerCommon)); + public IActionResult GetProcessDataStandardFormat() => + Content(_ExportRepository.GetProcessDataStandardFormat(GetHeaderCommon(Request.Body))); + + [HttpPost] + [Route("export")] + public IActionResult PostExport() => + Content(_ExportRepository.GetExport(GetHeaderCommon(Request.Body))); + + [HttpPost] + [Route("headers")] + public IActionResult PostHeaders() => + Json(_ExportRepository.GetHeaders(GetHeaderCommon(Request.Body)), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true }); + + [HttpPost] + [Route("logistics")] + public IActionResult PostLogistics() => + Json(_ExportRepository.GetLogistics(GetHeaderCommon(Request.Body)), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true }); + + [HttpPost] + [Route("pdsf")] + [Route("processDataStandardFormat")] + public IActionResult PostProcessDataStandardFormat() => + Content(_ExportRepository.GetProcessDataStandardFormat(GetHeaderCommon(Request.Body))); } \ No newline at end of file diff --git a/Server/ApiControllers/InboundController.cs b/Server/ApiControllers/InboundController.cs index 3fb4908..f26fe64 100644 --- a/Server/ApiControllers/InboundController.cs +++ b/Server/ApiControllers/InboundController.cs @@ -30,9 +30,24 @@ public partial class InboundController : ControllerBase, IInboundController IExportRepository.GetLogistics(HeaderCommon headerCommon) diff --git a/Shared/Models/Stateless/IExportController.cs b/Shared/Models/Stateless/IExportController.cs index 8d949ae..0e9020a 100644 --- a/Shared/Models/Stateless/IExportController.cs +++ b/Shared/Models/Stateless/IExportController.cs @@ -1,5 +1,3 @@ -using OI.Metrology.Shared.DataModels; - namespace OI.Metrology.Shared.Models.Stateless; public interface IExportController @@ -14,9 +12,13 @@ public interface IExportController } static string GetRouteName() => nameof(IExportController)[1..^10]; - T GetExport(HeaderCommon headerCommon); - T GetHeaders(HeaderCommon headerCommon); - T GetLogistics(HeaderCommon headerCommon); - T GetProcessDataStandardFormat(HeaderCommon headerCommon); + T GetExport(); + T GetHeaders(); + T GetLogistics(); + T GetProcessDataStandardFormat(); + T PostExport(); + T PostHeaders(); + T PostLogistics(); + T PostProcessDataStandardFormat(); } \ No newline at end of file diff --git a/Shared/Models/Stateless/IInboundController.cs b/Shared/Models/Stateless/IInboundController.cs index 4ae25ed..bec0f61 100644 --- a/Shared/Models/Stateless/IInboundController.cs +++ b/Shared/Models/Stateless/IInboundController.cs @@ -1,5 +1,3 @@ -using Newtonsoft.Json.Linq; - namespace OI.Metrology.Shared.Models.Stateless; public interface IInboundController @@ -11,7 +9,7 @@ public interface IInboundController } static string GetRouteName() => nameof(IInboundController)[1..^10]; - T Post(string tooltype, JToken jsonbody); + T Post(string tooltype); T AttachFile(string tooltype, long headerid, string datauniqueid = ""); } \ No newline at end of file diff --git a/Tests/UnitTestExportController.cs b/Tests/UnitTestExportController.cs index 4d5b206..4a5613f 100644 --- a/Tests/UnitTestExportController.cs +++ b/Tests/UnitTestExportController.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection; using OI.Metrology.Shared.DataModels; using OI.Metrology.Shared.Models.Stateless; using Serilog; +using System.Text; namespace OI.Metrology.Tests; @@ -39,6 +40,9 @@ public class UnitTestExportController private static HeaderCommon GetHeaderCommon() => new() { PSN = "5008", Reactor = "61", RDS = "579487", ID = 1678209360 }; + private static StringContent GetStringContent() => + new(System.Text.Json.JsonSerializer.Serialize(GetHeaderCommon()), Encoding.UTF8, "application/json"); + [TestMethod] public void GetExport() { @@ -55,12 +59,22 @@ public class UnitTestExportController { HttpClient httpClient = _WebApplicationFactory.CreateClient(); _Logger.Information("Starting Web Application"); - string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/headers"); + string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/export"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetExport)}.txt"), result); Assert.IsNotNull(result); _Logger.Information($"{_TestContext?.TestName} completed"); } + [TestMethod] + public async Task PostExportApi() + { + HttpClient httpClient = _WebApplicationFactory.CreateClient(); + _Logger.Information("Starting Web Application"); + HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/export", GetStringContent()); + Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK); + _Logger.Information($"{_TestContext?.TestName} completed"); + } + [TestMethod] public void GetHeaders() { @@ -84,6 +98,16 @@ public class UnitTestExportController _Logger.Information($"{_TestContext?.TestName} completed"); } + [TestMethod] + public async Task PostHeadersApi() + { + HttpClient httpClient = _WebApplicationFactory.CreateClient(); + _Logger.Information("Starting Web Application"); + HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/headers", GetStringContent()); + Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK); + _Logger.Information($"{_TestContext?.TestName} completed"); + } + [TestMethod] public void GetLogistics() { @@ -100,13 +124,23 @@ public class UnitTestExportController { HttpClient httpClient = _WebApplicationFactory.CreateClient(); _Logger.Information("Starting Web Application"); - string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/headers"); + string? json = await httpClient.GetStringAsync($"api/{_ControllerName}/logistics"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetLogistics)}.json"), json); Result? result = System.Text.Json.JsonSerializer.Deserialize>(json); Assert.IsNotNull(result?.Results); _Logger.Information($"{_TestContext?.TestName} completed"); } + [TestMethod] + public async Task PostLogisticsApi() + { + HttpClient httpClient = _WebApplicationFactory.CreateClient(); + _Logger.Information("Starting Web Application"); + HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/logistics", GetStringContent()); + Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK); + _Logger.Information($"{_TestContext?.TestName} completed"); + } + [TestMethod] public void GetProcessDataStandardFormat() { @@ -123,10 +157,20 @@ public class UnitTestExportController { HttpClient httpClient = _WebApplicationFactory.CreateClient(); _Logger.Information("Starting Web Application"); - string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/headers"); + string? result = await httpClient.GetStringAsync($"api/{_ControllerName}/processDataStandardFormat"); File.WriteAllText(Path.Combine(AppContext.BaseDirectory, $"{_ControllerName}-{nameof(GetProcessDataStandardFormat)}.pdsf"), result); Assert.IsNotNull(result); _Logger.Information($"{_TestContext?.TestName} completed"); } + [TestMethod] + public async Task PostProcessDataStandardFormatApi() + { + HttpClient httpClient = _WebApplicationFactory.CreateClient(); + _Logger.Information("Starting Web Application"); + HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"api/{_ControllerName}/processDataStandardFormat", GetStringContent()); + Assert.IsTrue(httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK); + _Logger.Information($"{_TestContext?.TestName} completed"); + } + } \ No newline at end of file