Skip to content

Commit

Permalink
Refactoring of ScreenshotsService and ScreenshotsRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
midwan committed Jun 5, 2016
1 parent 298002b commit e0872ec
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 183 deletions.
27 changes: 12 additions & 15 deletions Amigula.Domain.Test/Services/GamesServiceTest.cs
Expand Up @@ -62,16 +62,13 @@ public class GamesServiceTest
};

private IGamesRepository _gamesRepository;
private IFileOperations _fileOperations;
private GamesService _gamesService;

[TestInitialize]
public void Initialize()
{
_gamesRepository = A.Fake<IGamesRepository>();
_fileOperations = A.Fake<IFileOperations>();

_gamesService = new GamesService(_gamesRepository, _fileOperations);
_gamesService = new GamesService(_gamesRepository);
}

[TestMethod]
Expand All @@ -90,7 +87,7 @@ public void GetGamesList_ReturnsGamesDtos()
[TestMethod]
public void GetGameDisks_GameWithOneDisk_ReturnsFullPathString()
{
A.CallTo(() => _fileOperations.FilenameExists(A<string>.Ignored))
A.CallTo(() => _gamesRepository.IsGameExists(A<string>.Ignored))
.Returns(true)
.Once();
const string gameFilename = "International Karate Plus.adf";
Expand All @@ -105,7 +102,7 @@ public void GetGameDisks_GameWithOneDisk_ReturnsFullPathString()
[TestMethod]
public void GetGameDisks_GameWithFourDisksMethod1_ReturnsFullPathStringForAllDisks()
{
A.CallTo(() => _fileOperations.FilenameExists(A<string>.Ignored))
A.CallTo(() => _gamesRepository.IsGameExists(A<string>.Ignored))
.Returns(true)
.NumberOfTimes(3);

Expand All @@ -124,7 +121,7 @@ public void GetGameDisks_GameWithFourDisksMethod1_ReturnsFullPathStringForAllDis
[TestMethod]
public void GetGameDisks_GameWithFourDisksMethod2_ReturnsFullPathStringForAllDisks()
{
A.CallTo(() => _fileOperations.FilenameExists(A<string>.Ignored))
A.CallTo(() => _gamesRepository.IsGameExists(A<string>.Ignored))
.Returns(true)
.NumberOfTimes(3);

Expand All @@ -143,7 +140,7 @@ public void GetGameDisks_GameWithFourDisksMethod2_ReturnsFullPathStringForAllDis
[TestMethod]
public void GetGameDisks_GameWithFourDisksMethod3_ReturnsFullPathStringForAllDisks()
{
A.CallTo(() => _fileOperations.FilenameExists(A<string>.Ignored))
A.CallTo(() => _gamesRepository.IsGameExists(A<string>.Ignored))
.Returns(true)
.NumberOfTimes(3);
const string gameFilename = "Mortal Kombat (Disk 1 of 4).zip";
Expand All @@ -161,7 +158,7 @@ public void GetGameDisks_GameWithFourDisksMethod3_ReturnsFullPathStringForAllDis
[TestMethod]
public void GetGameDisks_GameWithFourDisksMethod4_ReturnsFullPathStringForAllDisks()
{
A.CallTo(() => _fileOperations.FilenameExists(A<string>.Ignored))
A.CallTo(() => _gamesRepository.IsGameExists(A<string>.Ignored))
.Returns(true)
.NumberOfTimes(3);
const string gameFilename = "Mortal Kombat (Disk 01 of 04).zip";
Expand All @@ -179,7 +176,7 @@ public void GetGameDisks_GameWithFourDisksMethod4_ReturnsFullPathStringForAllDis
[TestMethod]
public void GetGameDisks_GameWithFoudDisksMethod5_ReturnsFullPathStringForAllDisks()
{
A.CallTo(() => _fileOperations.FilenameExists(A<string>.Ignored))
A.CallTo(() => _gamesRepository.IsGameExists(A<string>.Ignored))
.Returns(true)
.NumberOfTimes(3);
const string gameFilename = "Mortal Kombat-1.zip";
Expand All @@ -197,7 +194,7 @@ public void GetGameDisks_GameWithFoudDisksMethod5_ReturnsFullPathStringForAllDis
[TestMethod]
public void GetGameDisks_GameWithElevenDisksMethod1_ReturnsFullPathStringForAllDisks()
{
A.CallTo(() => _fileOperations.FilenameExists(A<string>.Ignored))
A.CallTo(() => _gamesRepository.IsGameExists(A<string>.Ignored))
.Returns(true)
.NumberOfTimes(10);
const string gameFilename = "Mortal Kombat Disk1.zip";
Expand All @@ -215,7 +212,7 @@ public void GetGameDisks_GameWithElevenDisksMethod1_ReturnsFullPathStringForAllD
[TestMethod]
public void GetGameDisks_GameWithElevenDisksMethod2_ReturnsFullPathStringForAllDisks()
{
A.CallTo(() => _fileOperations.FilenameExists(A<string>.Ignored))
A.CallTo(() => _gamesRepository.IsGameExists(A<string>.Ignored))
.Returns(true)
.NumberOfTimes(10);
const string gameFilename = "Mortal Kombat Disk01.zip";
Expand All @@ -233,7 +230,7 @@ public void GetGameDisks_GameWithElevenDisksMethod2_ReturnsFullPathStringForAllD
[TestMethod]
public void GetGameDisks_GameWithElevenDisksMethod3_ReturnsFullPathStringForAllDisks()
{
A.CallTo(() => _fileOperations.FilenameExists(A<string>.Ignored))
A.CallTo(() => _gamesRepository.IsGameExists(A<string>.Ignored))
.Returns(true)
.NumberOfTimes(10);
const string gameFilename = "Mortal Kombat (Disk 1 of 11).zip";
Expand All @@ -251,7 +248,7 @@ public void GetGameDisks_GameWithElevenDisksMethod3_ReturnsFullPathStringForAllD
[TestMethod]
public void GetGameDisks_GameWithElevenDisksMethod4_ReturnsFullPathStringForAllDisks()
{
A.CallTo(() => _fileOperations.FilenameExists(A<string>.Ignored))
A.CallTo(() => _gamesRepository.IsGameExists(A<string>.Ignored))
.Returns(true)
.NumberOfTimes(10);
const string gameFilename = "Mortal Kombat (Disk 01 of 11).zip";
Expand All @@ -269,7 +266,7 @@ public void GetGameDisks_GameWithElevenDisksMethod4_ReturnsFullPathStringForAllD
[TestMethod]
public void GetGameDisks_GameWithElevenDisksMethod5_ReturnsFullPathStringforAllDisks()
{
A.CallTo(() => _fileOperations.FilenameExists(A<string>.Ignored))
A.CallTo(() => _gamesRepository.IsGameExists(A<string>.Ignored))
.Returns(true)
.NumberOfTimes(10);
const string gameFilename = "Mortal Kombat-1.zip";
Expand Down
61 changes: 32 additions & 29 deletions Amigula.Domain.Test/Services/ScreenshotsServiceTest.cs
Expand Up @@ -17,17 +17,16 @@ public class ScreenshotsServiceTest
public void Initialize()
{
_screenshotsRepository = A.Fake<IScreenshotsRepository>();

_screenshotsService = new ScreenshotsService(_screenshotsRepository);
}

[TestMethod]
public void PrepareTitleScreenshot_GameTitle_ReturnsGameScreenshotsDto()
public void GetGameScreenshots_GameTitle_ReturnsGameScreenshotsDto()
{
const string gameTitle = "Apidya v1.2 (1990) [Publisher name]";
A.CallTo(() => _screenshotsRepository.GetTitleSubfolder(A<string>.Ignored)).Returns("A\\");
A.CallTo(() => _screenshotsRepository.GetGameSubfolder(A<string>.Ignored)).Returns("A\\");

var result = _screenshotsService.PrepareTitleScreenshot(gameTitle);
var result = _screenshotsService.GetGameScreenshots(gameTitle);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(ScreenshotsDto));
Expand All @@ -39,12 +38,12 @@ public void PrepareTitleScreenshot_GameTitle_ReturnsGameScreenshotsDto()
}

[TestMethod]
public void PrepareTitleScreenshot_GameTitleWithSpaces_ReturnsGameScreenshotsDto()
public void GetGameScreenshots_GameTitleWithSpaces_ReturnsGameScreenshotsDto()
{
const string gameTitle = "International Karate Plus v1.3 (1988) [Publisher name]";
A.CallTo(() => _screenshotsRepository.GetTitleSubfolder(A<string>.Ignored)).Returns("I\\");
A.CallTo(() => _screenshotsRepository.GetGameSubfolder(A<string>.Ignored)).Returns("I\\");

var result = _screenshotsService.PrepareTitleScreenshot(gameTitle);
var result = _screenshotsService.GetGameScreenshots(gameTitle);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(ScreenshotsDto));
Expand All @@ -56,12 +55,12 @@ public void PrepareTitleScreenshot_GameTitleWithSpaces_ReturnsGameScreenshotsDto
}

[TestMethod]
public void PrepareTitleScreenshot_NumericGameTitle_ReturnsGameScreenshotsDto()
public void GetGameScreenshots_NumericGameTitle_ReturnsGameScreenshotsDto()
{
const string gameTitle = "1942 v1.3 (1988) [Publisher name]";
A.CallTo(() => _screenshotsRepository.GetTitleSubfolder(A<string>.Ignored)).Returns("0\\");
A.CallTo(() => _screenshotsRepository.GetGameSubfolder(A<string>.Ignored)).Returns("0\\");

var result = _screenshotsService.PrepareTitleScreenshot(gameTitle);
var result = _screenshotsService.GetGameScreenshots(gameTitle);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(ScreenshotsDto));
Expand All @@ -73,9 +72,9 @@ public void PrepareTitleScreenshot_NumericGameTitle_ReturnsGameScreenshotsDto()
}

[TestMethod]
public void PrepareTitleScreenshot_Null_ReturnsGameScreenshotsDto()
public void GetGameScreenshots_Null_ReturnsGameScreenshotsDto()
{
var result = _screenshotsService.PrepareTitleScreenshot(null);
var result = _screenshotsService.GetGameScreenshots(null);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(ScreenshotsDto));
Expand All @@ -89,12 +88,13 @@ public void PrepareTitleScreenshot_Null_ReturnsGameScreenshotsDto()
[TestMethod]
public void AddScreenshot_ScreenshotDoesNotExist_ReturnsSuccess()
{
const string screenshot = "Screenshot1.png";
const string originalFilename = @"D:\Temp\Screenshot1.png";
const string gameTitle = "Apidya";
A.CallTo(() => _screenshotsRepository.Add(gameTitle, A<string>.Ignored))
const string renamedFilename = "Apidya.png";
A.CallTo(() => _screenshotsRepository.Add(originalFilename, renamedFilename))
.Returns(new OperationResult {Success = true});

var result = _screenshotsService.Add(gameTitle, screenshot);
var result = _screenshotsService.Add(gameTitle, originalFilename);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(OperationResult));
Expand All @@ -104,12 +104,13 @@ public void AddScreenshot_ScreenshotDoesNotExist_ReturnsSuccess()
[TestMethod]
public void AddScreenshot_ScreenshotExists_ReturnsFailure()
{
const string screenshot = "screenshot.png";
const string originalFilename = @"D:\Temp\screenshot.png";
const string gameTitle = "Apidya";
A.CallTo(() => _screenshotsRepository.ScreenshotFileExists(A<string>.Ignored))
const string renamedFilename = "Apidya.png";
A.CallTo(() => _screenshotsRepository.IsFileExists(renamedFilename))
.Returns(true);

var result = _screenshotsService.Add(gameTitle, screenshot);
var result = _screenshotsService.Add(gameTitle, originalFilename);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(OperationResult));
Expand All @@ -119,15 +120,16 @@ public void AddScreenshot_ScreenshotExists_ReturnsFailure()
[TestMethod]
public void AddScreenshot_NewFile_IteratesOnceReturnsSuccess()
{
const string screenshot = "Screenshot1.png";
const string originalFilename = @"D:\Temp\Screenshot1.png";
const string gameTitle = "Apidya";
A.CallTo(() => _screenshotsRepository.ScreenshotFileExists(A<string>.Ignored))
const string renamedFilename = "Apidya.png";
A.CallTo(() => _screenshotsRepository.IsFileExists(renamedFilename))
.Returns(true)
.Once();
A.CallTo(() => _screenshotsRepository.Add(gameTitle, A<string>.Ignored))
A.CallTo(() => _screenshotsRepository.Add(originalFilename, A<string>.Ignored))
.Returns(new OperationResult { Success = true , Information = "apidya_1.png"});

var result = _screenshotsService.Add(gameTitle, screenshot);
var result = _screenshotsService.Add(gameTitle, originalFilename);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(OperationResult));
Expand All @@ -138,15 +140,16 @@ public void AddScreenshot_NewFile_IteratesOnceReturnsSuccess()
[TestMethod]
public void AddScreenshot_NewFile_IteratesTwiceReturnsSuccess()
{
const string screenshot = "Screenshot1.png";
const string originalFilename = @"D:\Temp\Screenshot1.png";
const string gameTitle = "Apidya";
A.CallTo(() => _screenshotsRepository.ScreenshotFileExists(A<string>.Ignored))
const string renamedFilename = "Apidya.png";
A.CallTo(() => _screenshotsRepository.IsFileExists(renamedFilename))
.Returns(true)
.Once();
A.CallTo(() => _screenshotsRepository.Add(gameTitle, A<string>.Ignored))
A.CallTo(() => _screenshotsRepository.Add(originalFilename, A<string>.Ignored))
.Returns(new OperationResult { Success = true, Information = "apidya_2.png" });

var result = _screenshotsService.Add(gameTitle, screenshot);
var result = _screenshotsService.Add(gameTitle, originalFilename);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(OperationResult));
Expand All @@ -157,11 +160,11 @@ public void AddScreenshot_NewFile_IteratesTwiceReturnsSuccess()
[TestMethod]
public void DeleteScreenshot_Filename_ReturnsSuccess()
{
const string screenshot = "Apidya_2.png";
A.CallTo(() => _screenshotsRepository.Delete(screenshot))
const string filename = "Apidya_2.png";
A.CallTo(() => _screenshotsRepository.Delete(filename))
.Returns(new OperationResult { Success = true, Information = "apidya_2.png" });

var result = _screenshotsService.Delete(screenshot);
var result = _screenshotsService.Delete(filename);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(OperationResult));
Expand Down
1 change: 0 additions & 1 deletion Amigula.Domain/Amigula.Domain.csproj
Expand Up @@ -53,7 +53,6 @@
<Compile Include="Helpers\OsBitCheck.cs" />
<Compile Include="Helpers\SafeNativeMethods.cs" />
<Compile Include="Interfaces\IEmulatorRepository.cs" />
<Compile Include="Interfaces\IFileOperations.cs" />
<Compile Include="Interfaces\IGamesRepository.cs" />
<Compile Include="Interfaces\IMetadataRepository.cs" />
<Compile Include="Interfaces\IMusicPlayerRepository.cs" />
Expand Down
12 changes: 0 additions & 12 deletions Amigula.Domain/Interfaces/IFileOperations.cs

This file was deleted.

1 change: 1 addition & 0 deletions Amigula.Domain/Interfaces/IGamesRepository.cs
Expand Up @@ -6,5 +6,6 @@ namespace Amigula.Domain.Interfaces
public interface IGamesRepository
{
IEnumerable<GamesDto> GetGamesList();
bool IsGameExists(string filename);
}
}
8 changes: 4 additions & 4 deletions Amigula.Domain/Interfaces/IScreenshotsRepository.cs
Expand Up @@ -6,9 +6,9 @@ namespace Amigula.Domain.Interfaces
public interface IScreenshotsRepository
{
BitmapImage LoadImage(string filename);
string GetTitleSubfolder(string gameTitle);
OperationResult Add(string gameTitle, string screenshot);
OperationResult Delete(string screenshot);
bool ScreenshotFileExists(string screenshot);
string GetGameSubfolder(string gameTitle);
OperationResult Add(string filename, string newFilename);
OperationResult Delete(string filename);
bool IsFileExists(string filename);
}
}
15 changes: 7 additions & 8 deletions Amigula.Domain/Services/GamesService.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using Amigula.Domain.DTO;
using Amigula.Domain.Interfaces;
Expand All @@ -8,13 +9,11 @@ namespace Amigula.Domain.Services
{
public class GamesService
{
private readonly IFileOperations _fileOperations;
private readonly IGamesRepository _gamesRepository;

public GamesService(IGamesRepository gamesRepository, IFileOperations fileOperations)
public GamesService(IGamesRepository gamesRepository)
{
_gamesRepository = gamesRepository;
_fileOperations = fileOperations;
}

public IEnumerable<GamesDto> GetGamesList()
Expand Down Expand Up @@ -90,7 +89,7 @@ private List<string> GetDisksFullPath(string gameFullPath, int method)
gameDisksFullPath.Add(Regex.Replace(gameFullPath, @"Disk(\d{1})\.", "Disk" + diskNumber + "."));
diskNumber++;
} while (
_fileOperations.FilenameExists(Regex.Replace(gameFullPath, @"Disk(\d{1})\.",
_gamesRepository.IsGameExists(Regex.Replace(gameFullPath, @"Disk(\d{1})\.",
"Disk" + diskNumber + ".")));

if (method == 2)
Expand All @@ -100,7 +99,7 @@ private List<string> GetDisksFullPath(string gameFullPath, int method)
"Disk" + diskNumber.ToString("00") + "."));
diskNumber++;
} while (
_fileOperations.FilenameExists(Regex.Replace(gameFullPath, @"Disk(\d{2})\.",
_gamesRepository.IsGameExists(Regex.Replace(gameFullPath, @"Disk(\d{2})\.",
"Disk" + diskNumber.ToString("00") + ".")));
if (method == 3)
do
Expand All @@ -109,7 +108,7 @@ private List<string> GetDisksFullPath(string gameFullPath, int method)
"Disk " + diskNumber + " of"));
diskNumber++;
} while (
_fileOperations.FilenameExists(Regex.Replace(gameFullPath, @"Disk\s(\d{1})\sof",
_gamesRepository.IsGameExists(Regex.Replace(gameFullPath, @"Disk\s(\d{1})\sof",
"Disk " + diskNumber + " of")));

if (method == 4)
Expand All @@ -119,7 +118,7 @@ private List<string> GetDisksFullPath(string gameFullPath, int method)
"Disk " + diskNumber.ToString("00") + " of"));
diskNumber++;
} while (
_fileOperations.FilenameExists(Regex.Replace(gameFullPath, @"Disk\s(\d{2})\sof",
_gamesRepository.IsGameExists(Regex.Replace(gameFullPath, @"Disk\s(\d{2})\sof",
"Disk " + diskNumber.ToString("00") + " of")));

if (method == 5)
Expand All @@ -128,7 +127,7 @@ private List<string> GetDisksFullPath(string gameFullPath, int method)
gameDisksFullPath.Add(Regex.Replace(gameFullPath, @"-(\d{1})\.", "-" + diskNumber + "."));
diskNumber++;
} while (
_fileOperations.FilenameExists(Regex.Replace(gameFullPath, @"-(\d{1})\.", "-" + diskNumber + ".")));
_gamesRepository.IsGameExists(Regex.Replace(gameFullPath, @"-(\d{1})\.", "-" + diskNumber + ".")));

return gameDisksFullPath;
}
Expand Down

0 comments on commit e0872ec

Please sign in to comment.