Skip to content

Commit

Permalink
Trying to fix integ test
Browse files Browse the repository at this point in the history
  • Loading branch information
Hartorn committed Sep 12, 2023
1 parent 94c33bb commit 9d59035
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
13 changes: 13 additions & 0 deletions backend/src/test/java/ai/giskard/utils/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ private static ObjectMapper createObjectMapper() {
return mapper;
}

/**
* Convert a JSON to a DTO.
*
* @param <T> Built type
* @param json json content
* @param toCast the class to deserialize from and cast to
* @return the parsed object
* @throws IOException raised if json does not represent T
*/
public static <T> T convertJsonStringToObject(String json, Class<T> toCast) throws IOException {
return mapper.readValue(json, toCast);
}

/**
* Convert an object to JSON byte array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,27 @@ void createUser() throws Exception {
managedUserVM.setActivated(true);
managedUserVM.setRoles(Collections.singleton(AuthoritiesConstants.AICREATOR));

restUserMockMvc
// To use ? https://www.baeldung.com/spring-boot-testresttemplate
AdminUserDTO res = TestUtil.convertJsonStringToObject(restUserMockMvc
.perform(
post("/api/v2/admin/users").contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(managedUserVM))
)
.andExpect(status().isCreated());
.andExpect(status().isCreated())
.andReturn().getResponse().getContentAsString(), AdminUserDTO.class);

// Validate the User in the database
// Validate the number of users in the database
assertPersistedUsers(users -> {
assertThat(users).hasSize(databaseSizeBeforeCreate + 1);
User testUser = users.get(users.size() - 1);
});

// Validate the User in the database
Consumer<List<User>> assertions = users -> {
User testUser = users.get(0);
assertThat(testUser.getLogin()).isEqualTo(DEFAULT_LOGIN);
assertThat(testUser.getDisplayName()).isEqualTo(DEFAULT_DISPLAY_NAME);
assertThat(testUser.getEmail()).isEqualTo(DEFAULT_EMAIL);
});
};
assertions.accept(userRepository.findAllById(List.of(res.getId())));
}

@Test
Expand Down

0 comments on commit 9d59035

Please sign in to comment.