Skip to content
Snippets Groups Projects
Commit 42a7448f authored by Alexandre Grison's avatar Alexandre Grison
Browse files

Add some integration testing

using Feign
parent 071f999a
No related branches found
No related tags found
No related merge requests found
......@@ -139,9 +139,9 @@
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:5.0.0.BUILD-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: com.h2database:h2:1.4.195" level="project" />
<orderEntry type="library" name="Maven: com.netflix.feign:feign-core:8.18.0" level="project" />
<orderEntry type="library" name="Maven: org.jvnet:animal-sniffer-annotation:1.0" level="project" />
<orderEntry type="library" name="Maven: io.github.openfeign:feign-jackson:9.4.0" level="project" />
<orderEntry type="library" name="Maven: io.github.openfeign:feign-core:9.4.0" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.jvnet:animal-sniffer-annotation:1.0" level="project" />
<orderEntry type="library" name="Maven: com.netflix.feign:feign-gson:8.18.0" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.github.slugify:slugify:2.1.9" level="project" />
</component>
</module>
\ No newline at end of file
......@@ -75,9 +75,9 @@
<version>8.18.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-jackson</artifactId>
<version>9.4.0</version>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-gson</artifactId>
<version>8.18.0</version>
</dependency>
<dependency>
<groupId>com.github.slugify</groupId>
......
package io.realworld.client
import feign.Headers
import feign.Param
import feign.RequestLine
import io.realworld.model.inout.Profile
import org.springframework.web.bind.annotation.PathVariable
import io.realworld.client.response.OutProfile
@Headers("Content-Type: application/json",
"Authorization: Token {token}")
interface ProfileClient {
@RequestLine("GET /api/profiles/{username}")
fun profile(@PathVariable username: String): Profile
fun profile(@Param("token") token: String, @Param("username") username: String): OutProfile
@RequestLine("POST /api/profiles/{username}/follow")
fun follow(@PathVariable username: String): Profile
fun follow(@Param("token") token: String, @Param("username") username: String): OutProfile
@RequestLine("DELETE /api/profiles/{username}/follow")
fun unfollow(@PathVariable username: String): Profile
fun unfollow(@Param("token") token: String, @Param("username") username: String): OutProfile
}
package io.realworld.client
import feign.Headers
import feign.RequestLine
import io.realworld.model.Tag
import io.realworld.client.response.OutTag
interface TagClient {
@RequestLine("GET /api/tags")
fun tags(): List<Tag>
@Headers("Content-Type: application/json")
fun tags(): OutTag
}
package io.realworld.client
import feign.Headers
import feign.RequestLine
import io.realworld.model.User
import io.realworld.model.inout.Login
import io.realworld.model.inout.Register
import io.realworld.client.response.InLogin
import io.realworld.client.response.InRegister
import io.realworld.client.response.OutUser
@Headers("Content-Type: application/json")
interface UserClient {
@RequestLine("POST /api/users/login")
fun login(login: Login): User
fun login(login: InLogin): OutUser
@RequestLine("POST /api/users")
fun register(register: Register): User
fun register(register: InRegister): OutUser
}
package io.realworld.client.response
import io.realworld.model.inout.Login
data class InLogin(val user: Login)
package io.realworld.client.response
import io.realworld.model.inout.Register
data class InRegister(val user: Register)
package io.realworld.client.response
import io.realworld.model.inout.Profile
data class OutProfile(var profile: Profile? = null)
package io.realworld.client.response
data class OutTag(var tags: List<String> = listOf())
package io.realworld.client.response
import io.realworld.model.User
data class OutUser(var user: User = User())
package io.realworld
import feign.Feign
import feign.Logger
import feign.jackson.JacksonDecoder
import feign.jackson.JacksonEncoder
import feign.gson.GsonDecoder
import feign.gson.GsonEncoder
import io.realworld.client.ProfileClient
import io.realworld.client.TagClient
import io.realworld.client.UserClient
import io.realworld.client.response.InLogin
import io.realworld.client.response.InRegister
import io.realworld.model.inout.Login
import io.realworld.model.inout.Register
import org.hamcrest.Matchers
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.core.env.Environment
import org.springframework.test.context.junit4.SpringRunner
@RunWith(SpringRunner::class)
......@@ -26,38 +30,70 @@ class ApiApplicationTests {
var environment: Environment? = null
var tagClient: TagClient? = null
var userClient: UserClient? = null
var profileClient: ProfileClient? = null
@Before
fun before() {
fun <T> buildClient(t: Class<T>): T {
environment.let {
randomServerPort = Integer.valueOf(it!!.getProperty("local.server.port"))
tagClient = Feign.builder()
.encoder(JacksonEncoder()).decoder(JacksonDecoder())
.target(TagClient::class.java, "http://localhost:$randomServerPort")
userClient = Feign.builder()
.encoder(JacksonEncoder()).decoder(JacksonDecoder())
.logger(Logger.JavaLogger())
.logLevel(Logger.Level.FULL)
.target(UserClient::class.java, "http://localhost:$randomServerPort")
return Feign.builder()
.encoder(GsonEncoder()).decoder(GsonDecoder())
.target(t, "http://localhost:${randomServerPort}")
}
}
@Before
fun before() {
tagClient = buildClient(TagClient::class.java)
userClient = buildClient(UserClient::class.java)
profileClient = buildClient(ProfileClient::class.java)
}
@Test
fun retrieveTags() {
println("> tags: " + tagClient?.let {
it.tags()
})
println("> tags: " + tagClient?.tags()?.tags)
}
@Test
fun registerAndLogin() {
println("> register: " + userClient?.let {
it.register(Register(username="Foo", email="foo@bar.com", password="LOL"))
})
println("> login: " + userClient?.let {
it.login(Login(email="foo@bar.com", password="LOL"))
})
}
fun userAndProfileTest() {
val fooRegister = userClient?.register(
InRegister(Register(username = "foo", email = "foo@foo.com", password = "foo")))
Assert.assertEquals("foo", fooRegister?.user?.username)
Assert.assertEquals("foo@foo.com", fooRegister?.user?.email)
Assert.assertThat(fooRegister?.user?.token, Matchers.notNullValue())
println("Register foo OK")
val fooLogin = userClient?.login(InLogin(Login(email = "foo@foo.com", password = "foo")))
Assert.assertEquals("foo", fooLogin?.user?.username)
Assert.assertEquals("foo@foo.com", fooLogin?.user?.email)
Assert.assertThat(fooLogin?.user?.token, Matchers.notNullValue())
println("Login foo OK")
val barRegister = userClient?.register(
InRegister(Register(username = "bar", email = "bar@bar.com", password = "bar")))
Assert.assertEquals("bar", barRegister?.user?.username)
Assert.assertEquals("bar@bar.com", barRegister?.user?.email)
Assert.assertThat(barRegister?.user?.token, Matchers.notNullValue())
println("Register bar OK")
val barLogin = userClient?.login(InLogin(Login(email = "bar@bar.com", password = "bar")))
Assert.assertEquals("bar", barLogin?.user?.username)
Assert.assertEquals("bar@bar.com", barLogin?.user?.email)
Assert.assertThat(barLogin?.user?.token, Matchers.notNullValue())
println("Login bar OK")
var profile = profileClient?.profile(barLogin?.user?.token!!, "foo")?.profile
Assert.assertEquals("foo", profile?.username)
Assert.assertFalse(profile?.following!!)
println("Profile foo requested by bar OK")
profile = profileClient?.follow(barLogin?.user?.token!!, "foo")?.profile
Assert.assertEquals("foo", profile?.username)
Assert.assertTrue(profile?.following!!)
println("Foo is followed by bar OK")
profile = profileClient?.unfollow(barLogin?.user?.token!!, "foo")?.profile
Assert.assertEquals("foo", profile?.username)
Assert.assertFalse(profile?.following!!)
println("Foo is unfollowed by bar OK")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment