<dependency>
<groupId>com.sap.cloud.s4hana</groupId>
<artifactId>testutil</artifactId>
<version>2.20.2</version>
<scope>test</scope>
</dependency>resources folder:credentials:
- alias: "DESTINATION_ALIAS"
username: "username"
password: "password"erp:
systems:
- alias: "DESTINATION_ALIAS"
uri: "http://localhost:1111/"
sapClient: 123
locale: CHsystems.yaml the fields of the mocked destination can be provided like sap-client or locale.private static final String DESTINATION = "UNIT_TEST_DESTINATION";
private static final String DESTINATION_ALIAS = "DESTINATION_ALIAS";
private static final MockUtil mockUtil = new MockUtil();
@BeforeClass
public static void classSetup(){
mockUtil.mockErpDestination(DESTINATION, DESTINATION_ALIAS);
}erpConfig = new ErpConfigContext(destination);
String client = erpConfig.getSapClient();<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>2.24.1</version>
<scope>test</scope>
</dependency>private static final int LOCAL_PORT = 1111;
private static WireMockServer wireMockServer;
@BeforeClass
public static void classSetup() throws IOException {
configureFor(LOCAL_PORT);
wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().port(LOCAL_PORT));
wireMockServer.start();
}
@AfterClass
public static void classTeardown() {
wireMockServer.stop();
}
@Before
public void setup() {
wireMockServer.resetAll();
createStubForMockServer();
}
private void createStubForMockServer() {
stubFor(get(urlEqualTo("/odata/Entity?$format=json")).withHeader("sap-client", equalTo("123"))
.withHeader("sap-language", equalTo("en")).willReturn(
aResponse().withStatus(200).withHeader("Content-Type", "application/json")
.withBody("some content")));
}BeforeClass method we configure and start the web server. In the AfterClass method we stop the web server.UNIT_TEST_DESTINATION. In the settings in systems.yaml we have defined that the URL for this destination is http://localhost:1111/./odata/Entity?$format=json the complete URL which is called is:http://localhost:1111/odata/Entity?$format=jsonconfigureFor(LOCAL_PORT);
wireMockServer = new WireMockServer(WireMockConfiguration
.wireMockConfig()
.port(LOCAL_PORT));
callBackendSystem.package com.sap.testapp;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.sap.cloud.sdk.cloudplatform.connectivity.Destination;
import com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpClientAccessor;
import com.sap.cloud.sdk.testutil.MockUtil;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CloudSdkExampleIT {
private static final String DESTINATION = "UNIT_TEST_DESTINATION";
private static final String DESTINATION_ALIAS = "DESTINATION_ALIAS";
private static final int LOCAL_PORT = 1111;
private static final String ODATA_PATH = "/odata/S4Entity?$format=json";
private static final String RESPONSE_CONTENT = "response content for testing";
private static WireMockServer wireMockServer;
private static final MockUtil mockUtil = new MockUtil();
@BeforeClass
public static void classSetup() {
//for mocking destination
mockUtil.mockErpDestination(DESTINATION, DESTINATION_ALIAS);
//for mocking OData calls
configureFor(LOCAL_PORT);
wireMockServer = new WireMockServer(WireMockConfiguration
.wireMockConfig()
.port(LOCAL_PORT));
wireMockServer.start();
}
@AfterClass
public static void classTeardown() {
wireMockServer.stop();
}
@Before
public void setup() {
wireMockServer.resetAll();
createStubForMockServer();
}
@Test
public void callODataWorking() throws IOException, URISyntaxException {
HttpResponse response = callBackendSystem(DESTINATION, ODATA_PATH);
InputStream responseBodyStream = response.getEntity().getContent();
String responseBody = IOUtils.toString(responseBodyStream,
StandardCharsets.UTF_8.name());
assertThat(response
.getStatusLine()
.getStatusCode())
.isEqualTo(HttpStatus.OK.value());
assertThat(responseBody).isEqualTo(RESPONSE_CONTENT);
}
@Test
public void callWrongUrlNotWorking() throws IOException, URISyntaxException {
HttpResponse response = callBackendSystem(DESTINATION, "some/other/path");
assertThat(response
.getStatusLine()
.getStatusCode())
.isEqualTo(HttpStatus.NOT_FOUND.value());
}
private void createStubForMockServer() {
stubFor(get(urlEqualTo(ODATA_PATH))
.withHeader("sap-client", equalTo("123"))
.withHeader("sap-language", equalTo("ch"))
.willReturn( aResponse()
.withStatus(HttpStatus.OK.value())
.withHeader("Content-Type", "application/json")
.withBody(RESPONSE_CONTENT)));
}
private HttpResponse callBackendSystem(String destinationString, String odataPath) throws URISyntaxException, IOException {
HttpClient client = HttpClientAccessor.getHttpClient(destinationString);
Destination destination = DestinationAccessor.getDestination(destinationString);
Map<String, String> destinationProperties = destination.getPropertiesByName();
URIBuilder builder = new URIBuilder(odataPath);
HttpGet getRequest = new HttpGet(builder.build().toString());
destinationProperties.forEach(getRequest::setHeader);
return client.execute(getRequest);
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 48 | |
| 47 | |
| 37 | |
| 34 | |
| 29 | |
| 23 | |
| 22 | |
| 22 | |
| 22 | |
| 21 |