|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Expedia, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.expediagroup.sdk.domain.rapid |
| 17 | + |
| 18 | +import com.expediagroup.sdk.core.client.BaseRapidClient |
| 19 | +import com.expediagroup.sdk.core.configuration.RapidClientConfiguration |
| 20 | +import com.expediagroup.sdk.core.model.Response |
| 21 | +import io.ktor.client.statement.HttpResponse |
| 22 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 23 | +import org.junit.jupiter.api.Assertions.assertNull |
| 24 | +import org.junit.jupiter.api.Nested |
| 25 | +import org.junit.jupiter.api.Test |
| 26 | + |
| 27 | +class RapidHelpersTest { |
| 28 | + companion object { |
| 29 | + private val rapidClient = |
| 30 | + object : BaseRapidClient("dummy", RapidClientConfiguration()) { |
| 31 | + override suspend fun throwServiceException( |
| 32 | + response: HttpResponse, |
| 33 | + operationId: String |
| 34 | + ) { |
| 35 | + throw UnsupportedOperationException() |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + val rapidHelpers = RapidHelpers(rapidClient) |
| 40 | + } |
| 41 | + |
| 42 | + @Nested |
| 43 | + inner class TestExtractToken { |
| 44 | + @Test |
| 45 | + fun `Verify extractToken returns token when present`() { |
| 46 | + assertEquals("12345", rapidHelpers.extractToken("https://www.example.com?token=12345")) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `Verify extractToken returns null when no token is present`() { |
| 51 | + assertNull(rapidHelpers.extractToken("https://www.example.com")) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun `Verify extractToken returns an empty string when token is empty`() { |
| 56 | + assertEquals("", rapidHelpers.extractToken("https://www.example.com?token=")) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `Verify extractToken returns token when it is not the first parameter`() { |
| 61 | + assertEquals("12345", rapidHelpers.extractToken("https://www.example.com?foo=bar&token=12345")) |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun `Verify extractToken returns null when token is not provided but other parameters are`() { |
| 66 | + assertNull(rapidHelpers.extractToken("https://www.example.com?foo=bar")) |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun `Verify extractToken returns null when token is not provided but multiple other parameters are`() { |
| 71 | + assertNull(rapidHelpers.extractToken("https://www.example.com?foo=bar&baz=qux")) |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun `Verify extractToken returns it when token is not the last parameter`() { |
| 76 | + assertEquals("12345", rapidHelpers.extractToken("https://www.example.com?token=12345&foo=bar")) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `extractToken should handle multiple parameters and return the correct token`() { |
| 81 | + assertEquals("xyz456", rapidHelpers.extractToken("https://example.com/page?param1=value1&token=xyz456¶m2=value2")) |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun `extractToken should handle URL-encoded characters in the token`() { |
| 86 | + assertEquals("abc%20456", rapidHelpers.extractToken("https://example.com/page?token=abc%20456¶m=value")) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun `extractToken should handle different token parameter names`() { |
| 91 | + assertEquals("abcd1234", rapidHelpers.extractToken("https://example.com/page?access_token=abcd1234")) |
| 92 | + assertEquals("efgh5678", rapidHelpers.extractToken("https://example.com/page?api_token=efgh5678")) |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun `extractToken should handle multiple tokens`() { |
| 97 | + assertEquals("abcd1234", rapidHelpers.extractToken("https://example.com/page?token=abcd1234&token=efgh5678")) |
| 98 | + assertEquals("efgh5678", rapidHelpers.extractToken("https://example.com/page?api_token=efgh5678&access_token=abcd1234")) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun `extractToken should get only the query param token`() { |
| 103 | + assertNull(rapidHelpers.extractToken("https://example.com/tokenPage?query=tokenValue")) |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun `extractToken should return an empty string when token is empty and other parameters are present`() { |
| 108 | + assertEquals("", rapidHelpers.extractToken("https://www.example.com?foo=bar&token=")) |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + fun `extractToken should return an empty string when token is empty in the middle of other parameters`() { |
| 113 | + assertEquals("", rapidHelpers.extractToken("https://www.example.com?foo=bar&token=&baz=qux")) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Nested |
| 118 | + inner class TestExtractRoomBookingId { |
| 119 | + @Test |
| 120 | + fun `Verify extractRoomBookingId returns roomId when present`() { |
| 121 | + assertEquals("abc-123-xyz", rapidHelpers.extractRoomBookingId("https://www.example.com/rooms/abc-123-xyz?token=abc")) |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + fun `Verify extractRoomBookingId returns null when roomId is not present`() { |
| 126 | + assertNull(rapidHelpers.extractRoomBookingId("https://www.example.com?token=abc")) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Nested |
| 131 | + inner class TestExtractTransactionId { |
| 132 | + @Test |
| 133 | + fun `Verify extractTransactionId returns transactionId when present`() { |
| 134 | + Response<String>( |
| 135 | + 200, |
| 136 | + "body", |
| 137 | + mapOf("transaction-id" to listOf("abc-123-xyz")) |
| 138 | + ).let { assertEquals("abc-123-xyz", rapidHelpers.extractTransactionId(it)) } |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + fun `Verify extractTransactionId returns null when transactionId is not present`() { |
| 143 | + Response<String>( |
| 144 | + 200, |
| 145 | + "body", |
| 146 | + mapOf("some-header" to listOf("some data")) |
| 147 | + ).let { assertNull(rapidHelpers.extractTransactionId(it)) } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments