Skip to content

Commit

Permalink
Add upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
johanah29 committed Jan 3, 2025
1 parent d154d6b commit 55b74f0
Showing 1 changed file with 66 additions and 4 deletions.
70 changes: 66 additions & 4 deletions openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import io.openbas.utils.mockUser.WithMockAdminUser;
import io.openbas.utils.mockUser.WithMockPlannerUser;
import jakarta.servlet.ServletException;

import java.util.List;

import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -41,15 +43,19 @@ public class PlayerApiTest {
static Tag TAG;
static Organization ORGANIZATION;

@Autowired private MockMvc mvc;
@Autowired
private MockMvc mvc;

@Value("${openbas.admin.email:#{null}}")
private String adminEmail;

@Autowired private OrganizationRepository organizationRepository;
@Autowired
private OrganizationRepository organizationRepository;

@Autowired private TagRepository tagRepository;
@Autowired private UserRepository userRepository;
@Autowired
private TagRepository tagRepository;
@Autowired
private UserRepository userRepository;

@BeforeAll
void beforeAll() {
Expand Down Expand Up @@ -111,6 +117,62 @@ void createPlayerWithRestrictedUserTest() throws Exception {
assertTrue(actualMessage.contains(expectedMessage));
}

@DisplayName("Upsert of a player")
@Test
@WithMockAdminUser
void upsertPlayerTest() throws Exception {
// --PREPARE--
User user = new User();
user.setUpdateAttributes(PLAYER_INPUT);
PLAYER = userRepository.save(user);
String newFirstname = "updatedFirstname";
PLAYER_INPUT.setFirstname(newFirstname);

// --EXECUTE--
String response =
mvc.perform(
post(PLAYER_URI + "/upsert")
.content(asJsonString(PLAYER_INPUT))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().is2xxSuccessful())
.andReturn()
.getResponse()
.getContentAsString();

// --ASSERT--
assertEquals("updatedFirstname", JsonPath.read(response, "$.user_firstname"));
// --THEN--
userRepository.deleteById(JsonPath.read(response, "$.user_id"));
}

@DisplayName("Upsert of a non existing player")
@Test
@WithMockAdminUser
void upsertNonExistingPlayerTest() throws Exception {
// --PREPARE--
User user = new User();
user.setEmail("[email protected]");
PLAYER = userRepository.save(user);

// --EXECUTE--
String response =
mvc.perform(
post(PLAYER_URI + "/upsert")
.content(asJsonString(PLAYER_INPUT))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().is2xxSuccessful())
.andReturn()
.getResponse()
.getContentAsString();

// --ASSERT--
assertEquals("Firstname", JsonPath.read(response, "$.user_firstname"));
// --THEN--
userRepository.deleteById(JsonPath.read(response, "$.user_id"));
}

@DisplayName("Edition of a player")
@Test
@WithMockAdminUser
Expand Down

0 comments on commit 55b74f0

Please sign in to comment.