-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
techtask/Separation-of-credentials-from-endpoints #35
Merged
michalskrzypek
merged 12 commits into
master
from
techtask/Separation-of-credentials-from-endpoints
Sep 9, 2019
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0532cf1
techtask/Separation-of-credentials-from-endpoints - config changes
yahorm a85b972
Merge branch 'techtask/Separation-of-credentials-from-endpoints' of h…
michalskrzypek ac77ec8
Add JUnit5 testcompile dependency
michalskrzypek cfe1446
Extract logic to Endpoint class
michalskrzypek 97be895
Add test module
michalskrzypek e413b0b
Add tests to Endpoint class
michalskrzypek 9fa5d01
Fix naming issue
michalskrzypek f6e6d91
Add resources to test module
michalskrzypek d3bee2b
Merge branch 'master' into techtask/Separation-of-credentials-from-en…
michalskrzypek f982e57
Code refactors, fix endpoints conf file
michalskrzypek 55a44df
Fix tests naming convention
michalskrzypek 3edf9e0
Fix optional checker
michalskrzypek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
cogboard-app/src/main/kotlin/com/cognifide/cogboard/config/Endpoint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.cognifide.cogboard.config | ||
|
||
import io.vertx.core.json.JsonArray | ||
import io.vertx.core.json.JsonObject | ||
import java.util.* | ||
|
||
class Endpoint(val endpoints: JsonArray, val credentials: JsonArray, val endpointId: String) { | ||
|
||
fun asJson(): JsonObject { | ||
return findJsonObjectById(endpointId, endpoints) | ||
.map { attachCredentials(it) } | ||
.orElse(JsonObject()) | ||
} | ||
|
||
private fun attachCredentials(endpoint: JsonObject): JsonObject { | ||
endpoint.remove(CREDENTIALS).toString().let { | ||
val credentials = findJsonObjectById(it, credentials) | ||
.orElse(JsonObject()) | ||
endpoint.put(USER, credentials.getString(USER) ?: "") | ||
endpoint.put(PASSWORD, credentials.getString(PASSWORD) ?: "") | ||
} | ||
|
||
return endpoint | ||
} | ||
|
||
private fun findJsonObjectById(id: String?, array: JsonArray): Optional<JsonObject> { | ||
return array.stream() | ||
.map { it as JsonObject } | ||
.filter { | ||
id == it.getString(ID) | ||
}.findFirst() | ||
} | ||
|
||
companion object { | ||
private const val ID = "id" | ||
private const val CREDENTIALS = "credentials" | ||
private const val ENDPOINTS = "endpoints" | ||
private const val USER = "user" | ||
private const val PASSWORD = "password" | ||
|
||
fun from(config: JsonObject, endpointId: String): Endpoint { | ||
val endpoints = config.getJsonArray(ENDPOINTS) ?: JsonArray() | ||
val credentials = config.getJsonArray(CREDENTIALS) ?: JsonArray() | ||
return Endpoint(endpoints, credentials, endpointId) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
cogboard-app/src/test/kotlin/com/cognifide/cogboard/config/EndpointTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.cognifide.cogboard.config | ||
|
||
import io.vertx.core.json.JsonArray | ||
import io.vertx.core.json.JsonObject | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertFalse | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class EndpointTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like that You introduced JUnit to the project ! |
||
|
||
private val endpoints: JsonArray = readJsonArrayFromResource("/com/cognifide/cogboard/config/endpoints-test.json", ENDPOINTS) | ||
private val credentials: JsonArray = readJsonArrayFromResource("/com/cognifide/cogboard/config/endpoints-test.json", CREDENTIALS) | ||
|
||
@Test | ||
fun shouldRemoveCredentialsProp() { | ||
val validEndpoint = Endpoint(endpoints, credentials,"validEndpoint").asJson() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. next time please use @beforeeach for initialize e.g. endpoint once.
|
||
assertFalse(validEndpoint.containsKey("credentials")) | ||
|
||
val invalidEndpoint = Endpoint(endpoints, credentials, "invalidEndpoint").asJson() | ||
assertFalse(invalidEndpoint.containsKey("credentials")) | ||
} | ||
|
||
@Test | ||
fun shouldAddUserAndPasswordProp() { | ||
val validEndpoint = Endpoint(endpoints, credentials,"validEndpoint").asJson() | ||
assert(validEndpoint.containsKey("user")) | ||
assert(validEndpoint.containsKey("password")) | ||
|
||
val invalidEndpoint = Endpoint(endpoints, credentials, "invalidEndpoint").asJson() | ||
assert(invalidEndpoint.containsKey("user")) | ||
assert(invalidEndpoint.containsKey("password")) | ||
} | ||
|
||
@Test | ||
fun checkIfUserAndPasswordAreCorrect() { | ||
val endpoint = Endpoint(endpoints, credentials,"validEndpoint").asJson() | ||
assertEquals("user1", endpoint.getString("user")) | ||
assertEquals("password1", endpoint.getString("password")) | ||
} | ||
|
||
@Test | ||
fun checkIfUserAndPasswordAreEmptyForInvalidId() { | ||
val endpoint = Endpoint(endpoints, credentials, "invalidEndpoint").asJson() | ||
assertEquals("", endpoint.getString("user")) | ||
assertEquals("", endpoint.getString("password")) | ||
} | ||
|
||
@Test | ||
fun shouldReturnValidCredentials() { | ||
val endpoint = Endpoint(endpoints, credentials,"validEndpoint").asJson() | ||
val validEndpoint = JsonObject(""" | ||
{ | ||
"id" : "validEndpoint", | ||
"label" : "Valid Endpoint", | ||
"url" : "url", | ||
"publicUrl" : "Public Url", | ||
"user" : "user1", | ||
"password" : "password1" | ||
} | ||
""") | ||
|
||
assertEquals(validEndpoint, endpoint) | ||
} | ||
|
||
@Test | ||
fun shouldReturnEmptyCredentials() { | ||
val endpoint = Endpoint(endpoints, credentials, "invalidEndpoint").asJson() | ||
val invalidEndpoint = JsonObject(""" | ||
{ | ||
"id" : "invalidEndpoint", | ||
"label" : "Invalid Endpoint", | ||
"url" : "url", | ||
"user" : "", | ||
"password" : "" | ||
} | ||
""") | ||
|
||
assertEquals(invalidEndpoint, endpoint) | ||
} | ||
|
||
companion object { | ||
private const val ENDPOINTS = "endpoints" | ||
private const val CREDENTIALS = "credentials" | ||
|
||
fun readJsonArrayFromResource(pathToJsonResource: String, jsonArrayId: String) : JsonArray { | ||
val endpointsContent = EndpointTest::class.java.getResource(pathToJsonResource).readText() | ||
val endpointsJson = JsonObject(endpointsContent.trimIndent()) | ||
return endpointsJson.getJsonArray(jsonArrayId) | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
cogboard-app/src/test/resources/com/cognifide/cogboard/config/endpoints-test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"endpoints": [ | ||
{ | ||
"id": "validEndpoint", | ||
"label": "Valid Endpoint", | ||
"url": "url", | ||
"publicUrl": "Public Url", | ||
"credentials": "credentials1" | ||
}, | ||
{ | ||
"id": "invalidEndpoint", | ||
"label": "Invalid Endpoint", | ||
"url": "url", | ||
"credentials": "nonExistingCredentials" | ||
} | ||
], | ||
"credentials": [ | ||
{ | ||
"id": "credentials1", | ||
"label": "My Credentials 1", | ||
"user": "user1", | ||
"password": "password1" | ||
}, | ||
{ | ||
"id": "credentials2", | ||
"label": "My Credentials 2", | ||
"user": "user2", | ||
"password": "password2" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice !