Skip to content

Commit

Permalink
🐛 : fix kotlin data classes deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Sep 27, 2019
1 parent ed1d70c commit 28777ae
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- jackson support for kotlin -->
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>${jackson.version}</version>
</dependency>

</dependencies>

<build>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/codeka/gaia/teams/bo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.springframework.data.mongodb.core.mapping.DBRef
/**
* A Gaia Team, which groups users
*/
data class Team(val id: String?)
data class Team(val id: String)

/**
* a Gaia user, which has granted access to modules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void teams_shouldBeAccessible_forAdminUser() {
void teams_shouldBeExposed_atSpecificUrl() throws Exception {
mockMvc.perform(get("/api/teams"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$..id", contains("Ze Team", "Not Ze Team")));
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$..id", contains("Ze Team", "Not Ze Team", "Sith")));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.codeka.gaia.teams.controller;

import io.codeka.gaia.teams.Team;
import io.codeka.gaia.teams.User;
import io.codeka.gaia.teams.repository.UserRepository;
import io.codeka.gaia.test.MongoContainer;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -13,11 +16,13 @@
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand All @@ -39,6 +44,9 @@ class UsersRestControllerIT {
@Autowired
private UsersRestController usersRestController;

@Autowired
private UserRepository userRepository;

@Autowired
private MockMvc mockMvc;

Expand All @@ -57,9 +65,9 @@ void users_shouldBeAccessible_forAdminUser() {
void users_shouldBeExposed_atSpecificUrl() throws Exception {
mockMvc.perform(get("/api/users"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$..username", contains("admin", "Mary J")))
.andExpect(jsonPath("$..admin", contains(true, false)))
.andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$..username", contains("admin", "Mary J", "Darth Vader")))
.andExpect(jsonPath("$..admin", contains(true, false, false)))
.andExpect(jsonPath("$..team.id", contains("Ze Team", "Not Ze Team")));
}

Expand All @@ -72,6 +80,35 @@ void saveUser_shouldBeExposed_atSpecificUrl() throws Exception {
.andExpect(jsonPath("$.username", is("Bob")))
.andExpect(jsonPath("$.admin", is(false)))
.andExpect(jsonPath("$.team", isEmptyOrNullString()));

assertThat(userRepository.existsById("Bob")).isTrue();
}

@Test
void users_canBeChangedOfTeam() throws Exception {
// given
assertThat(userRepository.findById("Darth Vader"))
.isPresent()
.map(User::getTeam)
.isNotPresent();

// when
mockMvc.perform(put("/api/users/Darth Vader")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"username\": \"Darth Vader\",\"team\": {\"id\": \"Sith\"}}"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.username", is("Darth Vader")))
.andExpect(jsonPath("$.admin", is(false)))
.andExpect(jsonPath("$.team.id", is("Sith")));

// then
assertThat(userRepository.findById("Darth Vader"))
.isPresent()
.map(User::getTeam)
.hasValue(new Team("Sith"));

mongoContainer.resetDatabase();
}

}
3 changes: 3 additions & 0 deletions src/test/resources/db/00_team.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ gaia.team.insert([
},
{
"_id": "Not Ze Team"
},
{
"_id": "Sith"
}
]);
4 changes: 4 additions & 0 deletions src/test/resources/db/10_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ gaia.user.insert([
"_id": "Mary J",
"team": {"$ref": "team", "$id": "Not Ze Team"}
}
,
{
"_id": "Darth Vader"
}
]);

0 comments on commit 28777ae

Please sign in to comment.