diff --git a/.github/workflows/camel-test-drives-pull-request.yml b/.github/workflows/camel-test-drives-pull-request.yml index 815cb3a..5229bf7 100644 --- a/.github/workflows/camel-test-drives-pull-request.yml +++ b/.github/workflows/camel-test-drives-pull-request.yml @@ -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 diff --git a/.github/workflows/camel-test-drives.yml b/.github/workflows/camel-test-drives.yml index 10ea5a1..f7ab4b5 100644 --- a/.github/workflows/camel-test-drives.yml +++ b/.github/workflows/camel-test-drives.yml @@ -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 diff --git a/jeorg-camel-app-atm/Readme.md b/jeorg-camel-app-atm/Readme.md index 036eff1..645f8c1 100644 --- a/jeorg-camel-app-atm/Readme.md +++ b/jeorg-camel-app-atm/Readme.md @@ -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 diff --git a/jeorg-camel-app-atm/pom.xml b/jeorg-camel-app-atm/pom.xml index 09a919e..4d4e5a0 100644 --- a/jeorg-camel-app-atm/pom.xml +++ b/jeorg-camel-app-atm/pom.xml @@ -7,9 +7,6 @@ org.jesperancinha.atm 1.0.0 - - 3.20.1 - 4.0.0 @@ -127,6 +124,12 @@ mockk-jvm test + + + com.ninja-squad + springmockk + test + @@ -252,6 +255,17 @@ omni-coveragereporter-maven-plugin ${omni-coveragereporter-maven-plugin.version} + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*Test + **/*IT + + + \ No newline at end of file diff --git a/jeorg-camel-app-atm/src/main/kotlin/org/jesperancinha/atm/finder/service/AtmLocatorService.kt b/jeorg-camel-app-atm/src/main/kotlin/org/jesperancinha/atm/finder/service/AtmLocatorService.kt index 3090e87..75f6b8d 100644 --- a/jeorg-camel-app-atm/src/main/kotlin/org/jesperancinha/atm/finder/service/AtmLocatorService.kt +++ b/jeorg-camel-app-atm/src/main/kotlin/org/jesperancinha/atm/finder/service/AtmLocatorService.kt @@ -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 @@ -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 { diff --git a/jeorg-camel-app-atm/src/main/kotlin/org/jesperancinha/atm/finder/service/payload/response/ATMGeoLocation.kt b/jeorg-camel-app-atm/src/main/kotlin/org/jesperancinha/atm/finder/service/payload/response/ATMGeoLocation.kt index 9f935a1..97fea75 100644 --- a/jeorg-camel-app-atm/src/main/kotlin/org/jesperancinha/atm/finder/service/payload/response/ATMGeoLocation.kt +++ b/jeorg-camel-app-atm/src/main/kotlin/org/jesperancinha/atm/finder/service/payload/response/ATMGeoLocation.kt @@ -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 -} \ No newline at end of file + val lng: Double, +) diff --git a/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/camel/AtmServiceIT.kt b/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/camel/AtmServiceIT.kt index c8864dd..5999ab6 100644 --- a/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/camel/AtmServiceIT.kt +++ b/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/camel/AtmServiceIT.kt @@ -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::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 } } @@ -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" } } \ No newline at end of file diff --git a/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/dao/Model.kt b/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/dao/Model.kt new file mode 100644 index 0000000..b48d37d --- /dev/null +++ b/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/dao/Model.kt @@ -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 = (1..122).map { atm }.toTypedArray() \ No newline at end of file diff --git a/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/service/AtmLocatorServiceImpIT.kt b/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/service/AtmLocatorServiceImpIT.kt index ef872f5..90143d9 100644 --- a/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/service/AtmLocatorServiceImpIT.kt +++ b/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/service/AtmLocatorServiceImpIT.kt @@ -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.* @@ -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 = atmLocatorService.getAtmPerCity(AMSTERDAM) + setField(atmLocatorService, ATM_ENDPOINT, HTTPS_WWW_ING_NL_API_LOCATOR_ATMS) + every { restTemplate.getForEntity( + any(), + 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" } } \ No newline at end of file diff --git a/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/service/AtmLocatorServiceTest.kt b/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/service/AtmLocatorServiceTest.kt index adfbc54..f689067 100644 --- a/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/service/AtmLocatorServiceTest.kt +++ b/jeorg-camel-app-atm/src/test/kotlin/com/jesperancinha/atm/finder/service/AtmLocatorServiceTest.kt @@ -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.* @@ -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) @@ -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" diff --git a/pom.xml b/pom.xml index 4a94a50..ca34be2 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,7 @@ org.jesperancinha.atm jeorg-camel-test-drives 1.0.0 + CamelTD :: Parent jeorg-camel-app-atm @@ -14,7 +15,7 @@ UTF-8 UTF-8 - 17 + 19 ${java.version} ${java.version} ${java.version} @@ -22,6 +23,7 @@ 3.3.2 3.3.0 1.8.0 + 4.0.0 2.0.24 1.1.2 @@ -45,6 +47,8 @@ 3.0.1 5.5.4 1.13.2 + 3.20.1 + 3.0.0-M8 @@ -189,6 +193,13 @@ test + + com.ninja-squad + springmockk + ${springmockk.version} + test + +