|
| 1 | +package org.wordpress.android.fluxc.network.rest.wpapi.taxonomy |
| 2 | + |
| 3 | +import kotlinx.coroutines.CoroutineScope |
| 4 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 5 | +import kotlinx.coroutines.test.TestCoroutineScheduler |
| 6 | +import kotlinx.coroutines.test.UnconfinedTestDispatcher |
| 7 | +import kotlinx.coroutines.test.runTest |
| 8 | +import org.junit.Assert.assertEquals |
| 9 | +import org.junit.Assert.assertNotNull |
| 10 | +import org.junit.Assert.assertNull |
| 11 | +import org.junit.Before |
| 12 | +import org.junit.Test |
| 13 | +import org.junit.runner.RunWith |
| 14 | +import org.mockito.ArgumentCaptor |
| 15 | +import org.mockito.Mock |
| 16 | +import org.mockito.MockitoAnnotations |
| 17 | +import org.mockito.kotlin.any |
| 18 | +import org.mockito.kotlin.mock |
| 19 | +import org.mockito.kotlin.verify |
| 20 | +import org.mockito.kotlin.whenever |
| 21 | +import org.robolectric.RobolectricTestRunner |
| 22 | +import org.wordpress.android.fluxc.Dispatcher |
| 23 | +import org.wordpress.android.fluxc.action.TaxonomyAction |
| 24 | +import org.wordpress.android.fluxc.annotations.action.Action |
| 25 | +import org.wordpress.android.fluxc.model.SiteModel |
| 26 | +import org.wordpress.android.fluxc.network.rest.wpapi.rs.WpApiClientProvider |
| 27 | +import org.wordpress.android.fluxc.store.TaxonomyStore.FetchTermsResponsePayload |
| 28 | +import org.wordpress.android.fluxc.store.TaxonomyStore.TaxonomyErrorType |
| 29 | +import org.wordpress.android.fluxc.utils.AppLogWrapper |
| 30 | +import rs.wordpress.api.kotlin.WpApiClient |
| 31 | +import rs.wordpress.api.kotlin.WpRequestResult |
| 32 | +import uniffi.wp_api.TagWithEditContext |
| 33 | +import uniffi.wp_api.TagsRequestListWithEditContextResponse |
| 34 | +import uniffi.wp_api.TaxonomyType |
| 35 | +import uniffi.wp_api.WpNetworkHeaderMap |
| 36 | + |
| 37 | +@ExperimentalCoroutinesApi |
| 38 | +@RunWith(RobolectricTestRunner::class) |
| 39 | +class TaxonomyRsApiRestClientTest { |
| 40 | + @Mock |
| 41 | + private lateinit var dispatcher: Dispatcher |
| 42 | + @Mock |
| 43 | + private lateinit var appLogWrapper: AppLogWrapper |
| 44 | + @Mock |
| 45 | + private lateinit var wpApiClientProvider: WpApiClientProvider |
| 46 | + @Mock |
| 47 | + private lateinit var wpApiClient: WpApiClient |
| 48 | + |
| 49 | + private lateinit var testScope: CoroutineScope |
| 50 | + private lateinit var taxonomyClient: TaxonomyRsApiRestClient |
| 51 | + |
| 52 | + private val testSite = SiteModel().apply { |
| 53 | + id = 123 |
| 54 | + url = "https://test.wordpress.com" |
| 55 | + } |
| 56 | + |
| 57 | + private val testTaxonomyName = "post_tag" |
| 58 | + |
| 59 | + @Before |
| 60 | + fun setUp() { |
| 61 | + MockitoAnnotations.openMocks(this) |
| 62 | + |
| 63 | + val testScheduler = TestCoroutineScheduler() |
| 64 | + val testDispatcher = UnconfinedTestDispatcher(testScheduler) |
| 65 | + testScope = CoroutineScope(testDispatcher) |
| 66 | + |
| 67 | + whenever(wpApiClientProvider.getWpApiClient(testSite)).thenReturn(wpApiClient) |
| 68 | + |
| 69 | + taxonomyClient = TaxonomyRsApiRestClient( |
| 70 | + testScope, |
| 71 | + dispatcher, |
| 72 | + appLogWrapper, |
| 73 | + wpApiClientProvider |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun `fetchPostTags with error response dispatches error action`() = runTest { |
| 79 | + // Use a concrete error type that we can create - UnknownError requires statusCode and response |
| 80 | + val errorResponse = WpRequestResult.UnknownError<Any>( |
| 81 | + statusCode = 500u, |
| 82 | + response = "Internal Server Error" |
| 83 | + ) |
| 84 | + |
| 85 | + whenever(wpApiClient.request<Any>(any())).thenReturn(errorResponse) |
| 86 | + |
| 87 | + taxonomyClient.fetchPostTags(testSite, testTaxonomyName) |
| 88 | + |
| 89 | + // Verify dispatcher was called with error action |
| 90 | + val actionCaptor = ArgumentCaptor.forClass(Action::class.java) |
| 91 | + verify(dispatcher).dispatch(actionCaptor.capture()) |
| 92 | + |
| 93 | + val capturedAction = actionCaptor.value |
| 94 | + val payload = capturedAction.payload as FetchTermsResponsePayload |
| 95 | + assertEquals(capturedAction.type, TaxonomyAction.FETCHED_TERMS) |
| 96 | + assertEquals(testTaxonomyName, payload.taxonomy) |
| 97 | + assertNotNull(payload.error) |
| 98 | + assertEquals(TaxonomyErrorType.GENERIC_ERROR, payload.error?.type) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun `fetchPostTags with success response dispatches success action`() = runTest { |
| 103 | + val tagWithEditContext = listOf( |
| 104 | + createTestTagWithEditContext(), |
| 105 | + createTestTagWithEditContext() |
| 106 | + ) |
| 107 | + |
| 108 | + // Create the correct response structure following the MediaRSApiRestClientTest pattern |
| 109 | + val tagResponse = TagsRequestListWithEditContextResponse( |
| 110 | + tagWithEditContext, |
| 111 | + mock<WpNetworkHeaderMap>(), |
| 112 | + null, |
| 113 | + null |
| 114 | + ) |
| 115 | + |
| 116 | + val successResponse: WpRequestResult<TagsRequestListWithEditContextResponse> = WpRequestResult.Success( |
| 117 | + response = tagResponse |
| 118 | + ) |
| 119 | + |
| 120 | + whenever(wpApiClient.request<TagsRequestListWithEditContextResponse>(any())).thenReturn(successResponse) |
| 121 | + |
| 122 | + taxonomyClient.fetchPostTags(testSite, testTaxonomyName) |
| 123 | + |
| 124 | + // Verify dispatcher was called with success action |
| 125 | + val actionCaptor = ArgumentCaptor.forClass(Action::class.java) |
| 126 | + verify(dispatcher).dispatch(actionCaptor.capture()) |
| 127 | + |
| 128 | + val capturedAction = actionCaptor.value |
| 129 | + val payload = capturedAction.payload as FetchTermsResponsePayload |
| 130 | + assertEquals(capturedAction.type, TaxonomyAction.FETCHED_TERMS) |
| 131 | + assertEquals(testTaxonomyName, payload.taxonomy) |
| 132 | + assertEquals(testSite, payload.site) |
| 133 | + assertNotNull(payload.terms) |
| 134 | + assertEquals(2, payload.terms.terms.size) |
| 135 | + assertNull(payload.error) |
| 136 | + } |
| 137 | + |
| 138 | + private fun createTestTagWithEditContext(): TagWithEditContext { |
| 139 | + return TagWithEditContext( |
| 140 | + id = 1L, |
| 141 | + count = 5L, |
| 142 | + description = "Test tag description", |
| 143 | + link = "https://example.com/tag/test", |
| 144 | + name = "Test Tag", |
| 145 | + slug = "test-tag", |
| 146 | + taxonomy = TaxonomyType.PostTag |
| 147 | + ) |
| 148 | + } |
| 149 | +} |
| 150 | + |
0 commit comments