1313
1414package com .amazon .ask .servlet ;
1515
16- import static com .amazon .ask .util .SdkConstants .FORMAT_VERSION ;
17- import static org .mockito .Mockito .when ;
18-
19- import java .io .ByteArrayInputStream ;
20- import java .io .ByteArrayOutputStream ;
21- import java .io .IOException ;
22- import java .io .InputStream ;
23- import java .io .OutputStream ;
24-
25- import javax .servlet .ServletInputStream ;
26- import javax .servlet .ServletOutputStream ;
27- import javax .servlet .http .HttpServletRequest ;
28- import javax .servlet .http .HttpServletResponse ;
29-
30- import com .amazon .ask .model .Application ;
31- import com .amazon .ask .model .Request ;
32- import com .amazon .ask .model .RequestEnvelope ;
33- import com .amazon .ask .model .Session ;
34- import com .amazon .ask .model .User ;
16+ import com .amazon .ask .model .*;
17+ import com .fasterxml .jackson .annotation .JsonInclude ;
18+ import com .fasterxml .jackson .databind .ObjectMapper ;
3519import com .fasterxml .jackson .databind .SerializationFeature ;
3620import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
21+ import jakarta .servlet .ServletInputStream ;
22+ import jakarta .servlet .ServletOutputStream ;
23+ import jakarta .servlet .http .HttpServletRequest ;
24+ import jakarta .servlet .http .HttpServletResponse ;
3725import org .mockito .Mockito ;
3826
39- import com .fasterxml .jackson .annotation .JsonInclude ;
40- import com .fasterxml .jackson .databind .ObjectMapper ;
27+ import java .io .*;
28+
29+ import static com .amazon .ask .util .SdkConstants .FORMAT_VERSION ;
30+ import static org .mockito .Mockito .when ;
4131
4232public class SkillServletTestBase {
4333 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
34+
4435 static {
4536 OBJECT_MAPPER .registerModule (new JavaTimeModule ());
4637 OBJECT_MAPPER .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS );
4738 OBJECT_MAPPER .setSerializationInclusion (JsonInclude .Include .NON_NULL );
4839 }
4940
50- /**
51- * Tuple for useful parameters for a servlet invocation.
52- */
53- protected static final class ServletInvocationParameters {
54- public final HttpServletRequest request ;
55- public final HttpServletResponse response ;
56- public final ByteArrayOutputStream output ;
57-
58- public ServletInvocationParameters (final HttpServletRequest request ,
59- final HttpServletResponse response , final ByteArrayOutputStream output ) {
60- this .request = request ;
61- this .response = response ;
62- this .output = output ;
63- }
64- }
65-
66- /**
67- * Trivial implementation of the ServletInputStream that wraps an InputStream.
68- */
69- private static class MyServletInputStream extends ServletInputStream {
70- private final InputStream in ;
71-
72- public MyServletInputStream (final InputStream in ) {
73- this .in = in ;
74- }
75-
76- @ Override
77- public int read () throws IOException {
78- return in .read ();
79- }
80- }
81-
82- /**
83- * Trivial implementation of the ServletOutputStream that wraps an OutputStream.
84- */
85- private static class MyServletOutputStream extends ServletOutputStream {
86- private final OutputStream out ;
87-
88- public MyServletOutputStream (final OutputStream out ) {
89- this .out = out ;
90- }
91-
92- @ Override
93- public void write (int b ) throws IOException {
94- out .write (b );
95- }
96- }
97-
98- // ------------------------------
99- // Helper methods for subclasses
100-
10141 /**
10242 * Common logic for all the tests.
10343 *
104- * @param version
105- * the envelope version for the request envelope
106- * @param request
107- * a SkillRequest
108- * @param session
109- * a session for this invocation
44+ * @param version the envelope version for the request envelope
45+ * @param request a SkillRequest
46+ * @param session a session for this invocation
11047 * @return invocation parameters for a Skill
111- *
11248 * @throws IOException
11349 */
11450 protected ServletInvocationParameters build (final String version , final Request request , final Session session ) throws IOException {
@@ -143,12 +79,9 @@ protected ServletInvocationParameters build(final String version, final Request
14379 * <li>The current SDK version as a version</li>
14480 * </ul>
14581 *
146- * @param request
147- * a SkillRequest or OnSessionEndedRequest
148- * @param session
149- * a session for this invocation
82+ * @param request a SkillRequest or OnSessionEndedRequest
83+ * @param session a session for this invocation
15084 * @return invocation parameters for a Skill
151- *
15285 * @throws IOException
15386 */
15487 protected ServletInvocationParameters build (final Request request ,
@@ -159,17 +92,18 @@ protected ServletInvocationParameters build(final Request request,
15992 /**
16093 * Same as above using a simple Session and User.
16194 *
162- * @param request
163- * a skill request
95+ * @param request a skill request
16496 * @return invocation parameters for a Skill
165- *
16697 * @throws IOException
16798 */
16899 protected ServletInvocationParameters build (final Request request )
169100 throws IOException {
170101 return build (request , buildSession ());
171102 }
172103
104+ // ------------------------------
105+ // Helper methods for subclasses
106+
173107 /**
174108 * @return a test session.
175109 */
@@ -186,4 +120,52 @@ protected Session buildSession(String applicationId) {
186120 .build ();
187121 }
188122
123+ /**
124+ * Tuple for useful parameters for a servlet invocation.
125+ */
126+ protected static final class ServletInvocationParameters {
127+ public final HttpServletRequest request ;
128+ public final HttpServletResponse response ;
129+ public final ByteArrayOutputStream output ;
130+
131+ public ServletInvocationParameters (final HttpServletRequest request ,
132+ final HttpServletResponse response , final ByteArrayOutputStream output ) {
133+ this .request = request ;
134+ this .response = response ;
135+ this .output = output ;
136+ }
137+ }
138+
139+ /**
140+ * Trivial implementation of the ServletInputStream that wraps an InputStream.
141+ */
142+ private static class MyServletInputStream extends ServletInputStream {
143+ private final InputStream in ;
144+
145+ public MyServletInputStream (final InputStream in ) {
146+ this .in = in ;
147+ }
148+
149+ @ Override
150+ public int read () throws IOException {
151+ return in .read ();
152+ }
153+ }
154+
155+ /**
156+ * Trivial implementation of the ServletOutputStream that wraps an OutputStream.
157+ */
158+ private static class MyServletOutputStream extends ServletOutputStream {
159+ private final OutputStream out ;
160+
161+ public MyServletOutputStream (final OutputStream out ) {
162+ this .out = out ;
163+ }
164+
165+ @ Override
166+ public void write (int b ) throws IOException {
167+ out .write (b );
168+ }
169+ }
170+
189171}
0 commit comments