Skip to content

Commit

Permalink
GH-346 GH-336: Implement Validate() method for checkin post objects
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikfroehling committed Oct 31, 2022
1 parent d13a65e commit 43d34b0
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace TraktNet.Objects.Post.Checkins
{
using Exceptions;
using Get.Episodes;
using Get.Shows;
using Objects.Json;
using System;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -32,22 +32,30 @@ public override Task<string> ToJson(CancellationToken cancellationToken = defaul
public override void Validate()
{
if (Episode == null)
throw new ArgumentNullException(nameof(Episode), "episode must not be null");

if (Episode.Number < 1)
throw new ArgumentOutOfRangeException(nameof(Episode.Number), "episode number must be at least 1");

if (Episode.SeasonNumber < 1)
throw new ArgumentOutOfRangeException(nameof(Episode.SeasonNumber), "episode season number must be at least 1");

if (Episode.Ids == null)
throw new ArgumentNullException(nameof(Episode.Ids), "episode.Ids must not be null");

if (!Episode.Ids.HasAnyId)
throw new ArgumentException("episode.Ids have no valid id", nameof(Episode.Ids));

if (Show != null && string.IsNullOrEmpty(Show.Title))
throw new ArgumentException("show title not valid", nameof(Show.Title));
throw new TraktPostValidationException(nameof(Episode), "episode must not be null");

if (Show == null)
{
if (Episode.Ids == null)
throw new TraktPostValidationException($"{nameof(Episode)}.Ids", "episode ids must not be null");

if (!Episode.Ids.HasAnyId)
throw new TraktPostValidationException($"{nameof(Episode)}.Ids", "episode ids have no valid id");
}
else
{
if (Show.Ids == null)
throw new TraktPostValidationException($"{nameof(Show)}.Ids", "show ids must not be null");

if (!Show.Ids.HasAnyId)
throw new TraktPostValidationException($"{nameof(Show)}.Ids", "show ids have no valid id");

if (Episode.SeasonNumber < 0)
throw new TraktPostValidationException($"{nameof(Episode)}.SeasonNumber", "episode season number must be valid, if episode ids not valid or empty");

if (Episode.Number < 1)
throw new TraktPostValidationException($"{nameof(Episode)}.Number", "episode number must be valid, if episode ids not valid or empty");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace TraktNet.Objects.Post.Checkins
{
using Exceptions;
using Get.Movies;
using Objects.Json;
using System;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -24,19 +24,13 @@ public override Task<string> ToJson(CancellationToken cancellationToken = defaul
public override void Validate()
{
if (Movie == null)
throw new ArgumentNullException(nameof(Movie), "movie must not be null");

if (string.IsNullOrEmpty(Movie.Title))
throw new ArgumentException("movie title not valid", nameof(Movie.Title));

if (Movie.Year <= 0 || Movie.Year.ToString().Length != 4)
throw new ArgumentOutOfRangeException(nameof(Movie), "movie year not valid");
throw new TraktPostValidationException(nameof(Movie), "movie must not be null");

if (Movie.Ids == null)
throw new ArgumentNullException(nameof(Movie.Ids), "movie.Ids must not be null");
throw new TraktPostValidationException($"{nameof(Movie)}.Ids", "movie ids must not be null");

if (!Movie.Ids.HasAnyId)
throw new ArgumentException("movie.Ids have no valid id", nameof(Movie.Ids));
throw new TraktPostValidationException($"{nameof(Movie)}.Ids", "movie ids have no valid id");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1302,27 +1302,7 @@ public async Task Test_TraktCheckinsModule_CheckIntoEpisodeWithShow_ArgumentExce
TraktClient client = TestUtility.GetOAuthMockClient(CHECKIN_URI, postJson, EPISODE_CHECKIN_POST_RESPONSE_JSON);

Func<Task<TraktResponse<ITraktEpisodeCheckinPostResponse>>> act = () => client.Checkins.CheckIntoEpisodeWithShowAsync(null, show);
await act.Should().ThrowAsync<ArgumentNullException>();

act = () => client.Checkins.CheckIntoEpisodeWithShowAsync(episode, null);
await act.Should().ThrowAsync<ArgumentNullException>();

episode.Number = -1;

act = () => client.Checkins.CheckIntoEpisodeWithShowAsync(episode, show);
await act.Should().ThrowAsync<ArgumentOutOfRangeException>();

episode.Number = 1;
episode.SeasonNumber = -1;

act = () => client.Checkins.CheckIntoEpisodeWithShowAsync(episode, show);
await act.Should().ThrowAsync<ArgumentOutOfRangeException>();

episode.SeasonNumber = 1;
show.Title = string.Empty;

act = () => client.Checkins.CheckIntoEpisodeWithShowAsync(episode, show);
await act.Should().ThrowAsync<ArgumentException>();
await act.Should().ThrowAsync<TraktPostValidationException>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1042,18 +1042,18 @@ public async Task Test_TraktCheckinsModule_CheckIntoEpisode_ArgumentExceptions()
TraktClient client = TestUtility.GetOAuthMockClient(CHECKIN_URI, postJson, EPISODE_CHECKIN_POST_RESPONSE_JSON);

Func<Task<TraktResponse<ITraktEpisodeCheckinPostResponse>>> act = () => client.Checkins.CheckIntoEpisodeAsync(null);
await act.Should().ThrowAsync<ArgumentNullException>();
await act.Should().ThrowAsync<TraktPostValidationException>();

act = () => client.Checkins.CheckIntoEpisodeAsync(new TraktEpisode());
await act.Should().ThrowAsync<ArgumentNullException>();
await act.Should().ThrowAsync<TraktPostValidationException>();

episode = new TraktEpisode
{
Ids = new TraktEpisodeIds()
};

act = () => client.Checkins.CheckIntoEpisodeAsync(episode);
await act.Should().ThrowAsync<ArgumentException>();
await act.Should().ThrowAsync<TraktPostValidationException>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -990,33 +990,17 @@ public async Task Test_TraktCheckinsModule_CheckIntoMovie_ArgumentExceptions()
TraktClient client = TestUtility.GetOAuthMockClient(CHECKIN_URI, postJson, MOVIE_CHECKIN_POST_RESPONSE_JSON);

Func<Task<TraktResponse<ITraktMovieCheckinPostResponse>>> act = () => client.Checkins.CheckIntoMovieAsync(null);
await act.Should().ThrowAsync<ArgumentNullException>();
await act.Should().ThrowAsync<TraktPostValidationException>();

movie.Year = 0;

act = () => client.Checkins.CheckIntoMovieAsync(movie);
await act.Should().ThrowAsync<ArgumentOutOfRangeException>();

movie.Year = 123;

act = () => client.Checkins.CheckIntoMovieAsync(movie);
await act.Should().ThrowAsync<ArgumentOutOfRangeException>();

movie.Year = 12345;

act = () => client.Checkins.CheckIntoMovieAsync(movie);
await act.Should().ThrowAsync<ArgumentOutOfRangeException>();

movie.Year = 2014;
movie.Ids = null;

act = () => client.Checkins.CheckIntoMovieAsync(movie);
await act.Should().ThrowAsync<ArgumentNullException>();
await act.Should().ThrowAsync<TraktPostValidationException>();

movie.Ids = new TraktMovieIds();

act = () => client.Checkins.CheckIntoMovieAsync(movie);
await act.Should().ThrowAsync<ArgumentException>();
await act.Should().ThrowAsync<TraktPostValidationException>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public TraktCheckinsModule_Tests()
}
};

Show = new TraktShow { Title = "Breaking Bad" };
Show = new TraktShow { Title = "Breaking Bad", Ids = new TraktShowIds { Trakt = 1 } };
}

private const string MOVIE_CHECKIN_POST_RESPONSE_JSON =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace TraktNet.Objects.Post.Tests.Checkins.Implementations
{
using FluentAssertions;
using System;
using Trakt.NET.Tests.Utility.Traits;
using TraktNet.Exceptions;
using TraktNet.Objects.Get.Episodes;
using TraktNet.Objects.Get.Shows;
using TraktNet.Objects.Post.Checkins;
using Xunit;

[Category("Objects.Post.Checkins.Implementations")]
public class TraktEpisodeCheckinPost_Tests
{
[Fact]
public void Test_TraktEpisodeCheckinPost_Validate()
{
ITraktEpisodeCheckinPost episodeCheckinPost = new TraktEpisodeCheckinPost();

// Episode = null, Show = null
Action act = () => episodeCheckinPost.Validate();
act.Should().Throw<TraktPostValidationException>();

// Episode != null, Show = null, Episode Ids = null
episodeCheckinPost.Episode = new TraktEpisode();
act.Should().Throw<TraktPostValidationException>();

// Episode != null, Show = null, Episode Ids have no valid id
episodeCheckinPost.Episode = new TraktEpisode { Ids = new TraktEpisodeIds() };
act.Should().Throw<TraktPostValidationException>();

// Episode != null, Show = null, Episode Ids = valid
episodeCheckinPost.Episode = new TraktEpisode { Ids = new TraktEpisodeIds { Trakt = 1 } };
act.Should().NotThrow();

// Episode != null, Show != null, Show Ids = null
episodeCheckinPost.Episode = new TraktEpisode();
episodeCheckinPost.Show = new TraktShow();
act.Should().Throw<TraktPostValidationException>();

// Episode != null, Show != null, Show Ids have no valid id
episodeCheckinPost.Episode = new TraktEpisode();
episodeCheckinPost.Show = new TraktShow { Ids = new TraktShowIds() };
act.Should().Throw<TraktPostValidationException>();

// Episode != null, Show != null, Show Ids = valid, Episode Season Number not valid
episodeCheckinPost.Episode = new TraktEpisode { SeasonNumber = -1, Number = 1 };
episodeCheckinPost.Show = new TraktShow { Ids = new TraktShowIds { Trakt = 1 } };
act.Should().Throw<TraktPostValidationException>();

// Episode != null, Show != null, Show Ids = valid, Episode Number not valid
episodeCheckinPost.Episode = new TraktEpisode { SeasonNumber = 0, Number = 0 };
episodeCheckinPost.Show = new TraktShow { Ids = new TraktShowIds { Trakt = 1 } };
act.Should().Throw<TraktPostValidationException>();

// Episode != null, Show != null, Show Ids = valid, Episode Numbers are valid
episodeCheckinPost.Episode = new TraktEpisode { SeasonNumber = 0, Number = 1 };
episodeCheckinPost.Show = new TraktShow { Ids = new TraktShowIds { Trakt = 1 } };
act.Should().NotThrow();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace TraktNet.Objects.Post.Tests.Checkins.Implementations
{
using FluentAssertions;
using System;
using Trakt.NET.Tests.Utility.Traits;
using TraktNet.Exceptions;
using TraktNet.Objects.Get.Movies;
using TraktNet.Objects.Post.Checkins;
using Xunit;

[Category("Objects.Post.Checkins.Implementations")]
public class TraktMovieCheckinPost_Tests
{
[Fact]
public void Test_TraktMovieCheckinPost_Validate()
{
ITraktMovieCheckinPost movieCheckinPost = new TraktMovieCheckinPost();

// Movie = null
Action act = () => movieCheckinPost.Validate();
act.Should().Throw<TraktPostValidationException>();

// Movie Ids = null
movieCheckinPost.Movie = new TraktMovie();
act.Should().Throw<TraktPostValidationException>();

// Movie IDs have no valid id
movieCheckinPost.Movie = new TraktMovie { Ids = new TraktMovieIds() };
act.Should().Throw<TraktPostValidationException>();

// valid
movieCheckinPost.Movie = new TraktMovie { Ids = new TraktMovieIds { Trakt = 1 } };
act.Should().NotThrow();
}
}
}

0 comments on commit 43d34b0

Please sign in to comment.