@@ -39,6 +39,17 @@ await Assert.ThrowsAsync<ArgumentNullException>(() =>
3939 _printService . PrintZPL ( emptyZpl , "192.168.1.100" , 9100 , null , null ) ) ;
4040 }
4141
42+ [ Fact ]
43+ public async Task PrintZPL_WithWhitespaceZpl_ThrowsArgumentNullException ( )
44+ {
45+ // Arrange
46+ string whitespaceZpl = " " ;
47+
48+ // Act & Assert
49+ await Assert . ThrowsAsync < ArgumentNullException > ( ( ) =>
50+ _printService . PrintZPL ( whitespaceZpl , "192.168.1.100" , 9100 , null , null ) ) ;
51+ }
52+
4253 [ Fact ]
4354 public async Task PrintZPL_WithData_CallsTemplateService ( )
4455 {
@@ -59,12 +70,55 @@ public async Task PrintZPL_WithData_CallsTemplateService()
5970 }
6071 catch ( Exception )
6172 {
73+ // Expected to fail due to no actual printer
6274 }
6375
6476 // Assert
6577 _mockTemplateService . Verify ( x => x . PopulateZplTemplate ( data , zpl , delimiter ) , Times . Once ) ;
6678 }
6779
80+ [ Fact ]
81+ public async Task PrintZPL_WithNullData_DoesNotCallTemplateService ( )
82+ {
83+ // Arrange
84+ var zpl = "^XA^FO50,50^FDTest^FS^XZ" ;
85+ Dictionary < string , string > ? nullData = null ;
86+
87+ // Act
88+ try
89+ {
90+ await _printService . PrintZPL ( zpl , "192.168.1.100" , 9100 , nullData , "$" ) ;
91+ }
92+ catch ( Exception )
93+ {
94+ // Expected to fail due to no actual printer
95+ }
96+
97+ // Assert
98+ _mockTemplateService . Verify ( x => x . PopulateZplTemplate ( It . IsAny < Dictionary < string , string > > ( ) , It . IsAny < string > ( ) , It . IsAny < string > ( ) ) , Times . Never ) ;
99+ }
100+
101+ [ Fact ]
102+ public async Task PrintZPL_WithEmptyData_DoesNotCallTemplateService ( )
103+ {
104+ // Arrange
105+ var zpl = "^XA^FO50,50^FDTest^FS^XZ" ;
106+ var emptyData = new Dictionary < string , string > ( ) ;
107+
108+ // Act
109+ try
110+ {
111+ await _printService . PrintZPL ( zpl , "192.168.1.100" , 9100 , emptyData , "$" ) ;
112+ }
113+ catch ( Exception )
114+ {
115+ // Expected to fail due to no actual printer
116+ }
117+
118+ // Assert
119+ _mockTemplateService . Verify ( x => x . PopulateZplTemplate ( It . IsAny < Dictionary < string , string > > ( ) , It . IsAny < string > ( ) , It . IsAny < string > ( ) ) , Times . Never ) ;
120+ }
121+
68122 [ Fact ]
69123 public async Task PrintZPL_WithInvalidIpAddress_ThrowsInvalidOperationException ( )
70124 {
@@ -94,4 +148,121 @@ public async Task PrintZPL_WithUnreachableIpAddress_ThrowsInvalidOperationExcept
94148 // Assert
95149 Assert . Contains ( "Failed to connect to printer" , exception . Message ) ;
96150 }
151+
152+ [ Fact ]
153+ public async Task PrintZPL_WithInvalidPort_ThrowsInvalidOperationException ( )
154+ {
155+ // Arrange
156+ var zpl = "^XA^FO50,50^FDTest^FS^XZ" ;
157+ var invalidPort = 99999 ; // Invalid port
158+
159+ // Act
160+ var exception = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) =>
161+ _printService . PrintZPL ( zpl , "192.168.1.100" , invalidPort , null , null ) ) ;
162+
163+ // Assert
164+ Assert . Contains ( "Failed to connect to printer" , exception . Message ) ;
165+ }
166+
167+ [ Fact ]
168+ public async Task PrintZPL_WithZeroPort_ThrowsInvalidOperationException ( )
169+ {
170+ // Arrange
171+ var zpl = "^XA^FO50,50^FDTest^FS^XZ" ;
172+ var zeroPort = 0 ;
173+
174+ // Act
175+ var exception = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) =>
176+ _printService . PrintZPL ( zpl , "192.168.1.100" , zeroPort , null , null ) ) ;
177+
178+ // Assert
179+ Assert . Contains ( "Failed to connect to printer" , exception . Message ) ;
180+ }
181+
182+ [ Fact ]
183+ public async Task PrintZPL_WithNegativePort_ThrowsInvalidOperationException ( )
184+ {
185+ // Arrange
186+ var zpl = "^XA^FO50,50^FDTest^FS^XZ" ;
187+ var negativePort = - 1 ;
188+
189+ // Act
190+ var exception = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) =>
191+ _printService . PrintZPL ( zpl , "192.168.1.100" , negativePort , null , null ) ) ;
192+
193+ // Assert
194+ Assert . Contains ( "Failed to connect to printer" , exception . Message ) ;
195+ }
196+
197+ [ Theory ]
198+ [ InlineData ( "localhost" ) ]
199+ [ InlineData ( "127.0.0.1" ) ]
200+ [ InlineData ( "192.168.1.1" ) ]
201+ [ InlineData ( "10.0.0.1" ) ]
202+ public async Task PrintZPL_WithValidIpFormats_AttemptsConnection ( string ipAddress )
203+ {
204+ // Arrange
205+ var zpl = "^XA^FO50,50^FDTest^FS^XZ" ;
206+
207+ // Act & Assert
208+ var exception = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) =>
209+ _printService . PrintZPL ( zpl , ipAddress , 9100 , null , null ) ) ;
210+
211+ Assert . Contains ( "Failed to connect to printer" , exception . Message ) ;
212+ }
213+
214+ [ Theory ]
215+ [ InlineData ( "^XA^FO50,50^FDTest^FS^XZ" ) ]
216+ [ InlineData ( "^XA^FO50,50^FDTest^FS^XZ\n " ) ]
217+ [ InlineData ( "^XA^FO50,50^FDTest^FS^XZ\r \n " ) ]
218+ public async Task PrintZPL_WithVariousZplFormats_AttemptsConnection ( string zpl )
219+ {
220+ // Arrange & Act & Assert
221+ var exception = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) =>
222+ _printService . PrintZPL ( zpl , "192.168.1.100" , 9100 , null , null ) ) ;
223+
224+ Assert . Contains ( "Failed to connect to printer" , exception . Message ) ;
225+ }
226+
227+ [ Fact ]
228+ public async Task PrintZPL_WithComplexZpl_AttemptsConnection ( )
229+ {
230+ // Arrange
231+ var complexZpl = @"^XA
232+ ^FO50,50^A0N,50,50^FDHello World!^FS
233+ ^FO50,120^A0N,30,30^FDLine 2^FS
234+ ^FO50,180^A0N,30,30^FDLine 3^FS
235+ ^XZ" ;
236+
237+ // Act & Assert
238+ var exception = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) =>
239+ _printService . PrintZPL ( complexZpl , "192.168.1.100" , 9100 , null , null ) ) ;
240+
241+ Assert . Contains ( "Failed to connect to printer" , exception . Message ) ;
242+ }
243+
244+ [ Fact ]
245+ public async Task PrintZPL_WithTemplateAndData_ProcessesCorrectly ( )
246+ {
247+ // Arrange
248+ var zpl = "^XA^FO50,50^FD$Name$^FS^FO50,100^FD$Date$^FS^XZ" ;
249+ var data = new Dictionary < string , string >
250+ {
251+ { "Name" , "John Doe" } ,
252+ { "Date" , "2023-12-01" }
253+ } ;
254+ var delimiter = "$" ;
255+ var processedZpl = "^XA^FO50,50^FDJohn Doe^FS^FO50,100^FD2023-12-01^FS^XZ" ;
256+
257+ _mockTemplateService
258+ . Setup ( x => x . PopulateZplTemplate ( data , zpl , delimiter ) )
259+ . Returns ( processedZpl ) ;
260+
261+ // Act & Assert
262+ var exception = await Assert . ThrowsAsync < InvalidOperationException > ( ( ) =>
263+ _printService . PrintZPL ( zpl , "192.168.1.100" , 9100 , data , delimiter ) ) ;
264+
265+ Assert . Contains ( "Failed to connect to printer" , exception . Message ) ;
266+ _mockTemplateService . Verify ( x => x . PopulateZplTemplate ( data , zpl , delimiter ) , Times . Once ) ;
267+ }
97268}
0 commit comments