Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LabraCORE
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
EIP
Labrador
LabraCORE
Merge requests
!32
Resolve "ClusterController testing"
Code
Review changes
Check out branch
Open in Workspace
Download
Patches
Plain diff
Expand sidebar
Merged
Resolve "ClusterController testing"
28-clustercontroller-testing
into
development
Overview
0
Commits
1
Pipelines
13
Changes
1
Merged
Resolve "ClusterController testing"
Chris Lemaire
requested to merge
28-clustercontroller-testing
into
development
May 11, 2020
Overview
0
Commits
1
Pipelines
13
Changes
1
Closes
#28 (closed)
Edited
May 11, 2020
by
Chris Lemaire
0
0
Merge request reports
Compare
development
version 4
f3a3b643
May 12, 2020
version 3
ac3163dc
May 12, 2020
version 2
9d83f157
May 12, 2020
version 1
52a0f4bb
May 11, 2020
development (base)
and
latest version
latest version
0f311130
1 commit,
May 14, 2020
version 4
f3a3b643
1 commit,
May 12, 2020
version 3
ac3163dc
1 commit,
May 12, 2020
version 2
9d83f157
1 commit,
May 12, 2020
version 1
52a0f4bb
1 commit,
May 11, 2020
1 file
+
134
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
src/test/java/nl/tudelft/labracore/controller/ClusterControllerTest.java
0 → 100644
+
134
−
0
View file @ 0f311130
Edit in single-file editor
Open in Web IDE
/*
* 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