Skip to content

Commit be88f1e

Browse files
committed
Add further test cases
1 parent 6fc9f80 commit be88f1e

2 files changed

Lines changed: 121 additions & 14 deletions

File tree

datamodel/odata-v4/odata-v4-core/src/test/java/com/sap/cloud/sdk/datamodel/odatav4/core/HttpResponseEvaluationTest.java

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import 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;
57
import static org.assertj.core.api.Assertions.assertThat;
68
import static org.mockito.ArgumentMatchers.any;
79
import static org.mockito.Mockito.mock;
@@ -24,7 +26,6 @@
2426
import org.apache.http.entity.InputStreamEntity;
2527
import org.apache.http.message.BasicHttpResponse;
2628
import org.junit.jupiter.api.AfterEach;
27-
import org.junit.jupiter.api.BeforeEach;
2829
import org.junit.jupiter.api.Test;
2930

3031
import 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
}

datamodel/odata/odata-core/src/test/java/com/sap/cloud/sdk/datamodel/odata/helper/HttpResponseEvaluationTest.java

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import 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;
57
import static org.assertj.core.api.Assertions.assertThat;
68
import static org.mockito.ArgumentMatchers.any;
79
import static org.mockito.Mockito.mock;
@@ -24,7 +26,6 @@
2426
import org.apache.http.entity.InputStreamEntity;
2527
import org.apache.http.message.BasicHttpResponse;
2628
import org.junit.jupiter.api.AfterEach;
27-
import org.junit.jupiter.api.BeforeEach;
2829
import org.junit.jupiter.api.Test;
2930

3031
import 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 = "{\"d\": {\"results\": []}}";
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, "{\"d\": {}}");
70+
7271
final ModificationResponse<TestVdmEntity> result =
7372
FluentHelperFactory
7473
.withServicePath("/path")
@@ -88,6 +87,8 @@ void testCreate()
8887
@Test
8988
void testUpdate()
9089
{
90+
mockHttpResponse(APPLICATION_JSON, "{\"d\": {}}");
91+
9192
final TestVdmEntity testEntity = new TestVdmEntity();
9293
testEntity.setIntegerValue(42);
9394

@@ -110,6 +111,8 @@ void testUpdate()
110111
@Test
111112
void testDelete()
112113
{
114+
mockHttpResponse(APPLICATION_JSON, "{\"d\": {}}");
115+
113116
final ModificationResponse<TestVdmEntity> result =
114117
FluentHelperFactory
115118
.withServicePath("/path")
@@ -129,6 +132,8 @@ void testDelete()
129132
@Test
130133
void testReadAll()
131134
{
135+
mockHttpResponse(APPLICATION_JSON, "{\"d\": {\"results\": []}}");
136+
132137
final List<TestVdmEntity> result =
133138
FluentHelperFactory
134139
.withServicePath("/path")
@@ -147,6 +152,8 @@ void testReadAll()
147152
@Test
148153
void testReadByKey()
149154
{
155+
mockHttpResponse(APPLICATION_JSON, "{\"d\":{}}");
156+
150157
final TestVdmEntity result =
151158
FluentHelperFactory
152159
.withServicePath("/path")
@@ -160,4 +167,43 @@ void testReadByKey()
160167
verify(httpEntity, times(1)).writeTo(any(OutputStream.class));
161168
verify(inputStream, times(1)).close();
162169
}
170+
171+
@SneakyThrows
172+
@Test
173+
void testFunction()
174+
{
175+
mockHttpResponse(APPLICATION_JSON, "{\"d\": {\"results\": []}}");
176+
177+
final List<String> result =
178+
FluentHelperFactory
179+
.withServicePath("/path")
180+
.functionMultipleGet(Map.of("para", "meter"), "functionName", String.class)
181+
.executeRequest(DESTINATION);
182+
183+
assertThat(result).isNotNull();
184+
185+
verify(httpClient, times(1)).execute(any(HttpUriRequest.class));
186+
verify(httpResponse, times(1)).getEntity();
187+
verify(httpEntity, times(1)).writeTo(any(OutputStream.class));
188+
verify(inputStream, times(1)).close();
189+
}
190+
191+
@SneakyThrows
192+
@Test
193+
void testReadCount()
194+
{
195+
mockHttpResponse(TEXT_PLAIN, "42");
196+
197+
final long result =
198+
FluentHelperFactory
199+
.withServicePath("/path")
200+
.read(TestVdmEntity.class, "TestEntitySet")
201+
.count()
202+
.executeRequest(DESTINATION);
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+
}
163209
}

0 commit comments

Comments
 (0)