Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into preflight-api
- Loading branch information
Showing
34 changed files
with
1,654 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
apply plugin: 'jacoco' | ||
apply plugin: 'groovy' | ||
|
||
dependencies { | ||
compile project(':api:api-base') | ||
|
||
testCompile project(path: ':api:api-base', configuration: 'testOutput') | ||
|
||
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: project.versions.junit5 | ||
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: project.versions.junit5 | ||
} |
74 changes: 74 additions & 0 deletions
74
...om/thoughtworks/go/apiv1/elasticprofileoperation/ElasticProfileOperationControllerV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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.ApiController; | ||
import com.thoughtworks.go.api.ApiVersion; | ||
import com.thoughtworks.go.api.spring.ApiAuthenticationHelper; | ||
import com.thoughtworks.go.apiv1.elasticprofileoperation.representers.ElasticProfileUsageRepresenter; | ||
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.Routes; | ||
import com.thoughtworks.go.spark.spring.SparkSpringController; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import spark.Request; | ||
import spark.Response; | ||
|
||
import java.util.Collection; | ||
|
||
import static spark.Spark.*; | ||
|
||
@Component | ||
public class ElasticProfileOperationControllerV1 extends ApiController implements SparkSpringController { | ||
private static final String PROFILE_ID_PARAM = "profile_id"; | ||
private ElasticProfileService elasticProfileService; | ||
private final ApiAuthenticationHelper apiAuthenticationHelper; | ||
|
||
@Autowired | ||
public ElasticProfileOperationControllerV1(ElasticProfileService elasticProfileService, ApiAuthenticationHelper apiAuthenticationHelper) { | ||
super(ApiVersion.v1); | ||
this.elasticProfileService = elasticProfileService; | ||
this.apiAuthenticationHelper = apiAuthenticationHelper; | ||
} | ||
|
||
@Override | ||
public String controllerBasePath() { | ||
return Routes.ElasticProfileAPI.INTERNAL_BASE; | ||
} | ||
|
||
@Override | ||
public void setupRoutes() { | ||
path(controllerBasePath(), () -> { | ||
before("", this::setContentType); | ||
before("/*", this::setContentType); | ||
before("", mimeType, apiAuthenticationHelper::checkAdminUserOrGroupAdminUserAnd403); | ||
before("/*", mimeType, apiAuthenticationHelper::checkAdminUserOrGroupAdminUserAnd403); | ||
|
||
get(Routes.ElasticProfileAPI.ID + Routes.ElasticProfileAPI.USAGES, mimeType, this::usages); | ||
exception(RecordNotFoundException.class, this::notFound); | ||
}); | ||
} | ||
|
||
public String usages(Request request, Response response) { | ||
final String elasticProfileId = StringUtils.stripToEmpty(request.params(PROFILE_ID_PARAM)); | ||
final Collection<ElasticProfileUsage> jobsUsingElasticProfile = elasticProfileService.getUsageInformation(elasticProfileId); | ||
return ElasticProfileUsageRepresenter.toJSON(jobsUsingElasticProfile); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...ughtworks/go/apiv1/elasticprofileoperation/ElasticProfileOperationControllerV1Test.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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.") | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.