Last active
November 8, 2021 19:31
-
-
Save arifsuhan/7c8b1401875fb1f1d43f7d51026e11eb to your computer and use it in GitHub Desktop.
xml parse java rest assured
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// used version | |
// testImplementation 'io.rest-assured:rest-assured:4.3.1' | |
// implementation 'com.squareup.okhttp3:okhttp:3.14.6' | |
import io.restassured.path.xml.XmlPath; | |
import io.restassured.specification.RequestSpecification; | |
import okhttp3.MediaType; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.RequestBody; | |
import org.junit.jupiter.api.Test; | |
import java.util.concurrent.TimeUnit; | |
import static io.restassured.RestAssured.given; | |
public class mainTest { | |
String baseURL = "https://www.dataaccess.com/webservicesserver/NumberConversion.wso"; | |
String requestBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n <soap:Body>\n <NumberToWords xmlns=\"http://www.dataaccess.com/webservicesserver/\">\n <ubiNum>500</ubiNum>\n </NumberToWords>\n </soap:Body>\n</soap:Envelope>"; | |
String contentTypeHeader = "Content-Type"; | |
String contentTypeValue = "text/xml; charset=utf-8"; | |
String queryString = "NumberToWordsResult"; | |
@Test | |
public void test1(){ | |
OkHttpClient client = new OkHttpClient().newBuilder() | |
.callTimeout(5000, TimeUnit.MILLISECONDS) | |
.build(); | |
MediaType mediaType = MediaType.parse(contentTypeHeader); | |
RequestBody body = RequestBody.create(mediaType, requestBody); | |
Request request = new Request.Builder() | |
.url(baseURL) | |
.method("POST", body) | |
.addHeader(contentTypeHeader, contentTypeValue) | |
.build(); | |
try{ | |
okhttp3.Response response = client.newCall(request).execute(); | |
XmlPath jsXpath = new XmlPath(response.body().string()); | |
String getText = jsXpath.getString(queryString); | |
System.out.println("Using OkHttpClient: " + getText); | |
}catch (Exception e){ | |
System.out.println(e.getStackTrace()); | |
} | |
} | |
@Test | |
public void test2(){ | |
RequestSpecification request = given().header(contentTypeHeader, contentTypeValue).body(requestBody); | |
io.restassured.response.Response response = request.post(baseURL); | |
XmlPath jsXpath = new XmlPath(response.asString()); | |
String getText = jsXpath.getString(queryString); | |
System.out.println("Using Rest Assured: " + getText); | |
} | |
} | |
// test { | |
// useJUnitPlatform() | |
// testLogging { | |
// events "passed", "skipped", "failed", "standardOut", "standardError" | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment