Skip to content

Commit 4be425d

Browse files
committed
fix: deserialization of UploadLicenses fixed
1 parent 773fab0 commit 4be425d

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

Fossology.Rest.Dotnet.Model/UploadLicenses.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public class UploadLicenses
4040
[JsonProperty("conclusions")]
4141
public List<string> Conclusions { get; }
4242

43+
/// <summary>
44+
/// Initializes a new instance of the <see cref="UploadLicenses"/> class.
45+
/// </summary>
46+
public UploadLicenses()
47+
{
48+
this.AgentFindings = new List<string>();
49+
this.Conclusions = new List<string>();
50+
} // UploadLicenses()
51+
4352
/// <inheritdoc />
4453
public override string ToString()
4554
{
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// ---------------------------------------------------------------------------
2+
// <copyright file="DeserializationTest.cs" company="Tethys">
3+
// Copyright (C) 2020 T. Graf
4+
// </copyright>
5+
//
6+
// Licensed under the MIT License.
7+
// SPDX-License-Identifier: MIT
8+
//
9+
// Unless required by applicable law or agreed to in writing,
10+
// software distributed under the License is distributed on an
11+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12+
// either express or implied.
13+
// ---------------------------------------------------------------------------
14+
15+
namespace Fossology.Rest.Dotnet.Test
16+
{
17+
using System;
18+
using System.Collections.Generic;
19+
using System.Text;
20+
using Microsoft.VisualStudio.TestTools.UnitTesting;
21+
using Model;
22+
using Newtonsoft.Json;
23+
24+
/// <summary>
25+
/// Unit test class.
26+
/// </summary>
27+
[TestClass]
28+
public class DeserializationTest
29+
{
30+
public class StringListClass
31+
{
32+
public List<string> MyList { get; }
33+
34+
public StringListClass()
35+
{
36+
this.MyList = new List<string>();
37+
}
38+
}
39+
40+
/// <summary>
41+
/// Unit test.
42+
/// </summary>
43+
[TestMethod]
44+
public void TestDeserializeStringList()
45+
{
46+
const string JsonText = "['A', 'B', 'C']";
47+
48+
var actual = JsonConvert.DeserializeObject<List<string>>(JsonText);
49+
Assert.AreEqual(3, actual.Count);
50+
}
51+
52+
/// <summary>
53+
/// Unit test.
54+
/// </summary>
55+
[TestMethod]
56+
public void TestDeserializeStringList2()
57+
{
58+
const string JsonText = "{'MyList': ['A', 'B', 'C']}";
59+
60+
var actual = JsonConvert.DeserializeObject<StringListClass>(JsonText);
61+
Assert.IsNotNull(actual);
62+
}
63+
64+
/// <summary>
65+
/// Unit test.
66+
/// </summary>
67+
[TestMethod]
68+
public void TestSerializeStringList()
69+
{
70+
var x = new StringListClass();
71+
x.MyList.Add("X");
72+
x.MyList.Add("Y");
73+
x.MyList.Add("Z");
74+
75+
var actual = JsonConvert.SerializeObject(x);
76+
Assert.IsNotNull(actual);
77+
}
78+
79+
/// <summary>
80+
/// Unit test.
81+
/// </summary>
82+
[TestMethod]
83+
public void TestDeserializeComplex1()
84+
{
85+
const string JsonText =
86+
"{"
87+
+ "\"filePath\":\"fetch-retry-master.zip/fetch-retry-master/package.json\","
88+
+ "\"agentFindings\":[\"MIT\"],"
89+
+ "\"conclusions\":null"
90+
+ "}";
91+
92+
var actual = JsonConvert.DeserializeObject<UploadLicenses>(JsonText);
93+
Assert.IsNotNull(actual);
94+
Assert.IsNotNull(actual.AgentFindings);
95+
Assert.AreEqual(1, actual.AgentFindings.Count);
96+
97+
// ==> no correct deserialization without constructor...
98+
}
99+
}
100+
}

Fossology.Rest.Dotnet.Test/Fossology.Rest.Dotnet.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<PrivateAssets>all</PrivateAssets>
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
</PackageReference>
17+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
1718
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
1819
<PrivateAssets>all</PrivateAssets>
1920
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

0 commit comments

Comments
 (0)