Skip to content

Commit

Permalink
Merge pull request #46 in XP/xp-v-golden-gate from shawn/FC-6076_gg_m…
Browse files Browse the repository at this point in the history
…etric_xp to master

* commit '7d247d15a069b29b3d9c1ed33257826c7149755e':
  FC-6076: add unit tests
  FC-6076: add gg natice exception
  • Loading branch information
ShawnW858 committed Mar 24, 2021
2 parents a17cffa + 7d247d1 commit 87aee2f
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.fitbit.goldengate.bindings

open class GoldenGateNativeException(
val title: String,
val nakCode: Int,
val nakNameSpace: String = "gg.result",
val nakMessage: String? = null,
cause: Throwable? = null
): Throwable(title, cause)
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.fitbit.goldengate.bindings

private const val GG_ERROR_BASE = -10000

enum class GoldenGateNativeResult(val code: Int, val title: String) {
GG_SUCCESS (( 0), "the operation or call succeeded"),
GG_FAILURE (( -1), "an unspecified failure"),
GG_ERROR_OUT_OF_MEMORY ((GG_ERROR_BASE - 0), "Out of memory"),
GG_ERROR_OUT_OF_RESOURCES ((GG_ERROR_BASE - 1), "Out of resources"),
GG_ERROR_INTERNAL ((GG_ERROR_BASE - 2), "Internal error"),
GG_ERROR_INVALID_PARAMETERS ((GG_ERROR_BASE - 3), "Invalid parameters"),
GG_ERROR_INVALID_STATE ((GG_ERROR_BASE - 4), "Invalid state"),
GG_ERROR_NOT_IMPLEMENTED ((GG_ERROR_BASE - 5), "Not implemented"),
GG_ERROR_OUT_OF_RANGE ((GG_ERROR_BASE - 6), "Out of range"),
GG_ERROR_ACCESS_DENIED ((GG_ERROR_BASE - 7), "Access denied"),
GG_ERROR_INVALID_SYNTAX ((GG_ERROR_BASE - 8), "Invalid syntax"),
GG_ERROR_NOT_SUPPORTED ((GG_ERROR_BASE - 9), "Not supported"),
GG_ERROR_INVALID_FORMAT ((GG_ERROR_BASE - 10), "Invalid format"),
GG_ERROR_NOT_ENOUGH_SPACE ((GG_ERROR_BASE - 11), "Not enough space"),
GG_ERROR_NO_SUCH_ITEM ((GG_ERROR_BASE - 12), "No such item (item not found)"),
GG_ERROR_OVERFLOW ((GG_ERROR_BASE - 13), "Overflow"),
GG_ERROR_TIMEOUT ((GG_ERROR_BASE - 14), "A timeout occurred"),
GG_ERROR_WOULD_BLOCK ((GG_ERROR_BASE - 15), "The operation would block / try again later"),
GG_ERROR_PERMISSION_DENIED ((GG_ERROR_BASE - 16), "Permission denied"),
GG_ERROR_INTERRUPTED ((GG_ERROR_BASE - 17), "Operation interrupted"),
GG_ERROR_IN_USE ((GG_ERROR_BASE - 18), "Resource already in use"),
GG_ERROR_BASE_IO ((GG_ERROR_BASE - 100), "I/O related error"),
GG_ERROR_BASE_SOCKET ((GG_ERROR_BASE - 200), "Socket related error"),
GG_ERROR_BASE_COAP ((GG_ERROR_BASE - 300), "CoAP related error"),
GG_ERROR_BASE_REMOTE ((GG_ERROR_BASE - 400), "Remote API related error"),
GG_ERROR_BASE_GATTLINK ((GG_ERROR_BASE - 500), "Gattlink related error"),
GG_ERROR_BASE_TLS ((GG_ERROR_BASE - 600), "TLS related error");

companion object {
fun getNativeResultFrom(code: Int): GoldenGateNativeResult{
return when(code) {
in 0 .. Int.MAX_VALUE -> return GG_SUCCESS
in -10099 .. -1 -> {
for (value in values()) {
if (value.code == code) return value
}
GG_FAILURE
}
in -10199 .. -10100 -> GG_ERROR_BASE_IO
in -10299 .. -10200 -> GG_ERROR_BASE_SOCKET
in -10399 .. -10300 -> GG_ERROR_BASE_COAP
in -10499 .. -10400 -> GG_ERROR_BASE_REMOTE
in -10599 .. -10500 -> GG_ERROR_BASE_GATTLINK
in -10699 .. -10600 -> GG_ERROR_BASE_TLS
else -> GG_FAILURE
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package com.fitbit.goldengate.bindings.stack
import androidx.annotation.Keep
import androidx.annotation.VisibleForTesting
import com.fitbit.goldengate.bindings.DataSinkDataSource
import com.fitbit.goldengate.bindings.GoldenGateNativeException
import com.fitbit.goldengate.bindings.GoldenGateNativeResult
import com.fitbit.goldengate.bindings.NativeReference
import com.fitbit.goldengate.bindings.dtls.DtlsProtocolStatus
import com.fitbit.goldengate.bindings.dtls.TlsKeyResolver
Expand Down Expand Up @@ -67,7 +69,8 @@ class Stack constructor(
TlsKeyResolverRegistry.resolvers
)
if(stackCreationResult.result < 0 ) {
throw IllegalStateException("Failed to create stack, error code: ${stackCreationResult.result}")
val error = GoldenGateNativeResult.getNativeResultFrom(stackCreationResult.result)
throw GoldenGateNativeException(error.title, stackCreationResult.result)
} else {
return stackCreationResult.stackPointer
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.fitbit.goldengate.bindings

import org.junit.Assert.*
import org.junit.Test

class GoldenGateNativeResultTest {

@Test
fun `should convert error code to GoldenGateNativeResult enum constant`() {
assertEquals(GoldenGateNativeResult.getNativeResultFrom(0), GoldenGateNativeResult.GG_SUCCESS)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(100), GoldenGateNativeResult.GG_SUCCESS)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(-1), GoldenGateNativeResult.GG_FAILURE)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(-100), GoldenGateNativeResult.GG_FAILURE)

assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10000), GoldenGateNativeResult.GG_ERROR_OUT_OF_MEMORY)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10013), GoldenGateNativeResult.GG_ERROR_OVERFLOW)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10018), GoldenGateNativeResult.GG_ERROR_IN_USE)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10099), GoldenGateNativeResult.GG_FAILURE)

assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10100), GoldenGateNativeResult.GG_ERROR_BASE_IO)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10199), GoldenGateNativeResult.GG_ERROR_BASE_IO)

assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10200), GoldenGateNativeResult.GG_ERROR_BASE_SOCKET)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10299), GoldenGateNativeResult.GG_ERROR_BASE_SOCKET)

assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10300), GoldenGateNativeResult.GG_ERROR_BASE_COAP)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10399), GoldenGateNativeResult.GG_ERROR_BASE_COAP)

assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10400), GoldenGateNativeResult.GG_ERROR_BASE_REMOTE)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10499), GoldenGateNativeResult.GG_ERROR_BASE_REMOTE)

assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10500), GoldenGateNativeResult.GG_ERROR_BASE_GATTLINK)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10599), GoldenGateNativeResult.GG_ERROR_BASE_GATTLINK)

assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10600), GoldenGateNativeResult.GG_ERROR_BASE_TLS)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10699), GoldenGateNativeResult.GG_ERROR_BASE_TLS)

assertEquals(GoldenGateNativeResult.getNativeResultFrom(-10700), GoldenGateNativeResult.GG_FAILURE)

assertEquals(GoldenGateNativeResult.getNativeResultFrom(Int.MAX_VALUE), GoldenGateNativeResult.GG_SUCCESS)
assertEquals(GoldenGateNativeResult.getNativeResultFrom(Int.MIN_VALUE), GoldenGateNativeResult.GG_FAILURE)
}
}

0 comments on commit 87aee2f

Please sign in to comment.