Skip to content

Commit aa31dd7

Browse files
authoredMar 12, 2025
Merge pull request #91 from contentstack/fix/dx-2190-2
Feat: Added Support for timeline preview
2 parents ccf24d6 + 3979010 commit aa31dd7

27 files changed

+337
-104
lines changed
 

‎CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
### Version: 2.21.0
2+
#### Date: March-03-2025
3+
4+
##### Feat:
5+
- Added Support for Timeline Preview
6+
17
### Version: 2.20.0
28
#### Date: Dec-19-2024
39

‎Contentstack.Core.Tests/ContentTypeTest.cs

-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
using Xunit;
33
using Contentstack.Core.Models;
44
using System.Threading.Tasks;
5-
using Contentstack.Core.Configuration;
65
using System.Collections.Generic;
7-
using System.Linq;
8-
using System.Text.RegularExpressions;
9-
using System.Collections;
106

117
namespace Contentstack.Core.Tests
128
{

‎Contentstack.Core.Tests/EntryTest.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public async Task FetchEntryByUIDPublishFallback()
7272
ContentType contenttype = client.ContentType(source);
7373
string uid = await GetUID("source1");
7474
Entry sourceEntry = contenttype.Entry(uid);
75-
await sourceEntry
75+
sourceEntry = await sourceEntry
7676
.SetLocale("ja-jp")
7777
.IncludeFallback()
7878
.Fetch<Entry>();
@@ -98,8 +98,7 @@ await sourceEntry
9898
else
9999
{
100100
Assert.True(result.Uid == sourceEntry.Uid);
101-
Assert.NotNull(result._variant);
102-
Assert.NotNull(result._variant["_uid"]);
101+
Assert.Null(result._variant);
103102
}
104103
});
105104
}
@@ -122,8 +121,7 @@ await sourceEntry
122121
else
123122
{
124123
Assert.True(result.Uid == sourceEntry.Uid);
125-
Assert.NotNull(result._variant);
126-
Assert.NotNull(result._variant["_uid"]);
124+
Assert.Null(result._variant);
127125
}
128126
});
129127
}
+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System;
2+
using Xunit;
3+
using Contentstack.Core.Configuration;
4+
using System.Threading.Tasks;
5+
using System.Collections.Generic;
6+
using Newtonsoft.Json.Linq;
7+
using Newtonsoft.Json;
8+
9+
namespace Contentstack.Core.Tests
10+
{
11+
public class TestContentstackClient : ContentstackClient
12+
{
13+
public TestContentstackClient(ContentstackOptions options)
14+
: base(options)
15+
{
16+
}
17+
18+
// Override GetLivePreviewData with a hardcoded response
19+
private new async Task<JObject> GetLivePreviewData()
20+
{
21+
var mockResponse = new
22+
{
23+
entry = new
24+
{
25+
uid = "mock_entry_uid",
26+
title = "Mocked Entry",
27+
content_type_uid = "mock_content_type",
28+
status = "preview"
29+
}
30+
};
31+
string jsonResponse = Newtonsoft.Json.JsonConvert.SerializeObject(mockResponse);
32+
JObject data = JsonConvert.DeserializeObject<JObject>(jsonResponse, this.SerializerSettings);
33+
return await Task.FromResult((JObject)data["entry"]);
34+
}
35+
36+
// Public method to access the private method in tests
37+
public async Task<JObject> TestGetLivePreviewData()
38+
{
39+
return await GetLivePreviewData();
40+
}
41+
}
42+
43+
public class LivePreviewTests
44+
{
45+
ContentstackClient client = StackConfig.GetStack();
46+
47+
ContentstackClient Lpclient = StackConfig.GetLPStack();
48+
49+
private String numbersContentType = "numbers_content_type";
50+
String source = "source";
51+
52+
public double EPSILON { get; private set; }
53+
54+
[Fact]
55+
public async Task CheckLivePreviewConfigNotSet()
56+
{
57+
var LPConfig = client.GetLivePreviewConfig();
58+
Assert.False(LPConfig.Enable);
59+
Assert.Null(LPConfig.PreviewToken);
60+
Assert.Null(LPConfig.Host);
61+
}
62+
63+
[Fact]
64+
public async Task CheckLivePreviewConfigSet()
65+
{
66+
var LPConfig = Lpclient.GetLivePreviewConfig();
67+
Assert.True(LPConfig.Enable);
68+
Assert.NotEmpty(LPConfig.PreviewToken);
69+
Assert.NotEmpty(LPConfig.Host);
70+
}
71+
72+
[Fact]
73+
public async Task setQueryWithLivePreview()
74+
{
75+
Dictionary<string, string> query = new Dictionary<string, string>
76+
{
77+
{ "content_type_uid", "ct1" },
78+
{ "live_preview", "lphash" },
79+
{ "release_id", "release_id" },
80+
{ "preview_timestamp", "preview_timestamp" },
81+
{ "entry_uid", "euid" }
82+
};
83+
Lpclient.LivePreviewQueryAsync(query);
84+
var LPConfig = Lpclient.GetLivePreviewConfig();
85+
Assert.Equal(LPConfig.PreviewTimestamp, "preview_timestamp");
86+
Assert.NotEmpty(LPConfig.PreviewToken);
87+
Assert.NotEmpty(LPConfig.PreviewToken);
88+
Assert.NotEmpty(LPConfig.Host);
89+
}
90+
91+
[Fact]
92+
public async Task TestGetLivePreviewData()
93+
{
94+
// Arrange
95+
var options = new ContentstackOptions
96+
{
97+
ApiKey = "test_api_key",
98+
DeliveryToken = "test_delivery_token",
99+
Environment = "test_environment",
100+
LivePreview = new LivePreviewConfig
101+
{
102+
Enable = true,
103+
PreviewToken = "preview_token", // Replace with a valid preview token
104+
Host = "test-host" // Replace with a valid preview host (e.g., "rest-preview.contentstack.com")
105+
106+
}
107+
};
108+
109+
var client = new TestContentstackClient(options);
110+
111+
// Act
112+
var result = await client.TestGetLivePreviewData();
113+
114+
// Assert
115+
Assert.NotNull(result);
116+
Assert.Equal("mock_entry_uid", result["uid"].ToString());
117+
Assert.Equal("Mocked Entry", result["title"].ToString());
118+
}
119+
120+
}
121+
}
122+

