use of com.thoughtworks.go.server.service.result.LocalizedOperationResult in project gocd by gocd.
the class MaterialServiceTest method shouldReturnNotFoundIfTheMaterialDoesNotBelongToTheGivenPipeline.
@Test
public void shouldReturnNotFoundIfTheMaterialDoesNotBelongToTheGivenPipeline() {
Username pavan = Username.valueOf("pavan");
when(securityService.hasViewPermissionForPipeline(pavan, "pipeline")).thenReturn(true);
LocalizedOperationResult operationResult = mock(LocalizedOperationResult.class);
when(goConfigService.materialForPipelineWithFingerprint("pipeline", "sha")).thenThrow(new RuntimeException("Not found"));
materialService.searchRevisions("pipeline", "sha", "23", pavan, operationResult);
verify(operationResult).notFound(LocalizedMessage.materialWithFingerPrintNotFound("pipeline", "sha"), HealthStateType.general(HealthStateScope.forPipeline("pipeline")));
}
use of com.thoughtworks.go.server.service.result.LocalizedOperationResult in project gocd by gocd.
the class MaterialServiceTest method shouldReturnTheRevisionsThatMatchTheGivenSearchString.
@Test
public void shouldReturnTheRevisionsThatMatchTheGivenSearchString() {
Username pavan = Username.valueOf("pavan");
when(securityService.hasViewPermissionForPipeline(pavan, "pipeline")).thenReturn(true);
LocalizedOperationResult operationResult = mock(LocalizedOperationResult.class);
MaterialConfig materialConfig = mock(MaterialConfig.class);
when(goConfigService.materialForPipelineWithFingerprint("pipeline", "sha")).thenReturn(materialConfig);
List<MatchedRevision> expected = asList(new MatchedRevision("23", "revision", "revision", "user", new DateTime(2009, 10, 10, 12, 0, 0, 0).toDate(), "comment"));
when(materialRepository.findRevisionsMatching(materialConfig, "23")).thenReturn(expected);
assertThat(materialService.searchRevisions("pipeline", "sha", "23", pavan, operationResult), is(expected));
}
use of com.thoughtworks.go.server.service.result.LocalizedOperationResult in project gocd by gocd.
the class PackageRepositoryService method checkConnection.
public void checkConnection(final PackageRepository packageRepository, final LocalizedOperationResult result) {
try {
Result checkConnectionResult = packageRepositoryExtension.checkConnectionToRepository(packageRepository.getPluginConfiguration().getId(), populateConfiguration(packageRepository.getConfiguration()));
String messages = checkConnectionResult.getMessagesForDisplay();
if (!checkConnectionResult.isSuccessful()) {
result.connectionError(LocalizedMessage.string("CHECK_CONNECTION_FAILED", "package repository", messages));
return;
}
result.setMessage(LocalizedMessage.string("CONNECTION_OK", messages));
return;
} catch (Exception e) {
result.internalServerError(LocalizedMessage.string("CHECK_CONNECTION_FAILED", "package repository", e.getMessage()));
}
}
use of com.thoughtworks.go.server.service.result.LocalizedOperationResult in project gocd by gocd.
the class ScheduleService method cancelAndTriggerRelevantStages.
// synchronized for updating job
public Stage cancelAndTriggerRelevantStages(Long stageId, Username username, LocalizedOperationResult result) throws Exception {
Stage stageForId;
LocalizedOperationResult opResult = result == null ? new DefaultLocalizedOperationResult() : result;
try {
stageForId = stageService.stageById(stageId);
} catch (Exception e) {
LOGGER.error("[Stage Cancellation] Failed to retrieve stage identifier", e);
opResult.notFound(LocalizedMessage.string("STAGE_FOR_LOCATOR_NOT_FOUND", stageId), HealthStateType.general(HealthStateScope.GLOBAL));
return null;
}
if (!stageForId.isActive()) {
opResult.setMessage(LocalizedMessage.string("STAGE_IS_NOT_ACTIVE_FOR_CANCELLATION"));
return stageForId;
}
String stageMutex = mutexForStageInstance(stageForId.getIdentifier());
synchronized (stageMutex) {
// reload stage so we see committed state after acquiring mutex
final Stage stage = stageService.stageById(stageId);
String pipelineName = stage.getIdentifier().getPipelineName();
String stageName = stage.getIdentifier().getStageName();
String user = username == null ? null : username.getUsername().toString();
if (!securityService.hasOperatePermissionForStage(pipelineName, stageName, user)) {
opResult.unauthorized(LocalizedMessage.string("UNAUTHORIZED_TO_OPERATE_STAGE", stageName), HealthStateType.unauthorised());
return null;
}
LOGGER.info("[Stage Cancellation] Cancelling stage {}", stage.getIdentifier());
transactionTemplate.executeWithExceptionHandling(new com.thoughtworks.go.server.transaction.TransactionCallbackWithoutResult() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) throws Exception {
stageService.cancelStage(stage);
}
});
transactionTemplate.executeWithExceptionHandling(new com.thoughtworks.go.server.transaction.TransactionCallbackWithoutResult() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) throws Exception {
automaticallyTriggerRelevantStagesFollowingCompletionOf(stage);
}
});
opResult.setMessage(LocalizedMessage.string("STAGE_CANCELLED_SUCCESSFULLY"));
return stage;
}
}
use of com.thoughtworks.go.server.service.result.LocalizedOperationResult in project gocd by gocd.
the class GoConfigServiceIntegrationTest method shouldNotUpdateConfigFromUIWhentheUserDoesNotHavePermissions.
@Test
public void shouldNotUpdateConfigFromUIWhentheUserDoesNotHavePermissions() {
final PipelineConfig pipelineConfig = configHelper.addPipeline("pipeline", "stage");
HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
String md5 = goConfigService.getConfigForEditing().getMd5();
final CruiseConfig[] configObtainedInCheckPermissions = new CruiseConfig[1];
ConfigUpdateResponse response = goConfigService.updateConfigFromUI(new AddStageToPipelineCommand("secondStage") {
public void checkPermission(CruiseConfig cruiseConfig, LocalizedOperationResult result) {
result.unauthorized(LocalizedMessage.string("UNAUTHORIZED_TO_EDIT_GROUP", "groupName"), null);
configObtainedInCheckPermissions[0] = cruiseConfig;
}
}, md5, Username.ANONYMOUS, result);
assertThat(configObtainedInCheckPermissions[0], is(goConfigService.getCurrentConfig()));
PipelineConfig config = goConfigService.getConfigForEditing().pipelineConfigByName(new CaseInsensitiveString("pipeline"));
assertThat(config.size(), is(1));
assertThat(config.get(0).name(), is(new CaseInsensitiveString("stage")));
assertThat(response.getCruiseConfig(), is(goConfigService.getConfigForEditing()));
assertThat(response.getNode(), is(pipelineConfig));
assertThat(result.isSuccessful(), is(false));
assertThat(result.httpCode(), is(401));
assertThat(result.message(localizer), is("Unauthorized to edit 'groupName' group."));
}
Aggregations