-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feature/fdp 1628 #7
Conversation
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
...ication/src/main/kotlin/org/gxf/crestdeviceservice/data/convertors/DatabaseFieldEncryptor.kt
Fixed
Show fixed
Hide fixed
...ication/src/main/kotlin/org/gxf/crestdeviceservice/data/convertors/DatabaseFieldEncryptor.kt
Fixed
Show fixed
Hide fixed
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
...ication/src/main/kotlin/org/gxf/crestdeviceservice/data/convertors/DatabaseFieldEncryptor.kt
Fixed
Show resolved
Hide resolved
...ication/src/main/kotlin/org/gxf/crestdeviceservice/data/convertors/DatabaseFieldEncryptor.kt
Fixed
Show fixed
Hide fixed
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
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.
There are still Sonar Encryption errors?
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.
Sonar issues are false postives, therefore approved
application/src/integrationTest/kotlin/org/gxf/crestdeviceservice/PskControllerTest.kt
Outdated
Show resolved
Hide resolved
class PskControllerTest { | ||
|
||
@Autowired | ||
private lateinit var mockMvc: MockMvc |
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.
Geen technische namen
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.
Deze vind ik lastig heb je een suggestie?
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.
Bijvoorbeeld pskRequest
, dat is wat je uitvoert met dit object in de code
val expectedKey = "1234" | ||
pskRepository.save(Psk(IDENTITY, Instant.MAX, expectedKey)) | ||
|
||
val currentPks = pskService.getCurrentPsk(IDENTITY) |
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.
Typo: currentPsk, niet Pks
} | ||
|
||
private fun constructSetPskCommand(key: String): String { | ||
return "PSK:${key};PSK:${key}SET" |
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.
mag zonder {
}
} | ||
|
||
private fun generatePsk(): String { | ||
val allowedChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
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.
Deze kan ook als constant
@Test | ||
fun shouldBeAbleTeEncryptAndDecryptData() { | ||
val expected = "data" | ||
val encrypted = databaseFieldEncryptor.convertToDatabaseColumn(expected) |
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.
Suggestie: zet hier een extra assert tussen om aan tegeven dat encrypted != expected. Nu toont je test aan dat de encryptor z'n werk perfect doet of dat 'ie helemaal niks doet...
build.gradle.kts
Outdated
@@ -39,7 +39,7 @@ subprojects { | |||
|
|||
group = "org.gxf.template" | |||
version = rootProject.version | |||
|
|||
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.
Remove whitespace
application/build.gradle.kts
Outdated
implementation("org.springframework.boot:spring-boot-starter-actuator") | ||
implementation("org.springframework.boot:spring-boot-starter-web") | ||
|
||
implementation("org.jetbrains.kotlin:kotlin-reflect") | ||
implementation("org.springframework:spring-aop") | ||
implementation("io.github.microutils:kotlin-logging-jvm:3.0.5") | ||
|
||
implementation("org.postgresql:postgresql:42.5.4") | ||
implementation("org.flywaydb:flyway-core:9.22.3") | ||
|
||
implementation("org.springframework.boot:spring-boot-starter-data-jpa:3.1.5") | ||
|
||
implementation("org.springframework.boot:spring-boot-autoconfigure") | ||
implementation("org.springframework.boot:spring-boot-starter-logging") | ||
|
||
implementation("org.springframework:spring-aspects") | ||
implementation("org.springframework:spring-aop") | ||
|
||
implementation("org.springframework.kafka:spring-kafka") | ||
implementation("com.microsoft.azure:msal4j:1.13.10") |
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.
- verify if the versions are not provided by spring boot starter(s)
- try to keep the dependencies organized in a logical manner.
- consider using a version catalogue for managing the dependencies.
application/build.gradle.kts
Outdated
implementation("org.springframework.boot:spring-boot-starter-data-jpa:3.1.5") | ||
implementation("com.gxf.utilities:kafka-avro:0.2") | ||
implementation("org.springframework.kafka:spring-kafka") | ||
implementation("org.springframework.boot:spring-boot-starter-test") | ||
implementation("org.springframework.kafka:spring-kafka-test") | ||
implementation("org.testcontainers:kafka:1.17.6") | ||
implementation("org.springframework.ws:spring-ws-test") | ||
runtimeOnly("com.h2database:h2:2.2.224") | ||
implementation("org.mockito.kotlin:mockito-kotlin:5.1.0") |
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.
See previous comment regarding versions
|
||
@Test | ||
fun shouldReturnKeyForIdentity() { | ||
Mockito.`when`(mock.getCurrentPsk(any())).thenReturn("key") |
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.
Mockito.`when`(mock.getCurrentPsk(any())).thenReturn("key") | |
`when`(mock.getCurrentPsk(any())).thenReturn("key") |
|
||
@Test | ||
fun shouldReturn404WhenPskForIdentityIsNotFound() { | ||
Mockito.`when`(mock.getCurrentPsk(any())).thenReturn(null) |
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.
Import static method, so that the code becomes:
Mockito.`when`(mock.getCurrentPsk(any())).thenReturn(null) | |
`when`(mock.getCurrentPsk(any())).thenReturn(null) |
import org.gxf.crestdeviceservice.data.entity.Psk | ||
import org.gxf.crestdeviceservice.psk.PskRepository | ||
import org.gxf.crestdeviceservice.psk.PskService | ||
import org.junit.jupiter.api.Assertions.* |
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.
Use AssertJ for assertions
dev-initial-psk: "ABCDEFGHIJKLMNOP" #Should be the same as the PSK in the simulator only relevant for dev | ||
dev-device-identity: "867787050253370" #Should be the same as the identity in the simulator only relevant for dev |
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.
I would prefer to have:
- dev properties in a separate application-dev.yaml file
- comments on a separate line:
dev-initial-psk: "ABCDEFGHIJKLMNOP" #Should be the same as the PSK in the simulator only relevant for dev | |
dev-device-identity: "867787050253370" #Should be the same as the identity in the simulator only relevant for dev | |
# Initial PSK should be the same as the PSK in the simulator, only relevant for dev | |
dev-initial-psk: "ABCDEFGHIJKLMNOP" | |
# Identity should be the same as the identity in the simulator, only relevant for dev | |
dev-device-identity: "867787050253370" |
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.
I do not know what our plans regarding integration tests are and how spring profiles fit in. For now I suggest to keep it like this and update when needed. Comments on new line is done.
|
||
/*** | ||
* Setup function for local development. | ||
* Runs flyway migrations manual, because the CommandLineRunner is run before spring runs the flyway migrations automatically. |
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.
* Runs flyway migrations manual, because the CommandLineRunner is run before spring runs the flyway migrations automatically. | |
* Runs flyway migrations manually, because the CommandLineRunner is run before spring runs the flyway migrations automatically. |
val flyway = Flyway.configure() | ||
.dataSource(dataSourceProperties.url, dataSourceProperties.username, dataSourceProperties.password) | ||
.load() |
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.
val flyway = Flyway.configure() | |
.dataSource(dataSourceProperties.url, dataSourceProperties.username, dataSourceProperties.password) | |
.load() | |
val flyway = Flyway | |
.configure() | |
.dataSource(dataSourceProperties.url, dataSourceProperties.username, dataSourceProperties.password) | |
.load() |
...ication/src/main/kotlin/org/gxf/crestdeviceservice/data/convertors/DatabaseFieldEncryptor.kt
Fixed
Show resolved
Hide resolved
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.
- Add license headers!!!
- Use AssertJ assertions
...ication/src/main/kotlin/org/gxf/crestdeviceservice/data/convertors/DatabaseFieldEncryptor.kt
Fixed
Show resolved
Hide resolved
application/build.gradle.kts
Outdated
implementation("org.springframework.boot:spring-boot-starter-actuator") | ||
implementation("org.springframework.boot:spring-boot-starter-web") | ||
|
||
implementation("org.jetbrains.kotlin:kotlin-reflect") | ||
implementation("org.springframework:spring-aop") | ||
implementation("io.github.microutils:kotlin-logging-jvm:3.0.5") | ||
|
||
implementation("org.postgresql:postgresql:42.5.4") | ||
implementation("org.flywaydb:flyway-core:9.22.3") | ||
|
||
implementation("org.springframework.boot:spring-boot-starter-data-jpa:3.1.5") | ||
|
||
implementation("org.springframework.boot:spring-boot-autoconfigure") | ||
implementation("org.springframework.boot:spring-boot-starter-logging") | ||
|
||
implementation("org.springframework:spring-aspects") | ||
implementation("org.springframework:spring-aop") | ||
|
||
implementation("org.springframework.kafka:spring-kafka") | ||
implementation("com.microsoft.azure:msal4j:1.13.10") |
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.
- Verify for each dependency if the version is really needed or already supplied via Spring Boot
- Consider organizing the dependencies (alphabetically and/or grouped by feature?)
- Consider using a version catalogue for dependency management
application/build.gradle.kts
Outdated
implementation(project()) | ||
implementation(project(":components:avro-measurement")) | ||
implementation("org.springframework.boot:spring-boot-starter-data-jpa:3.1.5") | ||
implementation("com.gxf.utilities:kafka-avro:0.2") | ||
implementation("org.springframework.kafka:spring-kafka") | ||
implementation("org.springframework.boot:spring-boot-starter-test") | ||
implementation("org.springframework.kafka:spring-kafka-test") | ||
implementation("org.testcontainers:kafka:1.17.6") | ||
implementation("org.springframework.ws:spring-ws-test") | ||
runtimeOnly("com.h2database:h2:2.2.224") | ||
implementation("org.mockito.kotlin:mockito-kotlin:5.1.0") |
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.
See previous comment regarding dependencies
|
||
@Test | ||
fun shouldReturn404WhenPskForIdentityIsNotFound() { | ||
Mockito.`when`(mock.getCurrentPsk(any())).thenReturn(null) |
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.
Use static method imports for Mockito.`when`
MockMvcRequestBuilders.get("/psk") | ||
.header("x-device-identity", "identity")) |
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.
I would prefer this layout for better readability.
MockMvcRequestBuilders.get("/psk") | |
.header("x-device-identity", "identity")) | |
MockMvcRequestBuilders | |
.get("/psk") | |
.header("x-device-identity", "identity")) |
dev-initial-psk: "ABCDEFGHIJKLMNOP" #Should be the same as the PSK in the simulator only relevant for dev | ||
dev-device-identity: "867787050253370" #Should be the same as the identity in the simulator only relevant for dev |
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.
dev-initial-psk: "ABCDEFGHIJKLMNOP" #Should be the same as the PSK in the simulator only relevant for dev | |
dev-device-identity: "867787050253370" #Should be the same as the identity in the simulator only relevant for dev | |
# Initial PSK should be the same as the PSK in the simulator, only relevant for dev | |
initial-psk: "ABCDEFGHIJKLMNOP" | |
# Device identity should be the same as the identity in the simulator, only relevant for dev | |
device-identity: "867787050253370" |
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.
Move class to package org.gxf.crestdeviceservice.coap
@@ -0,0 +1,40 @@ | |||
import org.gxf.crestdeviceservice.coap.DownlinkService |
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.
Import should not be needed when test class is in same package as class under test.
/*** | ||
* Setup function for local development. | ||
* Runs flyway migrations manual, because the CommandLineRunner is run before spring runs the flyway migrations automatically. | ||
* Sets the psk used by the simulator if it doesn't exist. | ||
*/ | ||
override fun run(vararg args: String?) { | ||
val flyway = Flyway.configure() | ||
.dataSource(dataSourceProperties.url, dataSourceProperties.username, dataSourceProperties.password) | ||
.load() | ||
|
||
flyway.migrate() |
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.
Have you tried adding @DependsOn({"flyway", "flywayInitializer"})
annotation to the class?
fun getDownlinkForIdentity(identity: String): String { | ||
|
||
if (pskService.hasDefaultKey(identity)) { | ||
logger.info { "Device $identity has default key creating new key " } | ||
|
||
val newKey = pskService.generateAndSetNewKeyForIdentity(identity) | ||
|
||
return constructSetPskCommand(newKey) | ||
} | ||
|
||
return "0" | ||
} |
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.
Probably sufficient for now, but in the (near) future we might also have to handle other downlinks as well...
I'm wondering if it should be the responsibility of the downlink service to check and recreate PSKs...
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
|
||
val consumer = createKafkaConsumer(embeddedKafkaBroker, crestMessageTopicName) | ||
val response = testRestTemplate.postForEntity("/sng/1", request, String::class.java) | ||
|
||
Assertions.assertEquals("0", response.body) | ||
assertThat("0").isEqualTo(response.body) |
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.
Wrong order of actual and expected values
|
||
val expectedJsonNode = ObjectMapper().readTree(getFileContentAsString("message.json")) | ||
val payloadJsonNode = ObjectMapper().readTree(records.records(crestMessageTopicName).first().value().payload) | ||
|
||
Assertions.assertEquals(expectedJsonNode, payloadJsonNode) | ||
assertThat(expectedJsonNode).isEqualTo(payloadJsonNode) |
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.
Wrong order of actual and expected values
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper | ||
import java.time.Duration | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@EmbeddedKafka( | ||
topics = ["\${crest-device-service.kafka.message-producer.topic-name}"], | ||
) | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) | ||
class MessageHandelingTest { |
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.
Fix typo, also rename file.
class MessageHandelingTest { | |
class MessageHandlingTest { |
application/src/integrationTest/kotlin/org/gxf/crestdeviceservice/PreSharedKeyRetrieveTest.kt
Outdated
Show resolved
Hide resolved
val result = restTemplate.exchange("/psk", | ||
HttpMethod.GET, HttpEntity<Unit>(headers), String::class.java) | ||
|
||
assertThat("0000111122223333").isEqualTo(result.body) |
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.
Switch actual and expected values.
@@ -24,7 +25,7 @@ class DownlinkServiceTest { | |||
|
|||
val result = downLinkService.getDownlinkForIdentity("identity") | |||
|
|||
assertEquals("0", result) | |||
assertThat("0").isEqualTo(result) |
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.
Switch actual and expected values
@@ -35,6 +36,6 @@ class DownlinkServiceTest { | |||
val result = downLinkService.getDownlinkForIdentity("identity") | |||
|
|||
// Psk command is formatted as: PSK:[Key];PSK:[Key]SET | |||
assertEquals("PSK:key;PSK:keySET", result) | |||
assertThat("PSK:key;PSK:keySET").isEqualTo(result) |
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.
Switch actual and expected values
assertThat("data").isNotEqualTo(encrypted) | ||
assertThat(expected).isEqualTo(decrypted) |
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.
Switch actual and expected values.
assertThat("data").isNotEqualTo(encrypted) | |
assertThat(expected).isEqualTo(decrypted) | |
assertThat(encrypted).isNotEqualTo("data") | |
assertThat(decrypted).isEqualTo(expected) |
settings.gradle.kts
Outdated
|
||
library("microsoftMsal", "com.microsoft.azure", "msal4j").version("1.13.10") | ||
|
||
library("assertJ", "org.assertj", "assertj-core").version("3.24.2") |
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.
Isn't the version provided by Spring Boot?
settings.gradle.kts
Outdated
library("kafkaTestContainers", "org.testcontainers", "kafka").version("1.17.6") | ||
} | ||
} | ||
} |
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.
Add newline
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
# Conflicts: # application/src/test/kotlin/MeasurementProducerTest.kt
application/src/integrationTest/kotlin/org/gxf/crestdeviceservice/MessageHandlingTest.kt
Outdated
Show resolved
Hide resolved
Signed-off-by: Luciano Favoroso <luciano.favoroso@alliander.com>
Kudos, SonarCloud Quality Gate passed! |
No description provided.