‎Contentstack.Core.Tests/PluginsTest.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Threading.Tasks;
1+
using System.Threading.Tasks;
32
using Contentstack.Core.Models;
43
using Contentstack.Core.Tests.Models;
54
using Xunit;

‎Contentstack.Core.Tests/QueryTest.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
22
using Xunit;
3-
using Contentstack.Core;
4-
using Contentstack.Core.Configuration;
53
using Contentstack.Core.Models;
64
using System.Threading.Tasks;
75
using System.Collections.Generic;

‎Contentstack.Core.Tests/StackConfig.cs

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using System;
2-
using Contentstack.Core;
3-
using Contentstack.Core.Models;
42
using System.Configuration;
53
using Microsoft.Extensions.Options;
4+
using Contentstack.Core.Configuration;
65

76
namespace Contentstack.Core.Tests
87
{
@@ -38,13 +37,48 @@ public static ContentstackClient GetStack()
3837
Environment = environment,
3938
Host = host,
4039
Timeout = 4500,
41-
//Proxy = new System.Net.WebProxy("http://example.com:8080")
40+
//Proxy = new System.Net.WebProxy("http://example.com:8080")
4241
};
4342

4443
ContentstackClient contentstackClient = new ContentstackClient(new OptionsWrapper<Configuration.ContentstackOptions>(contentstackOptions));
4544

4645
return contentstackClient;
4746

4847
}
48+
49+
public static ContentstackClient GetLPStack()
50+
{
51+
StackConfig config = new StackConfig();
52+
if (config.assemblyConfiguration.HasFile && string.Compare(config.assemblyConfiguration.FilePath, config.currentConfiguration.FilePath, true) != 0)
53+
{
54+
config.assemblyConfiguration.SaveAs(config.currentConfiguration.FilePath);
55+
ConfigurationManager.RefreshSection("appSettings");
56+
ConfigurationManager.RefreshSection("connectionStrings");
57+
}
58+
string apiKey = ConfigurationManager.AppSettings["api_key"];
59+
string delivery_token = ConfigurationManager.AppSettings["delivery_token"];
60+
string environment = ConfigurationManager.AppSettings["environment"];
61+
string host = ConfigurationManager.AppSettings["host"];
62+
Configuration.ContentstackOptions contentstackOptions = new Configuration.ContentstackOptions
63+
{
64+
ApiKey = apiKey,
65+
DeliveryToken = delivery_token,
66+
Environment = environment,
67+
Host = host,
68+
Timeout = 4500,
69+
LivePreview = new LivePreviewConfig
70+
{
71+
Enable = true,
72+
PreviewToken = "preview_token", // Replace with a valid preview token
73+
Host = "test_host" // Replace with a valid preview host (e.g., "rest-preview.contentstack.com")
74+
}
75+
//Proxy = new System.Net.WebProxy("http://example.com:8080")
76+
};
77+
78+
ContentstackClient contentstackClient = new ContentstackClient(new OptionsWrapper<Configuration.ContentstackOptions>(contentstackOptions));
79+
80+
return contentstackClient;
81+
82+
}
4983
}
5084
}

