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 ;
34
36
import java .io .StringWriter ;
35
37
import java .net .HttpURLConnection ;
36
38
import java .security .GeneralSecurityException ;
37
- import java .util .Arrays ;
38
- import java .util .HashMap ;
39
39
import java .util .List ;
40
40
import java .util .Map ;
41
41
import org .junit .Before ;
42
42
import org .junit .Test ;
43
- import org .mockito .ArgumentMatchers ;
44
43
import org .mockito .Mock ;
45
- import org .powermock . reflect . Whitebox ;
44
+ import org .mockito . MockitoAnnotations ;
46
45
47
46
public class SlackSlashCommandTest {
48
47
@@ -58,37 +57,27 @@ public class SlackSlashCommandTest {
58
57
59
58
@ Before
60
59
public void beforeTest () throws IOException {
61
- request = mock (HttpRequest .class );
62
- when (request .getReader ()).thenReturn (new BufferedReader (new StringReader ("" )));
60
+ MockitoAnnotations .initMocks (this );
63
61
64
- response = mock ( HttpResponse . class );
62
+ when ( request . getReader ()). thenReturn ( new BufferedReader ( new StringReader ( "" )) );
65
63
66
64
responseOut = new StringWriter ();
67
65
68
66
writerOut = new BufferedWriter (responseOut );
69
67
when (response .getWriter ()).thenReturn (writerOut );
70
68
71
- alwaysValidVerifier = mock (SlackSignature .Verifier .class );
72
- when (alwaysValidVerifier .isValid (
73
- ArgumentMatchers .any (),
74
- ArgumentMatchers .any (),
75
- ArgumentMatchers .any (),
76
- ArgumentMatchers .anyLong ())
77
- ).thenReturn (true );
69
+ when (alwaysValidVerifier .isValid (any (), any (), any (), anyLong ())).thenReturn (true );
78
70
79
71
// Construct valid header list
80
72
String validSlackSignature = System .getenv ("SLACK_TEST_SIGNATURE" );
81
73
String timestamp = "0" ; // start of Unix epoch
82
74
83
75
Map <String , List <String >> validHeaders = Map .of (
84
- "X-Slack-Signature" , Arrays .asList (validSlackSignature ),
85
- "X-Slack-Request-Timestamp" , Arrays .asList (timestamp )
86
- );
76
+ "X-Slack-Signature" , List .of (validSlackSignature ),
77
+ "X-Slack-Request-Timestamp" , List .of (timestamp ));
87
78
88
79
when (request .getHeaders ()).thenReturn (validHeaders );
89
-
90
- // Reset knowledge graph API key
91
- Whitebox .setInternalState (SlackSlashCommand .class , "API_KEY" , System .getenv ("KG_API_KEY" ));
80
+ when (request .getFirstHeader (any ())).thenCallRealMethod ();
92
81
}
93
82
94
83
@ Test
@@ -126,20 +115,19 @@ public void recognizesValidSlackTokenTest() throws IOException, GeneralSecurityE
126
115
verify (response , times (1 )).setStatusCode (HttpURLConnection .HTTP_BAD_REQUEST );
127
116
}
128
117
129
- @ Test ( expected = GoogleJsonResponseException . class )
118
+ @ Test
130
119
public void handlesSearchErrorTest () throws IOException , GeneralSecurityException {
131
120
String jsonStr = gson .toJson (Map .of ("text" , "foo" ));
132
121
StringReader requestReadable = new StringReader (jsonStr );
133
122
134
123
when (request .getReader ()).thenReturn (new BufferedReader (requestReadable ));
135
124
when (request .getMethod ()).thenReturn ("POST" );
136
125
137
- SlackSlashCommand functionInstance = new SlackSlashCommand ();
138
- Whitebox .setInternalState (functionInstance , "verifier" , alwaysValidVerifier );
139
- Whitebox .setInternalState (SlackSlashCommand .class , "API_KEY" , "gibberish" );
126
+ SlackSlashCommand functionInstance = new SlackSlashCommand (alwaysValidVerifier , "gibberish" );
140
127
141
128
// Should throw a GoogleJsonResponseException (due to invalid API key)
142
- functionInstance .service (request , response );
129
+ assertThrows (
130
+ GoogleJsonResponseException .class , () -> functionInstance .service (request , response ));
143
131
}
144
132
145
133
@ Test
@@ -150,9 +138,7 @@ public void handlesEmptyKgResultsTest() throws IOException, GeneralSecurityExcep
150
138
when (request .getReader ()).thenReturn (new BufferedReader (requestReadable ));
151
139
when (request .getMethod ()).thenReturn ("POST" );
152
140
153
- SlackSlashCommand functionInstance = new SlackSlashCommand ();
154
- Whitebox .setInternalState (functionInstance , "verifier" , alwaysValidVerifier );
155
-
141
+ SlackSlashCommand functionInstance = new SlackSlashCommand (alwaysValidVerifier );
156
142
157
143
functionInstance .service (request , response );
158
144
@@ -168,9 +154,7 @@ public void handlesPopulatedKgResultsTest() throws IOException, GeneralSecurityE
168
154
when (request .getReader ()).thenReturn (new BufferedReader (requestReadable ));
169
155
when (request .getMethod ()).thenReturn ("POST" );
170
156
171
- SlackSlashCommand functionInstance = new SlackSlashCommand ();
172
- Whitebox .setInternalState (functionInstance , "verifier" , alwaysValidVerifier );
173
-
157
+ SlackSlashCommand functionInstance = new SlackSlashCommand (alwaysValidVerifier );
174
158
175
159
functionInstance .service (request , response );
176
160
0 commit comments