Search in sources :

Example 1 with ProjectMetaData

use of com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData in project sechub by mercedes-benz.

the class ProjectAdministrationRestControllerMockTest method when_validator_marks_no_errors___calling_create_project_url_calls_create_service_and_returns_http_200.

@Test
public void when_validator_marks_no_errors___calling_create_project_url_calls_create_service_and_returns_http_200() throws Exception {
    /* execute + test @formatter:off */
    this.mockMvc.perform(post(https(PORT_USED).buildAdminCreatesProjectUrl()).contentType(MediaType.APPLICATION_JSON_VALUE).content("{\"name\":\"projectId1\",\"description\":\"description1\",\"owner\":\"ownerName1\",\"whiteList\":{\"uris\":[\"192.168.1.1\",\"192.168.1.2\"]}}")).andExpect(status().isCreated());
    verify(creationService).createProject("projectId1", "description1", "ownerName1", new LinkedHashSet<>(Arrays.asList(new URI("192.168.1.1"), new URI("192.168.1.2"))), new ProjectMetaData());
/* @formatter:on */
}
Also used : ProjectMetaData(com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData) URI(java.net.URI) Test(org.junit.Test) WebMvcTest(org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)

Example 2 with ProjectMetaData

use of com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData in project sechub by mercedes-benz.

the class ProjectAdministrationRestController method createProject.

/* @formatter:off */
@UseCaseAdminCreatesProject(@Step(number = 1, name = "Rest call", needsRestDoc = true, description = "Administrator creates a new project by calling rest api"))
@RequestMapping(path = AdministrationAPIConstants.API_CREATE_PROJECT, method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
@ResponseStatus(HttpStatus.CREATED)
public void createProject(@RequestBody @Valid ProjectJsonInput input) {
    Set<URI> whiteListedURIs = new LinkedHashSet<>();
    Optional<ProjectWhiteList> whitelistOption = input.getWhiteList();
    if (whitelistOption.isPresent()) {
        ProjectWhiteList whiteList = whitelistOption.get();
        whiteListedURIs.addAll(whiteList.getUris());
    }
    ProjectMetaData metaData = new ProjectMetaData();
    if (input.getMetaData().isPresent()) {
        metaData = input.getMetaData().get();
    }
    /* @formatter:on */
    creationService.createProject(input.getName(), input.getDescription(), input.getOwner(), whiteListedURIs, metaData);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ProjectMetaData(com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData) ProjectWhiteList(com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectWhiteList) URI(java.net.URI) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ProjectMetaData

use of com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData in project sechub by mercedes-benz.

the class ProjectCreationService method createProject.

@Validated
/* @formatter:off */
@UseCaseAdminCreatesProject(@Step(number = 2, name = "Create project", description = "The service will create the project when not already existing with such name."))
public /* @formatter:on */
void createProject(@NotNull String projectId, @NotNull String description, @NotNull String owner, @NotNull Set<URI> whitelist, @NotNull ProjectMetaData metaData) {
    LOG.info("Administrator {} triggers create of project:{}, having owner:{}", userContext.getUserId(), projectId, owner);
    assertion.assertIsValidProjectId(projectId);
    assertion.assertIsValidUserId(owner);
    assertion.assertIsValidProjectDescription(description);
    /* assert found */
    Optional<Project> foundProject = projectRepository.findById(projectId);
    if (foundProject.isPresent()) {
        throw new AlreadyExistsException("Project '" + projectId + "' already exists");
    }
    Optional<User> foundOwner = userRepository.findById(owner);
    if (!foundOwner.isPresent()) {
        throw new NotFoundException("Owner '" + owner + "' not found");
    }
    /* setup */
    Project project = new Project();
    project.id = projectId;
    project.description = description;
    User ownerUser = foundOwner.get();
    project.owner = ownerUser;
    /**
     * add only accepted/valid URIs - sanitize
     */
    whitelist.stream().filter(uri -> uriValidation.validate(uri).isValid()).forEach(project.getWhiteList()::add);
    List<ProjectMetaDataEntity> metaDataEntities = metaData.getMetaDataMap().entrySet().stream().map(entry -> new ProjectMetaDataEntity(projectId, entry.getKey(), entry.getValue())).collect(Collectors.toList());
    project.metaData.addAll(metaDataEntities);
    /* store */
    persistenceService.saveInOwnTransaction(project);
    sendProjectCreatedEvent(projectId, whitelist);
    sendRefreshUserAuth(ownerUser);
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) User(com.mercedesbenz.sechub.domain.administration.user.User) MessageID(com.mercedesbenz.sechub.sharedkernel.messaging.MessageID) ProjectMessage(com.mercedesbenz.sechub.sharedkernel.messaging.ProjectMessage) DomainMessage(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessage) Service(org.springframework.stereotype.Service) NotFoundException(com.mercedesbenz.sechub.sharedkernel.error.NotFoundException) Step(com.mercedesbenz.sechub.sharedkernel.Step) URI(java.net.URI) AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) MessageDataKeys(com.mercedesbenz.sechub.sharedkernel.messaging.MessageDataKeys) UserRepository(com.mercedesbenz.sechub.domain.administration.user.UserRepository) DomainMessageFactory(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageFactory) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject) UserInputAssertion(com.mercedesbenz.sechub.sharedkernel.validation.UserInputAssertion) Validated(org.springframework.validation.annotation.Validated) Logger(org.slf4j.Logger) UserContextService(com.mercedesbenz.sechub.sharedkernel.UserContextService) Set(java.util.Set) URIValidation(com.mercedesbenz.sechub.sharedkernel.validation.URIValidation) RoleConstants(com.mercedesbenz.sechub.sharedkernel.RoleConstants) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) IsSendingAsyncMessage(com.mercedesbenz.sechub.sharedkernel.messaging.IsSendingAsyncMessage) List(java.util.List) DomainMessageService(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageService) ProjectMetaData(com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData) Optional(java.util.Optional) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject) User(com.mercedesbenz.sechub.domain.administration.user.User) AlreadyExistsException(com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException) NotFoundException(com.mercedesbenz.sechub.sharedkernel.error.NotFoundException) Validated(org.springframework.validation.annotation.Validated) UseCaseAdminCreatesProject(com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject)

Example 4 with ProjectMetaData

use of com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData in project sechub by mercedes-benz.

the class ProjectJsonInputValidationTest method when_metadatavalidation_with_too_long_value_invalid_api_error.

@Test
public void when_metadatavalidation_with_too_long_value_invalid_api_error() {
    /* prepare */
    String value = TestUtil.createRAndomString(260);
    ProjectMetaData metaData = new ProjectMetaData();
    metaData.getMetaDataMap().put("key", value);
    when(input.getMetaData()).thenReturn(Optional.of(metaData));
    when(metaDataValidation.validate(any())).thenReturn(failedResult);
    /* execute */
    toTest.checkWhitelist(errors, input);
    /* test */
    verify(errors, never()).rejectValue(eq(ProjectJsonInput.PROPERTY_METADATA), eq("api.error.metadata.invalid"), any());
}
Also used : ProjectMetaData(com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData) Test(org.junit.Test)

Example 5 with ProjectMetaData

use of com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData in project sechub by mercedes-benz.

the class ProjectJsonInputValidationTest method when_metadatavalidation_with_too_long_key_invalid_api_error.

@Test
public void when_metadatavalidation_with_too_long_key_invalid_api_error() {
    /* prepare */
    String key = TestUtil.createRAndomString(61);
    ProjectMetaData metaData = new ProjectMetaData();
    metaData.getMetaDataMap().put(key, "value");
    when(input.getMetaData()).thenReturn(Optional.of(metaData));
    when(metaDataValidation.validate(any())).thenReturn(failedResult);
    /* execute */
    toTest.checkWhitelist(errors, input);
    /* test */
    verify(errors, never()).rejectValue(eq(ProjectJsonInput.PROPERTY_METADATA), eq("api.error.metadata.invalid"), any());
}
Also used : ProjectMetaData(com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData) Test(org.junit.Test)

Aggregations

ProjectMetaData (com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectMetaData)9 Test (org.junit.Test)6 URI (java.net.URI)3 UseCaseAdminCreatesProject (com.mercedesbenz.sechub.sharedkernel.usecases.admin.project.UseCaseAdminCreatesProject)2 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ProjectWhiteList (com.mercedesbenz.sechub.domain.administration.project.ProjectJsonInput.ProjectWhiteList)1 User (com.mercedesbenz.sechub.domain.administration.user.User)1 UserRepository (com.mercedesbenz.sechub.domain.administration.user.UserRepository)1 RoleConstants (com.mercedesbenz.sechub.sharedkernel.RoleConstants)1 Step (com.mercedesbenz.sechub.sharedkernel.Step)1 UserContextService (com.mercedesbenz.sechub.sharedkernel.UserContextService)1 AlreadyExistsException (com.mercedesbenz.sechub.sharedkernel.error.AlreadyExistsException)1 NotFoundException (com.mercedesbenz.sechub.sharedkernel.error.NotFoundException)1 DomainMessage (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessage)1 DomainMessageFactory (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageFactory)1 DomainMessageService (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageService)1 IsSendingAsyncMessage (com.mercedesbenz.sechub.sharedkernel.messaging.IsSendingAsyncMessage)1 MessageDataKeys (com.mercedesbenz.sechub.sharedkernel.messaging.MessageDataKeys)1 MessageID (com.mercedesbenz.sechub.sharedkernel.messaging.MessageID)1