‎Contentstack.Core.Tests/TaxonomyTest.cs

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
using System;
22
using Xunit;
3-
using Contentstack.Core;
4-
using Contentstack.Core.Configuration;
53
using Contentstack.Core.Models;
64
using System.Threading.Tasks;
7-
using System.Collections.Generic;
85
using System.Linq;
9-
using Contentstack.Core.Tests.Models;
10-
using Newtonsoft.Json.Linq;
11-
using System.Reflection.PortableExecutable;
126

137
namespace Contentstack.Core.Tests
148
{
@@ -39,7 +33,7 @@ public async Task TaxonomyExists()
3933
bool IsTrue = false;
4034
foreach (Entry data in result.Items)
4135
{
42-
IsTrue = data.GetContentType() != null;
36+
IsTrue = data.Get("_content_type_uid") != null;
4337
if (!IsTrue)
4438
{
4539
break;
@@ -69,7 +63,7 @@ public async Task TaxonomyEqualAndBelow()
6963
bool IsTrue = false;
7064
foreach (Entry data in result.Items)
7165
{
72-
IsTrue = data.GetContentType() != null;
66+
IsTrue = data.Get("_content_type_uid") != null;
7367
if (!IsTrue)
7468
{
7569
break;
@@ -99,7 +93,7 @@ public async Task TaxonomyBelow()
9993
bool IsTrue = false;
10094
foreach (Entry data in result.Items)
10195
{
102-
IsTrue = data.GetContentType() != null;
96+
IsTrue = data.Get("_content_type_uid") != null;
10397
if (!IsTrue)
10498
{
10599
break;
@@ -118,7 +112,7 @@ public async Task TaxonomyEqualAndAbove()
118112
{
119113
// Description: Taxonomy EqualAndAbove - Get Entries With Taxonomy Terms and Also Matching Its Parent Term ($eq_above, level)
120114
Taxonomy query = client.Taxonomies();
121-
query.EqualAndAbove("taxonomies.one", "term_one");
115+
query.EqualAndAbove("taxonomies.one", "term_one_child");
122116
var result = await query.Find<Entry>();
123117
if (result == null && result.Items.Count() == 0)
124118
{
@@ -129,7 +123,7 @@ public async Task TaxonomyEqualAndAbove()
129123
bool IsTrue = false;
130124
foreach (Entry data in result.Items)
131125
{
132-
IsTrue = data.GetContentType() != null;
126+
IsTrue = data.Get("_content_type_uid") != null;
133127
if (!IsTrue)
134128
{
135129
break;
@@ -148,7 +142,7 @@ public async Task TaxonomyAbove()
148142
{
149143
// Description: Taxonomy Above - Get Entries With Taxonomy Terms Parent and Excluding the term itself ($above, level)
150144
Taxonomy query = client.Taxonomies();
151-
query.Above("taxonomies.one", "term_one");
145+
query = query.Above("taxonomies.one", "term_one_child");
152146
var result = await query.Find<Entry>();
153147
if (result == null && result.Items.Count() == 0)
154148
{
@@ -157,9 +151,9 @@ public async Task TaxonomyAbove()
157151
else if (result != null)
158152
{
159153
bool IsTrue = false;
160-
foreach (Entry data in result.Items)
154+
foreach (var data in result.Items)
161155
{
162-
IsTrue = data.GetContentType() != null;
156+
IsTrue = data.Get("_content_type_uid") != null;
163157
if (!IsTrue)
164158
{
165159
break;

‎Contentstack.Core/Configuration/Config.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal class Config
1313
private string _Port;
1414
private string _Version;
1515
private string _Environment;
16-
private string _Branch;
16+
private string _Branch="main";
1717
private int _Timeout;
1818
private WebProxy _proxy;
1919
#endregion

‎Contentstack.Core/Configuration/LivePreviewConfig.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections;
3-
using Newtonsoft.Json.Linq;
1+
using Newtonsoft.Json.Linq;
42

53
namespace Contentstack.Core.Configuration
64
{
@@ -14,5 +12,7 @@ public class LivePreviewConfig
1412
internal string ContentTypeUID { get; set; }
1513
internal string EntryUID { get; set; }
1614
internal JObject PreviewResponse { get; set; }
15+
public string ReleaseId {get; set;}
16+
public string PreviewTimestamp {get; set;}
1717
}
1818
}

0 commit comments

Comments
 (0)
Please sign in to comment.