22
33import static java .nio .charset .StandardCharsets .UTF_8 ;
44
5+ import static org .apache .http .entity .ContentType .APPLICATION_JSON ;
6+ import static org .apache .http .entity .ContentType .TEXT_PLAIN ;
57import static org .assertj .core .api .Assertions .assertThat ;
68import static org .mockito .ArgumentMatchers .any ;
79import static org .mockito .Mockito .mock ;
2426import org .apache .http .entity .InputStreamEntity ;
2527import org .apache .http .message .BasicHttpResponse ;
2628import org .junit .jupiter .api .AfterEach ;
27- import org .junit .jupiter .api .BeforeEach ;
2829import org .junit .jupiter .api .Test ;
2930
3031import com .sap .cloud .sdk .cloudplatform .connectivity .DefaultHttpDestination ;
@@ -43,17 +44,13 @@ public class HttpResponseEvaluationTest
4344 private InputStream inputStream ;
4445
4546 @ SneakyThrows
46- @ BeforeEach
47- void setup ()
47+ void mockHttpResponse ( final ContentType contentType , final String payload )
4848 {
4949 httpClient = mock (HttpClient .class );
50-
51- final String payload = "{\" value\" : []}" ;
5250 inputStream = spy (new ByteArrayInputStream (payload .getBytes (UTF_8 )));
53- httpEntity = spy (new InputStreamEntity (inputStream , ContentType . APPLICATION_JSON ));
51+ httpEntity = spy (new InputStreamEntity (inputStream , contentType ));
5452 httpResponse = spy (new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_OK , "OK" ));
5553 httpResponse .setEntity (httpEntity );
56-
5754 HttpClientAccessor .setHttpClientFactory (destination -> httpClient );
5855 when (httpClient .execute (any ())).thenReturn (httpResponse );
5956 }
@@ -69,6 +66,8 @@ void teardown()
6966 @ Test
7067 void testCreate ()
7168 {
69+ mockHttpResponse (APPLICATION_JSON , "{}" );
70+
7271 final ModificationResponse <TestEntity > result =
7372 new CreateRequestBuilder <>("/path" , new TestEntity (), "TestEntitySet" )
7473 .withoutCsrfToken ()
@@ -86,6 +85,8 @@ void testCreate()
8685 @ Test
8786 void testUpdate ()
8887 {
88+ mockHttpResponse (APPLICATION_JSON , "{}" );
89+
8990 final TestEntity testEntity = new TestEntity ();
9091 testEntity .setId ("id" );
9192
@@ -104,6 +105,8 @@ void testUpdate()
104105 @ Test
105106 void testDelete ()
106107 {
108+ mockHttpResponse (APPLICATION_JSON , "{}" );
109+
107110 final ModificationResponse <TestEntity > result =
108111 new DeleteRequestBuilder <>("/path" , new TestEntity (), "TestEntitySet" )
109112 .withoutCsrfToken ()
@@ -121,6 +124,8 @@ void testDelete()
121124 @ Test
122125 void testReadAll ()
123126 {
127+ mockHttpResponse (APPLICATION_JSON , "{\" value\" : []}" );
128+
124129 final List <TestEntity > result =
125130 new GetAllRequestBuilder <>("/path" , TestEntity .class , "TestEntitySet" ).execute (DESTINATION );
126131
@@ -136,6 +141,8 @@ void testReadAll()
136141 @ Test
137142 void testReadByKey ()
138143 {
144+ mockHttpResponse (APPLICATION_JSON , "{}" );
145+
139146 final TestEntity result =
140147 new GetByKeyRequestBuilder <>("/path" , TestEntity .class , Map .of ("id" , "foo" ), "TestEntitySet" )
141148 .execute (DESTINATION );
@@ -147,4 +154,58 @@ void testReadByKey()
147154 verify (httpEntity , times (1 )).writeTo (any (OutputStream .class ));
148155 verify (inputStream , times (1 )).close ();
149156 }
157+
158+ @ SneakyThrows
159+ @ Test
160+ void testReadCount ()
161+ {
162+ mockHttpResponse (TEXT_PLAIN , "42" );
163+
164+ final Long result = new CountRequestBuilder <>("/path" , TestEntity .class , "TestEntitySet" ).execute (DESTINATION );
165+
166+ assertThat (result ).isNotNull ();
167+
168+ verify (httpClient , times (1 )).execute (any (HttpUriRequest .class ));
169+ verify (httpResponse , times (1 )).getEntity ();
170+ verify (httpEntity , times (1 )).writeTo (any (OutputStream .class ));
171+ verify (inputStream , times (1 )).close ();
172+ }
173+
174+ @ SneakyThrows
175+ @ Test
176+ void testAction ()
177+ {
178+ mockHttpResponse (APPLICATION_JSON , "{}" );
179+
180+ final ActionResponseSingle <TestEntity > result =
181+ new SingleValueActionRequestBuilder <>("/path" , "ActionName" , TestEntity .class )
182+ .withoutCsrfToken ()
183+ .execute (DESTINATION );
184+
185+ assertThat (result ).isNotNull ();
186+
187+ verify (httpClient , times (1 )).execute (any (HttpUriRequest .class ));
188+ verify (httpResponse , times (1 )).getEntity ();
189+ verify (httpEntity , times (1 )).writeTo (any (OutputStream .class ));
190+ verify (inputStream , times (1 )).close ();
191+ }
192+
193+ @ SneakyThrows
194+ @ Test
195+ void testFunction ()
196+ {
197+ mockHttpResponse (APPLICATION_JSON , "{\" value\" :[]}" );
198+
199+ final List <TestEntity > result =
200+ new CollectionValueFunctionRequestBuilder <>("/path" , "FunctionName" , TestEntity .class ).execute (DESTINATION );
201+
202+ assertThat (result ).isNotNull ();
203+
204+ verify (httpClient , times (1 )).execute (any (HttpUriRequest .class ));
205+ verify (httpResponse , times (1 )).getEntity ();
206+ verify (httpEntity , times (1 )).writeTo (any (OutputStream .class ));
207+ verify (inputStream , times (1 )).close ();
208+ }
209+
210+ // count function action
150211}
0 commit comments