Skip to content

Commit d1e4f84

Browse files
author
Angel Mendez
committed
Updated the GetDefaultMembershipSourceAttributes endpoint to remove _Code from attribute names in its response. Added a new field to the attributes, hasMapping to determine if this attribute is a code/desc attribute.
1 parent acda098 commit d1e4f84

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

Service/GroupMembershipManagement/Hosts/WebApi/Services.WebApi/GetDefaultSqlMembershipSourceAttributesHandler.cs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33
using Microsoft.Data.SqlClient;
4+
using Microsoft.Graph.Models;
45
using Models;
56
using Newtonsoft.Json;
67
using Repositories.Contracts;
@@ -35,30 +36,24 @@ protected override async Task<GetDefaultSqlMembershipSourceAttributesResponse> E
3536
{
3637
try
3738
{
38-
var sqlFilterAttributes = await GetSqlAttributes();
39+
var sqlFilterAttributes = await GetSqlAttributesAsync();
3940
var storedAttributeSettings = await _databaseSqlMembershipSourcesRepository.GetDefaultSourceAttributesAsync();
4041

4142
if (storedAttributeSettings != null)
4243
{
43-
storedAttributeSettings.RemoveAll(attribute =>
44-
!sqlFilterAttributes.Any(t => t.Name == attribute.Name && t.Type == attribute.Type)
45-
);
44+
storedAttributeSettings.RemoveAll(attribute => !sqlFilterAttributes.Any(t => t.Name == attribute.Name && t.Type == attribute.Type));
4645

4746
await _databaseSqlMembershipSourcesRepository.UpdateDefaultSourceAttributesAsync(storedAttributeSettings);
4847
}
4948

50-
var attributesToReturn = sqlFilterAttributes.Select(attributeTuple =>
49+
var attributesToReturn = sqlFilterAttributes.Select(sqlAttribute =>
5150
{
5251
var storedAttribute = storedAttributeSettings?.FirstOrDefault(attribute =>
53-
attribute.Name == attributeTuple.Name && attribute.Type == attributeTuple.Type
52+
attribute.Name == sqlAttribute.Name && attribute.Type == sqlAttribute.Type
5453
);
5554

56-
return storedAttribute ?? new SqlMembershipAttribute
57-
{
58-
Name = attributeTuple.Name,
59-
Type = attributeTuple.Type,
60-
CustomLabel = ""
61-
};
55+
return storedAttribute ?? sqlAttribute;
56+
6257
}).ToList();
6358

6459
return new GetDefaultSqlMembershipSourceAttributesResponse { Attributes = attributesToReturn };
@@ -70,14 +65,35 @@ protected override async Task<GetDefaultSqlMembershipSourceAttributesResponse> E
7065
}
7166
}
7267

73-
private async Task<List<(string Name, string Type)>> GetSqlAttributes()
68+
private async Task<List<SqlMembershipAttribute>> GetSqlAttributesAsync()
7469
{
7570
var tableName = await GetTableNameAsync();
76-
var attributes = await GetColumnNamesAsync(tableName);
71+
var columns = await GetColumnDetailsAsync(tableName);
72+
var attributes = columns.Select(column =>
73+
{
74+
var codeSuffix = "_Code";
75+
var attributeName = column.Name;
76+
var hasMapping = false;
77+
78+
if (attributeName.EndsWith(codeSuffix))
79+
{
80+
attributeName = attributeName.Substring(0, attributeName.Length - codeSuffix.Length);
81+
hasMapping = true;
82+
}
83+
84+
return new SqlMembershipAttribute
85+
{
86+
Name = attributeName,
87+
Type = column.Type,
88+
CustomLabel = "",
89+
HasMapping = hasMapping
90+
};
91+
}).ToList();
92+
7793
return attributes;
7894
}
7995

80-
private async Task<List<(string Name, string Type)>> GetColumnNamesAsync(string tableName)
96+
private async Task<List<(string Name, string Type)>> GetColumnDetailsAsync(string tableName)
8197
{
8298
var attributes = new List<(string Name, string Type)>();
8399

Service/GroupMembershipManagement/Hosts/WebApi/WebApi.Tests/SqlMembershipSourcesControllerTests.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4+
using Azure.Messaging.EventGrid.SystemEvents;
45
using Microsoft.AspNetCore.Http;
56
using Microsoft.AspNetCore.Mvc;
67
using Models;
@@ -71,7 +72,7 @@ public void Initialize()
7172

7273
_databaseSqlMembershipSourcesRepository.Setup(x => x.GetDefaultSourceAsync()).ReturnsAsync(() => _defaultSource);
7374
_databaseSqlMembershipSourcesRepository.Setup(x => x.GetDefaultSourceAttributesAsync()).ReturnsAsync(() => _storedAttributeSettings);
74-
_sqlMembershipRepository.Setup(x => x.GetColumnDetailsAsync(It.IsAny<string>())).ReturnsAsync(new List<(string Name, string Type)> { ("Name1", "nvarchar"), ("Name2", "int"), ("Name3", "nvarchar") });
75+
_sqlMembershipRepository.Setup(x => x.GetColumnDetailsAsync(It.IsAny<string>())).ReturnsAsync(new List<(string Name, string Type)> { ("Name1", "nvarchar"), ("Name2", "int"), ("Name3_Code", "nvarchar") });
7576
_sqlMembershipRepository.Setup(x => x.GetAttributeValuesAsync(It.IsAny<string>(), It.IsAny<string>())).ReturnsAsync(new List<(string Code, string Description)> { ("Code1", "Description1"), ("Code2", "Description2"), ("Code3", "Description3") });
7677
_sqlMembershipRepository.Setup(x => x.CheckIfTableExistsAsync(It.IsAny<string>())).ReturnsAsync(true);
7778
_sqlMembershipRepository.Setup(x => x.CheckIfMappingsTableExistsAsync(It.IsAny<string>())).ReturnsAsync(true);
@@ -119,6 +120,30 @@ public async Task NoStoredCustomLabelsTestAsync()
119120
Assert.AreEqual(attributes.Count, 3);
120121
Assert.AreEqual(attributes[0].Name, "Name1");
121122
Assert.AreEqual(attributes[0].CustomLabel, "");
123+
Assert.IsFalse(attributes[0].HasMapping);
124+
}
125+
126+
[TestMethod]
127+
public async Task CodeColumnRenameTestAsync()
128+
{
129+
_databaseSqlMembershipSourcesRepository.Setup(x => x.GetDefaultSourceAttributesAsync()).ReturnsAsync(() => null);
130+
131+
var response = await _sqlMembershipSourcesController.GetDefaultSourceAttributesAsync();
132+
133+
Assert.IsNotNull(response);
134+
135+
var okResult = response as OkObjectResult;
136+
137+
Assert.IsNotNull(okResult);
138+
Assert.IsNotNull(okResult.Value);
139+
140+
var attributes = okResult.Value as List<SqlMembershipAttribute>;
141+
Assert.IsNotNull(attributes);
142+
143+
Assert.AreEqual(attributes.Count, 3);
144+
Assert.AreEqual(attributes[2].Name, "Name3");
145+
Assert.AreEqual(attributes[2].CustomLabel, "");
146+
Assert.IsTrue(attributes[2].HasMapping);
122147
}
123148

124149
[TestMethod]
@@ -130,13 +155,15 @@ public async Task TestRemovalOfStaleStoredSettingsAsync()
130155
{
131156
Name = "Name1",
132157
CustomLabel = "CustomLabel1",
133-
Type = "nvarchar"
158+
Type = "nvarchar",
159+
HasMapping = false
134160
},
135161
new SqlMembershipAttribute
136162
{
137163
Name = "RemovedAttribute1",
138164
CustomLabel = "RemovedCustomLabel1",
139-
Type = "nvarchar"
165+
Type = "nvarchar",
166+
HasMapping = false
140167
}
141168
};
142169

@@ -157,6 +184,7 @@ public async Task TestRemovalOfStaleStoredSettingsAsync()
157184
Assert.AreEqual(attributes.Count, 3);
158185
Assert.AreEqual(attributes[0].Name, "Name1");
159186
Assert.AreEqual(attributes[0].CustomLabel, "CustomLabel1");
187+
Assert.IsFalse(attributes[0].HasMapping);
160188
Assert.AreEqual(attributes[1].CustomLabel, "");
161189

162190
_databaseSqlMembershipSourcesRepository.Verify(x => x.UpdateDefaultSourceAttributesAsync(It.Is<List<SqlMembershipAttribute>>(list => !list.Any(a => a.Name == "RemovedAttribute1"))), Times.Once());

Service/GroupMembershipManagement/Models/SqlMembershipAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public class SqlMembershipAttribute
88
public string Name { get; set; }
99
public string CustomLabel { get; set; }
1010
public string Type { get; set; }
11+
public bool HasMapping { get; set; }
1112
}
1213
}

0 commit comments

Comments
 (0)