Skip to content

Commit

Permalink
#39 #72 Correction of integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoraboeuf committed Sep 21, 2014
1 parent e1da44d commit f1d287b
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 161 deletions.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Sep 21 13:03:28 CEST 2014
#Sun Sep 21 13:09:15 CEST 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-all.zip
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
package net.nemerosa.ontrack.it;

import net.nemerosa.ontrack.common.RunProfile;
import net.nemerosa.ontrack.model.security.*;
import net.nemerosa.ontrack.model.structure.*;
import net.nemerosa.ontrack.model.structure.NameDescription;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.Callable;

import static net.nemerosa.ontrack.test.TestUtils.uid;

@RunWith(SpringJUnit4ClassRunner.class)
Expand All @@ -33,12 +23,6 @@
@ActiveProfiles(profiles = {RunProfile.UNIT_TEST})
public abstract class AbstractITTestSupport extends AbstractJUnit4SpringContextTests {

@Autowired
protected AccountService accountService;

@Autowired
protected StructureService structureService;

@Configuration
@Profile(RunProfile.UNIT_TEST)
@ComponentScan("net.nemerosa.ontrack")
Expand All @@ -53,133 +37,4 @@ public static NameDescription nameDescription() {
);
}

protected Account doCreateAccount() throws Exception {
return asUser().with(AccountManagement.class).call(() -> {
String name = uid("A");
return accountService.create(
new AccountInput(
name,
"Test " + name,
name + "@test.com",
"test",
Collections.emptyList()
)
);
});
}

protected Project doCreateProject() throws Exception {
return doCreateProject(nameDescription());
}

protected Project doCreateProject(NameDescription nameDescription) throws Exception {
return asUser().with(ProjectCreation.class).call(() -> structureService.newProject(
Project.of(nameDescription)
));
}

protected Branch doCreateBranch() throws Exception {
return doCreateBranch(doCreateProject(), nameDescription());
}

protected Branch doCreateBranch(Project project, NameDescription nameDescription) throws Exception {
return asUser().with(project.id(), BranchCreate.class).call(() -> structureService.newBranch(
Branch.of(project, nameDescription)
));
}

protected UserCall asUser() {
return new UserCall();
}

protected AccountCall asAccount(Account account) {
return new AccountCall(account);
}

protected <T> T view(ProjectEntity projectEntity, Callable<T> callable) throws Exception {
return asUser().with(projectEntity.projectId(), ProjectView.class).call(callable);
}

protected static interface ContextCall {
<T> T call(Callable<T> call) throws Exception;
}

protected static abstract class AbstractContextCall implements ContextCall {

@Override
public <T> T call(Callable<T> call) throws Exception {
// Gets the current context
SecurityContext oldContext = SecurityContextHolder.getContext();
try {
// Sets the new context
contextSetup();
// Call
return call.call();
} finally {
// Restores the context
SecurityContextHolder.setContext(oldContext);
}
}

protected abstract void contextSetup();
}

protected static class AccountCall extends AbstractContextCall {

protected final Account account;

public AccountCall(Account account) {
this.account = account;
}

public AccountCall(String name, SecurityRole role) {
this(Account.of(name, name, name + "@test.com", role, AuthenticationSource.none()));
}

@Override
protected void contextSetup() {
SecurityContext context = new SecurityContextImpl();
TestingAuthenticationToken authentication = new TestingAuthenticationToken(
(AccountHolder) () -> account,
"",
account.getRole().name()
);
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
}
}

protected static class UserCall extends AccountCall {

public UserCall() {
super("user", SecurityRole.USER);
}

public UserCall with(Class<? extends GlobalFunction> fn) {
account.withGlobalRole(
Optional.of(
new GlobalRole(
"test", "Test global role", "",
Collections.singleton(fn),
Collections.emptySet()
)
)
);
return this;
}

public UserCall with(int projectId, Class<? extends ProjectFunction> fn) {
account.withProjectRole(
new ProjectRoleAssociation(
projectId,
new ProjectRole(
"test", "Test", "",
Collections.singleton(fn)
)
)
);
return this;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package net.nemerosa.ontrack.it;

import net.nemerosa.ontrack.model.security.*;
import net.nemerosa.ontrack.model.structure.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;

import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.Callable;

import static net.nemerosa.ontrack.test.TestUtils.uid;

public abstract class AbstractServiceTestSupport extends AbstractITTestSupport {

@Autowired
protected AccountService accountService;

@Autowired
protected StructureService structureService;

protected Account doCreateAccount() throws Exception {
return asUser().with(AccountManagement.class).call(() -> {
String name = uid("A");
return accountService.create(
new AccountInput(
name,
"Test " + name,
name + "@test.com",
"test",
Collections.emptyList()
)
);
});
}

protected Project doCreateProject() throws Exception {
return doCreateProject(nameDescription());
}

protected Project doCreateProject(NameDescription nameDescription) throws Exception {
return asUser().with(ProjectCreation.class).call(() -> structureService.newProject(
Project.of(nameDescription)
));
}

protected Branch doCreateBranch() throws Exception {
return doCreateBranch(doCreateProject(), nameDescription());
}

protected Branch doCreateBranch(Project project, NameDescription nameDescription) throws Exception {
return asUser().with(project.id(), BranchCreate.class).call(() -> structureService.newBranch(
Branch.of(project, nameDescription)
));
}

protected UserCall asUser() {
return new UserCall();
}

protected AccountCall asAccount(Account account) {
return new AccountCall(account);
}

protected <T> T view(ProjectEntity projectEntity, Callable<T> callable) throws Exception {
return asUser().with(projectEntity.projectId(), ProjectView.class).call(callable);
}

protected static interface ContextCall {
<T> T call(Callable<T> call) throws Exception;
}

protected static abstract class AbstractContextCall implements ContextCall {

@Override
public <T> T call(Callable<T> call) throws Exception {
// Gets the current context
SecurityContext oldContext = SecurityContextHolder.getContext();
try {
// Sets the new context
contextSetup();
// Call
return call.call();
} finally {
// Restores the context
SecurityContextHolder.setContext(oldContext);
}
}

protected abstract void contextSetup();
}

protected static class AccountCall extends AbstractContextCall {

protected final Account account;

public AccountCall(Account account) {
this.account = account;
}

public AccountCall(String name, SecurityRole role) {
this(Account.of(name, name, name + "@test.com", role, AuthenticationSource.none()));
}

@Override
protected void contextSetup() {
SecurityContext context = new SecurityContextImpl();
TestingAuthenticationToken authentication = new TestingAuthenticationToken(
(AccountHolder) () -> account,
"",
account.getRole().name()
);
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
}
}

protected static class UserCall extends AccountCall {

public UserCall() {
super("user", SecurityRole.USER);
}

public UserCall with(Class<? extends GlobalFunction> fn) {
account.withGlobalRole(
Optional.of(
new GlobalRole(
"test", "Test global role", "",
Collections.singleton(fn),
Collections.emptySet()
)
)
);
return this;
}

public UserCall with(int projectId, Class<? extends ProjectFunction> fn) {
account.withProjectRole(
new ProjectRoleAssociation(
projectId,
new ProjectRole(
"test", "Test", "",
Collections.singleton(fn)
)
)
);
return this;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.nemerosa.ontrack.repository;

import net.nemerosa.ontrack.it.AbstractITTestSupport;

public abstract class AbstractRepositoryTestSupport extends AbstractITTestSupport {

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.nemerosa.ontrack.repository;

import net.nemerosa.ontrack.it.AbstractITTestSupport;
import net.nemerosa.ontrack.model.structure.Branch;
import net.nemerosa.ontrack.model.structure.ID;
import net.nemerosa.ontrack.model.structure.Project;
Expand All @@ -10,7 +9,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class StructureJdbcRepositoryIT extends AbstractITTestSupport {
public class StructureJdbcRepositoryIT extends AbstractRepositoryTestSupport {

@Autowired
private StructureRepository repository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.nemerosa.ontrack.service;

import net.nemerosa.ontrack.it.AbstractITTestSupport;
import net.nemerosa.ontrack.it.AbstractServiceTestSupport;
import net.nemerosa.ontrack.json.JsonUtils;
import net.nemerosa.ontrack.model.Ack;
import net.nemerosa.ontrack.model.buildfilter.BuildFilterResource;
Expand All @@ -19,7 +20,7 @@

import static org.junit.Assert.*;

public class BuildFilterServiceIT extends AbstractITTestSupport {
public class BuildFilterServiceIT extends AbstractServiceTestSupport {

@Autowired
private BuildFilterService buildFilterService;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.nemerosa.ontrack.service.security;

import net.nemerosa.ontrack.it.AbstractITTestSupport;
import net.nemerosa.ontrack.it.AbstractServiceTestSupport;
import net.nemerosa.ontrack.model.security.*;
import net.nemerosa.ontrack.model.structure.Project;
import net.nemerosa.ontrack.model.structure.StructureService;
Expand All @@ -13,7 +13,7 @@

import static org.junit.Assert.*;

public class AccountServiceIT extends AbstractITTestSupport {
public class AccountServiceIT extends AbstractServiceTestSupport {

@Autowired
private AccountService accountService;
Expand Down
Loading

0 comments on commit f1d287b

Please sign in to comment.