/
ElasticProfileOperationControllerV1Test.groovy
118 lines (95 loc) · 3.96 KB
/
ElasticProfileOperationControllerV1Test.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2018 ThoughtWorks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.thoughtworks.go.apiv1.elasticprofileoperation
import com.thoughtworks.go.api.SecurityTestTrait
import com.thoughtworks.go.api.spring.ApiAuthenticationHelper
import com.thoughtworks.go.config.exceptions.RecordNotFoundException
import com.thoughtworks.go.domain.ElasticProfileUsage
import com.thoughtworks.go.server.service.ElasticProfileService
import com.thoughtworks.go.spark.ControllerTrait
import com.thoughtworks.go.spark.GroupAdminUserSecurity
import com.thoughtworks.go.spark.SecurityServiceTrait
import groovy.json.JsonBuilder
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.mockito.Mock
import static org.mockito.Mockito.when
import static org.mockito.MockitoAnnotations.initMocks
class ElasticProfileOperationControllerV1Test implements SecurityServiceTrait, ControllerTrait<ElasticProfileOperationControllerV1> {
@Mock
private ElasticProfileService elasticProfileService
@BeforeEach
void setup() {
initMocks(this)
}
@Override
ElasticProfileOperationControllerV1 createControllerInstance() {
new ElasticProfileOperationControllerV1(elasticProfileService, new ApiAuthenticationHelper(securityService, goConfigService))
}
@Nested
class Usages {
@Nested
class Security implements SecurityTestTrait, GroupAdminUserSecurity {
@Override
String getControllerMethodUnderTest() {
return "usages"
}
@Override
void makeHttpCall() {
getWithApiHeader(controller.controllerPath("/docker/usages"))
}
}
@Nested
class AsAGroupAdmin {
@BeforeEach
void setUp() {
enableSecurity()
loginAsGroupAdmin()
}
@Test
void 'should list jobs associated with a profile id'() {
def elasticProfileUsages = Arrays.asList(
new ElasticProfileUsage("LinuxPR", "build", "compile", "linux-pr"),
new ElasticProfileUsage("LinuxPR", "build", "test", "linux-pr"),
new ElasticProfileUsage("WindowsPR", "clean", "clean-dirs"),
new ElasticProfileUsage("WindowsPR", "clean", "clean-artifacts")
)
when(elasticProfileService.getUsageInformation("docker")).thenReturn(elasticProfileUsages)
getWithApiHeader(controller.controllerPath("/docker/usages"))
def expectedResponse = [
[pipeline_name: "LinuxPR", stage_name: "build", job_name: "compile", template_name: "linux-pr"],
[pipeline_name: "LinuxPR", stage_name: "build", job_name: "test", template_name: "linux-pr"],
[pipeline_name: "WindowsPR", stage_name: "clean", job_name: "clean-dirs"],
[pipeline_name: "WindowsPR", stage_name: "clean", job_name: "clean-artifacts"]
]
assertThatResponse()
.isOk()
.hasContentType(controller.mimeType)
.hasBodyWithJson(new JsonBuilder(expectedResponse).toString())
}
@Test
void 'should return 404 when profile with id does not exist'() {
when(elasticProfileService.getUsageInformation("docker")).thenThrow(new RecordNotFoundException("docker"))
getWithApiHeader(controller.controllerPath("/docker/usages"))
assertThatResponse()
.isNotFound()
.hasContentType(controller.mimeType)
.hasJsonMessage("Either the resource you requested was not found, or you are not authorized to perform this action.")
}
}
}
}