1
+ using Amazon ;
2
+ using AWSFileUploaderWithImageCompression . Classes ;
3
+ using NUnit . Framework ;
4
+ using System ;
5
+ using System . IO ;
6
+ using System . Threading . Tasks ;
7
+ using System . Text . Json ;
8
+ using System . Text . Json . Serialization ;
9
+ using Moq ;
10
+ using AWSFileUploaderWithImageCompression . Interfaces ;
11
+ using Amazon . S3 . Model ;
12
+
13
+ namespace AWSFileUploaderWithImageCompression . Test
14
+ {
15
+ public class ImageServiceTest
16
+ {
17
+ private IImageService imgService ;
18
+
19
+ [ OneTimeSetUp ]
20
+ public void Setup ( )
21
+ {
22
+ //Mocking s3Uploader
23
+ var s3Uploader = new Mock < IS3ImageUploader > ( ) ;
24
+ s3Uploader . Setup ( s => s . UploadAsync ( It . IsAny < Stream > ( ) , It . IsAny < string > ( ) , It . IsAny < string > ( ) , null ) ) . Returns ( Task . FromResult ( new PutObjectResponse ( ) { HttpStatusCode = System . Net . HttpStatusCode . OK } ) ) ;
25
+
26
+ imgService = new ImageService ( s3Uploader . Object ) ;
27
+ }
28
+
29
+
30
+ /// <summary>
31
+ /// This will test the resize and compression of large image files.
32
+ /// </summary>
33
+ [ Test , TestCaseSource ( typeof ( TestSourceProvider ) , nameof ( TestSourceProvider . GetLargeImages ) ) ]
34
+ public async Task CompressLargeImagesTestAsync ( FileInfo originalFile )
35
+ {
36
+ //Arrange
37
+ var outputDirectory = new DirectoryInfo ( Path . Combine ( Environment . CurrentDirectory , "Assets" , "Compressed" ) ) . FullName ;
38
+ if ( ! Directory . Exists ( outputDirectory ) )
39
+ Directory . CreateDirectory ( outputDirectory ) ;
40
+ var outputFileName = Path . Combine ( outputDirectory , Guid . NewGuid ( ) . ToString ( ) + originalFile . Extension ) ;
41
+
42
+
43
+ //Act
44
+ var result = await imgService . ImageCompressor . CompressImage ( originalFile . FullName , outputFileName ) ;
45
+ var serializedResult = JsonSerializer . Serialize ( result , new JsonSerializerOptions { WriteIndented = true } ) ;
46
+
47
+ //Assert
48
+ if ( result . ImageCompressionSucccess || ( result . AfterCompressionSizeInBytes < result . OriginalSizeInBytes ) )
49
+ Assert . Pass ( $ "Successfully compressed the image\n -------------------------------------\n FileName: { originalFile . Name } \n { serializedResult } ") ;
50
+ else
51
+ Assert . Fail ( $ "Compression Failed!\n -------------------------------------\n FileName: { originalFile . Name } \n { serializedResult } ") ;
52
+
53
+ }
54
+
55
+ /// <summary>
56
+ /// This will test that no compression should be performed if the images are already smaller in size.
57
+ /// </summary>
58
+ [ Test , TestCaseSource ( typeof ( TestSourceProvider ) , nameof ( TestSourceProvider . GetSmallImages ) ) ]
59
+ public async Task DontCompressSmallImageAsync ( FileInfo originalFile )
60
+ {
61
+ //Arrange
62
+ var outputDirectory = new DirectoryInfo ( Path . Combine ( Environment . CurrentDirectory , "Assets" , "Compressed" ) ) . FullName ;
63
+ if ( ! Directory . Exists ( outputDirectory ) )
64
+ Directory . CreateDirectory ( outputDirectory ) ;
65
+ var outputFileName = Path . Combine ( outputDirectory , Guid . NewGuid ( ) . ToString ( ) + originalFile . Extension ) ;
66
+
67
+
68
+ //Act
69
+ var result = await imgService . ImageCompressor . CompressImage ( originalFile . FullName , outputFileName ) ;
70
+ var serializedResult = JsonSerializer . Serialize ( result , new JsonSerializerOptions { WriteIndented = true } ) ;
71
+
72
+ //Assert
73
+ Assert . IsTrue ( result . ImageCompressionSucccess == false ) ;
74
+ }
75
+
76
+ /// <summary>
77
+ /// This will test the if the watermark to the image has been applied successfully.
78
+ /// </summary>
79
+ [ Test ]
80
+ public async Task WaterMarkTest ( )
81
+ {
82
+ //Arrange
83
+ var watermarkImage = File . OpenRead ( Path . Combine ( Environment . CurrentDirectory , "Assets" , "Original" , "SmallImages" , "spiderman.png" ) ) ;
84
+ var outputDirectory = new DirectoryInfo ( Path . Combine ( Environment . CurrentDirectory , "Assets" , "Watermark" ) ) . FullName ;
85
+ if ( ! Directory . Exists ( outputDirectory ) )
86
+ Directory . CreateDirectory ( outputDirectory ) ;
87
+ var sourceFile = File . OpenRead ( Path . Combine ( Environment . CurrentDirectory , "Assets" , "Original" , "LargeImages" , "2.jpg" ) ) ;
88
+ var outputFile = $ "{ Path . Combine ( outputDirectory ) } { Guid . NewGuid ( ) . ToString ( ) } .jpg";
89
+
90
+ imgService . ImageCompressor . UpdateImageServiceConfiguration ( new Classes . Models . ImgCompressorConfiguration
91
+ {
92
+ WaterMarkTransperency = 5 //50% transparency
93
+ } ) ;
94
+
95
+ //Act
96
+ var success = await imgService . ImageCompressor . AddWaterMark ( sourceFile , watermarkImage , outputFile ) ;
97
+
98
+ //Assert
99
+ Assert . IsTrue ( success ) ;
100
+ }
101
+
102
+
103
+
104
+ /// <summary>
105
+ /// This will test the Compress and Upload
106
+ /// </summary>
107
+ [ TestCase ( "2.jpg" ) ]
108
+ [ TestCase ( "3.jpg" ) ]
109
+ [ TestCase ( "4.jpg" ) ]
110
+ public async Task CompressAndUploadTest ( string fileName )
111
+ {
112
+ var sourceFile = File . OpenRead ( Path . Combine ( Environment . CurrentDirectory , "Assets" , "Original" , "LargeImages" , fileName ) ) ;
113
+ var resp = await imgService . CompressAndUploadImageAsync ( sourceFile ) ;
114
+
115
+ Assert . IsNotNull ( resp ) ;
116
+ Assert . IsTrue ( resp . HttpStatusCode == System . Net . HttpStatusCode . OK ) ;
117
+ }
118
+
119
+
120
+ [ OneTimeTearDown ]
121
+ public void ClearImages ( )
122
+ {
123
+ if ( Directory . Exists ( Path . Combine ( Environment . CurrentDirectory , "Assets" , "Compressed" ) ) )
124
+ Directory . Delete ( Path . Combine ( Environment . CurrentDirectory , "Assets" , "Compressed" ) , true ) ;
125
+ if ( Directory . Exists ( Path . Combine ( Environment . CurrentDirectory , "Assets" , "Watermark" ) ) )
126
+ Directory . Delete ( Path . Combine ( Environment . CurrentDirectory , "Assets" , "Watermark" ) , true ) ;
127
+ }
128
+ }
129
+
130
+
131
+ }
0 commit comments