-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathJsonConverterUnitTests.cs
More file actions
110 lines (94 loc) · 3.44 KB
/
JsonConverterUnitTests.cs
File metadata and controls
110 lines (94 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.IO;
using System.Reflection;
using AutoFixture;
using Contentstack.Core;
using Contentstack.Core.Configuration;
using Contentstack.Core.Internals;
using Contentstack.Core.Models;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Xunit;
namespace Contentstack.Core.Unit.Tests
{
public class EntryJsonConverterUnitTests
{
private readonly IFixture _fixture = new Fixture();
private ContentstackClient CreateClient()
{
var options = new ContentstackOptions()
{
ApiKey = _fixture.Create<string>(),
DeliveryToken = _fixture.Create<string>(),
Environment = _fixture.Create<string>()
};
return new ContentstackClient(new OptionsWrapper<ContentstackOptions>(options));
}
[Fact]
public void ReadJson_WithValidJson_ReturnsEntry()
{
// Arrange
var converter = new EntryJsonConverter();
var json = "{\"uid\":\"test_uid\",\"title\":\"Test Title\"}";
var reader = new JsonTextReader(new StringReader(json));
// Act
var entry = converter.ReadJson(reader, typeof(Entry), null, JsonSerializer.CreateDefault());
// Assert
Assert.NotNull(entry);
Assert.IsType<Entry>(entry);
}
[Fact]
public void WriteJson_WithEntry_DoesNothing()
{
// Arrange
var converter = new EntryJsonConverter();
var client = CreateClient();
var contentType = client.ContentType("test_content_type");
var entry = contentType.Entry("test_uid");
var writer = new JsonTextWriter(new StringWriter());
// Act - Should not throw
converter.WriteJson(writer, entry, JsonSerializer.CreateDefault());
// Assert
Assert.True(true);
}
}
public class AssetJsonConverterUnitTests
{
private readonly IFixture _fixture = new Fixture();
private ContentstackClient CreateClient()
{
var options = new ContentstackOptions()
{
ApiKey = _fixture.Create<string>(),
DeliveryToken = _fixture.Create<string>(),
Environment = _fixture.Create<string>()
};
return new ContentstackClient(new OptionsWrapper<ContentstackOptions>(options));
}
[Fact]
public void ReadJson_WithValidJson_ReturnsAsset()
{
// Arrange
var converter = new AssetJsonConverter();
var json = "{\"uid\":\"test_uid\",\"title\":\"Test Asset\"}";
var reader = new JsonTextReader(new StringReader(json));
// Act
var asset = converter.ReadJson(reader, typeof(Asset), null, JsonSerializer.CreateDefault());
// Assert
Assert.NotNull(asset);
Assert.IsType<Asset>(asset);
}
[Fact]
public void WriteJson_WithAsset_ThrowsAssetException()
{
// Arrange
var converter = new AssetJsonConverter();
var client = CreateClient();
var asset = client.Asset("test_uid");
var writer = new JsonTextWriter(new StringWriter());
// Act & Assert
Assert.Throws<AssetException>(() =>
converter.WriteJson(writer, asset, JsonSerializer.CreateDefault()));
}
}
}