Skip to content

Commit

Permalink
code rabbit comments handler part 4
Browse files Browse the repository at this point in the history
  • Loading branch information
amanpras committed Feb 13, 2025
1 parent 38bc853 commit 1c02022
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public partial class APIModelPage : GingerUIPage
ModelParamsPage modelParamsPage;
private bool saveWasDone = false;
General.eRIPageViewMode mPageViewMode;
public WireMockAPI MockAPI;
public APIModelPage(ApplicationAPIModel applicationAPIModelBase, General.eRIPageViewMode viewMode = General.eRIPageViewMode.Standalone)
{
mApplicationAPIModel = applicationAPIModelBase;
Expand Down Expand Up @@ -1020,7 +1019,7 @@ public void xRealAPIRadioButton_Checked(object sender, RoutedEventArgs e)
mApplicationAPIModel.UseRealAPI = true;
}

public async void xMockAPIRadioButton_Checked(object sender, RoutedEventArgs e)
public void xMockAPIRadioButton_Checked(object sender, RoutedEventArgs e)
{
mApplicationAPIModel.UseRealAPI = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void xWireMockMappingToggle_Checked(object sender, RoutedEventArgs e)
}
else
{
AddAPIModelWizard.toCreateWireMock = true;
AddAPIModelWizard.ToCreateWireMock = true;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public enum eAPITypeTemp

public RepositoryFolder<ApplicationAPIModel> APIModelFolder;

public bool toCreateWireMock;

/// <summary>
/// Gets or sets a value indicating whether WireMock mappings should be created during the finish process.
/// </summary>
public bool ToCreateWireMock { get; set; }

public string URL { get; set; }

Expand Down Expand Up @@ -120,7 +124,7 @@ public override void Finish()
}
}

