Skip to content

Commit

Permalink
refactor: make context broker URL a global variable of NGSI-LD services
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Jun 29, 2021
1 parent bc6134d commit 66daa31
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 118 deletions.
10 changes: 4 additions & 6 deletions lib/src/main/kotlin/io/egm/kngsild/api/BatchEntityService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import java.net.http.HttpRequest
import java.net.http.HttpResponse

class BatchEntityService(
private val contextBrokerUrl: String,
private val authUtils: AuthUtils
) {

Expand All @@ -38,12 +39,11 @@ class BatchEntityService(
)

fun create(
brokerUrl: String,
payload: String
): Either<ApplicationError, HttpResponse<String>> {
return authUtils.getToken().flatMap {
val request = HttpRequest.newBuilder().uri(
URI.create("$brokerUrl/ngsi-ld/v1/entityOperations/create")
URI.create("$contextBrokerUrl/ngsi-ld/v1/entityOperations/create")
)
.setHeader("Content-Type", APPLICATION_JSONLD)
.setHeader("Authorization", "Bearer $it")
Expand All @@ -67,14 +67,13 @@ class BatchEntityService(
}

fun upsert(
brokerUrl: String,
payload: String,
queryParams: Map<String, String>
): Either<ApplicationError, HttpResponse<String>> {
val params: String = HttpUtils.paramsUrlBuilder(queryParams)
return authUtils.getToken().flatMap {
val request = HttpRequest.newBuilder().uri(
URI.create("$brokerUrl/ngsi-ld/v1/entityOperations/upsert$params")
URI.create("$contextBrokerUrl/ngsi-ld/v1/entityOperations/upsert$params")
)
.setHeader("Content-Type", APPLICATION_JSONLD)
.setHeader("Authorization", "Bearer $it")
Expand All @@ -98,12 +97,11 @@ class BatchEntityService(
}

fun delete(
brokerUrl: String,
payload: String
): Either<ApplicationError, HttpResponse<String>> {
return authUtils.getToken().flatMap {
val request = HttpRequest.newBuilder().uri(
URI.create("$brokerUrl/ngsi-ld/v1/entityOperations/delete")
URI.create("$contextBrokerUrl/ngsi-ld/v1/entityOperations/delete")
)
.setHeader("Content-Type", APPLICATION_JSON)
.setHeader("Authorization", "Bearer $it")
Expand Down
29 changes: 13 additions & 16 deletions lib/src/main/kotlin/io/egm/kngsild/api/EntityService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,33 @@ import java.net.http.HttpRequest
import java.net.http.HttpResponse

class EntityService(
private val contextBrokerUrl: String,
private val authUtils: AuthUtils
) {

private val logger = LoggerFactory.getLogger(javaClass)
private val patchSuccessCode = listOf(HttpURLConnection.HTTP_NO_CONTENT)

fun create(
brokerUrl: String,
entityPayload: String
): Either<ApplicationError, HttpResponse<String>> {
return authUtils.getToken().flatMap {
val request = HttpRequest.newBuilder().uri(
URI.create("$brokerUrl/ngsi-ld/v1/entities")
URI.create("$contextBrokerUrl/ngsi-ld/v1/entities")
)
.setHeader("Content-Type", APPLICATION_JSONLD)
.setHeader("Authorization", "Bearer $it")
.POST(HttpRequest.BodyPublishers.ofString(entityPayload)).build()
try {
val response = httpClient.send(request, HttpResponse.BodyHandlers.ofString())
if (response.statusCode() == HttpURLConnection.HTTP_CREATED)
response.right()
else if (response.statusCode() == HttpURLConnection.HTTP_CONFLICT)
AlreadyExists("Entity already exists").left()
else ContextBrokerError(
"Failed to create entity, " +
"received ${response.statusCode()} (${response.body()}) from context broker"
).left()
when {
response.statusCode() == HttpURLConnection.HTTP_CREATED -> response.right()
response.statusCode() == HttpURLConnection.HTTP_CONFLICT -> AlreadyExists("Entity already exists").left()
else -> ContextBrokerError(
"Failed to create entity, " +
"received ${response.statusCode()} (${response.body()}) from context broker"
).left()
}
} catch (e: IOException) {
val errorMessage = e.message ?: "Error encountered while creating entity in context broker"
ContextBrokerError(errorMessage).left()
Expand All @@ -60,7 +60,6 @@ class EntityService(
}

fun query(
brokerUrl: String,
queryParams: Map<String, String>,
contextUrl: String
): Either<ApplicationError, List<NgsildEntity>> {
Expand All @@ -69,7 +68,7 @@ class EntityService(
val request = HttpRequest
.newBuilder()
.uri(
URI.create("$brokerUrl/ngsi-ld/v1/entities$params")
URI.create("$contextBrokerUrl/ngsi-ld/v1/entities$params")
)
.setHeader("Accept", APPLICATION_JSONLD)
.setHeader("Link", httpLinkHeaderBuilder(contextUrl))
Expand Down Expand Up @@ -97,7 +96,6 @@ class EntityService(
}

fun retrieve(
brokerUrl: String,
entityId: URI,
queryParams: Map<String, String>,
contextUrl: String
Expand All @@ -107,7 +105,7 @@ class EntityService(
val request = HttpRequest
.newBuilder()
.uri(
URI.create("$brokerUrl/ngsi-ld/v1/entities/$entityId$params")
URI.create("$contextBrokerUrl/ngsi-ld/v1/entities/$entityId$params")
)
.setHeader("Accept", APPLICATION_JSONLD)
.setHeader("Link", httpLinkHeaderBuilder(contextUrl))
Expand Down Expand Up @@ -136,7 +134,6 @@ class EntityService(
}

fun updateAttributes(
brokerUrl: String,
entityId: URI,
attributesPayload: String,
contextUrl: String
Expand All @@ -146,7 +143,7 @@ class EntityService(
.newBuilder()
.uri(
URI
.create("$brokerUrl/ngsi-ld/v1/entities/$entityId/attrs")
.create("$contextBrokerUrl/ngsi-ld/v1/entities/$entityId/attrs")
)
.method("PATCH", HttpRequest.BodyPublishers.ofString(attributesPayload))
.setHeader("Content-Type", APPLICATION_JSON)
Expand Down
4 changes: 2 additions & 2 deletions lib/src/main/kotlin/io/egm/kngsild/api/SubscriptionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import java.net.http.HttpRequest
import java.net.http.HttpResponse

class SubscriptionService(
private val contextBrokerUrl: String,
private val authUtils: AuthUtils
) {

fun create(
brokerUrl: String,
subscriptionPayload: String
): Either<ApplicationError, HttpResponse<String>> {
return authUtils.getToken().flatMap {
val request = HttpRequest.newBuilder().uri(
URI.create("$brokerUrl/ngsi-ld/v1/subscriptions")
URI.create("$contextBrokerUrl/ngsi-ld/v1/subscriptions")
)
.setHeader("Content-Type", APPLICATION_JSONLD)
.setHeader("Authorization", "Bearer $it")
Expand Down
6 changes: 6 additions & 0 deletions lib/src/main/kotlin/io/egm/kngsild/utils/NgsiLdUtils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.egm.kngsild.utils

import io.egm.kngsild.utils.UriUtils.toUri
import java.net.URI

typealias NgsildEntity = Map<String, Any>
typealias NgsildAttribute = List<Map<String, Any>>
typealias NgsiLdRelationshipInstance = Map<String, Any>
Expand All @@ -13,3 +16,6 @@ object NgsiLdUtils {
"value" to value
)
}

fun NgsildEntity.getRelationshipObject(relationshipName: String): URI? =
((this[relationshipName] as NgsiLdRelationshipInstance?)?.get("object") as String?)?.toUri()
59 changes: 16 additions & 43 deletions lib/src/test/kotlin/io/egm/kngsild/api/BatchEntityServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ class BatchEntityServiceTest {
`when`(
mockedAuthUtils.getToken()
).thenReturn("token".right())
val batchEntityService = BatchEntityService(mockedAuthUtils)
val batchEntityService = BatchEntityService("http://localhost:8089", mockedAuthUtils)

val response = batchEntityService.create(
"http://localhost:8089",
batchEntityPayload
)
val response = batchEntityService.create(batchEntityPayload)

assertTrue(response.isRight())
assertTrue(response.exists { it.statusCode() == HttpURLConnection.HTTP_CREATED })
Expand All @@ -96,12 +93,9 @@ class BatchEntityServiceTest {
`when`(
mockedAuthUtils.getToken()
).thenReturn("token".right())
val batchEntityService = BatchEntityService(mockedAuthUtils)
val batchEntityService = BatchEntityService("http://localhost:8089", mockedAuthUtils)

val response = batchEntityService.create(
"http://localhost:8089",
batchEntityPayload
)
val response = batchEntityService.create(batchEntityPayload)

assertTrue(response.isLeft())
}
Expand All @@ -115,12 +109,9 @@ class BatchEntityServiceTest {
`when`(
mockedAuthUtils.getToken()
).thenReturn(AccessTokenNotRetrieved("Unable to get an access token").left())
val batchEntityService = BatchEntityService(mockedAuthUtils)
val batchEntityService = BatchEntityService("http://localhost:8089", mockedAuthUtils)

val response = batchEntityService.create(
"http://localhost:8089",
batchEntityPayload
)
val response = batchEntityService.create(batchEntityPayload)

assertTrue(response.isLeft())
assertEquals(response, AccessTokenNotRetrieved("Unable to get an access token").left())
Expand All @@ -140,13 +131,9 @@ class BatchEntityServiceTest {
`when`(
mockedAuthUtils.getToken()
).thenReturn("token".right())
val batchEntityService = BatchEntityService(mockedAuthUtils)
val batchEntityService = BatchEntityService("http://localhost:8089", mockedAuthUtils)

val response = batchEntityService.upsert(
"http://localhost:8089",
batchEntityPayload,
emptyMap()
)
val response = batchEntityService.upsert(batchEntityPayload, emptyMap())

assertTrue(response.isRight())
assertTrue(response.exists { it.statusCode() == HttpURLConnection.HTTP_CREATED })
Expand All @@ -166,13 +153,9 @@ class BatchEntityServiceTest {
`when`(
mockedAuthUtils.getToken()
).thenReturn("token".right())
val batchEntityService = BatchEntityService(mockedAuthUtils)
val batchEntityService = BatchEntityService("http://localhost:8089", mockedAuthUtils)

val response = batchEntityService.upsert(
"http://localhost:8089",
batchEntityPayload,
emptyMap()
)
val response = batchEntityService.upsert(batchEntityPayload, emptyMap())

assertTrue(response.isLeft())
}
Expand All @@ -186,13 +169,9 @@ class BatchEntityServiceTest {
`when`(
mockedAuthUtils.getToken()
).thenReturn(AccessTokenNotRetrieved("Unable to get an access token").left())
val batchEntityService = BatchEntityService(mockedAuthUtils)
val batchEntityService = BatchEntityService("http://localhost:8089", mockedAuthUtils)

val response = batchEntityService.upsert(
"http://localhost:8089",
batchEntityPayload,
emptyMap()
)
val response = batchEntityService.upsert(batchEntityPayload, emptyMap())

assertTrue(response.isLeft())
assertEquals(response, AccessTokenNotRetrieved("Unable to get an access token").left())
Expand All @@ -212,12 +191,9 @@ class BatchEntityServiceTest {
`when`(
mockedAuthUtils.getToken()
).thenReturn("token".right())
val batchEntityService = BatchEntityService(mockedAuthUtils)
val batchEntityService = BatchEntityService("http://localhost:8089", mockedAuthUtils)

val response = batchEntityService.delete(
"http://localhost:8089",
batchEntityPayload
)
val response = batchEntityService.delete(batchEntityPayload)

assertTrue(response.isRight())
assertTrue(response.exists { it.statusCode() == HttpURLConnection.HTTP_NO_CONTENT })
Expand All @@ -237,12 +213,9 @@ class BatchEntityServiceTest {
`when`(
mockedAuthUtils.getToken()
).thenReturn("token".right())
val batchEntityService = BatchEntityService(mockedAuthUtils)
val batchEntityService = BatchEntityService("http://localhost:8089", mockedAuthUtils)

val response = batchEntityService.delete(
"http://localhost:8089",
batchEntityPayload
)
val response = batchEntityService.delete(batchEntityPayload)

assertTrue(response.isLeft())
}
Expand Down
Loading

0 comments on commit 66daa31

Please sign in to comment.