Walkthrough: using Stylelabs.M.SDK.Fluent through the Web Client SDK
This section walks you through the following:
Create a project
The following examples use the .NET Core CLI, you can use the equivalent functionality in your preferred IDE.
In order to create a new project execute the following commands:
mkdir Walkthrough
cd Walkthrough
dotnet new integration-tests --framework net6.0
Once the project is created a reference to Content Hub SDK Fluent libraries is required:
dotnet add package Stylelabs.M.Sdk.Fluent.Testing --source https://nuget.sitecore.com/resources/v3/index.json
Create a base class
These examples use xUnit as testing framework
Create a base class that takes care all the common functionality, for example, creating the SDK client.
using System;
using Stylelabs.M.Sdk.Fluent;
using Stylelabs.M.Sdk.WebClient.Authentication;
namespace Integration.Tests.Walkthrough
{
public abstract class TestBase
{
protected readonly IWebMClient _mClient;
protected readonly FluentClient _fluentClient;
protected TestBase()
{
_mClient = CreateMClient();
_fluentClient = new FluentClient(_mClient);
}
private static IWebMClient CreateMClient()
{
var endpoint = new Uri(Configuration.Authentication.Url);
var oath = new OAuthPasswordGrant
{
ClientId = Configuration.Authentication.ClientId,
ClientSecret = Configuration.Authentication.ClientSecret,
Password = Configuration.Authentication.Password,
UserName = Configuration.Authentication.Username
};
return Stylelabs.M.Sdk.WebClient.MClientFactory.CreateMClient(endpoint, oath);
}
}
}
Create entities
Create a new test class. The tests on this class will:
-
Create a new
Entity Definition
and validate its members -
Create a new
Entity
and validate its members
using System.Collections.Generic;
using System.Globalization;
using Stylelabs.M.Sdk.Fluent.Testing.Extensions;
using Stylelabs.M.Sdk.Models.Base.PropertyDefinitions;
using Stylelabs.M.Sdk.WebClient.IntegrationTests.Fluent.Wrappers;
using Xunit;
namespace Sdk.Fluent.Testing.Walkthrough
{
public class NewEntities : FluentTestBase
{
[Fact]
public void Create_New_EntityDefinition()
{
var definition = _fluentClient.EntityDefinitions
.New("My.Definition")
.NewMemberGroup("MyGroup", overviewGroup = overviewGroup
.AddPropertyDefinition<StringPropertyDefinition> (
"myStringProperty",
stringPropertyDefinition =
{
stringPropertyDefinition.IncludedInContent = true;
stringPropertyDefinition.IsMultiValue = false;
stringPropertyDefinition.Labels = new Dictionary<CultureInfo, string>
{ { CultureInfo.InvariantCulture, "myStringProperty" } };
})
.AddPropertyDefinition<BooleanPropertyDefinition>("myBooleanProperty")
.AddPropertyDefinition<IntegerPropertyDefinition>("myIntProperty"))
.Save();
definition
.AssertMemberGroup("MyGroup")
.MemberGroup("MyGroup", group = group
.AssertPropertyExists("myStringProperty")
.AssertPropertyExists("myBooleanProperty")
.AssertPropertyExists("myIntProperty"));
}
[Fact]
public void Create_New_Entity()
{
var entity = _fluentClient.EntityFactory
.Create("My.Definition")
.SetProperty("myStringProperty", "this is an string value")
.SetProperty("myBooleanProperty", true)
.SetProperty("myIntProperty", 100)
.Save();
entity
.AssertPropertyEqual("myBooleanProperty", true)
.AssertPropertyNotEmpty("myStringProperty")
.AssertPropertyGreaterOrEqual("myIntProperty", 100);
}
}
}
Query entities
Create a new test class. The tests on this class will:
-
Query
Entities
by entity definition -
Query
Entity
by id -
Query many
Entities
using Stylelabs.M.Sdk.Fluent.Models;
using Stylelabs.M.Sdk.Fluent.Testing.Extensions;
using Stylelabs.M.Sdk.WebClient.IntegrationTests.Fluent.Wrappers;
using Xunit;
namespace Sdk.Fluent.Testing.Walkthrough
{
public class QueryEntities : FluentTestBase
{
[Fact]
public void Query_Entity_ByEntityDefinition()
{
var entities = _fluentClient.Entities
.GetByDefinition("My.Definition");
foreach (FluentEntity entity in entities)
{
entity
.AssertPropertyEqual("myBooleanProperty", true)
.AssertPropertyNotEmpty("myStringProperty")
.AssertPropertyEqual("myIntProperty", 100);
}
}
[Fact]
public void Query_Many_Entities()
{
var entities = _fluentClient.Entities
.GetMany(new long[] { 12345, 67890 }); // this should be a list of valid ids
foreach (FluentEntity entity in entities)
{
entity
.AssertPropertyEqual("myBooleanProperty", true)
.AssertPropertyNotEmpty("myStringProperty")
.AssertPropertyEqual("myIntProperty", 100);
}
}
[Fact]
public void Query_Entity_ById()
{
_fluentClient.Entities
.Get(12345) // this should be a valid id
.AssertPropertyEqual("myBooleanProperty", true)
.AssertPropertyNotEmpty("myStringProperty")
.AssertPropertyNotEqual("myIntProperty", 10);
}
}
}