Skip to content

Commit a7df6ff

Browse files
authored
Ai 292 hl 7 configuration (#495)
* changes for HL7Configuration endpoint Signed-off-by: Lillie Dae <[email protected]> --------- Signed-off-by: Lillie Dae <[email protected]>
1 parent 076cc86 commit a7df6ff

17 files changed

+1183
-6
lines changed

src/Api/Hl7ApplicationConfigEntity.cs

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright 2022 MONAI Consortium
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using System;
18+
using System.Collections.Generic;
19+
using System.ComponentModel;
20+
using System.ComponentModel.DataAnnotations;
21+
using System.Linq;
22+
using FellowOakDicom;
23+
using Monai.Deploy.InformaticsGateway.Common;
24+
using Newtonsoft.Json;
25+
26+
namespace Monai.Deploy.InformaticsGateway.Api
27+
{
28+
public class Hl7ApplicationConfigEntity : MongoDBEntityBase
29+
{
30+
/// <summary>
31+
/// Gets or sets the sending identifier.
32+
/// </summary>
33+
[JsonProperty("sending_identifier")]
34+
public StringKeyValuePair SendingId { get; set; } = new();
35+
36+
/// <summary>
37+
/// Gets or sets the data link.
38+
/// Value is either PatientId or StudyInstanceUid
39+
/// </summary>
40+
[JsonProperty("data_link")]
41+
public DataKeyValuePair DataLink { get; set; } = new();
42+
43+
/// <summary>
44+
/// Gets or sets the data mapping.
45+
/// Value is a DICOM Tag
46+
/// </summary>
47+
[JsonProperty("data_mapping")]
48+
public List<StringKeyValuePair> DataMapping { get; set; } = new();
49+
50+
public IEnumerable<string> Validate()
51+
{
52+
var errors = new List<string>();
53+
if (string.IsNullOrWhiteSpace(SendingId.Key))
54+
errors.Add($"{nameof(SendingId.Key)} is missing.");
55+
if (string.IsNullOrWhiteSpace(SendingId.Value))
56+
errors.Add($"{nameof(SendingId.Value)} is missing.");
57+
58+
if (string.IsNullOrWhiteSpace(DataLink.Key))
59+
errors.Add($"{nameof(DataLink.Key)} is missing.");
60+
61+
if (DataMapping.IsNullOrEmpty())
62+
errors.Add($"{nameof(DataMapping)} is missing values.");
63+
64+
ValidateDataMapping(errors);
65+
66+
return errors;
67+
}
68+
69+
private void ValidateDataMapping(List<string> errors)
70+
{
71+
for (var idx = 0; idx < DataMapping.Count; idx++)
72+
{
73+
var dataMapKvp = DataMapping[idx];
74+
75+
if (string.IsNullOrWhiteSpace(dataMapKvp.Key) || dataMapKvp.Value.Length < 8)
76+
{
77+
if (string.IsNullOrWhiteSpace(dataMapKvp.Key))
78+
errors.Add($"{nameof(DataMapping)} is missing a name at index {idx}.");
79+
80+
if (string.IsNullOrWhiteSpace(dataMapKvp.Value) || dataMapKvp.Value.Length < 8)
81+
errors.Add($"{nameof(DataMapping)} ({dataMapKvp.Key}) @ index {idx} is not a valid DICOM Tag.");
82+
83+
continue;
84+
}
85+
86+
try
87+
{
88+
DicomTag.Parse(dataMapKvp.Value);
89+
}
90+
catch (Exception e)
91+
{
92+
errors.Add($"DataMapping.Value is not a valid DICOM Tag. {e.Message}");
93+
}
94+
}
95+
}
96+
97+
public override string ToString()
98+
{
99+
return JsonConvert.SerializeObject(this);
100+
}
101+
}
102+
103+
//string key, string value
104+
public class StringKeyValuePair : IKeyValuePair<string, string>
105+
{
106+
[Key]
107+
public string Key { get; set; } = string.Empty;
108+
public string Value { get; set; } = string.Empty;
109+
110+
public static implicit operator StringKeyValuePair(KeyValuePair<string, string> kvp)
111+
{
112+
return new StringKeyValuePair { Key = kvp.Key, Value = kvp.Value };
113+
}
114+
115+
public static List<StringKeyValuePair> FromDictionary(Dictionary<string, string> dictionary) =>
116+
dictionary.Select(kvp => new StringKeyValuePair { Key = kvp.Key, Value = kvp.Value }).ToList();
117+
118+
}
119+
120+
public class DataKeyValuePair : IKeyValuePair<string, DataLinkType>
121+
{
122+
[Key]
123+
public string Key { get; set; } = string.Empty;
124+
public DataLinkType Value { get; set; }
125+
126+
public static implicit operator DataKeyValuePair(KeyValuePair<string, DataLinkType> kvp)
127+
{
128+
return new DataKeyValuePair { Key = kvp.Key, Value = kvp.Value };
129+
}
130+
}
131+
132+
public interface IKeyValuePair<TKey, TValue>
133+
{
134+
public TKey Key { get; set; }
135+
public TValue Value { get; set; }
136+
}
137+
138+
public enum DataLinkType
139+
{
140+
PatientId,
141+
StudyInstanceUid
142+
}
143+
}

src/Api/Test/BaseApplicationEntityTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Monai.Deploy.InformaticsGateway.Api.Test
2121
public class BaseApplicationEntityTest
2222
{
2323
[Fact]
24-
public void GivenABaseApplicationEntity_WhenNameIsNotSet_ExepectSetDefaultValuesToSetName()
24+
public void GivenABaseApplicationEntity_WhenNameIsNotSet_ExpectSetDefaultValuesToSetName()
2525
{
2626
var entity = new BaseApplicationEntity
2727
{
@@ -35,7 +35,7 @@ public void GivenABaseApplicationEntity_WhenNameIsNotSet_ExepectSetDefaultValues
3535
}
3636

3737
[Fact]
38-
public void GivenABaseApplicationEntity_WhenNameIsSet_ExepectSetDefaultValuesToNotSetName()
38+
public void GivenABaseApplicationEntity_WhenNameIsSet_ExpectSetDefaultValuesToNotSetName()
3939
{
4040
var entity = new BaseApplicationEntity
4141
{

src/Api/Test/DestinationApplicationEntityTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Monai.Deploy.InformaticsGateway.Api.Test
2121
public class MonaiApplicationEntityTest
2222
{
2323
[Fact]
24-
public void GivenAMonaiApplicationEntity_WhenNameIsNotSet_ExepectSetDefaultValuesToBeUsed()
24+
public void GivenAMonaiApplicationEntity_WhenNameIsNotSet_ExpectSetDefaultValuesToBeUsed()
2525
{
2626
var entity = new MonaiApplicationEntity
2727
{
@@ -41,7 +41,7 @@ public void GivenAMonaiApplicationEntity_WhenNameIsNotSet_ExepectSetDefaultValue
4141
}
4242

4343
[Fact]
44-
public void GivenAMonaiApplicationEntity_WhenNameIsSet_ExepectSetDefaultValuesToNotOverwrite()
44+
public void GivenAMonaiApplicationEntity_WhenNameIsSet_ExpectSetDefaultValuesToNotOverwrite()
4545
{
4646
var entity = new MonaiApplicationEntity
4747
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
* Copyright 2021-2023 MONAI Consortium
3+
* Copyright 2019-2021 NVIDIA Corporation
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
using System;
19+
using System.Collections.Generic;
20+
using Newtonsoft.Json;
21+
using Xunit;
22+
23+
namespace Monai.Deploy.InformaticsGateway.Api.Test
24+
{
25+
public class Hl7ApplicationConfigEntityTest
26+
{
27+
[Fact]
28+
public void GivenAHl7ApplicationConfigEntity_WhenSendingIdKeyIsNotSet_ExpectValidateToReturnError()
29+
{
30+
var entity = new Hl7ApplicationConfigEntity
31+
{
32+
SendingId = new KeyValuePair<string, string>(string.Empty, "SendingIdValue"),
33+
DataLink = new KeyValuePair<string, DataLinkType>("DataLinkKey", DataLinkType.PatientId),
34+
DataMapping = StringKeyValuePair.FromDictionary(new Dictionary<string, string> { { "DataMappingKey", "DataMappingValue" } })
35+
};
36+
37+
var errors = entity.Validate();
38+
39+
Assert.NotEmpty(errors);
40+
Assert.Contains($"{nameof(entity.SendingId.Key)} is missing.", errors);
41+
}
42+
43+
[Fact]
44+
public void GivenAHl7ApplicationConfigEntity_WhenSendingIdValueIsNotSet_ExpectValidateToReturnError()
45+
{
46+
var entity = new Hl7ApplicationConfigEntity
47+
{
48+
SendingId = new KeyValuePair<string, string>("SendingIdKey", string.Empty),
49+
DataLink = new KeyValuePair<string, DataLinkType>("DataLinkKey", DataLinkType.PatientId),
50+
DataMapping = StringKeyValuePair.FromDictionary(new Dictionary<string, string> { { "DataMappingKey", "DataMappingValue" } })
51+
};
52+
53+
var errors = entity.Validate();
54+
55+
Assert.NotEmpty(errors);
56+
Assert.Contains($"{nameof(entity.SendingId.Value)} is missing.", errors);
57+
}
58+
59+
[Fact]
60+
public void GivenAHl7ApplicationConfigEntity_WhenDataLinkKeyIsNotSet_ExpectValidateToReturnError()
61+
{
62+
var entity = new Hl7ApplicationConfigEntity
63+
{
64+
SendingId = new KeyValuePair<string, string>("SendingIdKey", "SendingIdValue"),
65+
DataLink = new KeyValuePair<string, DataLinkType>(string.Empty, DataLinkType.PatientId),
66+
DataMapping = StringKeyValuePair.FromDictionary(new Dictionary<string, string> { { "DataMappingKey", "DataMappingValue" } })
67+
};
68+
69+
var errors = entity.Validate();
70+
71+
Assert.NotEmpty(errors);
72+
Assert.Contains($"{nameof(entity.DataLink.Key)} is missing.", errors);
73+
}
74+
75+
[Fact]
76+
public void GivenAHl7ApplicationConfigEntity_WhenDataMappingIsNotSet_ExpectValidateToReturnError()
77+
{
78+
var entity = new Hl7ApplicationConfigEntity
79+
{
80+
SendingId = new KeyValuePair<string, string>("SendingIdKey", "SendingIdValue"),
81+
DataLink = new KeyValuePair<string, DataLinkType>("DataLinkKey", DataLinkType.PatientId),
82+
DataMapping = StringKeyValuePair.FromDictionary(new Dictionary<string, string>())
83+
};
84+
85+
var errors = entity.Validate();
86+
87+
Assert.NotEmpty(errors);
88+
Assert.Contains($"{nameof(entity.DataMapping)} is missing values.", errors);
89+
}
90+
91+
[Fact]
92+
public void GivenAHl7ApplicationConfigEntity_WhenDataMappingKeyIsNotSet_ExpectValidateToReturnError()
93+
{
94+
var entity = new Hl7ApplicationConfigEntity
95+
{
96+
SendingId = new KeyValuePair<string, string>("SendingIdKey", "SendingIdValue"),
97+
DataLink = new KeyValuePair<string, DataLinkType>("DataLinkKey", DataLinkType.PatientId),
98+
DataMapping = StringKeyValuePair.FromDictionary(new Dictionary<string, string> { { string.Empty, "DataMappingValue" } })
99+
};
100+
101+
var errors = entity.Validate();
102+
103+
Assert.NotEmpty(errors);
104+
Assert.Contains($"{nameof(entity.DataMapping)} is missing a name at index 0.", errors);
105+
}
106+
107+
[Fact]
108+
public void GivenAHl7ApplicationConfigEntity_WhenDataMappingValueIsNotSet_ExpectValidateToReturnError()
109+
{
110+
var entity = new Hl7ApplicationConfigEntity
111+
{
112+
SendingId = new KeyValuePair<string, string>("SendingIdKey", "SendingIdValue"),
113+
DataLink = new KeyValuePair<string, DataLinkType>("DataLinkKey", DataLinkType.PatientId),
114+
DataMapping = StringKeyValuePair.FromDictionary(new Dictionary<string, string> { { "DataMappingKey", string.Empty } })
115+
};
116+
117+
var errors = entity.Validate();
118+
119+
Assert.NotEmpty(errors);
120+
Assert.Contains($"{nameof(entity.DataMapping)} (DataMappingKey) @ index 0 is not a valid DICOM Tag.", errors);
121+
}
122+
123+
[Fact]
124+
public void GivenAHl7ApplicationConfigEntity_WhenDataMappingValueIsNotAValidDicomTag_ExpectValidateToReturnError()
125+
{
126+
var entity = new Hl7ApplicationConfigEntity
127+
{
128+
SendingId = new KeyValuePair<string, string>("SendingIdKey", "SendingIdValue"),
129+
DataLink = new KeyValuePair<string, DataLinkType>("DataLinkKey", DataLinkType.PatientId),
130+
DataMapping = StringKeyValuePair.FromDictionary(new Dictionary<string, string> { { "DataMappingKey", "DataMappingValue" } })
131+
};
132+
133+
var errors = entity.Validate();
134+
135+
Assert.NotEmpty(errors);
136+
Assert.Contains("DataMapping.Value is not a valid DICOM Tag. Error parsing DICOM tag ['DataMappingValue']", errors);
137+
}
138+
139+
[Fact]
140+
public void GivenAHl7ApplicationConfigEntity_WhenDataMappingValueIsAValidDicomTag_ExpectValidateToReturnNoErrors()
141+
{
142+
var entity = new Hl7ApplicationConfigEntity
143+
{
144+
SendingId = new KeyValuePair<string, string>("SendingIdKey", "SendingIdValue"),
145+
DataLink = new KeyValuePair<string, DataLinkType>("DataLinkKey", DataLinkType.PatientId),
146+
DataMapping = StringKeyValuePair.FromDictionary(new Dictionary<string, string> { { "DataMappingKey", "0020,000D" } })
147+
};
148+
149+
var errors = entity.Validate();
150+
151+
Assert.Empty(errors);
152+
}
153+
154+
[Fact]
155+
public void GivenAHl7ApplicationConfigEntity_WhenDataMappingValueIsEmpty_ExpectValidateToReturnError()
156+
{
157+
var entity = new Hl7ApplicationConfigEntity
158+
{
159+
SendingId = new KeyValuePair<string, string>("SendingIdKey", "SendingIdValue"),
160+
DataLink = new KeyValuePair<string, DataLinkType>("DataLinkKey", DataLinkType.PatientId),
161+
DataMapping = StringKeyValuePair.FromDictionary(new Dictionary<string, string> { { "DataMappingKey", "" } })
162+
};
163+
164+
var errors = entity.Validate();
165+
166+
Assert.NotEmpty(errors);
167+
Assert.Contains($"{nameof(entity.DataMapping)} (DataMappingKey) @ index 0 is not a valid DICOM Tag.", errors);
168+
}
169+
170+
[Fact]
171+
public void GivenAHl7ApplicationConfigEntity_WhenToStringIsCalled_ExpectToStringToReturnExpectedValue()
172+
{
173+
var guid = Guid.NewGuid();
174+
var dt = DateTime.UtcNow;
175+
var entity = new Hl7ApplicationConfigEntity
176+
{
177+
Id = guid,
178+
DateTimeCreated = dt,
179+
SendingId = new KeyValuePair<string, string>("SendingIdKey", "SendingIdValue"),
180+
DataLink = new KeyValuePair<string, DataLinkType>("DataLinkKey", DataLinkType.PatientId),
181+
DataMapping = StringKeyValuePair.FromDictionary(new Dictionary<string, string> { { "DataMappingKey", "0020,000D" } })
182+
};
183+
184+
var result = entity.ToString();
185+
186+
var expected = JsonConvert.SerializeObject(entity);
187+
Assert.Equal(expected, result);
188+
}
189+
}
190+
}

src/Api/Test/MonaiApplicationEntityTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Monai.Deploy.InformaticsGateway.Api.Test
2121
public class DestinationApplicationEntityTest
2222
{
2323
[Fact]
24-
public void GivenADestinationApplicationEntity_WhenNameIsNotSet_ExepectSetDefaultValuesToSetName()
24+
public void GivenADestinationApplicationEntity_WhenNameIsNotSet_ExpectSetDefaultValuesToSetName()
2525
{
2626
var entity = new DestinationApplicationEntity
2727
{
@@ -36,7 +36,7 @@ public void GivenADestinationApplicationEntity_WhenNameIsNotSet_ExepectSetDefaul
3636
}
3737

3838
[Fact]
39-
public void GivenADestinationApplicationEntity_WhenNameIsSet_ExepectSetDefaultValuesToNotSetName()
39+
public void GivenADestinationApplicationEntity_WhenNameIsSet_ExpectSetDefaultValuesToNotSetName()
4040
{
4141
var entity = new DestinationApplicationEntity
4242
{

0 commit comments

Comments
 (0)