Skip to content
Snippets Groups Projects

Resolve "ClusterController testing"

/*
* Labracore - A connecting core service for Labrador products
* Copyright (C) 2020- Delft University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package nl.tudelft.labracore.controller;
import static nl.tudelft.labracore.test.JsonContentMatcher.jsonContent;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.List;
import javax.transaction.Transactional;
import nl.tudelft.labracore.dto.create.ClusterCreateDTO;
import nl.tudelft.labracore.dto.id.CohortIdDTO;
import nl.tudelft.labracore.dto.id.ProgramIdDTO;
import nl.tudelft.labracore.dto.patch.ClusterPatchDTO;
import nl.tudelft.labracore.model.Cluster;
import nl.tudelft.labracore.model.Cohort;
import nl.tudelft.labracore.model.Program;
import nl.tudelft.labracore.repository.ClusterRepository;
import nl.tudelft.labracore.repository.CohortRepository;
import nl.tudelft.labracore.repository.ProgramRepository;
import nl.tudelft.labracore.test.RestControllerTest;
import nl.tudelft.labracore.test.TestDatabaseLoader;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;
@Transactional
@SpringBootTest
@AutoConfigureMockMvc
public class ClusterControllerTest extends RestControllerTest {
@Autowired
private MockMvc mvc;
@Autowired
private TestDatabaseLoader db;
@Autowired
private ClusterRepository cr;
@Autowired
private CohortRepository cor;
@Autowired
private ProgramRepository pr;
private Cluster b;
private Cohort c;
private Program p;
@BeforeEach
void setup() {
b = cr.getOne(db.getMagics().getId());
c = cor.getOne(db.getFirstGen().getId());
p = pr.getOne(db.getAvengersInitiative().getId());
}
@Test
@WithUserDetails("All-access Key")
void addClusterReturnsIdOfSavedCluster() throws Exception {
mvc.perform(postJson("/api/cluster", ClusterCreateDTO.builder()
.name("EWI")
.cohort(new CohortIdDTO(c.getId()))
.program(new ProgramIdDTO(p.getId()))
.people(List.of())
.build()))
.andExpect(status().isOk())
.andExpect(jsonContent(Long.class).predicate(id -> id > 0 && cr.findById(id).isPresent()))
.andDo(print());
}
@Test
@WithUserDetails("All-access Key")
void patchClusterPerformsUpdate() throws Exception {
mvc.perform(patchJson("/api/cluster/" + b.getId(), ClusterPatchDTO.builder()
.name("B2").build()));
assertThat(cr.findByIdOrThrow(b.getId()).getName())
.isEqualTo("B2");
}
@Test
@WithUserDetails("All-access Key")
void patchClusterReturnsSameId() throws Exception {
mvc.perform(patchJson("/api/cluster/" + b.getId(), ClusterPatchDTO.builder()
.name("B2").build()))
.andExpect(status().isOk())
.andExpect(jsonContent(Long.class).isEqualTo(b.getId()));
}
@WithUserDetails("No-access Key")
@MethodSource("protectedEndpoints")
@ParameterizedTest
void endpointsShouldBeKeyProtected(MockHttpServletRequestBuilder request) throws Exception {
mvc.perform(request.with(csrf()))
.andExpect(status().isForbidden());
}
private static List<MockHttpServletRequestBuilder> protectedEndpoints() throws JsonProcessingException {
return List.of(
postJson("/api/cluster", new ClusterCreateDTO()),
patchJson("/api/cluster/1", new ClusterPatchDTO()));
}
}
Loading