Search in sources :

Example 31 with DistributionJobModel

use of com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel in project hub-alert by blackducksoftware.

the class JobConfigControllerTestIT method testUpdateConfig.

@Test
@WithMockUser(roles = AlertIntegrationTestConstants.ROLE_ALERT_ADMIN)
public void testUpdateConfig() throws Exception {
    ConfigurationModel providerGlobalConfig = addGlobalConfiguration(blackDuckProviderKey, Map.of(ProviderDescriptor.KEY_PROVIDER_CONFIG_NAME, List.of(DEFAULT_BLACK_DUCK_CONFIG), BlackDuckDescriptor.KEY_BLACKDUCK_URL, List.of(testProperties.getBlackDuckURL()), BlackDuckDescriptor.KEY_BLACKDUCK_API_KEY, List.of(testProperties.getBlackDuckAPIToken())));
    JobFieldModel fieldModel = createTestJobFieldModel("1", "2", providerGlobalConfig);
    Map<String, Collection<String>> fieldValueModels = new HashMap<>();
    for (FieldModel newFieldModel : fieldModel.getFieldModels()) {
        fieldValueModels.putAll(newFieldModel.getKeyToValues().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getValues())));
    }
    DistributionJobRequestModel jobRequestModel = createDistributionJobRequestModel(providerGlobalConfig.getConfigurationId());
    DistributionJobModel distributionJobModel = addDistributionJob(jobRequestModel);
    String configId = String.valueOf(distributionJobModel.getJobId());
    String urlPath = REQUEST_URL + "/" + configId;
    MockHttpServletRequestBuilder request = MockMvcRequestBuilders.put(urlPath).with(SecurityMockMvcRequestPostProcessors.user("admin").roles(AlertIntegrationTestConstants.ROLE_ALERT_ADMIN)).with(SecurityMockMvcRequestPostProcessors.csrf());
    fieldModel.setJobId(configId);
    request.content(gson.toJson(fieldModel));
    request.contentType(MEDIA_TYPE);
    mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isNoContent());
}
Also used : ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) HashMap(java.util.HashMap) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) Collection(java.util.Collection) JobFieldModel(com.synopsys.integration.alert.common.rest.model.JobFieldModel) FieldModel(com.synopsys.integration.alert.common.rest.model.FieldModel) JobFieldModel(com.synopsys.integration.alert.common.rest.model.JobFieldModel) JobProviderProjectFieldModel(com.synopsys.integration.alert.common.rest.model.JobProviderProjectFieldModel) DistributionJobRequestModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobRequestModel) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test) DatabaseConfiguredFieldTest(com.synopsys.integration.alert.util.DatabaseConfiguredFieldTest)

Example 32 with DistributionJobModel

use of com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel in project hub-alert by blackducksoftware.

the class AuditEntryActionsTest method testPagedRequest.

@Test
public void testPagedRequest() {
    int totalPages = 2;
    int currentPage = 0;
    int pageSize = 2;
    OffsetDateTime createdAt = DateUtils.createCurrentDateTimestamp();
    AlertNotificationModel entity_1 = new AlertNotificationModel(1L, 1L, "provider", "providerConfigName", "notificationType", "{content: \"content is here...\"}", createdAt, createdAt, false);
    entity_1.setId(1L);
    AlertNotificationModel entity_2 = new AlertNotificationModel(2L, 2L, "provider", "providerConfigName", "notificationType", "{content: \"content is here...\"}", createdAt, createdAt, false);
    entity_2.setId(2L);
    List<AlertNotificationModel> pagedEntryList = Arrays.asList(entity_1, entity_2);
    Page<AlertNotificationModel> pageResponse = Mockito.mock(Page.class);
    Mockito.when(pageResponse.getContent()).thenReturn(pagedEntryList);
    Mockito.when(pageResponse.getTotalPages()).thenReturn(totalPages);
    Mockito.when(pageResponse.getNumber()).thenReturn(currentPage);
    Mockito.when(pageResponse.getSize()).thenReturn(pageSize);
    AuthorizationManager authorizationManager = Mockito.mock(AuthorizationManager.class);
    Mockito.when(authorizationManager.hasReadPermission(Mockito.any(ConfigContextEnum.class), Mockito.any(DescriptorKey.class))).thenReturn(Boolean.TRUE);
    AuditDescriptorKey auditDescriptorKey = new AuditDescriptorKey();
    AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
    DefaultNotificationAccessor notificationManager = Mockito.mock(DefaultNotificationAccessor.class);
    Mockito.when(notificationManager.findAll(Mockito.any(PageRequest.class), Mockito.anyBoolean())).thenReturn(pageResponse);
    PageRequest pageRequest = PageRequest.of(currentPage, pageSize, Sort.unsorted());
    Mockito.when(notificationManager.getPageRequestForNotifications(Mockito.anyInt(), Mockito.anyInt(), Mockito.any(), Mockito.any())).thenReturn(pageRequest);
    NotificationContentRepository notificationRepository = Mockito.mock(NotificationContentRepository.class);
    AuditNotificationRepository auditNotificationRepository = Mockito.mock(AuditNotificationRepository.class);
    JobAccessor jobAccessor = Mockito.mock(JobAccessor.class);
    Mockito.when(jobAccessor.getJobById(Mockito.any())).thenReturn(null);
    ConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = Mockito.mock(ConfigurationModelConfigurationAccessor.class);
    NotificationEntity notificationContent = new MockNotificationContent(DateUtils.createCurrentDateTimestamp(), "provider", DateUtils.createCurrentDateTimestamp(), "notificationType", "{content: \"content is here...\"}", 1L, 1L).createEntity();
    ContentConverter contentConverter = new ContentConverter(new DefaultConversionService());
    DistributionJobModel distributionJob = DistributionJobModel.builder().jobId(UUID.randomUUID()).enabled(true).blackDuckGlobalConfigId(2L).channelDescriptorName("distributionType").name("name").createdAt(OffsetDateTime.now()).distributionFrequency(FrequencyType.REAL_TIME).filterByProject(false).notificationTypes(List.of("type")).processingType(ProcessingType.DEFAULT).build();
    Mockito.doReturn(Optional.of(distributionJob)).when(jobAccessor).getJobById(Mockito.any());
    Mockito.when(notificationRepository.findAllById(Mockito.anyList())).thenReturn(Collections.singletonList(notificationContent));
    DefaultRestApiAuditAccessor auditEntryUtility = new DefaultRestApiAuditAccessor(auditEntryRepository, auditNotificationRepository, jobAccessor, configurationModelConfigurationAccessor, notificationManager, contentConverter);
    AuditEntryActions auditEntryActions = new AuditEntryActions(authorizationManager, auditDescriptorKey, auditEntryUtility, notificationManager, jobAccessor, null, null);
    ActionResponse<AuditEntryPageModel> response = auditEntryActions.get(currentPage, pageSize, null, null, null, true);
    assertTrue(response.hasContent());
    AuditEntryPageModel restModel = response.getContent().orElse(null);
    assertEquals(pageResponse.getTotalPages(), restModel.getTotalPages());
    assertEquals(pageResponse.getNumber(), restModel.getCurrentPage());
    assertEquals(pageResponse.getSize(), restModel.getPageSize());
    for (int index = 0; index < pageSize; index++) {
        AlertNotificationModel entity = pageResponse.getContent().get(index);
        AuditEntryModel entryRestModel = restModel.getContent().get(index);
        assertEquals(String.valueOf(entity.getId()), entryRestModel.getId());
    }
}
Also used : AlertNotificationModel(com.synopsys.integration.alert.common.rest.model.AlertNotificationModel) NotificationContentRepository(com.synopsys.integration.alert.database.notification.NotificationContentRepository) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) AuditDescriptorKey(com.synopsys.integration.alert.component.audit.AuditDescriptorKey) AuditDescriptorKey(com.synopsys.integration.alert.component.audit.AuditDescriptorKey) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) PageRequest(org.springframework.data.domain.PageRequest) JobAccessor(com.synopsys.integration.alert.common.persistence.accessor.JobAccessor) AuditNotificationRepository(com.synopsys.integration.alert.database.audit.AuditNotificationRepository) DefaultRestApiAuditAccessor(com.synopsys.integration.alert.database.api.DefaultRestApiAuditAccessor) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) NotificationEntity(com.synopsys.integration.alert.database.notification.NotificationEntity) AuditEntryModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryModel) MockNotificationContent(com.synopsys.integration.alert.mock.entity.MockNotificationContent) DefaultNotificationAccessor(com.synopsys.integration.alert.database.api.DefaultNotificationAccessor) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor) AuditEntryPageModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryPageModel) ContentConverter(com.synopsys.integration.alert.common.ContentConverter) AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) OffsetDateTime(java.time.OffsetDateTime) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) Test(org.junit.jupiter.api.Test)

Example 33 with DistributionJobModel

use of com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel in project hub-alert by blackducksoftware.

the class AuditEntryActionsTest method testPagedRequestEmptyList.

@Test
public void testPagedRequestEmptyList() {
    int totalPages = 1;
    int currentPage = 1;
    int pageSize = 1;
    Page<AlertNotificationModel> pageResponse = Mockito.mock(Page.class);
    Mockito.when(pageResponse.getContent()).thenReturn(Collections.emptyList());
    Mockito.when(pageResponse.getTotalPages()).thenReturn(totalPages);
    Mockito.when(pageResponse.getNumber()).thenReturn(currentPage);
    Mockito.when(pageResponse.getSize()).thenReturn(0);
    AuthorizationManager authorizationManager = Mockito.mock(AuthorizationManager.class);
    Mockito.when(authorizationManager.hasReadPermission(Mockito.any(ConfigContextEnum.class), Mockito.any(DescriptorKey.class))).thenReturn(Boolean.TRUE);
    AuditDescriptorKey auditDescriptorKey = new AuditDescriptorKey();
    AuditEntryRepository auditEntryRepository = Mockito.mock(AuditEntryRepository.class);
    DefaultNotificationAccessor notificationManager = Mockito.mock(DefaultNotificationAccessor.class);
    Mockito.when(notificationManager.findAll(Mockito.any(PageRequest.class), Mockito.anyBoolean())).thenReturn(pageResponse);
    PageRequest pageRequest = PageRequest.of(currentPage, pageSize, Sort.unsorted());
    Mockito.when(notificationManager.getPageRequestForNotifications(Mockito.anyInt(), Mockito.anyInt(), Mockito.any(), Mockito.any())).thenReturn(pageRequest);
    NotificationContentRepository notificationRepository = Mockito.mock(NotificationContentRepository.class);
    AuditNotificationRepository auditNotificationRepository = Mockito.mock(AuditNotificationRepository.class);
    JobAccessor jobAccessor = Mockito.mock(JobAccessor.class);
    ConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = Mockito.mock(ConfigurationModelConfigurationAccessor.class);
    ContentConverter contentConverter = new ContentConverter(new DefaultConversionService());
    NotificationEntity notificationContent = new MockNotificationContent(DateUtils.createCurrentDateTimestamp(), "provider", DateUtils.createCurrentDateTimestamp(), "notificationType", "{content: \"content is here...\"}", 1L, 1L).createEntity();
    DistributionJobModel distributionJob = DistributionJobModel.builder().jobId(UUID.randomUUID()).enabled(true).blackDuckGlobalConfigId(2L).channelDescriptorName("distributionType").name("name").createdAt(OffsetDateTime.now()).distributionFrequency(FrequencyType.REAL_TIME).filterByProject(false).notificationTypes(List.of("type")).processingType(ProcessingType.DEFAULT).build();
    Mockito.doReturn(Optional.of(distributionJob)).when(jobAccessor).getJobById(Mockito.any());
    Mockito.when(notificationRepository.findAllById(Mockito.anyList())).thenReturn(Collections.singletonList(notificationContent));
    DefaultRestApiAuditAccessor auditEntryUtility = new DefaultRestApiAuditAccessor(auditEntryRepository, auditNotificationRepository, jobAccessor, configurationModelConfigurationAccessor, notificationManager, contentConverter);
    AuditEntryActions auditEntryActions = new AuditEntryActions(authorizationManager, auditDescriptorKey, auditEntryUtility, notificationManager, jobAccessor, null, null);
    ActionResponse<AuditEntryPageModel> response = auditEntryActions.get(currentPage, pageSize, null, null, null, true);
    assertTrue(response.hasContent());
    AuditEntryPageModel restModel = response.getContent().orElse(null);
    assertEquals(pageResponse.getTotalPages(), restModel.getTotalPages());
    assertEquals(pageResponse.getNumber(), restModel.getCurrentPage());
    // Assert 0 because there aren't any entries in the pageResponse content
    assertEquals(0, restModel.getPageSize());
    assertTrue(restModel.getContent().isEmpty());
}
Also used : AlertNotificationModel(com.synopsys.integration.alert.common.rest.model.AlertNotificationModel) MockNotificationContent(com.synopsys.integration.alert.mock.entity.MockNotificationContent) DefaultNotificationAccessor(com.synopsys.integration.alert.database.api.DefaultNotificationAccessor) NotificationContentRepository(com.synopsys.integration.alert.database.notification.NotificationContentRepository) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) AuditDescriptorKey(com.synopsys.integration.alert.component.audit.AuditDescriptorKey) AuditDescriptorKey(com.synopsys.integration.alert.component.audit.AuditDescriptorKey) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) AuditEntryPageModel(com.synopsys.integration.alert.common.persistence.model.AuditEntryPageModel) ContentConverter(com.synopsys.integration.alert.common.ContentConverter) AuditEntryRepository(com.synopsys.integration.alert.database.audit.AuditEntryRepository) PageRequest(org.springframework.data.domain.PageRequest) JobAccessor(com.synopsys.integration.alert.common.persistence.accessor.JobAccessor) AuditNotificationRepository(com.synopsys.integration.alert.database.audit.AuditNotificationRepository) DefaultRestApiAuditAccessor(com.synopsys.integration.alert.database.api.DefaultRestApiAuditAccessor) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) NotificationEntity(com.synopsys.integration.alert.database.notification.NotificationEntity) Test(org.junit.jupiter.api.Test)

Example 34 with DistributionJobModel

use of com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel in project hub-alert by blackducksoftware.

the class AuditEntryHandlerTestIT method resendNotificationTestIT.

@Test
public void resendNotificationTestIT() throws Exception {
    String content = ResourceUtil.getResourceAsString(getClass(), "/json/policyOverrideNotification.json", StandardCharsets.UTF_8);
    MockNotificationContent mockNotification = new MockNotificationContent(DateUtils.createCurrentDateTimestamp(), blackDuckProviderKey.getUniversalKey(), DateUtils.createCurrentDateTimestamp(), "POLICY_OVERRIDE", content, 1L, providerConfigModel.getConfigurationId());
    ConfigurationFieldModel providerConfigId = ConfigurationFieldModel.create(ProviderDescriptor.KEY_PROVIDER_CONFIG_ID);
    providerConfigId.setFieldValue(String.valueOf(providerConfigModel.getConfigurationId()));
    DistributionJobRequestModel jobRequestModel = createJobRequestModel();
    DistributionJobModel jobModel = jobAccessor.createJob(jobRequestModel);
    NotificationEntity savedNotificationEntity = notificationContentRepository.save(mockNotification.createEntity());
    AuditEntryEntity savedAuditEntryEntity = auditEntryRepository.save(new AuditEntryEntity(jobModel.getJobId(), DateUtils.createCurrentDateTimestamp(), DateUtils.createCurrentDateTimestamp(), AuditEntryStatus.SUCCESS.toString(), null, null));
    auditNotificationRepository.save(new AuditNotificationRelation(savedAuditEntryEntity.getId(), savedNotificationEntity.getId()));
    AuthorizationManager authorizationManager = Mockito.mock(AuthorizationManager.class);
    Mockito.when(authorizationManager.hasExecutePermission(Mockito.eq(ConfigContextEnum.GLOBAL.name()), Mockito.eq(AuditDescriptor.AUDIT_COMPONENT))).thenReturn(true);
    AuditEntryActions auditEntryActions = createAuditActions(authorizationManager);
    try {
        auditEntryActions.resendNotification(savedNotificationEntity.getId(), null);
        auditEntryActions.resendNotification(savedNotificationEntity.getId(), null);
        auditEntryActions.resendNotification(savedNotificationEntity.getId(), jobModel.getJobId());
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        fail("Expected the Audit POST request(s) not to throw an exception");
    }
    assertResponseStatusException(HttpStatus.GONE, () -> auditEntryActions.resendNotification(-1L, null));
    assertResponseStatusException(HttpStatus.GONE, () -> auditEntryActions.resendNotification(savedNotificationEntity.getId(), UUID.randomUUID()));
}
Also used : MockNotificationContent(com.synopsys.integration.alert.mock.entity.MockNotificationContent) ConfigurationFieldModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel) AuditNotificationRelation(com.synopsys.integration.alert.database.audit.AuditNotificationRelation) AuditEntryEntity(com.synopsys.integration.alert.database.audit.AuditEntryEntity) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) DistributionJobRequestModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobRequestModel) NotificationEntity(com.synopsys.integration.alert.database.notification.NotificationEntity) ResponseStatusException(org.springframework.web.server.ResponseStatusException) DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel) Test(org.junit.jupiter.api.Test) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest)

Example 35 with DistributionJobModel

use of com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel in project hub-alert by blackducksoftware.

the class StaticJobAccessorTestIT method createAndAssertJob.

private void createAndAssertJob(DistributionJobRequestModel jobRequestModel) {
    DistributionJobModel createdJob = staticJobAccessor.createJob(jobRequestModel);
    assertNotNull(createdJob);
    Optional<DistributionJobModel> retrievedJob = staticJobAccessor.getJobById(createdJob.getJobId());
    assertTrue(retrievedJob.isPresent());
    DistributionJobModel job = retrievedJob.get();
    assertNotNull(job.getJobId());
    createdJobIds.add(job.getJobId());
    assertNotNull(job.getChannelGlobalConfigId());
    assertEquals(jobRequestModel.getChannelGlobalConfigId(), job.getChannelGlobalConfigId());
}
Also used : DistributionJobModel(com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel)

Aggregations

DistributionJobModel (com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel)52 Test (org.junit.jupiter.api.Test)35 UUID (java.util.UUID)24 DistributionJobRequestModel (com.synopsys.integration.alert.common.persistence.model.job.DistributionJobRequestModel)20 DistributionJobEntity (com.synopsys.integration.alert.database.job.DistributionJobEntity)15 BlackDuckJobDetailsEntity (com.synopsys.integration.alert.database.job.blackduck.BlackDuckJobDetailsEntity)14 JobFieldModel (com.synopsys.integration.alert.common.rest.model.JobFieldModel)10 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)9 JobAccessor (com.synopsys.integration.alert.common.persistence.accessor.JobAccessor)9 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)8 ConfigurationFieldModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel)8 AlertPagedModel (com.synopsys.integration.alert.common.rest.model.AlertPagedModel)8 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)8 AuditEntryEntity (com.synopsys.integration.alert.database.audit.AuditEntryEntity)8 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)7 ConfigurationModelConfigurationAccessor (com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor)7 NotificationEntity (com.synopsys.integration.alert.database.notification.NotificationEntity)7 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)7 OffsetDateTime (java.time.OffsetDateTime)7 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)6