17
17
package functions ;
18
18
19
19
import static com .google .common .truth .Truth .assertThat ;
20
+ import static org .junit .Assert .assertThrows ;
21
+ import static org .mockito .Mockito .any ;
22
+ import static org .mockito .Mockito .anyLong ;
20
23
import static org .mockito .Mockito .times ;
21
24
import static org .mockito .Mockito .verify ;
22
- import static org .powermock .api .mockito .PowerMockito .mock ;
23
- import static org .powermock .api .mockito .PowerMockito .when ;
25
+ import static org .mockito .Mockito .when ;
24
26
25
27
import com .github .seratch .jslack .app_backend .SlackSignature ;
26
28
import com .google .api .client .googleapis .json .GoogleJsonResponseException ;
33
35
import java .io .StringWriter ;
34
36
import java .net .HttpURLConnection ;
35
37
import java .security .GeneralSecurityException ;
36
- import java .util .Arrays ;
37
- import java .util .HashMap ;
38
38
import java .util .List ;
39
+ import java .util .Map ;
39
40
import org .junit .Before ;
40
41
import org .junit .Test ;
41
- import org .mockito .ArgumentMatchers ;
42
42
import org .mockito .Mock ;
43
- import org .powermock . reflect . Whitebox ;
43
+ import org .mockito . MockitoAnnotations ;
44
44
45
45
public class SlackSlashCommandTest {
46
46
@@ -54,36 +54,26 @@ public class SlackSlashCommandTest {
54
54
55
55
@ Before
56
56
public void beforeTest () throws IOException {
57
- request = mock (HttpRequest .class );
58
- when (request .getReader ()).thenReturn (new BufferedReader (new StringReader ("" )));
57
+ MockitoAnnotations .initMocks (this );
59
58
60
- response = mock ( HttpResponse . class );
59
+ when ( request . getReader ()). thenReturn ( new BufferedReader ( new StringReader ( "" )) );
61
60
62
61
responseOut = new StringWriter ();
63
62
64
63
writerOut = new BufferedWriter (responseOut );
65
64
when (response .getWriter ()).thenReturn (writerOut );
66
65
67
- alwaysValidVerifier = mock (SlackSignature .Verifier .class );
68
- when (alwaysValidVerifier .isValid (
69
- ArgumentMatchers .any (),
70
- ArgumentMatchers .any (),
71
- ArgumentMatchers .any (),
72
- ArgumentMatchers .anyLong ())
73
- ).thenReturn (true );
66
+ when (alwaysValidVerifier .isValid (any (), any (), any (), anyLong ())).thenReturn (true );
74
67
75
68
// Construct valid header list
76
- HashMap <String , List <String >> validHeaders = new HashMap <String , List <String >>();
77
69
String validSlackSignature = System .getenv ("SLACK_TEST_SIGNATURE" );
78
70
String timestamp = "0" ; // start of Unix epoch
79
71
80
- validHeaders .put ("X-Slack-Signature" , Arrays .asList (validSlackSignature ));
81
- validHeaders .put ("X-Slack-Request-Timestamp" , Arrays .asList (timestamp ));
72
+ Map <String , List <String >> validHeaders = Map .of (
73
+ "X-Slack-Signature" , List .of (validSlackSignature ),
74
+ "X-Slack-Request-Timestamp" , List .of (timestamp ));
82
75
83
76
when (request .getHeaders ()).thenReturn (validHeaders );
84
-
85
- // Reset knowledge graph API key
86
- Whitebox .setInternalState (SlackSlashCommand .class , "API_KEY" , System .getenv ("KG_API_KEY" ));
87
77
}
88
78
89
79
@ Test
@@ -120,19 +110,18 @@ public void recognizesValidSlackTokenTest() throws IOException, GeneralSecurityE
120
110
verify (response , times (1 )).setStatusCode (HttpURLConnection .HTTP_BAD_REQUEST );
121
111
}
122
112
123
- @ Test ( expected = GoogleJsonResponseException . class )
113
+ @ Test
124
114
public void handlesSearchErrorTest () throws IOException , GeneralSecurityException {
125
115
StringReader requestReadable = new StringReader ("{ \" text\" : \" foo\" }\n " );
126
116
127
117
when (request .getReader ()).thenReturn (new BufferedReader (requestReadable ));
128
118
when (request .getMethod ()).thenReturn ("POST" );
129
119
130
- SlackSlashCommand functionInstance = new SlackSlashCommand ();
131
- Whitebox .setInternalState (functionInstance , "verifier" , alwaysValidVerifier );
132
- Whitebox .setInternalState (SlackSlashCommand .class , "API_KEY" , "gibberish" );
120
+ SlackSlashCommand functionInstance = new SlackSlashCommand (alwaysValidVerifier , "gibberish" );
133
121
134
122
// Should throw a GoogleJsonResponseException (due to invalid API key)
135
- functionInstance .service (request , response );
123
+ assertThrows (
124
+ GoogleJsonResponseException .class , () -> functionInstance .service (request , response ));
136
125
}
137
126
138
127
@ Test
@@ -142,9 +131,7 @@ public void handlesEmptyKgResultsTest() throws IOException, GeneralSecurityExcep
142
131
when (request .getReader ()).thenReturn (new BufferedReader (requestReadable ));
143
132
when (request .getMethod ()).thenReturn ("POST" );
144
133
145
- SlackSlashCommand functionInstance = new SlackSlashCommand ();
146
- Whitebox .setInternalState (functionInstance , "verifier" , alwaysValidVerifier );
147
-
134
+ SlackSlashCommand functionInstance = new SlackSlashCommand (alwaysValidVerifier );
148
135
149
136
functionInstance .service (request , response );
150
137
@@ -159,9 +146,7 @@ public void handlesPopulatedKgResultsTest() throws IOException, GeneralSecurityE
159
146
when (request .getReader ()).thenReturn (new BufferedReader (requestReadable ));
160
147
when (request .getMethod ()).thenReturn ("POST" );
161
148
162
- SlackSlashCommand functionInstance = new SlackSlashCommand ();
163
- Whitebox .setInternalState (functionInstance , "verifier" , alwaysValidVerifier );
164
-
149
+ SlackSlashCommand functionInstance = new SlackSlashCommand (alwaysValidVerifier );
165
150
166
151
functionInstance .service (request , response );
167
152
0 commit comments