Skip to content

Commit

Permalink
Kotlinization for the good
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperancinha committed Jan 21, 2023
1 parent 50e9d7f commit 8376362
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/camel-test-drives-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
- name: Set up JDK 19
uses: actions/setup-java@v3
with:
java-version: '17'
java-version: '19'
distribution: 'adopt'

- name: Build and Test
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/camel-test-drives.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
- name: Set up JDK 19
uses: actions/setup-java@v3
with:
java-version: '17'
java-version: '19'
distribution: 'adopt'

- name: Build and Test
Expand Down
2 changes: 1 addition & 1 deletion jeorg-camel-app-atm/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ After running your tomcat server you should be able to access the application th

* Spring MVC

* Sprinc Security
* Spring Security

* Camel DSL JSON

Expand Down
20 changes: 17 additions & 3 deletions jeorg-camel-app-atm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<groupId>org.jesperancinha.atm</groupId>
<version>1.0.0</version>
</parent>
<properties>
<camel-test.version>3.20.1</camel-test.version>
</properties>

<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -127,6 +124,12 @@
<artifactId>mockk-jvm</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.ninja-squad</groupId>
<artifactId>springmockk</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -252,6 +255,17 @@
<artifactId>omni-coveragereporter-maven-plugin</artifactId>
<version>${omni-coveragereporter-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<includes>
<include>**/*Test</include>
<include>**/*IT</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jesperancinha.atm.finder.service
import com.fasterxml.jackson.databind.ObjectMapper
import org.apache.camel.BeanInject
import org.jesperancinha.atm.finder.service.payload.response.ATMMachine
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import org.springframework.web.client.RestTemplate
Expand All @@ -14,14 +15,14 @@ import java.util.*
*/
@Service
class AtmLocatorService {
@BeanInject
@Autowired
lateinit var restTemplate: RestTemplate

@BeanInject
lateinit var mapper: ObjectMapper
@Autowired
lateinit var mapper: ObjectMapper

@Value("\${atm.endpoint}")
lateinit var atmEndpoint: String
lateinit var atmEndpoint: String

@Throws(IOException::class)
fun getAtmPerCity(city: String): Array<ATMMachine> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import com.fasterxml.jackson.annotation.JsonProperty
* Created by joaofilipesabinoesperancinha on 28-07-16.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
class ATMGeoLocation {
data class ATMGeoLocation(
@JsonProperty("lat")
private val lat: Double? = null
val lat: Double,

@JsonProperty("lng")
private val lng: Double? = null
}
val lng: Double,
)
Original file line number Diff line number Diff line change
@@ -1,58 +1,89 @@
package com.jesperancinha.atm.finder.camel

import com.fasterxml.jackson.databind.ObjectMapper
import com.jesperancinha.atm.finder.dao.atms
import com.ninjasquad.springmockk.MockkBean
import io.kotest.matchers.shouldBe
import io.mockk.every
import org.apache.camel.RoutesBuilder
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.language.bean.Bean
import org.apache.camel.test.spring.junit5.CamelSpringBootTest
import org.jesperancinha.atm.finder.camel.AtmProvider
import org.jesperancinha.atm.finder.camel.AtmService
import org.jesperancinha.atm.finder.service.AtmLocatorService
import org.jesperancinha.atm.finder.service.config.AtmFinderConfiguration
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Configuration
import org.springframework.http.ResponseEntity
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.util.ReflectionTestUtils
import org.springframework.test.util.ReflectionTestUtils.*
import org.springframework.web.client.RestTemplate
import java.util.*


/**
* Created by joaofilipesabinoesperancinha on 29-07-16.
*/
@CamelSpringBootTest
@EnableAutoConfiguration
@SpringBootTest
class AtmServiceIT @Autowired constructor(
private val atmService: AtmService,
private val atmLocatorService: AtmLocatorService,
) {
@ContextConfiguration(
classes = [AtmFinderConfiguration::class, AtmLocatorService::class, AtmService::class]
)
class AtmServiceIT {

@Autowired
lateinit var atmService: AtmService

@Autowired
lateinit var objectMapper: ObjectMapper

@MockkBean
lateinit var restTemplate: RestTemplate

@BeforeEach
fun setUp(){
every { restTemplate.getForEntity(
any<String>(),
String::class.java)
} returns ResponseEntity.of(Optional.of("\n${objectMapper.writeValueAsString(atms)}"))
}

@Throws(Exception::class)
@Test
fun `should uppercase atm providers`() {
val atmLocatorService: AtmLocatorService = ReflectionTestUtils.getField(
val atmLocatorService: AtmLocatorService = getField(
atmService,
ATM_LOCATOR_SERVICE
) as AtmLocatorService
ReflectionTestUtils.setField(atmLocatorService, ATM_ENDPOINT, HTTPS_WWW_ING_NL_API_LOCATOR_ATMS)
val result: AtmProvider = atmService.getATMProvider(AMSTERDAM_UPPERCASE)
setField(atmLocatorService, ATM_ENDPOINT, HTTPS_WWW_ING_NL_API_LOCATOR_ATMS)
val result: AtmProvider = atmService.getATMProvider(GOUDA_UPPERCASE)
result.atmMachines.size shouldBe 122
result.atmMachines
.forEach { atmMachine ->
atmMachine.address.city shouldBe AMSTERDAM_UPPERCASE
atmMachine.address.city shouldBe GOUDA_UPPERCASE
}
}

@Throws(Exception::class)
@Test
fun `should lowercase atm providers`() {
val atmLocatorService: AtmLocatorService = ReflectionTestUtils.getField(
val atmLocatorService: AtmLocatorService = getField(
atmService,
ATM_LOCATOR_SERVICE
) as AtmLocatorService
ReflectionTestUtils.setField(atmLocatorService, ATM_ENDPOINT, HTTPS_WWW_ING_NL_API_LOCATOR_ATMS)
val result: AtmProvider = atmService.getATMProvider(AMSTERDAM_LOWERCASE)
setField(atmLocatorService, ATM_ENDPOINT, HTTPS_WWW_ING_NL_API_LOCATOR_ATMS)
val result: AtmProvider = atmService.getATMProvider(GOUDA_LOWERCASE)
result.atmMachines.size shouldBe 122
result.atmMachines
.forEach { atmMachine ->
atmMachine.address.city shouldBe AMSTERDAM_UPPERCASE
atmMachine.address.city shouldBe GOUDA_UPPERCASE
}
}

Expand All @@ -72,8 +103,8 @@ class AtmServiceIT @Autowired constructor(
companion object {
private const val HTTPS_WWW_ING_NL_API_LOCATOR_ATMS = "https://www.ing.nl/api/locator/atms/"
private const val ATM_ENDPOINT = "atmEndpoint"
private const val AMSTERDAM_UPPERCASE = "AMSTERDAM"
private const val AMSTERDAM_LOWERCASE = "amsterdam"
private const val GOUDA_UPPERCASE = "GOUDA"
private const val GOUDA_LOWERCASE = "gouda"
private const val ATM_LOCATOR_SERVICE = "atmLocatorService"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.jesperancinha.atm.finder.dao

import org.jesperancinha.atm.finder.service.payload.response.ATMAddress
import org.jesperancinha.atm.finder.service.payload.response.ATMGeoLocation
import org.jesperancinha.atm.finder.service.payload.response.ATMMachine

val atm = ATMMachine(
address = ATMAddress(
street = "Kaas Plein",
housenumber = "616",
postalcode = "2801XX",
city = "GOUDA",
geoLocation = ATMGeoLocation(
52.0174611, 4.7040779
)
),
distance = 0,
type = "We are all the same"
)
val atms: Array<ATMMachine> = (1..122).map { atm }.toTypedArray()
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.jesperancinha.atm.finder.service

import com.jesperancinha.atm.finder.camel.AtmServiceIT
import com.fasterxml.jackson.databind.ObjectMapper
import com.jesperancinha.atm.finder.dao.atms
import com.ninjasquad.springmockk.MockkBean
import io.kotest.matchers.shouldBe
import org.apache.camel.BeanInject
import org.apache.camel.RoutesBuilder
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.language.bean.Bean
import io.mockk.every
import org.apache.camel.test.spring.junit5.CamelSpringBootTest
import org.jesperancinha.atm.finder.service.AtmLocatorService
import org.jesperancinha.atm.finder.service.config.AtmFinderConfiguration
import org.jesperancinha.atm.finder.service.payload.response.ATMMachine
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.Configuration
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.ResponseEntity
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.util.ReflectionTestUtils
import org.springframework.test.util.ReflectionTestUtils.*
import org.springframework.web.client.RestTemplate
import java.util.*


Expand All @@ -23,42 +25,40 @@ import java.util.*
*/
@CamelSpringBootTest
@EnableAutoConfiguration
@SpringBootTest
@ContextConfiguration(
classes = [AtmFinderConfiguration::class, AtmLocatorServiceImpIT.ContextConfig::class]
classes = [AtmFinderConfiguration::class, AtmLocatorService::class]
)
class AtmLocatorServiceImpIT @BeanInject constructor(
private val atmLocatorService: AtmLocatorService
) {
class AtmLocatorServiceImpIT {

@Autowired
lateinit var atmLocatorService: AtmLocatorService

@Autowired
lateinit var objectMapper: ObjectMapper

@MockkBean
lateinit var restTemplate: RestTemplate

@Throws(Exception::class)
@Test
fun `should find atm per city`() {
ReflectionTestUtils.setField(atmLocatorService, ATM_ENDPOINT, HTTPS_WWW_ING_NL_API_LOCATOR_ATMS)
val result: Array<ATMMachine> = atmLocatorService.getAtmPerCity(AMSTERDAM)
setField(atmLocatorService, ATM_ENDPOINT, HTTPS_WWW_ING_NL_API_LOCATOR_ATMS)
every { restTemplate.getForEntity(
any<String>(),
String::class.java)
} returns ResponseEntity.of(Optional.of("\n${objectMapper.writeValueAsString(atms)}"))
val result = atmLocatorService.getAtmPerCity(GOUDA)
result.size shouldBe 122
Arrays.stream(result)
.forEach { atmMachine ->
atmMachine.address.city shouldBe AMSTERDAM
}
}

@Configuration
internal class ContextConfig {
@Bean(ref="ref")
fun route(): RoutesBuilder {
return object : RouteBuilder() {
@Throws(java.lang.Exception::class)
override fun configure() {
from("direct:test").to("mock:test")
}
atmMachine.address.city shouldBe GOUDA
}
}
}

companion object {
private const val HTTPS_WWW_ING_NL_API_LOCATOR_ATMS = "https://www.ing.nl/api/locator/atms/"
private const val ATM_ENDPOINT = "atmEndpoint"
private const val AMSTERDAM = "AMSTERDAM"
private const val GOUDA = "GOUDA"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.http.ResponseEntity
import org.springframework.test.util.ReflectionTestUtils
import org.springframework.test.util.ReflectionTestUtils.*
import org.springframework.web.client.RestTemplate
import java.io.InputStreamReader
import java.util.*
Expand All @@ -29,11 +30,13 @@ class AtmLocatorServiceTest {

@MockK
lateinit var restTemplate: RestTemplate

private val mapper: ObjectMapper by lazy { ObjectMapper() }

@BeforeEach
fun setUp() {
ReflectionTestUtils.setField(atmLocatorService, MAPPER, mapper)
setField(atmLocatorService, MAPPER, mapper)
setField(atmLocatorService, ATM_ENDPOINT, "")
}

@Throws(Exception::class)
Expand Down Expand Up @@ -66,6 +69,7 @@ class AtmLocatorServiceTest {

companion object {
private const val MAPPER = "mapper"
private const val ATM_ENDPOINT = "atmEndpoint"
private const val MOCK_CLEAN_RESPONSE1_JSON = "/mockCleanResponse1.json"
private const val AMSTERDAM = "AMSTERDAM"
private const val MOCK_GARBAGE_RESPONSE1_JSON = "/mockGarbageResponse1.json"
Expand Down
Loading

0 comments on commit 8376362

Please sign in to comment.