Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions UrlShortener.Tests/Controllers/GetUrlStatsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using UrlShortener.Models;

namespace UrlShortener.Tests.Controllers
{
[TestClass]
public class GetUrlStatsTests : MainControllerTests
{
[TestMethod]
public void GetUrlStatsTests_Success()
{
urlDataRepository.Setup(x => x.GetUrlStats(It.IsAny<string>())).Returns(new GetUrlStatsResponse()
{
Created_at = DateTime.Now,
Last_usage = DateTime.Now,
Usage_count = 1
});

var response = urlsController.GetUrlStats("Test12");
Assert.IsTrue(response is IActionResult);
Assert.IsTrue(response is OkObjectResult);
Assert.IsTrue(((OkObjectResult)response).Value is GetUrlStatsResponse);
}

[TestMethod]
public void GetUrlStatsTests_Code_NotFound_Error()
{
urlDataRepository.Setup(x => x.GetUrlStats("Test12")).Returns(new GetUrlStatsResponse()
{
Created_at = DateTime.Now,
Last_usage = DateTime.Now,
Usage_count = 1
});

var response = urlsController.GetUrlStats("12Test");
Assert.IsTrue(response is IActionResult);
Assert.IsTrue(response is NotFoundResult);
}

[TestMethod]
public void GetUrlStatsTests_EmptyCode_NotFound_Error()
{
var response = urlsController.GetUrlStats(String.Empty);
Assert.IsTrue(response is IActionResult);
Assert.IsTrue(response is NotFoundResult);
}
}
}
42 changes: 42 additions & 0 deletions UrlShortener.Tests/Controllers/GetUrlTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Net;
using System.Net.Http;

namespace UrlShortener.Tests.Controllers
{
[TestClass]
public class GetUrlTests : MainControllerTests
{
[TestMethod]
public void GetUrlTests_Success()
{
urlDataRepository.Setup(x => x.GetUrl(It.IsAny<string>())).Returns(new Uri("http://www.google.com/"));

var response = urlsController.GetUrl("Test12");
Assert.IsTrue(response is IActionResult);
Assert.IsTrue(response is RedirectResult);
Assert.IsTrue(((RedirectResult)response).Url.Equals("http://www.google.com/"));
}

[TestMethod]
public void GetUrlTests_Code_NotFound_Error()
{
urlDataRepository.Setup(x => x.GetUrl("Test12")).Returns(new Uri("http://www.google.com/"));

var response = urlsController.GetUrl("12Test");
Assert.IsTrue(response is IActionResult);
Assert.IsTrue(response is NotFoundResult);
}

[TestMethod]
public void GetUrlTests_EmptyCode_NotFound_Error()
{
var response = urlsController.GetUrl(String.Empty);
Assert.IsTrue(response is IActionResult);
Assert.IsTrue(response is NotFoundResult);
}
}
}
20 changes: 20 additions & 0 deletions UrlShortener.Tests/Controllers/MainControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using UrlShortener.Contracts;
using UrlShortener.Controllers;

namespace UrlShortener.Tests.Controllers
{
[TestClass]
public class MainControllerTests
{
protected Mock<IUrlDataRepository> urlDataRepository;
protected UrlsController urlsController;

public MainControllerTests()
{
urlDataRepository = new Mock<IUrlDataRepository>();
urlsController = new UrlsController(urlDataRepository.Object);
}
}
}
90 changes: 90 additions & 0 deletions UrlShortener.Tests/Controllers/PostNewUrlTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System.Net;
using UrlShortener.Models;

namespace UrlShortener.Tests.Controllers
{
[TestClass]
public class PostNewUrlTests : MainControllerTests
{
[TestMethod]
public void PostNewUrlTests_Success()
{
urlDataRepository.Setup(x => x.PostNewUrl(It.IsAny<PostNewUrlRequest>(), It.IsAny<string>())).Returns(true);

var request = new PostNewUrlRequest()
{
Code = "Test12",
Url = "http://www.google.com"
};
var response = urlsController.PostNewUrl(request);
Assert.IsTrue(response is IActionResult);
Assert.IsTrue(response is CreatedResult);
Assert.IsTrue(((CreatedResult)response).Value is PostNewUrlResponse);
}

[TestMethod]
public void PostNewUrlTests_EmptyCode_Success()
{
urlDataRepository.Setup(x => x.PostNewUrl(It.IsAny<PostNewUrlRequest>(), It.IsAny<string>())).Returns(true);

var request = new PostNewUrlRequest()
{
Code = "",
Url = "http://www.google.com"
};
var response = urlsController.PostNewUrl(request);
Assert.IsTrue(response is IActionResult);
Assert.IsTrue(response is CreatedResult);
Assert.IsTrue(((CreatedResult)response).Value is PostNewUrlResponse);
}

[TestMethod]
public void PostNewUrlTests_EmptyUrl_Error()
{
var request = new PostNewUrlRequest()
{
Code = "Test12",
Url = ""
};
var response = urlsController.PostNewUrl(request);
Assert.IsTrue(response is IActionResult);
Assert.IsTrue(response is BadRequestResult);
}

[TestMethod]
public void PostNewUrlTests_CodeConflict_Error()
{
urlDataRepository.Setup(x => x.PostNewUrl(It.IsAny<PostNewUrlRequest>(), It.IsAny<string>())).Returns(false);

var request = new PostNewUrlRequest()
{
Code = "Test12",
Url = "http://www.google.com"
};
var response = urlsController.PostNewUrl(request);
Assert.IsTrue(response is IActionResult);
Assert.IsTrue(response is ConflictResult);
}

[TestMethod]
public void PostNewUrlTests_UnprocessableCode_Error()
{
var request = new PostNewUrlRequest()
{
Code = "Test1234",
Url = "http://www.google.com"
};
var response = urlsController.PostNewUrl(request);
Assert.IsTrue(response is IActionResult);
Assert.IsTrue(response is UnprocessableEntityResult);

request.Code = "Te%&12";
response = urlsController.PostNewUrl(request);
Assert.IsTrue(response is IActionResult);
Assert.IsTrue(response is UnprocessableEntityResult);
}
}
}
22 changes: 22 additions & 0 deletions UrlShortener.Tests/UrlShortener.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>

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

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
<PackageReference Include="Moq" Version="4.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\UrlShortener\UrlShortener.csproj" />
</ItemGroup>

</Project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading