Commit c5ee464e authored by Adrien's avatar Adrien Committed by Romain Bioteau
Browse files

feat(app) improve token validation (#383)

The content of the applicationWorkingCopy is now took into account,
uniqueness is ensured even if the user didn't saved before to change the
token.

closes[BS-16371](https://bonitasoft.atlassian.net/browse/BS-16371)
parent 712eb1c2
......@@ -9,6 +9,7 @@ import java.util.Arrays;
import java.util.List;
import org.bonitasoft.engine.business.application.xml.ApplicationNode;
import org.bonitasoft.engine.business.application.xml.ApplicationNodeBuilder;
import org.bonitasoft.engine.business.application.xml.ApplicationNodeContainer;
import org.bonitasoft.studio.common.repository.RepositoryAccessor;
import org.bonitasoft.studio.la.repository.ApplicationFileStore;
......@@ -20,7 +21,9 @@ public class ApplicationTokenUnicityValidatorTest {
@Test
public void should_validate_uniqueness_without_current_token() throws Exception {
final RepositoryAccessor repositoryAccessor = initRepositoryAccessor();
final ApplicationTokenUnicityValidator validator = new ApplicationTokenUnicityValidator(repositoryAccessor);
ApplicationNodeContainer nodeContainer = new ApplicationNodeContainer();
final ApplicationTokenUnicityValidator validator = new ApplicationTokenUnicityValidator(repositoryAccessor,
nodeContainer, "filename.xml");
assertThat(validator.validate("token1")).isNotOK();
assertThat(validator.validate("token2")).isNotOK();
......@@ -30,8 +33,9 @@ public class ApplicationTokenUnicityValidatorTest {
@Test
public void should_validate_uniqueness_with_current_token() throws Exception {
final RepositoryAccessor repositoryAccessor = initRepositoryAccessor();
ApplicationNodeContainer nodeContainer = new ApplicationNodeContainer();
final ApplicationTokenUnicityValidator validator = new ApplicationTokenUnicityValidator(repositoryAccessor,
"token2");
nodeContainer, "filename.xml", "token2");
assertThat(validator.validate("token1")).isNotOK();
assertThat(validator.validate("token2")).isOK();
......@@ -41,12 +45,32 @@ public class ApplicationTokenUnicityValidatorTest {
@Test
public void should_validation_fails_with_current_token() throws Exception {
final RepositoryAccessor repositoryAccessor = initRepositoryAccessor();
ApplicationNodeContainer nodeContainer = new ApplicationNodeContainer();
final ApplicationTokenUnicityValidator validator = new ApplicationTokenUnicityValidator(repositoryAccessor,
"token4");
nodeContainer, "filename.xml", "token4");
assertThat(validator.validate("token4")).isNotOK();
}
@Test
public void sould_take_into_account_working_copy() throws Exception {
final RepositoryAccessor repositoryAccessor = initRepositoryAccessor();
ApplicationNodeContainer workingCopy = new ApplicationNodeContainer();
workingCopy.addApplication(ApplicationNodeBuilder.newApplication("workingcpy_token1", "name1", "1.0").create());
workingCopy.addApplication(ApplicationNodeBuilder.newApplication("workingcpy_token2", "name2", "1.0").create());
workingCopy.addApplication(ApplicationNodeBuilder.newApplication("token1", "name3", "1.0").create());
ApplicationTokenUnicityValidator validator = new ApplicationTokenUnicityValidator(repositoryAccessor,
workingCopy, "filename.xml", "workingcpy_token2");
assertThat(validator.validate("token1")).isNotOK();
assertThat(validator.validate("workingcpy_token1")).isNotOK();
assertThat(validator.validate("workingcpy_token2")).isOK();
validator = new ApplicationTokenUnicityValidator(repositoryAccessor,
workingCopy, "myApp.xml");
assertThat(validator.validate("token2")).isOK();
}
private RepositoryAccessor initRepositoryAccessor() throws Exception {
final List<ApplicationNode> applications = Arrays.asList(
createAppWithToken("token1"),
......@@ -59,6 +83,7 @@ public class ApplicationTokenUnicityValidatorTest {
final ApplicationFileStore applicationFileStore = mock(ApplicationFileStore.class);
when(applicationFileStore.getContent()).thenReturn(applicationNodeContainer);
when(applicationFileStore.getName()).thenReturn("myApp.xml");
final List<ApplicationFileStore> applicationFileStores = new ArrayList<>();
applicationFileStores.add(applicationFileStore);
......
......@@ -20,6 +20,7 @@ import java.util.Optional;
import java.util.stream.Collectors;
import org.bonitasoft.engine.business.application.xml.ApplicationNode;
import org.bonitasoft.engine.business.application.xml.ApplicationNodeContainer;
import org.bonitasoft.studio.common.jface.databinding.validator.UniqueValidator;
import org.bonitasoft.studio.common.log.BonitaStudioLog;
import org.bonitasoft.studio.common.repository.RepositoryAccessor;
......@@ -33,33 +34,47 @@ public class ApplicationTokenUnicityValidator extends UniqueValidator {
public static class Builder implements ValidatorBuilder<UniqueValidator> {
private final RepositoryAccessor repositoryAccessor;
private ApplicationNodeContainer applicationWorkingCopy;
private String filename;
public Builder(RepositoryAccessor repositoryAccessor) {
public Builder(RepositoryAccessor repositoryAccessor, ApplicationNodeContainer applicationWorkingCopy,
String filename) {
Objects.requireNonNull(repositoryAccessor);
Objects.requireNonNull(applicationWorkingCopy);
this.repositoryAccessor = repositoryAccessor;
this.applicationWorkingCopy = applicationWorkingCopy;
this.filename = filename;
}
@Override
public ApplicationTokenUnicityValidator create() {
return new ApplicationTokenUnicityValidator(repositoryAccessor);
return new ApplicationTokenUnicityValidator(repositoryAccessor, applicationWorkingCopy, filename);
}
}
private final RepositoryAccessor repositoryAccessor;
private Optional<String> currentToken;
private ApplicationNodeContainer applicationWorkingCopy;
private String filename;
public ApplicationTokenUnicityValidator(RepositoryAccessor repositoryAccessor) {
this(repositoryAccessor, null);
public ApplicationTokenUnicityValidator(RepositoryAccessor repositoryAccessor,
ApplicationNodeContainer applicationWorkingCopy, String fileName) {
this(repositoryAccessor, applicationWorkingCopy, fileName, null);
}
public ApplicationTokenUnicityValidator(RepositoryAccessor repositoryAccessor, String currentToken) {
public ApplicationTokenUnicityValidator(RepositoryAccessor repositoryAccessor,
ApplicationNodeContainer applicationWorkingCopy, String filename, String currentToken) {
this.repositoryAccessor = repositoryAccessor;
this.applicationWorkingCopy = applicationWorkingCopy;
this.currentToken = Optional.ofNullable(currentToken);
this.filename = filename;
}
public List<String> getTokenList() {
final List<String> allTokens = repositoryAccessor.getRepositoryStore(ApplicationRepositoryStore.class).getChildren()
.stream()
.filter(fStore -> !Objects.equals(fStore.getName(), filename))
.map(fStore -> {
try {
return fStore.getContent();
......@@ -73,6 +88,11 @@ public class ApplicationTokenUnicityValidator extends UniqueValidator {
.flatMap(container -> container.getApplications().stream())
.map(ApplicationNode::getToken)
.collect(Collectors.toList());
applicationWorkingCopy.getApplications().stream()
.filter(application -> !Objects.equals(currentToken.orElse(""), application.getToken()))
.map(ApplicationNode::getToken)
.forEach(allTokens::add);
currentToken.ifPresent(allTokens::remove);
return allTokens;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment