use of com.thoughtworks.go.server.materials.postcommit.PostCommitHookMaterialType in project gocd by gocd.
the class MaterialUpdateServiceTest method shouldReturn404WhenThereAreNoMaterialsToSchedule_WhenInvokingPostCommitHookMaterialUpdate.
@Test
public void shouldReturn404WhenThereAreNoMaterialsToSchedule_WhenInvokingPostCommitHookMaterialUpdate() {
when(goConfigService.isUserAdmin(username)).thenReturn(true);
PostCommitHookMaterialType materialType = mock(PostCommitHookMaterialType.class);
when(postCommitHookMaterialType.toType("type")).thenReturn(materialType);
PostCommitHookImplementer hookImplementer = mock(PostCommitHookImplementer.class);
when(materialType.getImplementer()).thenReturn(hookImplementer);
when(materialType.isKnown()).thenReturn(true);
CruiseConfig config = mock(BasicCruiseConfig.class);
when(goConfigService.currentCruiseConfig()).thenReturn(config);
when(config.getGroups()).thenReturn(new PipelineGroups());
when(hookImplementer.prune(anySet(), anyMap())).thenReturn(new HashSet<Material>());
final HashMap params = new HashMap();
params.put(MaterialUpdateService.TYPE, "type");
service.notifyMaterialsForUpdate(username, params, result);
HttpLocalizedOperationResult operationResult = new HttpLocalizedOperationResult();
operationResult.notFound(LocalizedMessage.string("MATERIAL_SUITABLE_FOR_NOTIFICATION_NOT_FOUND"), HealthStateType.general(HealthStateScope.GLOBAL));
assertThat(result, is(operationResult));
verify(hookImplementer).prune(anySet(), anyMap());
}
use of com.thoughtworks.go.server.materials.postcommit.PostCommitHookMaterialType in project gocd by gocd.
the class MaterialUpdateService method notifyMaterialsForUpdate.
public void notifyMaterialsForUpdate(Username username, Object params, HttpLocalizedOperationResult result) {
if (!goConfigService.isUserAdmin(username)) {
result.unauthorized(LocalizedMessage.string("API_ACCESS_UNAUTHORIZED"), HealthStateType.unauthorised());
return;
}
final Map attributes = (Map) params;
if (attributes.containsKey(MaterialUpdateService.TYPE)) {
PostCommitHookMaterialType materialType = postCommitHookMaterialType.toType((String) attributes.get(MaterialUpdateService.TYPE));
if (!materialType.isKnown()) {
result.badRequest(LocalizedMessage.string("API_BAD_REQUEST"));
return;
}
final PostCommitHookImplementer materialTypeImplementer = materialType.getImplementer();
final CruiseConfig cruiseConfig = goConfigService.currentCruiseConfig();
Set<Material> allUniquePostCommitSchedulableMaterials = materialConfigConverter.toMaterials(cruiseConfig.getAllUniquePostCommitSchedulableMaterials());
final Set<Material> prunedMaterialList = materialTypeImplementer.prune(allUniquePostCommitSchedulableMaterials, attributes);
if (prunedMaterialList.isEmpty()) {
result.notFound(LocalizedMessage.string("MATERIAL_SUITABLE_FOR_NOTIFICATION_NOT_FOUND"), HealthStateType.general(HealthStateScope.GLOBAL));
return;
}
for (Material material : prunedMaterialList) {
updateMaterial(material);
}
result.accepted(LocalizedMessage.string("MATERIAL_SCHEDULE_NOTIFICATION_ACCEPTED"));
} else {
result.badRequest(LocalizedMessage.string("API_BAD_REQUEST"));
}
}
use of com.thoughtworks.go.server.materials.postcommit.PostCommitHookMaterialType in project gocd by gocd.
the class MaterialUpdateServiceTest method setUp.
@Before
public void setUp() throws Exception {
queue = mock(MaterialUpdateQueue.class);
configQueue = mock(ConfigMaterialUpdateQueue.class);
watchList = mock(GoConfigWatchList.class);
completed = mock(MaterialUpdateCompletedTopic.class);
goConfigService = mock(GoConfigService.class);
postCommitHookMaterialType = mock(PostCommitHookMaterialTypeResolver.class);
serverHealthService = mock(ServerHealthService.class);
systemEnvironment = new SystemEnvironment();
scmMaterialSource = mock(SCMMaterialSource.class);
dependencyMaterialUpdateNotifier = mock(DependencyMaterialUpdateNotifier.class);
materialConfigConverter = mock(MaterialConfigConverter.class);
MDUPerformanceLogger mduPerformanceLogger = mock(MDUPerformanceLogger.class);
dependencyMaterialUpdateQueue = mock(DependencyMaterialUpdateQueue.class);
service = new MaterialUpdateService(queue, configQueue, completed, watchList, goConfigService, systemEnvironment, serverHealthService, postCommitHookMaterialType, mduPerformanceLogger, materialConfigConverter, dependencyMaterialUpdateQueue);
service.registerMaterialSources(scmMaterialSource);
service.registerMaterialUpdateCompleteListener(scmMaterialSource);
service.registerMaterialUpdateCompleteListener(dependencyMaterialUpdateNotifier);
HashSet<MaterialConfig> materialConfigs = new HashSet(Collections.singleton(MATERIAL_CONFIG));
HashSet<Material> materials = new HashSet(Collections.singleton(svnMaterial));
when(goConfigService.getSchedulableMaterials()).thenReturn(materialConfigs);
when(materialConfigConverter.toMaterials(materialConfigs)).thenReturn(materials);
username = new Username(new CaseInsensitiveString("loser"));
result = new HttpLocalizedOperationResult();
validMaterialType = mock(PostCommitHookMaterialType.class);
when(validMaterialType.isKnown()).thenReturn(true);
when(validMaterialType.isValid(anyString())).thenReturn(true);
invalidMaterialType = mock(PostCommitHookMaterialType.class);
when(invalidMaterialType.isKnown()).thenReturn(false);
when(invalidMaterialType.isValid(anyString())).thenReturn(false);
}
Aggregations