|
| 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 | +} |
0 commit comments