|
| 1 | +package com.amadeus.ordering; |
| 2 | + |
| 3 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 4 | +import static com.github.tomakehurst.wiremock.client.WireMock.post; |
| 5 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 7 | + |
| 8 | +import com.amadeus.Amadeus; |
| 9 | +import com.amadeus.Params; |
| 10 | +import com.amadeus.exceptions.ResponseException; |
| 11 | +import com.amadeus.resources.TransferOrder; |
| 12 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 13 | +import com.google.gson.JsonObject; |
| 14 | +import com.google.gson.JsonParser; |
| 15 | +import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | +import java.nio.file.Files; |
| 18 | +import org.junit.jupiter.api.AfterEach; |
| 19 | +import org.junit.jupiter.api.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +// https://developers.amadeus.com/self-service/category/cars-and-transfers/api-doc/transfer-booking |
| 23 | +public class TransferOrdersIT { |
| 24 | + |
| 25 | + WireMockServer wireMockServer; |
| 26 | + |
| 27 | + private Amadeus amadeus; |
| 28 | + |
| 29 | + /** |
| 30 | + * Authentication is conducted for each test. |
| 31 | + */ |
| 32 | + @BeforeEach |
| 33 | + public void setup() { |
| 34 | + wireMockServer = new WireMockServer(8080); |
| 35 | + wireMockServer.start(); |
| 36 | + |
| 37 | + // https://developers.amadeus.com/self-service/apis-docs/guides/authorization-262 |
| 38 | + String address = "/v1/security/oauth2/token" |
| 39 | + + "?grant_type=client_credentials&client_secret=DEMO&client_id=DEMO"; |
| 40 | + wireMockServer.stubFor(post(urlEqualTo(address)) |
| 41 | + .willReturn(aResponse().withHeader("Content-Type", "application/json") |
| 42 | + .withStatus(200) |
| 43 | + .withBodyFile("auth_ok.json"))); |
| 44 | + |
| 45 | + amadeus = Amadeus |
| 46 | + .builder("DEMO", "DEMO") |
| 47 | + .setHost("localhost") |
| 48 | + .setPort(8080) |
| 49 | + .setSsl(false) |
| 50 | + .setLogLevel("debug") |
| 51 | + .build(); |
| 52 | + } |
| 53 | + |
| 54 | + @AfterEach |
| 55 | + public void teardown() { |
| 56 | + wireMockServer.stop(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void givenClientWhenCallTransferOrdersWithParamsThenOK() |
| 61 | + throws ResponseException, IOException { |
| 62 | + |
| 63 | + //Given |
| 64 | + String address = "/v1/ordering/transfer-orders" + "?offerId=123456"; |
| 65 | + wireMockServer.stubFor(post(urlEqualTo(address)) |
| 66 | + .willReturn(aResponse().withHeader("Content-Type", "application/json") |
| 67 | + .withStatus(200) |
| 68 | + .withBodyFile("transfer_orders_response_ok.json"))); |
| 69 | + |
| 70 | + JsonObject request = getRequestFromResources("transfer_orders_request_ok.json"); |
| 71 | + Params params = Params.with("offerId", "123456"); |
| 72 | + |
| 73 | + //When |
| 74 | + TransferOrder result = amadeus.ordering.transferOrders.post(request, params); |
| 75 | + |
| 76 | + //Then |
| 77 | + assertNotNull(result); |
| 78 | + } |
| 79 | + |
| 80 | + private JsonObject getRequestFromResources(String jsonFile) throws IOException { |
| 81 | + |
| 82 | + final String folder = "__files/"; |
| 83 | + |
| 84 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 85 | + File file = new File(classLoader.getResource(folder + jsonFile).getFile()); |
| 86 | + String jsonString = new String(Files.readAllBytes(file.toPath())); |
| 87 | + |
| 88 | + return new JsonParser().parse(jsonString).getAsJsonObject(); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments