Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/gradle-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Gradle Build

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
java: [17, 21]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: temurin
java-version: ${{ matrix.java }}
- uses: gradle/gradle-build-action@v3

- name: Grant execute permission to Gradle wrapper
run: chmod +x ./gradlew

- run: ./gradlew clean build

- name: Run unit tests
run: ./gradlew test
9 changes: 9 additions & 0 deletions data-core/src/test/java/android/util/Log.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package android.util

object Log {
@JvmStatic fun d(tag: String, msg: String) = 0
@JvmStatic fun e(tag: String, msg: String) = 0
@JvmStatic fun i(tag: String, msg: String) = 0
@JvmStatic fun v(tag: String, msg: String) = 0
@JvmStatic fun w(tag: String, msg: String) = 0
}
39 changes: 39 additions & 0 deletions data-core/src/test/java/com/tymex/github/LinkHeaderTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.tymex.github.data.core

import com.tymex.github.data.core.data.ext.isLastPage
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test

class LinkHeaderTest {

@Test
fun `when link header contains rel next, should return false`() {
val linkHeader = "<https://api.github.com/users?per_page=20&since=100>; rel=\"next\""
assertThat(linkHeader.isLastPage(), `is`(false))
}

@Test
fun `when link header does not contain rel next, should return true`() {
val linkHeader = "<https://api.github.com/users?per_page=20&since=100>; rel=\"prev\""
assertThat(linkHeader.isLastPage(), `is`(true))
}

@Test
fun `when link header is empty, should return true`() {
val linkHeader = ""
assertThat(linkHeader.isLastPage(), `is`(true))
}

@Test
fun `when link header has unrelated text, should return true`() {
val linkHeader = "Some random header text"
assertThat(linkHeader.isLastPage(), `is`(true))
}

@Test
fun `when link header contains both rel next and rel prev, should return false`() {
val linkHeader = "<https://api.github.com/users?per_page=20&since=100>; rel=\"next\", <https://api.github.com/users?per_page=20&since=50>; rel=\"prev\""
assertThat(linkHeader.isLastPage(), `is`(false))
}
}
56 changes: 56 additions & 0 deletions data-core/src/test/java/com/tymex/github/NetworkResponseTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.tymex.github.data.core

import com.tymex.github.data.core.data.ext.toNetworkResponse
import com.tymex.github.data.core.data.model.NetworkResponse
import junit.framework.TestCase.assertTrue
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.ResponseBody
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
import org.mockito.Mockito
import retrofit2.Response

class NetworkResponseTest {

@Test
fun `when response is successful and body is present, should return NetworkResponse Success`() {
val mockResponse = Response.success("Hello World")

val result = mockResponse.toNetworkResponse()

assertTrue(result is NetworkResponse.Success)
assertThat((result as NetworkResponse.Success).data, `is`("Hello World"))
}

@Test
fun `when response is successful but body is null, should return NetworkResponse Error`() {
val mockResponse: Response<String> = Response.success(null)

val result = mockResponse.toNetworkResponse()

assertTrue(result is NetworkResponse.Error)
assertThat((result as NetworkResponse.Error).message, `is`("Response body is empty"))
}

@Test
fun `when response is unsuccessful with error body, should return NetworkResponse Error`() {
val errorBody = ResponseBody.create("application/json".toMediaType(), "Not Found")
val mockResponse: Response<String> = Response.error(404, errorBody)

val result = mockResponse.toNetworkResponse()

assertTrue(result is NetworkResponse.Error)
assertThat((result as NetworkResponse.Error).message, `is`("Not Found"))
}

@Test
fun `when response is unsuccessful with null error body, should return NetworkResponse Error with Unknown`() {
val mockResponse: Response<String> = Response.error(500, Mockito.mock(ResponseBody::class.java))

val result = mockResponse.toNetworkResponse()

assertTrue(result is NetworkResponse.Error)
assertThat((result as NetworkResponse.Error).message, `is`("Unknown"))
}
}
Loading