1+ package io .imagekit .sdk .tasks ;
2+
3+
4+ import org .junit .After ;
5+ import org .junit .Before ;
6+ import org .junit .Test ;
7+
8+ import java .util .Calendar ;
9+ import java .util .Map ;
10+ import java .util .UUID ;
11+
12+ import static org .hamcrest .CoreMatchers .is ;
13+ import static org .junit .Assert .*;
14+
15+ public class CalculationTest {
16+ Calculation SUT ;
17+
18+ @ Before
19+ public void setUp () throws Exception {
20+ SUT =new Calculation ();
21+ }
22+
23+ @ Test (expected = RuntimeException .class )
24+ public void nullPrivateKey_getAuthenticatedParams_throwException () {
25+ Calculation .getAuthenticatedParams (UUID .randomUUID ().toString (), Calendar .getInstance ().getTimeInMillis (),null );
26+ }
27+
28+ @ Test
29+ public void privateKeyProvided_getAuthenticatedParams_with_static_input_ExpectedData () {
30+ String token ="your_token" ;
31+ String privateKey ="private_key_test" ;
32+ long expire =1582269249 ;
33+ Map <String , String > authenticatedParams = Calculation .getAuthenticatedParams (token , expire , privateKey );
34+ assertEquals (token ,authenticatedParams .get ("token" ));
35+ assertEquals (String .valueOf (expire ),authenticatedParams .get ("expire" ));
36+ assertEquals ("e71bcd6031016b060d349d212e23e85c791decdd" ,authenticatedParams .get ("signature" ));
37+ }
38+
39+ @ Test
40+ public void privateKeyProvided_getAuthenticatedParams_ExpectedData () {
41+ String token =UUID .randomUUID ().toString ();
42+ String privateKey ="my_private_key" ;
43+ long expire =Calendar .getInstance ().getTimeInMillis ();
44+ Map <String , String > authenticatedParams = Calculation .getAuthenticatedParams (token , expire , privateKey );
45+ assertThat (authenticatedParams .get ("token" ),is (token ));
46+ assertEquals (String .valueOf (expire ),authenticatedParams .get ("expire" ));
47+ assertNotNull (authenticatedParams .get ("signature" ));
48+ }
49+
50+ @ Test
51+ public void expireNotProvided_getAuthenticatedParams_ExpectedData () {
52+ String token =UUID .randomUUID ().toString ();
53+ String privateKey ="my_private_key" ;
54+ Map <String , String > authenticatedParams = Calculation .getAuthenticatedParams (token ,0 , privateKey );
55+ assertThat (authenticatedParams .get ("token" ),is (token ));
56+ assertNotNull (authenticatedParams .get ("expire" ));
57+ assertNotNull (authenticatedParams .get ("signature" ));
58+ }
59+
60+ @ Test
61+ public void sameImage_getHammingDistance_expectedSuccessWith () {
62+ int hammingDistance = Calculation .getHammingDistance ("f06830ca9f1e3e90" , "f06830ca9f1e3e90" );
63+ assertEquals (0 ,hammingDistance );
64+ }
65+
66+ @ Test
67+ public void similarImage_getHammingDistance_expectedSuccessWith () {
68+ int hammingDistance = Calculation .getHammingDistance ("2d5ad3936d2e015b" , "2d6ed293db36a4fb" );
69+ assertEquals (17 ,hammingDistance );
70+ }
71+
72+ @ Test
73+ public void dissimilarImage_getHammingDistance_expectedSuccessWith () {
74+ int hammingDistance = Calculation .getHammingDistance ("a4a65595ac94518b" , "7838873e791f8400" );
75+ assertEquals (37 ,hammingDistance );
76+ }
77+
78+ @ Test (expected = RuntimeException .class )
79+ public void invalidHash_getHammingDistance_throwException () {
80+ int hammingDistance = Calculation .getHammingDistance ("a4a65595ac94518Z" , "7838873e791f8400" );
81+ }
82+
83+ @ Test (expected = RuntimeException .class )
84+ public void differentLength_getHammingDistance_throwException () {
85+ int hammingDistance = Calculation .getHammingDistance ("a4a65595ac94518b3" , "7838873e791f8400" );
86+ }
87+
88+ @ Test
89+ public void correctHex_isValidHex_trueExpected () {
90+ boolean result =Calculation .isValidHex ("a4a65595ac94518b" );
91+ assertTrue (result );
92+ }
93+
94+ @ Test
95+ public void incorrectHex_isValidHex_falseExpected () {
96+ boolean result =Calculation .isValidHex ("a4a65595ac94518T" );
97+ assertFalse (result );
98+ }
99+
100+ @ After
101+ public void tearDown () throws Exception {
102+ SUT =null ;
103+ }
104+ }
0 commit comments