Skip to content

Commit

Permalink
Merge pull request #51 from EnessenE/feature/system.text
Browse files Browse the repository at this point in the history
Convert Newtsonsoft.Json into System.Text.Json
  • Loading branch information
EnessenE authored Dec 5, 2022
2 parents 1cee95c + 7749056 commit 9f6f331
Show file tree
Hide file tree
Showing 54 changed files with 680 additions and 372 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: 6.0.x
dotnet-version: |
6.0.x
7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ jobs:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
with:
dotnet-version: |
6.0.x
7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- System.Text.Json, replaces Newtsonsoft, possibly breaking
- Add Telegram as tag for the project
- Multi target to .NET 7 and use .NET 7 included System.Text.Json
### Changed
- Enable "Treat Warnings as Errors" to adhere to code guidelines
- Give internal response body messagerrorcode the correct type
- Some internal refactoring
- Changed PackageIconUrl > PackageIcon and added icon manually as it PackageIconUrl was deprecated
- Change RichContent interface to support derived types
### Removed
- Newtsonsoft.Json dependency

## [2.5.2] - 2022-10-14
- Enable nupkg generation in project

Expand Down
40 changes: 40 additions & 0 deletions CM.Text.NET6.Tests/BuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using CM.Text.BusinessMessaging;
using CM.Text.BusinessMessaging.Model;
using CM.Text.BusinessMessaging.Model.MultiChannel;
using FluentAssertions;

namespace CM.Text.NET6.Tests
{
[TestClass]
public class BuilderTests
{
[TestMethod]
public void BuildTest()
{
var builder = new MessageBuilder("Message Text", "Sender_name", "Recipient_PhoneNumber");

var mediaName = "cm.com";
var mediaUri = "https://avatars3.githubusercontent.com/u/8234794?s=200&v=4";
var mediaType = "image/png";

builder
.WithAllowedChannels(Channel.WhatsApp)
.WithRichMessage(
new MediaMessage(
mediaName,
mediaUri,
mediaType
)
);
var message = builder.Build();

message.Should().NotBeNull();
message.RichContent.Conversation.Should().NotBeNull();
message.RichContent.Conversation.Length.Should().Be(1);
var media = (MediaMessage) message.RichContent.Conversation.First();
media.Media.MediaName.Should().Be(mediaName);
media.Media.MediaUri.Should().Be(mediaUri);
media.Media.MimeType.Should().Be(mediaType);
}
}
}
97 changes: 97 additions & 0 deletions CM.Text.NET6.Tests/BusinessMessagingApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using CM.Text.BusinessMessaging;
using CM.Text.BusinessMessaging.Model;
using CM.Text.BusinessMessaging.Model.MultiChannel;
using FluentAssertions;

namespace CM.Text.NET6.Tests
{
[TestClass]
public class BusinessMessagingApiTests
{
[TestMethod]
public void TestPostBody()
{
var guid = Guid.NewGuid();
var message = "This is a unit test";
var sender = "CM.com";
var reference = "ReferenceForMeToFind";
var number1 = "0031612345678";
var number2 = "0031612345679";

var data = BusinessMessagingApi.GetHttpPostBody(guid, message, sender,
new[] {number1, number2}, reference);

data.Should().NotBeNull();
//Simple to check if all values survived our logic
data.Should().Contain(guid.ToString());
data.Should().Contain(message);
data.Should().Contain(sender);
data.Should().Contain(reference);
data.Should().Contain(number1);
data.Should().Contain(number2);
}

[TestMethod]
public void TestRichPostBody()
{
var builder = new MessageBuilder("Message Text", "Sender_name", "Recipient_PhoneNumber");

var mediaName = "cm.com icon";
var mediaUri = "https://avatars3.githubusercontent.com/u/8234794";
var mediaType = "image/png";

builder
.WithAllowedChannels(Channel.WhatsApp)
.WithRichMessage(
new MediaMessage(
mediaName,
mediaUri,
mediaType
)
);
var message = builder.Build();

Guid fakeApiKey = Guid.NewGuid();
var data = BusinessMessagingApi.GetHttpPostBody(fakeApiKey, message);

data.Should().NotBeNull();
//Simple to check if all values survived our logic
data.Should().Contain(fakeApiKey.ToString(), "the api key should be present in the body");
data.Should().Contain(mediaName, "the media name needs to be sent");
data.Should().Contain(mediaType, "the media type has to be sent");
data.Should().Contain(mediaUri, "the media url has to be sent");
}


[TestMethod]
public void TestResponseBody()
{
var guid = Guid.NewGuid();
// Arrange
string message = @"{
""messages"": [{
""to"": ""0031612345678"",
""parts"": 1,
""status"": ""Accepted"",
""reference"": ""test-reference-1"",
""messageErrorCode"": 0,
""messageDetails"": null
}],
""details"": ""Created 1 message(s)"",
""errorCode"": 0
}";


var data = BusinessMessagingApi.GetTextApiResult(message);

data.Should().NotBeNull();
//Simple to check if all values survived our logic
data.details.Should().NotBeNull();
data.details.First().reference.Should().Be("test-reference-1");
data.details.First().status.Should().Be("Accepted");
data.details.First().to.Should().Be("0031612345678");
data.details.First().parts.Should().Be(1);
data.details.First().details.Should().BeNull();
}
}
}
23 changes: 23 additions & 0 deletions CM.Text.NET6.Tests/CM.Text.NET6.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CM.Text\CM.Text.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions CM.Text.NET6.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
21 changes: 19 additions & 2 deletions CM.Text.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29102.190
# Visual Studio Version 17
VisualStudioVersion = 17.2.32616.157
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CM.Text", "CM.Text\CM.Text.csproj", "{1EB6D5AC-4759-4A77-8D9D-F5B23C254B7A}"
EndProject
Expand All @@ -12,6 +12,16 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CM.Text.NET6.Tests", "CM.Text.NET6.Tests\CM.Text.NET6.Tests.csproj", "{5389A890-EBC2-45F0-95E3-374B0BD239FF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{8AC75C4D-CF2F-4E5F-85D2-4BB92C8ED1DE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{83568C54-6C9B-4579-BC2A-E6739DE072D5}"
ProjectSection(SolutionItems) = preProject
.github\workflows\main.yml = .github\workflows\main.yml
.github\workflows\nuget.yml = .github\workflows\nuget.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -22,10 +32,17 @@ Global
{1EB6D5AC-4759-4A77-8D9D-F5B23C254B7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1EB6D5AC-4759-4A77-8D9D-F5B23C254B7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1EB6D5AC-4759-4A77-8D9D-F5B23C254B7A}.Release|Any CPU.Build.0 = Release|Any CPU
{5389A890-EBC2-45F0-95E3-374B0BD239FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5389A890-EBC2-45F0-95E3-374B0BD239FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5389A890-EBC2-45F0-95E3-374B0BD239FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5389A890-EBC2-45F0-95E3-374B0BD239FF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{83568C54-6C9B-4579-BC2A-E6739DE072D5} = {8AC75C4D-CF2F-4E5F-85D2-4BB92C8ED1DE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {33F57A4C-CDF2-44F4-8F81-45D5C71E3DE1}
EndGlobalSection
Expand Down
15 changes: 3 additions & 12 deletions CM.Text/BusinessMessaging/BusinessMessagingApi.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using CM.Text.BusinessMessaging.Model;
using Newtonsoft.Json;

namespace CM.Text.BusinessMessaging
{
Expand Down Expand Up @@ -37,7 +37,7 @@ internal static string GetHttpPostBody(
/// <returns></returns>
internal static string GetHttpPostBody(Guid apiKey, Message message)
{
return JsonConvert.SerializeObject(
return JsonSerializer.Serialize(
new
{
messages = new Request.MessagesEnvelope
Expand All @@ -56,7 +56,7 @@ internal static string GetHttpPostBody(Guid apiKey, Message message)
/// <returns></returns>
internal static TextClientResult GetTextApiResult(string requestResultContent)
{
var deserializedResponse = JsonConvert.DeserializeObject<Response.HttpResponseBody>(requestResultContent);
var deserializedResponse = JsonSerializer.Deserialize<HttpResponseBody>(requestResultContent);

return new TextClientResult
{
Expand All @@ -75,14 +75,5 @@ internal static TextClientResult GetTextApiResult(string requestResultContent)
.ToArray()
};
}

internal static class Constant
{
internal const string BusinessMessagingGatewayJsonEndpoint = "https://gw.cmtelecom.com/v1.0/message";
internal const string BusinessMessagingGatewayMediaTypeJson = "application/json";
internal const string BusinessMessagingBodyTypeAuto = "AUTO";
internal const int BusinessMessagingMessagePartsMinDefault = 1;
internal const int BusinessMessagingMessagePartsMaxDefault = 8;
}
}
}
4 changes: 2 additions & 2 deletions CM.Text/BusinessMessaging/MessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public MessageBuilder(string messageText, string from, params string[] to)
Body = new Body
{
Content = messageText,
Type = BusinessMessagingApi.Constant.BusinessMessagingBodyTypeAuto
Type = Constant.BusinessMessagingBodyTypeAuto
},
Recipients = to.Select(toEntry => new Recipient { Number = toEntry })
.ToArray(),
Expand Down Expand Up @@ -133,7 +133,7 @@ public MessageBuilder WithSuggestions(params SuggestionBase[] suggestions)
}

/// <summary>
/// Used for Hybrid messaging, see https://docs.cmtelecom.com/en/hybrid-messaging/v2.0.0 for more information
/// Used for Hybrid messaging, see https://developers.cm.com/messaging/docs for more information
/// Messages will be sent over the <see cref="Channel.Push" /> channel.
/// </summary>
public MessageBuilder WitHybridAppKey(Guid appKey)
Expand Down
9 changes: 5 additions & 4 deletions CM.Text/BusinessMessaging/Model/Body.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using JetBrains.Annotations;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using JetBrains.Annotations;

namespace CM.Text.BusinessMessaging.Model
{
Expand All @@ -22,7 +22,7 @@ public class Body
/// Another note is that not all operators in the world are able to handle Unicode messages, so you will need to test
/// for which operators it works.
/// </summary>
[JsonProperty("content")]
[JsonPropertyName("content")]
public string Content { get; set; }

/// <summary>
Expand All @@ -34,7 +34,8 @@ public class Body
/// You can limit the number of parts by setting the maximum number of message parts.
/// <see cref="Message.MaximumNumberOfMessageParts" />
/// </summary>
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonPropertyName("type")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string Type { get; set; }
}
}
4 changes: 3 additions & 1 deletion CM.Text/BusinessMessaging/Model/Channel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using JetBrains.Annotations;

namespace CM.Text.BusinessMessaging.Model
Expand All @@ -12,6 +13,7 @@ namespace CM.Text.BusinessMessaging.Model
/// For those flows to work, we need to be contacted.
/// </remarks>
[PublicAPI]
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum Channel
{
/// <summary>
Expand All @@ -29,7 +31,7 @@ public enum Channel

/// <summary>
/// Sends messages to push using Hybrid messages.
/// See also https://docs.cmtelecom.com/en/hybrid-messaging/v2.0.0
/// See also https://developers.cm.com/messaging/docs
/// </summary>
/// <remarks>Works only when <see cref="Message.HybridAppKey" /> is set</remarks>
Push,
Expand Down
29 changes: 29 additions & 0 deletions CM.Text/BusinessMessaging/Model/HttpResponseBody.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Text.Json.Serialization;

namespace CM.Text.BusinessMessaging.Model
{

/// <summary>
/// CM Messaging API response body containing API related info
/// </summary>
public class HttpResponseBody
{
/// <summary>
/// API Request details
/// </summary>
[JsonInclude]
public string details { get; private set; }

/// <summary>
/// JSON POST Error codes. Full description of each code available in the development documentation
/// </summary>
[JsonInclude]
public int errorCode { get; private set; }

/// <summary>
/// Each message that was sent in the original request
/// </summary>
[JsonInclude]
public ResponseMessageDetail[] messages { get; private set; }
}
}
Loading

0 comments on commit 9f6f331

Please sign in to comment.