diff --git a/build.gradle b/build.gradle index 33bcd14..79e805d 100644 --- a/build.gradle +++ b/build.gradle @@ -7,6 +7,7 @@ plugins { allprojects { repositories { mavenCentral() + maven {url "https://jitpack.io"} } } @@ -39,6 +40,9 @@ dependencies { // OkHttp Logging Interceptor : Square’s meticulous HTTP client for Java and Kotlin. implementation 'com.squareup.okhttp3:logging-interceptor:4.9.2' + + //Expekt + testImplementation "com.github.theapache64:expekt:$expekt_version" } compileKotlin { diff --git a/gpm.json b/gpm.json index 7d9ce76..1760073 100644 --- a/gpm.json +++ b/gpm.json @@ -1,158 +1,172 @@ { - "added": [ - { - "id": 1, - "type": "implementation", - "installed_name": "retrofit", - "gpm_dep": { - "artifact_id": "retrofit", - "default_type": "implementation", - "docs": "https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit", - "get_from": "Central", - "group_id": "com.squareup.retrofit2", - "name": "Retrofit", - "description": "A type-safe HTTP client for Android and Java." - } - }, - { - "id": 2, - "type": "implementation", - "installed_name": "moshi-convert", - "gpm_dep": { - "artifact_id": "converter-moshi", - "default_type": "implementation", - "docs": "https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-moshi", - "get_from": "Central", - "group_id": "com.squareup.retrofit2", - "name": "Converter: Moshi", - "description": "A Retrofit Converter which uses Moshi for serialization." - } - }, - { - "id": 3, - "type": "implementation", - "installed_name": "coroutines", - "gpm_dep": { - "artifact_id": "kotlinx-coroutines-core", - "default_type": "implementation", - "docs": "https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core", - "get_from": "Central", - "group_id": "org.jetbrains.kotlinx", - "name": "Kotlinx Coroutines Core", - "description": "Coroutines support libraries for Kotlin" - } - }, - { - "id": 4, - "type": "kapt", - "installed_name": "moshi-codegen", - "gpm_dep": { - "artifact_id": "moshi-kotlin-codegen", - "default_type": "implementation", - "docs": "https://mvnrepository.com/artifact/com.squareup.moshi/moshi-kotlin-codegen", - "get_from": "Central", - "group_id": "com.squareup.moshi", - "name": "Moshi Kotlin Codegen", - "description": "Moshi Kotlin Codegen" - } - }, - { - "id": 5, - "type": "implementation", - "installed_name": "moshi", - "gpm_dep": { - "artifact_id": "moshi", - "default_type": "implementation", - "docs": "https://mvnrepository.com/artifact/com.squareup.moshi/moshi", - "get_from": "Central", - "group_id": "com.squareup.moshi", - "name": "Moshi", - "description": "Moshi" - } - }, - { - "id": 6, - "type": "implementation", - "installed_name": "fastcsv", - "gpm_dep": { - "artifact_id": "fastcsv", - "default_type": "implementation", - "docs": "https://mvnrepository.com/artifact/de.siegmar/fastcsv", - "get_from": "Central", - "group_id": "de.siegmar", - "name": "FastCSV", - "description": "Ultra fast and simple RFC 4180 compliant CSV library." - } - }, - { - "id": 7, - "type": "implementation", - "installed_name": "json", - "gpm_dep": { - "artifact_id": "json", - "default_type": "implementation", - "docs": "https://mvnrepository.com/artifact/org.json/json", - "get_from": "Central", - "group_id": "org.json", - "name": "JSON In Java", - "description": "JSON is a light-weight, language independent, data interchange format.See http://www.JSON.org/The files in this package implement JSON encoders/decoders in Java.It also includes the capability to convert between JSON and XML, HTTPheaders, Cookies, and CDL.This is a reference implementation. There is a large number of JSON packagesin Java. Perhaps someday the Java community will standardize on one. Untilthen, choose carefully.The license includes this restriction: \"The software ..." - } - }, - { - "id": 8, - "type": "implementation", - "installed_name": "expekt", - "gpm_dep": { - "artifact_id": "expekt", - "default_type": "implementation", - "docs": "https://mvnrepository.com/artifact/com.theapache64/expekt", - "get_from": "JCenter", - "group_id": "com.theapache64", - "name": "Expekt", - "description": "An assertion library for Kotlin" - } - }, - { - "id": 9, - "type": "implementation", - "installed_name": "materialratingbar", - "gpm_dep": { - "artifact_id": "library", - "default_type": "implementation", - "docs": "https://mvnrepository.com/artifact/me.zhanghai.android.materialratingbar/library", - "get_from": "Central", - "group_id": "me.zhanghai.android.materialratingbar", - "name": "MaterialRatingBar Library", - "description": "A Material Design RatingBar with consistent appearance" - } - }, - { - "id": 10, - "type": "implementation", - "installed_name": "scalars-converter", - "gpm_dep": { - "artifact_id": "converter-scalars", - "default_type": "implementation", - "docs": "https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-scalars", - "get_from": "Central", - "group_id": "com.squareup.retrofit2", - "name": "Converter: Scalars", - "description": "A Retrofit Converter for Java's scalar value types." - } - }, - { - "id": 11, - "type": "implementation", - "installed_name": "okhttp-interceptor", - "gpm_dep": { - "artifact_id": "logging-interceptor", - "default_type": "implementation", - "docs": "https://mvnrepository.com/artifact/com.squareup.okhttp3/logging-interceptor", - "get_from": "Central", - "group_id": "com.squareup.okhttp3", - "name": "OkHttp Logging Interceptor", - "description": "Square’s meticulous HTTP client for Java and Kotlin." - } - } - ] + "added": [ + { + "id": 1, + "type": "implementation", + "installed_name": "retrofit", + "gpm_dep": { + "artifact_id": "retrofit", + "default_type": "implementation", + "docs": "https://mvnrepository.com/artifact/com.squareup.retrofit2/retrofit", + "get_from": "Central", + "group_id": "com.squareup.retrofit2", + "name": "Retrofit", + "description": "A type-safe HTTP client for Android and Java." + } + }, + { + "id": 2, + "type": "implementation", + "installed_name": "moshi-convert", + "gpm_dep": { + "artifact_id": "converter-moshi", + "default_type": "implementation", + "docs": "https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-moshi", + "get_from": "Central", + "group_id": "com.squareup.retrofit2", + "name": "Converter: Moshi", + "description": "A Retrofit Converter which uses Moshi for serialization." + } + }, + { + "id": 3, + "type": "implementation", + "installed_name": "coroutines", + "gpm_dep": { + "artifact_id": "kotlinx-coroutines-core", + "default_type": "implementation", + "docs": "https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core", + "get_from": "Central", + "group_id": "org.jetbrains.kotlinx", + "name": "Kotlinx Coroutines Core", + "description": "Coroutines support libraries for Kotlin" + } + }, + { + "id": 4, + "type": "kapt", + "installed_name": "moshi-codegen", + "gpm_dep": { + "artifact_id": "moshi-kotlin-codegen", + "default_type": "implementation", + "docs": "https://mvnrepository.com/artifact/com.squareup.moshi/moshi-kotlin-codegen", + "get_from": "Central", + "group_id": "com.squareup.moshi", + "name": "Moshi Kotlin Codegen", + "description": "Moshi Kotlin Codegen" + } + }, + { + "id": 5, + "type": "implementation", + "installed_name": "moshi", + "gpm_dep": { + "artifact_id": "moshi", + "default_type": "implementation", + "docs": "https://mvnrepository.com/artifact/com.squareup.moshi/moshi", + "get_from": "Central", + "group_id": "com.squareup.moshi", + "name": "Moshi", + "description": "Moshi" + } + }, + { + "id": 6, + "type": "implementation", + "installed_name": "fastcsv", + "gpm_dep": { + "artifact_id": "fastcsv", + "default_type": "implementation", + "docs": "https://mvnrepository.com/artifact/de.siegmar/fastcsv", + "get_from": "Central", + "group_id": "de.siegmar", + "name": "FastCSV", + "description": "Ultra fast and simple RFC 4180 compliant CSV library." + } + }, + { + "id": 7, + "type": "implementation", + "installed_name": "json", + "gpm_dep": { + "artifact_id": "json", + "default_type": "implementation", + "docs": "https://mvnrepository.com/artifact/org.json/json", + "get_from": "Central", + "group_id": "org.json", + "name": "JSON In Java", + "description": "JSON is a light-weight, language independent, data interchange format.See http://www.JSON.org/The files in this package implement JSON encoders/decoders in Java.It also includes the capability to convert between JSON and XML, HTTPheaders, Cookies, and CDL.This is a reference implementation. There is a large number of JSON packagesin Java. Perhaps someday the Java community will standardize on one. Untilthen, choose carefully.The license includes this restriction: \"The software ..." + } + }, + { + "id": 8, + "type": "implementation", + "installed_name": "expekt", + "gpm_dep": { + "artifact_id": "expekt", + "default_type": "implementation", + "docs": "https://mvnrepository.com/artifact/com.theapache64/expekt", + "get_from": "JCenter", + "group_id": "com.theapache64", + "name": "Expekt", + "description": "An assertion library for Kotlin" + } + }, + { + "id": 9, + "type": "implementation", + "installed_name": "materialratingbar", + "gpm_dep": { + "artifact_id": "library", + "default_type": "implementation", + "docs": "https://mvnrepository.com/artifact/me.zhanghai.android.materialratingbar/library", + "get_from": "Central", + "group_id": "me.zhanghai.android.materialratingbar", + "name": "MaterialRatingBar Library", + "description": "A Material Design RatingBar with consistent appearance" + } + }, + { + "id": 10, + "type": "implementation", + "installed_name": "scalars-converter", + "gpm_dep": { + "artifact_id": "converter-scalars", + "default_type": "implementation", + "docs": "https://mvnrepository.com/artifact/com.squareup.retrofit2/converter-scalars", + "get_from": "Central", + "group_id": "com.squareup.retrofit2", + "name": "Converter: Scalars", + "description": "A Retrofit Converter for Java's scalar value types." + } + }, + { + "id": 11, + "type": "implementation", + "installed_name": "okhttp-interceptor", + "gpm_dep": { + "artifact_id": "logging-interceptor", + "default_type": "implementation", + "docs": "https://mvnrepository.com/artifact/com.squareup.okhttp3/logging-interceptor", + "get_from": "Central", + "group_id": "com.squareup.okhttp3", + "name": "OkHttp Logging Interceptor", + "description": "Square’s meticulous HTTP client for Java and Kotlin." + } + }, + { + "id": 12, + "type": "testImplementation", + "installed_name": "coroutines-test", + "gpm_dep": { + "artifact_id": "kotlinx-coroutines-test", + "default_type": "implementation", + "docs": "https://mvnrepository.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-test", + "get_from": "Central", + "group_id": "org.jetbrains.kotlinx", + "name": "Kotlinx Coroutines Test", + "description": "Coroutines support libraries for Kotlin" + } + } + ] } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 9692887..5f56c63 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,4 +5,5 @@ retrosheet_version=2.0.0-beta01 retrofit_version=2.9.0 moshi_version=1.12.0 coroutines_version=1.5.2-native-mt -junit_version=4.13.2 \ No newline at end of file +junit_version=4.13.2 +expekt_version=1.0.0 \ No newline at end of file diff --git a/retrosheet/build.gradle b/retrosheet/build.gradle index ea55a90..7f56b8d 100644 --- a/retrosheet/build.gradle +++ b/retrosheet/build.gradle @@ -90,7 +90,7 @@ dependencies { kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version" // Expekt : An assertion library for Kotlin - testImplementation 'com.github.theapache64:expekt:1.0.0' + testImplementation "com.github.theapache64:expekt:$expekt_version" // Mockito Core : Mockito mock objects library core API and implementation testImplementation 'org.mockito:mockito-inline:4.0.0' diff --git a/src/main/kotlin/com/github/theapache64/retrosheet/sample/notes/Notes.kt b/src/main/kotlin/com/github/theapache64/retrosheet/sample/notes/Notes.kt index b2fbb2f..f44ad9a 100644 --- a/src/main/kotlin/com/github/theapache64/retrosheet/sample/notes/Notes.kt +++ b/src/main/kotlin/com/github/theapache64/retrosheet/sample/notes/Notes.kt @@ -1,52 +1,90 @@ package com.github.theapache64.retrosheet.sample.notes import com.github.theapache64.retrosheet.RetrosheetInterceptor +import com.github.theapache64.retrosheet.annotations.Read +import com.github.theapache64.retrosheet.annotations.Write import com.squareup.moshi.Moshi import kotlinx.coroutines.runBlocking import okhttp3.OkHttpClient import retrofit2.Retrofit import retrofit2.converter.moshi.MoshiConverterFactory +import retrofit2.http.Body +import retrofit2.http.GET +import retrofit2.http.POST +import retrofit2.http.Query /** * Created by theapache64 : Jul 21 Tue,2020 @ 02:11 */ -const val ADD_NOTE_ENDPOINT = "add_note" -fun main() = runBlocking { +object Notes { + private const val SHEET_NAME = "notes" + private const val ADD_NOTE_ENDPOINT = "add_note" + + interface NotesApi { + + @Read("SELECT * WHERE title = :title") + @GET(SHEET_NAME) + suspend fun getNote( + @Query("title") title: String + ): Note + + @Read("SELECT *") + @GET(SHEET_NAME) // sheet name + suspend fun getNotes(): List + + + @Write + @POST(ADD_NOTE_ENDPOINT) // form name + suspend fun addNote(@Body addNoteRequest: AddNoteRequest): AddNoteRequest + + + /** + * To test failure scenario. + */ + @Read("SELECT *") + @GET("invalid_sheet") // sheet name + suspend fun getNotesFromInvalidSheet(): List + } - val retrosheetInterceptor = RetrosheetInterceptor.Builder() - .setLogging(false) - // To Read - .addSheet( - "notes", // sheet name - "created_at", "title", "description" // columns in same order - ) - // To write - .addForm( - ADD_NOTE_ENDPOINT, - "https://docs.google.com/forms/d/e/1FAIpQLSdmavg6P4eZTmIu-0M7xF_z-qDCHdpGebX8MGL43HSGAXcd3w/viewform?usp=sf_link" // form link - ) - .build() - - val okHttpClient = OkHttpClient.Builder() - .addInterceptor(retrosheetInterceptor) - .build() - - val moshi = Moshi.Builder().build() - - val retrofit = Retrofit.Builder() - .baseUrl("https://docs.google.com/spreadsheets/d/1YTWKe7_mzuwl7AO1Es1aCtj5S9buh3vKauKCMjx1j_M/") // Sheet's public URL - .client(okHttpClient) - .addConverterFactory(MoshiConverterFactory.create(moshi)) - .build() - - val notesApi = retrofit.create(NotesApi::class.java) + fun createApi(): NotesApi { + val retrosheetInterceptor = RetrosheetInterceptor.Builder() + .setLogging(true) + // To Read + .addSheet( + SHEET_NAME, // sheet name + "created_at", "title", "description" // columns in same order + ) + // To write + .addForm( + ADD_NOTE_ENDPOINT, + "https://docs.google.com/forms/d/e/1FAIpQLSdmavg6P4eZTmIu-0M7xF_z-qDCHdpGebX8MGL43HSGAXcd3w/viewform?usp=sf_link" // form link + ) + .build() + + val okHttpClient = OkHttpClient.Builder() + .addInterceptor(retrosheetInterceptor) + .build() + + val moshi = Moshi.Builder().build() + + val retrofit = Retrofit.Builder() + .baseUrl("https://docs.google.com/spreadsheets/d/1YTWKe7_mzuwl7AO1Es1aCtj5S9buh3vKauKCMjx1j_M/") // Sheet's public URL + .client(okHttpClient) + .addConverterFactory(MoshiConverterFactory.create(moshi)) + .build() + + return retrofit.create(NotesApi::class.java) + } +} + +fun main() = runBlocking { + val notesApi = Notes.createApi() println(notesApi.getNotes()) // Adding sample order val addNote = notesApi.addNote( - AddNoteRequest("Dynamic Note 1", "Dynamic Desc 1") + AddNoteRequest("Dynamic Note 1", "Dynamic Desc 1: ${java.util.Date()}") ) println(addNote) - Unit } diff --git a/src/main/kotlin/com/github/theapache64/retrosheet/sample/notes/NotesApi.kt b/src/main/kotlin/com/github/theapache64/retrosheet/sample/notes/NotesApi.kt index a2f128c..c43dcc8 100644 --- a/src/main/kotlin/com/github/theapache64/retrosheet/sample/notes/NotesApi.kt +++ b/src/main/kotlin/com/github/theapache64/retrosheet/sample/notes/NotesApi.kt @@ -6,13 +6,3 @@ import retrofit2.http.Body import retrofit2.http.GET import retrofit2.http.POST -interface NotesApi { - - @Read("SELECT *") - @GET("notes") // sheet name - suspend fun getNotes(): List - - @Write - @POST(ADD_NOTE_ENDPOINT) // form name - suspend fun addNote(@Body addNoteRequest: AddNoteRequest): AddNoteRequest -} diff --git a/src/test/java/com/github/theapache64/retrosheet/integration/ReadTest.kt b/src/test/java/com/github/theapache64/retrosheet/integration/ReadTest.kt index 838ed58..30165bf 100644 --- a/src/test/java/com/github/theapache64/retrosheet/integration/ReadTest.kt +++ b/src/test/java/com/github/theapache64/retrosheet/integration/ReadTest.kt @@ -1,23 +1,35 @@ package com.github.theapache64.retrosheet.integration +import com.github.theapache64.expekt.should +import com.github.theapache64.retrosheet.sample.notes.Notes +import com.github.theapache64.retrosheet.util.runBlockingTest +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.runBlocking +import okio.IOException import org.junit.Test +@ExperimentalCoroutinesApi class ReadTest { + private val notesApi = Notes.createApi() @Test - fun `Reads data`() { - + fun `Reads data`() = runBlockingTest { + notesApi.getNote("Do not delete this row").description.should + .equal("This is custom desc") + Unit } @Test - fun `Reads list of data`() { - + fun `Reads list of data`() = runBlockingTest { + notesApi.getNotes().size.should.above(1) + Unit } - @Test - fun `Fails to read from an invalid sheet`() { - + @Test(expected = IOException::class) + fun `Fails to read from an invalid sheet`() = runBlocking { + notesApi.getNotesFromInvalidSheet() + Unit } } \ No newline at end of file diff --git a/src/test/java/com/github/theapache64/retrosheet/util/runBlockingTest.kt b/src/test/java/com/github/theapache64/retrosheet/util/runBlockingTest.kt new file mode 100644 index 0000000..dbfe8cc --- /dev/null +++ b/src/test/java/com/github/theapache64/retrosheet/util/runBlockingTest.kt @@ -0,0 +1,9 @@ +package com.github.theapache64.retrosheet.util + +import kotlinx.coroutines.runBlocking + +fun runBlockingTest( + invoke: suspend () -> Unit +) = runBlocking { + invoke() +} \ No newline at end of file