Skip to content

Commit 3266749

Browse files
committed
JUnit Test for ClientServerUtility class
1 parent a06762d commit 3266749

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

ClientServerUtilityTest.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package client;
2+
import static org.junit.Assert.*;
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
public class ClientServerUtilityTest {
7+
8+
String mockData = "ENTS/1.0 Request\r\nfileName\r\nintegrityValue\r\n";
9+
ClientServerUtility clientServerUtilityObj;
10+
11+
/*
12+
* Create new ClientServerUtility object before every test method
13+
*/
14+
@Before
15+
public void setUp() throws Exception {
16+
clientServerUtilityObj = new ClientServerUtility();
17+
}
18+
19+
@Test
20+
public void testConstructedObject() {
21+
//Testing if the above constructed object is not null
22+
assertNotNull(clientServerUtilityObj);
23+
}// end of testConstructedObject()
24+
25+
/*
26+
* messageInBytesShouldReturnMessageInBytes()
27+
* This test checks if the message is converted to bytes.
28+
*/
29+
@Test
30+
public void messageInBytesShouldReturnMessageInBytes() {
31+
assertArrayEquals(mockData.getBytes(), clientServerUtilityObj.messageInBytes(mockData));
32+
}//end of messageInBytesShouldReturnMessageInBytes()
33+
34+
35+
/*
36+
* The next 5 methods tests isFileNameSyntaxCorrect() method.
37+
*/
38+
39+
/*
40+
* isFileNameSyntaxCorrectShouldReturnTrue()
41+
* This test checks if the name of the requested file has the correct syntax
42+
*/
43+
@Test
44+
public void isFileNameSyntaxCorrectShouldReturnTrue() {
45+
String fileName = "directors_message.txt";
46+
assertTrue(clientServerUtilityObj.isFileNameSyntaxCorrect(fileName, "request"));
47+
}// end of isFileNameSyntaxCorrectShouldReturnTrue()
48+
49+
/*
50+
* fileNameWithoutExtensionShouldThrowException()
51+
* This test checks if the name of the requested file has the correct syntax.
52+
* The file name has no extension. Hence it should throw an exception.
53+
*/
54+
@Test (expected = IllegalArgumentException.class)
55+
public void fileNameWithoutExtensionShouldThrowException() {
56+
String fileName = "directors_message";
57+
assertFalse(clientServerUtilityObj.isFileNameSyntaxCorrect(fileName, "request"));
58+
}// end of fileNameWithoutExtensionShouldThrowException()
59+
60+
/*
61+
* fileNameWithSpecialCharacterShouldThrowException()
62+
* This test checks if the name of the requested file has the correct syntax.
63+
* The file name has special characters. Hence it should throw an exception.
64+
*/
65+
@Test (expected = IllegalArgumentException.class)
66+
public void fileNameWithSpecialCharacterShouldThrowException() {
67+
String fileName = "$$$$directors_message@@@@@.txt";
68+
assertFalse(clientServerUtilityObj.isFileNameSyntaxCorrect(fileName, "request"));
69+
}//end of fileNameWithSpecialCharacterShouldThrowException()
70+
71+
/*
72+
* fileNameDoesNotStartWithAlphabetsShouldThrowException()
73+
* This test checks if the name of the requested file has the correct syntax.
74+
* The file name does not start with alphabets. Hence it should throw an exception.
75+
*/
76+
@Test (expected = IllegalArgumentException.class)
77+
public void fileNameDoesNotStartWithAlphabetsShouldThrowException() {
78+
String fileName = "1234directors_message.txt";
79+
assertFalse(clientServerUtilityObj.isFileNameSyntaxCorrect(fileName, "request"));
80+
}// end of fileNameDoesNotStartWithAlphabetsShouldThrowException()
81+
82+
/*
83+
* extensionHasSpecialCharactersShouldThrowException()
84+
* This test checks if the name of the requested file has the correct syntax.
85+
* The extension should be alphanumeric, but here it contains special characters. Hence it should throw an exception.
86+
*/
87+
@Test (expected = IllegalArgumentException.class)
88+
public void extensionHasSpecialCharactersShouldThrowException() {
89+
String fileName = "directors_message.@@@txt";
90+
assertFalse(clientServerUtilityObj.isFileNameSyntaxCorrect(fileName, "request"));
91+
}// end of extensionHasSpecialCharactersShouldThrowException()
92+
93+
94+
95+
/*
96+
* getIntegrityCheckValueShouldReturnIntegrityValueAsString()
97+
* This test checks if the returned integrity value matches the expected integrity value for the gives message.
98+
*/
99+
@Test
100+
public void getIntegrityCheckValueShouldReturnIntegrityValueAsString() {
101+
String mockDataWithoutIntegrityValue = "ENTS/1.0 Request\r\nfileName\r\n"; //We calculate integrity value for a message without the integrity field.
102+
assertEquals("11482", clientServerUtilityObj.getIntegrityCheckValue(mockDataWithoutIntegrityValue));
103+
}//end of getIntegrityCheckValueShouldReturnIntegrityValueAsString()
104+
105+
/*
106+
* isIntegrityValueOfMessageCorrectShouldReturnTrue()
107+
* This test checks if the integrity value of the "request" is correct.
108+
*/
109+
@Test
110+
public void isIntegrityValueOfRequestMessageCorrectShouldReturnTrue() {
111+
String mockDataWithIntegrityValue = "ENTS/1.0 Request\r\nfileName\r\n11482\r\n";
112+
assertTrue(clientServerUtilityObj.isIntegrityValueOfMessageCorrect(mockDataWithIntegrityValue, "request"));
113+
}//end of isIntegrityValueOfRequestMessageCorrectShouldReturnTrue()
114+
115+
}//end of test class ClientServerUtilityTest

0 commit comments

Comments
 (0)