|
| 1 | +/* |
| 2 | + * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.amplifyframework.kotlin.geo |
| 17 | + |
| 18 | +import com.amplifyframework.core.Consumer |
| 19 | +import com.amplifyframework.geo.GeoCategoryBehavior |
| 20 | +import com.amplifyframework.geo.GeoException |
| 21 | +import com.amplifyframework.geo.models.Coordinates |
| 22 | +import com.amplifyframework.geo.models.MapStyle |
| 23 | +import com.amplifyframework.geo.models.MapStyleDescriptor |
| 24 | +import com.amplifyframework.geo.options.GeoSearchByCoordinatesOptions |
| 25 | +import com.amplifyframework.geo.options.GeoSearchByTextOptions |
| 26 | +import com.amplifyframework.geo.options.GetMapStyleDescriptorOptions |
| 27 | +import com.amplifyframework.geo.result.GeoSearchResult |
| 28 | +import io.mockk.every |
| 29 | +import io.mockk.mockk |
| 30 | +import junit.framework.Assert.assertSame |
| 31 | +import kotlinx.coroutines.runBlocking |
| 32 | +import org.junit.Test |
| 33 | + |
| 34 | +/** |
| 35 | + * Unit tests for the [KotlinGeoFacade] class. |
| 36 | + */ |
| 37 | +internal class KotlinGeoFacadeTest { |
| 38 | + private val delegate = mockk<GeoCategoryBehavior>() |
| 39 | + private val geo = KotlinGeoFacade(delegate) |
| 40 | + |
| 41 | + @Test |
| 42 | + fun `gets available maps`() = runBlocking { |
| 43 | + val maps = listOf( |
| 44 | + MapStyle("a", "b"), |
| 45 | + MapStyle("c", "d") |
| 46 | + ) |
| 47 | + every { delegate.getAvailableMaps(any(), any()) } answers { |
| 48 | + val callback = firstArg<Consumer<Collection<MapStyle>>>() |
| 49 | + callback.accept(maps) |
| 50 | + } |
| 51 | + val result = geo.getAvailableMaps() |
| 52 | + assertSame(maps, result) |
| 53 | + } |
| 54 | + |
| 55 | + @Test(expected = GeoException::class) |
| 56 | + fun `throws available map error`(): Unit = runBlocking { |
| 57 | + val error = GeoException("message", "suggestion") |
| 58 | + every { delegate.getAvailableMaps(any(), any()) } answers { |
| 59 | + val callback = secondArg<Consumer<GeoException>>() |
| 60 | + callback.accept(error) |
| 61 | + } |
| 62 | + geo.getAvailableMaps() |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `gets default map`() = runBlocking { |
| 67 | + val map = MapStyle("name", "style") |
| 68 | + every { delegate.getDefaultMap(any(), any()) } answers { |
| 69 | + val callback = firstArg<Consumer<MapStyle>>() |
| 70 | + callback.accept(map) |
| 71 | + } |
| 72 | + val result = geo.getDefaultMap() |
| 73 | + assertSame(map, result) |
| 74 | + } |
| 75 | + |
| 76 | + @Test(expected = GeoException::class) |
| 77 | + fun `throws default map error`(): Unit = runBlocking { |
| 78 | + val error = GeoException("message", "suggestion") |
| 79 | + every { delegate.getDefaultMap(any(), any()) } answers { |
| 80 | + val callback = secondArg<Consumer<GeoException>>() |
| 81 | + callback.accept(error) |
| 82 | + } |
| 83 | + geo.getDefaultMap() |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun `returns map style descriptor`() = runBlocking { |
| 88 | + val descriptor = MapStyleDescriptor("") |
| 89 | + every { delegate.getMapStyleDescriptor(any(), any(), any()) } answers { |
| 90 | + val callback = secondArg<Consumer<MapStyleDescriptor>>() |
| 91 | + callback.accept(descriptor) |
| 92 | + } |
| 93 | + val result = geo.getMapStyleDescriptor() |
| 94 | + assertSame(descriptor, result) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun `returns map style descriptor with options`() = runBlocking { |
| 99 | + val descriptor = MapStyleDescriptor("") |
| 100 | + val options = GetMapStyleDescriptorOptions.builder().mapName("map").build() |
| 101 | + every { delegate.getMapStyleDescriptor(options, any(), any()) } answers { |
| 102 | + val callback = secondArg<Consumer<MapStyleDescriptor>>() |
| 103 | + callback.accept(descriptor) |
| 104 | + } |
| 105 | + val result = geo.getMapStyleDescriptor(options) |
| 106 | + assertSame(descriptor, result) |
| 107 | + } |
| 108 | + |
| 109 | + @Test(expected = GeoException::class) |
| 110 | + fun `throws map style descriptor error`(): Unit = runBlocking { |
| 111 | + val error = GeoException("message", "suggestion") |
| 112 | + every { delegate.getMapStyleDescriptor(any(), any(), any()) } answers { |
| 113 | + val callback = lastArg<Consumer<GeoException>>() |
| 114 | + callback.accept(error) |
| 115 | + } |
| 116 | + geo.getMapStyleDescriptor() |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + fun `returns search by text result`() = runBlocking { |
| 121 | + val query = "query" |
| 122 | + val searchResult = GeoSearchResult.withPlaces(emptyList()) |
| 123 | + every { delegate.searchByText(query, any(), any(), any()) } answers { |
| 124 | + val callback = thirdArg<Consumer<GeoSearchResult>>() |
| 125 | + callback.accept(searchResult) |
| 126 | + } |
| 127 | + val result = geo.searchByText(query) |
| 128 | + assertSame(searchResult, result) |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + fun `returns search by text result with options`() = runBlocking { |
| 133 | + val query = "query" |
| 134 | + val options = GeoSearchByTextOptions.builder().maxResults(4).build() |
| 135 | + val searchResult = GeoSearchResult.withPlaces(emptyList()) |
| 136 | + every { delegate.searchByText(query, options, any(), any()) } answers { |
| 137 | + val callback = thirdArg<Consumer<GeoSearchResult>>() |
| 138 | + callback.accept(searchResult) |
| 139 | + } |
| 140 | + val result = geo.searchByText(query, options) |
| 141 | + assertSame(searchResult, result) |
| 142 | + } |
| 143 | + |
| 144 | + @Test(expected = GeoException::class) |
| 145 | + fun `throws search by text error`(): Unit = runBlocking { |
| 146 | + val query = "query" |
| 147 | + val error = GeoException("message", "suggestion") |
| 148 | + every { delegate.searchByText(query, any(), any(), any()) } answers { |
| 149 | + val callback = lastArg<Consumer<GeoException>>() |
| 150 | + callback.accept(error) |
| 151 | + } |
| 152 | + geo.searchByText(query) |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + fun `returns search by coordinates result`() = runBlocking { |
| 157 | + val position = Coordinates() |
| 158 | + val searchResult = GeoSearchResult.withPlaces(emptyList()) |
| 159 | + every { delegate.searchByCoordinates(position, any(), any(), any()) } answers { |
| 160 | + val callback = thirdArg<Consumer<GeoSearchResult>>() |
| 161 | + callback.accept(searchResult) |
| 162 | + } |
| 163 | + val result = geo.searchByCoordinates(position) |
| 164 | + assertSame(searchResult, result) |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + fun `returns search by coordinates result with options`() = runBlocking { |
| 169 | + val position = Coordinates() |
| 170 | + val options = GeoSearchByCoordinatesOptions.builder().maxResults(3).build() |
| 171 | + val searchResult = GeoSearchResult.withPlaces(emptyList()) |
| 172 | + every { delegate.searchByCoordinates(position, options, any(), any()) } answers { |
| 173 | + val callback = thirdArg<Consumer<GeoSearchResult>>() |
| 174 | + callback.accept(searchResult) |
| 175 | + } |
| 176 | + val result = geo.searchByCoordinates(position, options) |
| 177 | + assertSame(searchResult, result) |
| 178 | + } |
| 179 | + |
| 180 | + @Test(expected = GeoException::class) |
| 181 | + fun `throws search by coordinates error`(): Unit = runBlocking { |
| 182 | + val position = Coordinates() |
| 183 | + val error = GeoException("message", "suggestion") |
| 184 | + every { delegate.searchByCoordinates(position, any(), any(), any()) } answers { |
| 185 | + val callback = lastArg<Consumer<GeoException>>() |
| 186 | + callback.accept(error) |
| 187 | + } |
| 188 | + geo.searchByCoordinates(position) |
| 189 | + } |
| 190 | +} |
0 commit comments