if (toCreateWireMock)
if (ToCreateWireMock)
{
CreateWireMockMappingsAsync(General.ConvertListToObservableList(LearnedAPIModelsList.Where(x => x.IsSelected).ToList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</Grid.Resources>
<DockPanel>
<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" >
<Ginger:ucGrid x:Name="xGridMappingOutput" ShowAdd="Collapsed" ShowCopy="Collapsed" ShowPaste="Collapsed" IsReadOnly="True" ShowClearAll="Visible" ShowDelete="Collapsed" ShowEdit="Collapsed" ShowUndo="Collapsed" ShowRefresh="Collapsed" ShowUpDown="Collapsed" ShowTagsFilter="Collapsed" ShowTitle="Collapsed">
<Ginger:ucGrid x:Name="xGridMappingOutput" ShowAdd="Collapsed" ShowCopy="Collapsed" ShowPaste="Collapsed" IsReadOnly="True" ShowClearAll="Collapsed" ShowDelete="Collapsed" ShowEdit="Collapsed" ShowUndo="Collapsed" ShowRefresh="Collapsed" ShowUpDown="Collapsed" ShowTagsFilter="Collapsed" ShowTitle="Collapsed">
</Ginger:ucGrid>
</ScrollViewer>
</DockPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
Expand Down Expand Up @@ -66,6 +67,7 @@ private void SetGridView()

xGridMappingOutput.SetAllColumnsDefaultView(view);
xGridMappingOutput.InitViewItems();
xGridMappingOutput.AddToolbarTool(Amdocs.Ginger.Common.Enums.eImageType.Delete, "Delete All selected mapping", DeleteAllButton_Click);
xGridMappingOutput.AddToolbarTool("@ArrowDown_16x16.png", "Download Mapping", xImportMapping_Click, 0);
xGridMappingOutput.AddToolbarTool(Amdocs.Ginger.Common.Enums.eImageType.ID, "Copy selected item ID", CopySelectedItemID);
xGridMappingOutput.AddToolbarTool(Amdocs.Ginger.Common.Enums.eImageType.Add, "Add New Mapping", AddNewMappingAsync);
Expand All @@ -83,7 +85,35 @@ private void CopySelectedItemID(object sender, RoutedEventArgs e)
Reporter.ToUser(eUserMsgKey.NoItemWasSelected);
}
}
/// <summary>
/// Deletes All the mappings
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void DeleteAllButton_Click(object sender, RoutedEventArgs e)
{
try
{
HttpResponseMessage result = await wmController.mockAPI.DeleteAllMappingsAsync();
if (result.IsSuccessStatusCode)
{
// Remove the mapping from the grid
xGridMappingOutput.DataSourceList.ClearAll();
Reporter.ToUser(eUserMsgKey.WireMockMappingDeleteSuccess);
}
else
{
Reporter.ToLog(eLogLevel.ERROR, $"Failed to delete WireMock mapping, response received from API :{result}");
Reporter.ToUser(eUserMsgKey.WireMockAPIError);
}
}
catch (Exception ex)
{
Reporter.ToLog(eLogLevel.ERROR, "Failed to delete WireMock mapping", ex);
Reporter.ToUser(eUserMsgKey.WireMockAPIError);
}

}
private async void AddNewMappingAsync(object sender, RoutedEventArgs e)
{
try
Expand Down Expand Up @@ -344,12 +374,15 @@ private async void xDeleteMappingBtn_Click(object sender, RoutedEventArgs e)
{
try
{
string result = await wmController.mockAPI.DeleteStubAsync(mapping.Id);
if (!string.IsNullOrEmpty(result))
HttpResponseMessage result = await wmController.mockAPI.DeleteStubAsync(mapping.Id);
if (result.IsSuccessStatusCode)
{
// Remove the mapping from the grid
xGridMappingOutput.DataSourceList.Remove(mapping);
Reporter.ToUser(eUserMsgKey.WireMockMappingDeleteSuccess);

// Refresh the grid data to ensure the mappings are updated
await wmController.DeserializeWireMockResponseAsync();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Ginger/Ginger/GeneralWindows/WireMockMappingPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</Grid.Resources>
<DockPanel>
<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" >
<Ginger:ucGrid x:Name="xGridMapping" Title="Available API Mappings" ShowAdd="Collapsed" ShowClearAll="Visible" ShowDelete="Collapsed" ShowEdit="Collapsed" ShowRefresh="Visible" ShowUpDown="Collapsed" IsReadOnly="False" ShowTagsFilter="Collapsed">
<Ginger:ucGrid x:Name="xGridMapping" Title="Available API Mappings" ShowAdd="Collapsed" ShowClearAll="Collapsed" ShowDelete="Collapsed" ShowEdit="Collapsed" ShowRefresh="Visible" ShowUpDown="Collapsed" IsReadOnly="True" ShowTagsFilter="Collapsed">

</Ginger:ucGrid>
</ScrollViewer>
Expand Down
52 changes: 29 additions & 23 deletions Ginger/Ginger/GeneralWindows/WireMockMappingPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -58,6 +59,7 @@ private void SetGridView()

xGridMapping.SetAllColumnsDefaultView(view);
xGridMapping.InitViewItems();
xGridMapping.AddToolbarTool(Amdocs.Ginger.Common.Enums.eImageType.Delete, "Delete All selected mapping", DeleteAllButton_Click);
xGridMapping.AddToolbarTool("@ArrowDown_16x16.png", "Download Mapping", xDownloadMapping_Click, 0);
xGridMapping.AddToolbarTool(Amdocs.Ginger.Common.Enums.eImageType.ID, "Copy selected item ID", CopySelectedItemID);

Expand Down Expand Up @@ -96,35 +98,34 @@ private async void CloseButton_Click(object sender, RoutedEventArgs e)
genWin.Close();
}

private void EditButton_Click(object sender, RoutedEventArgs e)
/// <summary>
/// Deletes All the mappings
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void DeleteAllButton_Click(object sender, RoutedEventArgs e)
{
// Implement edit logic here
}

private async void DeleteButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.DataContext is Mapping mapping)
try
{
try
HttpResponseMessage result = await wmController.mockAPI.DeleteAllMappingsAsync();
if (result.IsSuccessStatusCode)
{
string result = await wmController.mockAPI.DeleteStubAsync(mapping.Id);
if (!string.IsNullOrEmpty(result))
{
// Remove the mapping from the grid
xGridMapping.DataSourceList.Remove(mapping);
Reporter.ToUser(eUserMsgKey.WireMockConnectionFail);
}
else
{
Reporter.ToUser(eUserMsgKey.WireMockAPIError);
}
// Remove the mapping from the grid
xGridMapping.DataSourceList.ClearAll();
Reporter.ToUser(eUserMsgKey.WireMockMappingDeleteSuccess);
}
catch (Exception ex)
else
{
Reporter.ToLog(eLogLevel.ERROR, "Failed to delete WireMock mapping", ex);
Reporter.ToLog(eLogLevel.ERROR, $"Failed to delete WireMock mapping, response received from API :{result}");
Reporter.ToUser(eUserMsgKey.WireMockAPIError);
}
}
catch (Exception ex)
{
Reporter.ToLog(eLogLevel.ERROR, "Failed to delete WireMock mapping", ex);
Reporter.ToUser(eUserMsgKey.WireMockAPIError);
}

}


Expand Down Expand Up @@ -298,14 +299,19 @@ private async void xEditMappingbtn_Click(object sender, RoutedEventArgs e)
}
}

/// <summary>
/// Delete all mappings
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void xDeleteMappingBtn_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.DataContext is Mapping mapping)
{
try
{
string result = await wmController.mockAPI.DeleteStubAsync(mapping.Id);
if (!string.IsNullOrEmpty(result))
HttpResponseMessage result = await wmController.mockAPI.DeleteStubAsync(mapping.Id);
if (result.IsSuccessStatusCode)
{
// Remove the mapping from the grid
xGridMapping.DataSourceList.Remove(mapping);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Ginger.UserControlsLib.TextEditor
public partial class DocumentEditorPage : Page
{
static List<TextEditorBase> TextEditors = null;
public WireMockAPI mockAPI;
private WireMockAPI mockAPI;
private bool isFromWireMock;
private string wireMockmappingId;

Expand Down
5 changes: 2 additions & 3 deletions Ginger/GingerCoreCommon/Actions/Webservices/ActWebAPIBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public override List<ePlatformType> Platforms
public static string UseLegacyJSONParsing = "UseLegacyJSONParsing";
}

public bool UseRealAPI = true;
[IsSerializedForLocalRepository]
public bool UseRealAPI { get; set; } = true;

[IsSerializedForLocalRepository]
public ObservableList<ActInputValue> DynamicElements = [];
Expand Down Expand Up @@ -140,8 +141,6 @@ public ApplicationAPIUtils.eNetworkCredentials NetworkCredentialsRadioButton
}
}

private string ReqBody = String.Empty;

public bool mUseTemplateFile = true;

public bool mUseRequestBody = true;
Expand Down
14 changes: 7 additions & 7 deletions Ginger/GingerCoreNET/External/WireMock/WireMockAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public async Task<bool> TestWireMockConnectionAsync(string url)
return response.IsSuccessStatusCode;
}
}
catch
catch (Exception ex)
{
Reporter.ToLog(eLogLevel.ERROR, "Error testing WireMock connection", ex);
return false;
}
}
Expand Down Expand Up @@ -167,15 +168,14 @@ public async Task<string> UpdateStubAsync(string stubId, string stubMapping)
}

// Deleting a stub API
public async Task<string> DeleteStubAsync(string stubId)
public async Task<HttpResponseMessage> DeleteStubAsync(string stubId)
{
try
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.DeleteAsync($"{_baseUrl}{MappingEndpoint}/{stubId}");
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
return response.EnsureSuccessStatusCode();
}
}
catch (Exception ex)
Expand All @@ -187,15 +187,15 @@ public async Task<string> DeleteStubAsync(string stubId)
}

// Delete all Mapping
public async Task<string> DeleteAllMappingsAsync()
public async Task<HttpResponseMessage> DeleteAllMappingsAsync()
{
try
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.DeleteAsync($"{_baseUrl}{MappingEndpoint}");
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
return response.EnsureSuccessStatusCode();

}
}
catch (Exception ex)
Expand Down
49 changes: 25 additions & 24 deletions Ginger/GingerCoreNET/External/WireMock/WireMockMappingGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,31 @@ public async static void CreateWireMockMapping(ApplicationAPIModel model)
}
};

if (ReqitemType == ApplicationAPIUtils.eRequestContentType.JSon)
{
string mappingJson = JsonConvert.SerializeObject(stubTemplate).Replace("Content_Type", "Content-Type");
await WireMockAPI.CreateStubAsync(mappingJson, ReqcontentType);
}
else if (ReqitemType == ApplicationAPIUtils.eRequestContentType.XML)
{

string mappingJson = JsonConvert.SerializeObject(stubTemplate).Replace("Content_Type", "Content-Type");
await WireMockAPI.CreateStubAsync(mappingJson, ReqcontentType);


}
else if (ReqitemType == ApplicationAPIUtils.eRequestContentType.TextPlain)
{
string mappingJson = JsonConvert.SerializeObject(stubTemplate).Replace("Content_Type", "Content-Type");
await WireMockAPI.CreateStubAsync(mappingJson, ReqcontentType);
}
else if (ReqitemType == ApplicationAPIUtils.eRequestContentType.XwwwFormUrlEncoded)
{
string mappingJson = JsonConvert.SerializeObject(stubTemplate).Replace("Content_Type", "Content-Type");
await WireMockAPI.CreateStubAsync(mappingJson, ReqcontentType);
}

//if (ReqitemType == ApplicationAPIUtils.eRequestContentType.JSon)

Check warning on line 65 in Ginger/GingerCoreNET/External/WireMock/WireMockMappingGenerator.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Ginger/GingerCoreNET/External/WireMock/WireMockMappingGenerator.cs#L65

Remove this commented out code.
//{
// string mappingJson = JsonConvert.SerializeObject(stubTemplate).Replace("Content_Type", "Content-Type");
// await WireMockAPI.CreateStubAsync(mappingJson, ReqcontentType);
//}
//else if (ReqitemType == ApplicationAPIUtils.eRequestContentType.XML)
//{

// string mappingJson = JsonConvert.SerializeObject(stubTemplate).Replace("Content_Type", "Content-Type");
// await WireMockAPI.CreateStubAsync(mappingJson, ReqcontentType);


//}
//else if (ReqitemType == ApplicationAPIUtils.eRequestContentType.TextPlain)
//{
// string mappingJson = JsonConvert.SerializeObject(stubTemplate).Replace("Content_Type", "Content-Type");
// await WireMockAPI.CreateStubAsync(mappingJson, ReqcontentType);
//}
//else if (ReqitemType == ApplicationAPIUtils.eRequestContentType.XwwwFormUrlEncoded)
//{
// string mappingJson = JsonConvert.SerializeObject(stubTemplate).Replace("Content_Type", "Content-Type");
// await WireMockAPI.CreateStubAsync(mappingJson, ReqcontentType);
//}
string mappingJson = JsonConvert.SerializeObject(stubTemplate).Replace("Content_Type", "Content-Type");
await WireMockAPI.CreateStubAsync(mappingJson, ReqcontentType);
}

public static string TrimApiEndpointUrl(string url)
Expand Down

0 comments on commit 1c02022

Please sign in to comment.