Corrected expected exception checks in tests

This commit is contained in:
Kai Zimmermann
2016-03-29 13:43:18 +02:00
parent a950fa0337
commit f33a8c37e8
11 changed files with 263 additions and 99 deletions

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.security; package org.eclipse.hawkbit.security;
import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import org.junit.Test; import org.junit.Test;
@@ -20,6 +21,7 @@ import org.springframework.security.authentication.InsufficientAuthenticationExc
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken; import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import ru.yandex.qatools.allure.annotations.Description;
import ru.yandex.qatools.allure.annotations.Features; import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.allure.annotations.Stories; import ru.yandex.qatools.allure.annotations.Stories;
@@ -38,11 +40,8 @@ public class PreAuthTokenSourceTrustAuthenticationProviderTest {
@Mock @Mock
private TenantAwareWebAuthenticationDetails webAuthenticationDetailsMock; private TenantAwareWebAuthenticationDetails webAuthenticationDetailsMock;
/** @Test
* Testing in case the containing controllerId in the URI request path does @Description("Testing in case the containing controllerId in the URI request path does not accord with the controllerId in the request header.")
* not accord with the controllerId in the request header.
*/
@Test(expected = BadCredentialsException.class)
public void principalAndCredentialsNotTheSameThrowsAuthenticationException() { public void principalAndCredentialsNotTheSameThrowsAuthenticationException() {
final String principal = "controllerIdURL"; final String principal = "controllerIdURL";
final String credentials = "controllerIdHeader"; final String credentials = "controllerIdHeader";
@@ -51,15 +50,17 @@ public class PreAuthTokenSourceTrustAuthenticationProviderTest {
token.setDetails(webAuthenticationDetailsMock); token.setDetails(webAuthenticationDetailsMock);
// test, should throw authentication exception // test, should throw authentication exception
underTestWithoutSourceIpCheck.authenticate(token); try {
underTestWithoutSourceIpCheck.authenticate(token);
fail("Should not work with wrong credentials");
} catch (final BadCredentialsException e) {
}
} }
/**
* Testing that the controllerId within the URI request path is the same
* with the controllerId within the request header and no source IP check is
* in place.
*/
@Test @Test
@Description("Testing that the controllerId within the URI request path is the same with the controllerId within the request header and no source IP check is in place.")
public void principalAndCredentialsAreTheSameWithNoSourceIpCheckIsSuccessful() { public void principalAndCredentialsAreTheSameWithNoSourceIpCheckIsSuccessful() {
final String principal = "controllerId"; final String principal = "controllerId";
final String credentials = "controllerId"; final String credentials = "controllerId";
@@ -71,12 +72,8 @@ public class PreAuthTokenSourceTrustAuthenticationProviderTest {
assertThat(authenticate.isAuthenticated()).isTrue(); assertThat(authenticate.isAuthenticated()).isTrue();
} }
/** @Test
* Testing that the controllerId in the URI request match with the @Description("Testing that the controllerId in the URI request match with the controllerId in the request header but the request are not coming from a trustful source.")
* controllerId in the request header but the request are not coming from a
* trustful source.
*/
@Test(expected = InsufficientAuthenticationException.class)
public void priniciapAndCredentialsAreTheSameButSourceIpRequestNotMatching() { public void priniciapAndCredentialsAreTheSameButSourceIpRequestNotMatching() {
final String remoteAddress = "192.168.1.1"; final String remoteAddress = "192.168.1.1";
final String principal = "controllerId"; final String principal = "controllerId";
@@ -89,15 +86,16 @@ public class PreAuthTokenSourceTrustAuthenticationProviderTest {
// test, should throw authentication exception // test, should throw authentication exception
final Authentication authenticate = underTestWithSourceIpCheck.authenticate(token); final Authentication authenticate = underTestWithSourceIpCheck.authenticate(token);
assertThat(authenticate.isAuthenticated()).isTrue(); try {
assertThat(authenticate.isAuthenticated()).isTrue();
fail("as source is not trusted.");
} catch (final InsufficientAuthenticationException e) {
}
} }
/** @Test
* Testing that the controllerId in the URI request match with the @Description("Testing that the controllerId in the URI request match with the controllerId in the request header and the source Ip is matching the allowed remote IP address.")
* controllerId in the request header and the source Ip is matching the
* allowed remote IP address.
*/
@Test()
public void priniciapAndCredentialsAreTheSameAndSourceIpIsTrusted() { public void priniciapAndCredentialsAreTheSameAndSourceIpIsTrusted() {
final String principal = "controllerId"; final String principal = "controllerId";
final String credentials = "controllerId"; final String credentials = "controllerId";
@@ -112,7 +110,7 @@ public class PreAuthTokenSourceTrustAuthenticationProviderTest {
assertThat(authenticate.isAuthenticated()).isTrue(); assertThat(authenticate.isAuthenticated()).isTrue();
} }
@Test() @Test
public void priniciapAndCredentialsAreTheSameAndSourceIpIsWithinList() { public void priniciapAndCredentialsAreTheSameAndSourceIpIsWithinList() {
final String[] trustedIPAddresses = new String[] { "192.168.1.1", "192.168.1.2", REQUEST_SOURCE_IP, final String[] trustedIPAddresses = new String[] { "192.168.1.1", "192.168.1.2", REQUEST_SOURCE_IP,
"192.168.1.3" }; "192.168.1.3" };
@@ -148,6 +146,11 @@ public class PreAuthTokenSourceTrustAuthenticationProviderTest {
// test, should throw authentication exception // test, should throw authentication exception
final Authentication authenticate = underTestWithList.authenticate(token); final Authentication authenticate = underTestWithList.authenticate(token);
assertThat(authenticate.isAuthenticated()).isTrue(); try {
assertThat(authenticate.isAuthenticated()).isTrue();
fail("as source is not trusted.");
} catch (final InsufficientAuthenticationException e) {
}
} }
} }

View File

@@ -8,6 +8,8 @@
*/ */
package org.eclipse.hawkbit.repository; package org.eclipse.hawkbit.repository;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
@@ -37,7 +39,7 @@ public class ArtifactManagementNoMongoDbTest extends AbstractIntegrationTest {
System.setProperty("spring.data.mongodb.port", "1020"); System.setProperty("spring.data.mongodb.port", "1020");
} }
@Test(expected = ArtifactUploadFailedException.class) @Test
@Description("Checks if the expected ArtifactUploadFailedException is thrown in case of MongoDB down") @Description("Checks if the expected ArtifactUploadFailedException is thrown in case of MongoDB down")
public void createLocalArtifactWithMongoDbDown() throws IOException { public void createLocalArtifactWithMongoDbDown() throws IOException {
SoftwareModule sm = new SoftwareModule(softwareManagement.findSoftwareModuleTypeByKey("os"), "name 1", SoftwareModule sm = new SoftwareModule(softwareManagement.findSoftwareModuleTypeByKey("os"), "name 1",
@@ -46,7 +48,12 @@ public class ArtifactManagementNoMongoDbTest extends AbstractIntegrationTest {
final byte random[] = RandomStringUtils.random(5 * 1024).getBytes(); final byte random[] = RandomStringUtils.random(5 * 1024).getBytes();
artifactManagement.createLocalArtifact(new ByteArrayInputStream(random), sm.getId(), "file1", false); try {
artifactManagement.createLocalArtifact(new ByteArrayInputStream(random), sm.getId(), "file1", false);
fail("Should not have worked with MongoDb down.");
} catch (final ArtifactUploadFailedException e) {
}
} }
} }

View File

@@ -352,11 +352,16 @@ public class ArtifactManagementTest extends AbstractIntegrationTestWithMongoDB {
artifactManagement.loadLocalArtifactBinary(result).getFileInputStream())); artifactManagement.loadLocalArtifactBinary(result).getFileInputStream()));
} }
@Test(expected = InsufficientPermissionException.class) @Test
@WithUser(allSpPermissions = true, removeFromAllPermission = { SpPermission.DOWNLOAD_REPOSITORY_ARTIFACT }) @WithUser(allSpPermissions = true, removeFromAllPermission = { SpPermission.DOWNLOAD_REPOSITORY_ARTIFACT })
@Description("Trys and fails to load an artifact without required permission. Checks if expected InsufficientPermissionException is thrown.") @Description("Trys and fails to load an artifact without required permission. Checks if expected InsufficientPermissionException is thrown.")
public void loadLocalArtifactBinaryWithoutDownloadArtifactThrowsPermissionDenied() { public void loadLocalArtifactBinaryWithoutDownloadArtifactThrowsPermissionDenied() {
artifactManagement.loadLocalArtifactBinary(new LocalArtifact()); try {
artifactManagement.loadLocalArtifactBinary(new LocalArtifact());
fail("Should not have worked with missing permission.");
} catch (final InsufficientPermissionException e) {
}
} }
@Test @Test

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.repository; package org.eclipse.hawkbit.repository;
import static org.fest.assertions.api.Assertions.assertThat; import static org.fest.assertions.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -73,7 +74,7 @@ public class ControllerManagementTest extends AbstractIntegrationTest {
.isEqualTo(3); .isEqualTo(3);
} }
@Test(expected = ConstraintViolationException.class) @Test
@Description("Register a controller which does not exist") @Description("Register a controller which does not exist")
public void testfindOrRegisterTargetIfItDoesNotexist() { public void testfindOrRegisterTargetIfItDoesNotexist() {
final Target target = controllerManagament.findOrRegisterTargetIfItDoesNotexist("AA", null); final Target target = controllerManagament.findOrRegisterTargetIfItDoesNotexist("AA", null);
@@ -84,7 +85,12 @@ public class ControllerManagementTest extends AbstractIntegrationTest {
assertThat(targetRepository.count()).as("Only 1 target should be registred").isEqualTo(1L); assertThat(targetRepository.count()).as("Only 1 target should be registred").isEqualTo(1L);
// throws exception // throws exception
controllerManagament.findOrRegisterTargetIfItDoesNotexist("", null); try {
controllerManagament.findOrRegisterTargetIfItDoesNotexist("", null);
fail("should fail as target does not exist");
} catch (final ConstraintViolationException e) {
}
} }
@Test @Test

View File

@@ -100,7 +100,7 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
.isEqualTo("test123"); .isEqualTo("test123");
} }
@Test(expected = EntityReadOnlyException.class) @Test
@Description("Tests the unsuccessfull update of used distribution set type (module addition).") @Description("Tests the unsuccessfull update of used distribution set type (module addition).")
public void addModuleToAssignedDistributionSetTypeFails() { public void addModuleToAssignedDistributionSetTypeFails() {
final DistributionSetType nonUpdatableType = distributionSetManagement final DistributionSetType nonUpdatableType = distributionSetManagement
@@ -111,10 +111,17 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
.createDistributionSet(new DistributionSet("newtypesoft", "1", "", nonUpdatableType, null)); .createDistributionSet(new DistributionSet("newtypesoft", "1", "", nonUpdatableType, null));
nonUpdatableType.addMandatoryModuleType(osType); nonUpdatableType.addMandatoryModuleType(osType);
distributionSetManagement.updateDistributionSetType(nonUpdatableType);
try {
distributionSetManagement.updateDistributionSetType(nonUpdatableType);
fail("Should not have worked as DS is in use.");
} catch (final EntityReadOnlyException e) {
}
} }
@Test(expected = EntityReadOnlyException.class) @Test
@Description("Tests the unsuccessfull update of used distribution set type (module removal).") @Description("Tests the unsuccessfull update of used distribution set type (module removal).")
public void removeModuleToAssignedDistributionSetTypeFails() { public void removeModuleToAssignedDistributionSetTypeFails() {
DistributionSetType nonUpdatableType = distributionSetManagement DistributionSetType nonUpdatableType = distributionSetManagement
@@ -128,7 +135,12 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
.createDistributionSet(new DistributionSet("newtypesoft", "1", "", nonUpdatableType, null)); .createDistributionSet(new DistributionSet("newtypesoft", "1", "", nonUpdatableType, null));
nonUpdatableType.removeModuleType(osType.getId()); nonUpdatableType.removeModuleType(osType.getId());
nonUpdatableType = distributionSetManagement.updateDistributionSetType(nonUpdatableType); try {
distributionSetManagement.updateDistributionSetType(nonUpdatableType);
fail("Should not have worked as DS is in use.");
} catch (final EntityReadOnlyException e) {
}
} }
@Test @Test
@@ -156,13 +168,17 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
assertThat(distributionSetManagement.findDistributionSetTypeByKey("softdeleted").isDeleted()).isEqualTo(true); assertThat(distributionSetManagement.findDistributionSetTypeByKey("softdeleted").isDeleted()).isEqualTo(true);
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Ensures that it is not possible to create a DS that already exists (unique constraint is on name,version for DS).") @Description("Ensures that it is not possible to create a DS that already exists (unique constraint is on name,version for DS).")
public void createDuplicateDistributionSetsFailsWithException() { public void createDuplicateDistributionSetsFailsWithException() {
TestDataUtil.generateDistributionSet("a", softwareManagement, distributionSetManagement); TestDataUtil.generateDistributionSet("a", softwareManagement, distributionSetManagement);
TestDataUtil.generateDistributionSet("a", softwareManagement, distributionSetManagement); try {
TestDataUtil.generateDistributionSet("a", softwareManagement, distributionSetManagement);
fail("Should not have worked as DS with same UK already exists.");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test @Test
@@ -196,13 +212,18 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Verfies that a DS entity cannot be used for creation.") @Description("Verfies that a DS entity cannot be used for creation.")
public void createDistributionSetFailsOnExistingEntity() { public void createDistributionSetFailsOnExistingEntity() {
final DistributionSet set = distributionSetManagement final DistributionSet set = distributionSetManagement
.createDistributionSet(new DistributionSet("newtypesoft", "1", "", null, null)); .createDistributionSet(new DistributionSet("newtypesoft", "1", "", null, null));
distributionSetManagement.createDistributionSet(set);
try {
distributionSetManagement.createDistributionSet(set);
fail("Should not have worked to create based on a persisted entity.");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test @Test
@@ -279,17 +300,22 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
} }
} }
@Test(expected = DistributionSetTypeUndefinedException.class) @Test
@Description("Ensures that it is not possible to add a software module to a set that has no type defined.") @Description("Ensures that it is not possible to add a software module to a set that has no type defined.")
public void updateDistributionSetModuleWithUndefinedTypeFails() { public void updateDistributionSetModuleWithUndefinedTypeFails() {
final DistributionSet testSet = new DistributionSet(); final DistributionSet testSet = new DistributionSet();
final SoftwareModule module = new SoftwareModule(appType, "agent-hub2", "1.0.5", null, ""); final SoftwareModule module = new SoftwareModule(appType, "agent-hub2", "1.0.5", null, "");
// update data // update data
testSet.addModule(module); try {
testSet.addModule(module);
fail("Should not have worked as DS type is undefined.");
} catch (final DistributionSetTypeUndefinedException e) {
}
} }
@Test(expected = UnsupportedSoftwareModuleForThisDistributionSetException.class) @Test
@Description("Ensures that it is not possible to add a software module that is not defined of the DS's type.") @Description("Ensures that it is not possible to add a software module that is not defined of the DS's type.")
public void updateDistributionSetUnsupportedModuleFails() { public void updateDistributionSetUnsupportedModuleFails() {
final DistributionSet set = new DistributionSet("agent-hub2", "1.0.5", "desc", final DistributionSet set = new DistributionSet("agent-hub2", "1.0.5", "desc",
@@ -297,7 +323,12 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
final SoftwareModule module = new SoftwareModule(appType, "agent-hub2", "1.0.5", null, ""); final SoftwareModule module = new SoftwareModule(appType, "agent-hub2", "1.0.5", null, "");
// update data // update data
set.addModule(module); try {
set.addModule(module);
fail("Should not have worked as module type is not in DS type.");
} catch (final UnsupportedSoftwareModuleForThisDistributionSetException e) {
}
} }
@Test @Test
@@ -596,7 +627,7 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
.findDistributionSetsByFilters(pageReq, distributionSetFilterBuilder.build()).getContent()).hasSize(0); .findDistributionSetsByFilters(pageReq, distributionSetFilterBuilder.build()).getContent()).hasSize(0);
// combine deleted and complete and type // combine deleted and complete and type
expected = new ArrayList<DistributionSet>(); expected = new ArrayList<>();
expected.addAll(ds100Group1); expected.addAll(ds100Group1);
expected.addAll(ds100Group2); expected.addAll(ds100Group2);
distributionSetFilterBuilder = getDistributionSetFilterBuilder().setIsDeleted(Boolean.FALSE) distributionSetFilterBuilder = getDistributionSetFilterBuilder().setIsDeleted(Boolean.FALSE)
@@ -605,7 +636,7 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
.findDistributionSetsByFilters(pageReq, distributionSetFilterBuilder.build()).getContent()).hasSize(200) .findDistributionSetsByFilters(pageReq, distributionSetFilterBuilder.build()).getContent()).hasSize(200)
.containsOnly(expected.toArray(new DistributionSet[0])); .containsOnly(expected.toArray(new DistributionSet[0]));
expected = new ArrayList<DistributionSet>(); expected = new ArrayList<>();
expected.add(dsDeleted); expected.add(dsDeleted);
distributionSetFilterBuilder = getDistributionSetFilterBuilder().setIsComplete(Boolean.TRUE) distributionSetFilterBuilder = getDistributionSetFilterBuilder().setIsComplete(Boolean.TRUE)
.setType(standardDsType).setIsDeleted(Boolean.TRUE); .setType(standardDsType).setIsDeleted(Boolean.TRUE);
@@ -618,7 +649,7 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
assertThat(distributionSetManagement assertThat(distributionSetManagement
.findDistributionSetsByFilters(pageReq, distributionSetFilterBuilder.build()).getContent()).hasSize(0); .findDistributionSetsByFilters(pageReq, distributionSetFilterBuilder.build()).getContent()).hasSize(0);
expected = new ArrayList<DistributionSet>(); expected = new ArrayList<>();
expected.add(dsNewType); expected.add(dsNewType);
distributionSetFilterBuilder = getDistributionSetFilterBuilder().setIsComplete(Boolean.TRUE).setType(newType); distributionSetFilterBuilder = getDistributionSetFilterBuilder().setIsComplete(Boolean.TRUE).setType(newType);
assertThat(distributionSetManagement assertThat(distributionSetManagement
@@ -626,7 +657,7 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
.containsOnly(expected.toArray(new DistributionSet[0])); .containsOnly(expected.toArray(new DistributionSet[0]));
// combine deleted and complete and type and text // combine deleted and complete and type and text
expected = new ArrayList<DistributionSet>(); expected = new ArrayList<>();
expected.addAll(ds100Group2); expected.addAll(ds100Group2);
distributionSetFilterBuilder = getDistributionSetFilterBuilder().setIsComplete(Boolean.TRUE) distributionSetFilterBuilder = getDistributionSetFilterBuilder().setIsComplete(Boolean.TRUE)
.setType(standardDsType).setSearchText("%test2"); .setType(standardDsType).setSearchText("%test2");
@@ -650,7 +681,7 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
.findDistributionSetsByFilters(pageReq, distributionSetFilterBuilder.build()).getContent()).hasSize(0); .findDistributionSetsByFilters(pageReq, distributionSetFilterBuilder.build()).getContent()).hasSize(0);
// combine deleted and complete and type and text and tag // combine deleted and complete and type and text and tag
expected = new ArrayList<DistributionSet>(); expected = new ArrayList<>();
expected.addAll(ds100Group2); expected.addAll(ds100Group2);
distributionSetFilterBuilder = getDistributionSetFilterBuilder().setIsComplete(true).setType(standardDsType) distributionSetFilterBuilder = getDistributionSetFilterBuilder().setIsComplete(true).setType(standardDsType)
.setSearchText("%test2").setTagNames(Lists.newArrayList(dsTagA.getName())); .setSearchText("%test2").setTagNames(Lists.newArrayList(dsTagA.getName()));
@@ -672,7 +703,7 @@ public class DistributionSetManagementTest extends AbstractIntegrationTest {
private List<Target> sendUpdateActionStatusToTargets(final DistributionSet dsA, final Iterable<Target> targs, private List<Target> sendUpdateActionStatusToTargets(final DistributionSet dsA, final Iterable<Target> targs,
final Status status, final String... msgs) { final Status status, final String... msgs) {
final List<Target> result = new ArrayList<Target>(); final List<Target> result = new ArrayList<>();
for (final Target t : targs) { for (final Target t : targs) {
final List<Action> findByTarget = actionRepository.findByTarget(t); final List<Action> findByTarget = actionRepository.findByTarget(t);
for (final Action action : findByTarget) { for (final Action action : findByTarget) {

View File

@@ -10,6 +10,7 @@ package org.eclipse.hawkbit.repository;
import static org.fest.assertions.api.Assertions.assertThat; import static org.fest.assertions.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
@@ -141,38 +142,60 @@ public class SoftwareManagementTest extends AbstractIntegrationTestWithMongoDB {
assertThat(updated.getVendor()).as("Updated vendor is").isEqualTo("changed"); assertThat(updated.getVendor()).as("Updated vendor is").isEqualTo("changed");
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Create Software Module call fails when called for existing entity.") @Description("Create Software Module call fails when called for existing entity.")
public void createModuleCallFailsForExistingModule() { public void createModuleCallFailsForExistingModule() {
final SoftwareModule ah = softwareManagement final SoftwareModule ah = softwareManagement
.createSoftwareModule(new SoftwareModule(appType, "agent-hub", "1.0.1", "test desc", "test vendor")); .createSoftwareModule(new SoftwareModule(appType, "agent-hub", "1.0.1", "test desc", "test vendor"));
softwareManagement.createSoftwareModule(ah); try {
softwareManagement.createSoftwareModule(ah);
fail("Should not have worked as module already exists.");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Create Software Modules call fails when called for existing entities.") @Description("Create Software Modules call fails when called for existing entities.")
public void createModulesCallFailsForExistingModule() { public void createModulesCallFailsForExistingModule() {
final List<SoftwareModule> modules = softwareManagement.createSoftwareModule( final List<SoftwareModule> modules = softwareManagement.createSoftwareModule(
Lists.newArrayList(new SoftwareModule(appType, "agent-hub", "1.0.1", "test desc", "test vendor"), Lists.newArrayList(new SoftwareModule(appType, "agent-hub", "1.0.1", "test desc", "test vendor"),
new SoftwareModule(appType, "agent-hub", "1.0.2", "test desc", "test vendor"))); new SoftwareModule(appType, "agent-hub", "1.0.2", "test desc", "test vendor")));
softwareManagement.createSoftwareModule(modules); try {
softwareManagement.createSoftwareModule(modules);
fail("Should not have worked as module already exists.");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Create Software Module Type call fails when called for existing entity.") @Description("Create Software Module Type call fails when called for existing entity.")
public void createModuleTypeCallFailsForExistingType() { public void createModuleTypeCallFailsForExistingType() {
final SoftwareModuleType created = softwareManagement final SoftwareModuleType created = softwareManagement
.createSoftwareModuleType(new SoftwareModuleType("test-key", "test-name", "test-desc", 1)); .createSoftwareModuleType(new SoftwareModuleType("test-key", "test-name", "test-desc", 1));
softwareManagement.createSoftwareModuleType(created);
try {
softwareManagement.createSoftwareModuleType(created);
fail("Should not have worked as module already exists.");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Create Software Module Types call fails when called for existing entities.") @Description("Create Software Module Types call fails when called for existing entities.")
public void createModuleTypesCallFailsForExistingTypes() { public void createModuleTypesCallFailsForExistingTypes() {
final List<SoftwareModuleType> created = softwareManagement.createSoftwareModuleType( final List<SoftwareModuleType> created = softwareManagement.createSoftwareModuleType(
Lists.newArrayList(new SoftwareModuleType("test-key", "test-name", "test-desc", 1), Lists.newArrayList(new SoftwareModuleType("test-key-bumlux", "test-name", "test-desc", 1),
new SoftwareModuleType("test-key2", "test-name", "test-desc", 1))); new SoftwareModuleType("test-key-bumlux2", "test-name2", "test-desc", 1)));
softwareManagement.createSoftwareModuleType(created);
try {
softwareManagement.createSoftwareModuleType(created);
fail("Should not have worked as module already exists.");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test @Test
@@ -652,9 +675,8 @@ public class SoftwareManagementTest extends AbstractIntegrationTestWithMongoDB {
new CustomSoftwareModule(two, true), new CustomSoftwareModule(unassigned, false)); new CustomSoftwareModule(two, true), new CustomSoftwareModule(unassigned, false));
// without any filter // without any filter
assertThat(softwareManagement assertThat(softwareManagement.findSoftwareModuleOrderBySetAssignmentAndModuleNameAscModuleVersionAsc(pageReq,
.findSoftwareModuleOrderBySetAssignmentAndModuleNameAscModuleVersionAsc(pageReq, set.getId(), null, null) set.getId(), null, null).getContent()).as("Found modules with the assigned ones first").containsExactly(
.getContent()).as("Found modules with the assigned ones first").containsExactly(
new CustomSoftwareModule(differentName, true), new CustomSoftwareModule(one, true), new CustomSoftwareModule(differentName, true), new CustomSoftwareModule(one, true),
new CustomSoftwareModule(two, true), new CustomSoftwareModule(four, true), new CustomSoftwareModule(two, true), new CustomSoftwareModule(four, true),
new CustomSoftwareModule(unassigned, false)); new CustomSoftwareModule(unassigned, false));
@@ -731,21 +753,32 @@ public class SoftwareManagementTest extends AbstractIntegrationTestWithMongoDB {
.isEqualTo(found); .isEqualTo(found);
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Verfies that it is not possible to create a type that alrady exists.") @Description("Verfies that it is not possible to create a type that alrady exists.")
public void createSoftwareModuleTypeFailsWithExistingEntity() { public void createSoftwareModuleTypeFailsWithExistingEntity() {
final SoftwareModuleType created = softwareManagement final SoftwareModuleType created = softwareManagement
.createSoftwareModuleType(new SoftwareModuleType("thetype", "thename", "desc", 100)); .createSoftwareModuleType(new SoftwareModuleType("thetype", "thename", "desc", 100));
softwareManagement.createSoftwareModuleType(created); try {
softwareManagement.createSoftwareModuleType(created);
fail("should not have worked as module type already exists");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Verfies that it is not possible to create a list of types where one already exists.") @Description("Verfies that it is not possible to create a list of types where one already exists.")
public void createSoftwareModuleTypesFailsWithExistingEntity() { public void createSoftwareModuleTypesFailsWithExistingEntity() {
final SoftwareModuleType created = softwareManagement final SoftwareModuleType created = softwareManagement
.createSoftwareModuleType(new SoftwareModuleType("thetype", "thename", "desc", 100)); .createSoftwareModuleType(new SoftwareModuleType("thetype", "thename", "desc", 100));
softwareManagement.createSoftwareModuleType( try {
Lists.newArrayList(created, new SoftwareModuleType("anothertype", "anothername", "desc", 100))); softwareManagement.createSoftwareModuleType(
Lists.newArrayList(created, new SoftwareModuleType("anothertype", "anothername", "desc", 100)));
fail("should not have worked as module type already exists");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test @Test
@@ -819,7 +852,7 @@ public class SoftwareManagementTest extends AbstractIntegrationTestWithMongoDB {
assertThat(softwareModuleMetadata.get(0).getSoftwareModule().getId()).isEqualTo(ah.getId()); assertThat(softwareModuleMetadata.get(0).getSoftwareModule().getId()).isEqualTo(ah.getId());
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Checks that metadata for a software module cannot be created for an existing key.") @Description("Checks that metadata for a software module cannot be created for an existing key.")
public void createSoftwareModuleMetadataFailsIfKeyExists() { public void createSoftwareModuleMetadataFailsIfKeyExists() {
@@ -832,7 +865,12 @@ public class SoftwareManagementTest extends AbstractIntegrationTestWithMongoDB {
softwareManagement.createSoftwareModuleMetadata(new SoftwareModuleMetadata(knownKey1, ah, knownValue1)); softwareManagement.createSoftwareModuleMetadata(new SoftwareModuleMetadata(knownKey1, ah, knownValue1));
softwareManagement.createSoftwareModuleMetadata(new SoftwareModuleMetadata(knownKey1, ah, knownValue2)); try {
softwareManagement.createSoftwareModuleMetadata(new SoftwareModuleMetadata(knownKey1, ah, knownValue2));
fail("should not have worked as module metadata already exists");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test @Test
@@ -902,7 +940,7 @@ public class SoftwareManagementTest extends AbstractIntegrationTestWithMongoDB {
.isEmpty(); .isEmpty();
} }
@Test(expected = EntityNotFoundException.class) @Test
@Description("Verfies that non existing metadata find results in exception.") @Description("Verfies that non existing metadata find results in exception.")
public void findSoftwareModuleMetadataFailsIfEntryDoesNotExist() { public void findSoftwareModuleMetadataFailsIfEntryDoesNotExist() {
final String knownKey1 = "myKnownKey1"; final String knownKey1 = "myKnownKey1";
@@ -914,8 +952,12 @@ public class SoftwareManagementTest extends AbstractIntegrationTestWithMongoDB {
ah = softwareManagement.createSoftwareModuleMetadata(new SoftwareModuleMetadata(knownKey1, ah, knownValue1)) ah = softwareManagement.createSoftwareModuleMetadata(new SoftwareModuleMetadata(knownKey1, ah, knownValue1))
.getSoftwareModule(); .getSoftwareModule();
softwareManagement.findSoftwareModuleMetadata(new SwMetadataCompositeKey(ah, "doesnotexist")); try {
softwareManagement.findSoftwareModuleMetadata(new SwMetadataCompositeKey(ah, "doesnotexist"));
fail("should not have worked as module metadata with that key does not exist");
} catch (final EntityNotFoundException e) {
}
} }
@Test @Test

View File

@@ -10,6 +10,7 @@ package org.eclipse.hawkbit.repository;
import static org.fest.assertions.api.Assertions.assertThat; import static org.fest.assertions.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@@ -362,38 +363,59 @@ public class TagManagementTest extends AbstractIntegrationTest {
} }
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Ensures that a tag cannot be created if one exists already with that name (ecpects EntityAlreadyExistsException).") @Description("Ensures that a tag cannot be created if one exists already with that name (ecpects EntityAlreadyExistsException).")
public void failedDuplicateTargetTagNameException() { public void failedDuplicateTargetTagNameException() {
tagManagement.createTargetTag(new TargetTag("A")); tagManagement.createTargetTag(new TargetTag("A"));
tagManagement.createTargetTag(new TargetTag("A"));
try {
tagManagement.createTargetTag(new TargetTag("A"));
fail("should not have worked as tag already exists");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Ensures that a tag cannot be updated to a name that already exists on another tag (ecpects EntityAlreadyExistsException).") @Description("Ensures that a tag cannot be updated to a name that already exists on another tag (ecpects EntityAlreadyExistsException).")
public void failedDuplicateTargetTagNameExceptionAfterUpdate() { public void failedDuplicateTargetTagNameExceptionAfterUpdate() {
tagManagement.createTargetTag(new TargetTag("A")); tagManagement.createTargetTag(new TargetTag("A"));
final TargetTag tag = tagManagement.createTargetTag(new TargetTag("B")); final TargetTag tag = tagManagement.createTargetTag(new TargetTag("B"));
tag.setName("A"); tag.setName("A");
tagManagement.updateTargetTag(tag); try {
tagManagement.updateTargetTag(tag);
fail("should not have worked as tag already exists");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Ensures that a tag cannot be created if one exists already with that name (ecpects EntityAlreadyExistsException).") @Description("Ensures that a tag cannot be created if one exists already with that name (ecpects EntityAlreadyExistsException).")
public void failedDuplicateDsTagNameException() { public void failedDuplicateDsTagNameException() {
tagManagement.createDistributionSetTag(new DistributionSetTag("A")); tagManagement.createDistributionSetTag(new DistributionSetTag("A"));
tagManagement.createDistributionSetTag(new DistributionSetTag("A")); try {
tagManagement.createDistributionSetTag(new DistributionSetTag("A"));
fail("should not have worked as tag already exists");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Ensures that a tag cannot be updated to a name that already exists on another tag (ecpects EntityAlreadyExistsException).") @Description("Ensures that a tag cannot be updated to a name that already exists on another tag (ecpects EntityAlreadyExistsException).")
public void failedDuplicateDsTagNameExceptionAfterUpdate() { public void failedDuplicateDsTagNameExceptionAfterUpdate() {
tagManagement.createDistributionSetTag(new DistributionSetTag("A")); tagManagement.createDistributionSetTag(new DistributionSetTag("A"));
final DistributionSetTag tag = tagManagement.createDistributionSetTag(new DistributionSetTag("B")); final DistributionSetTag tag = tagManagement.createDistributionSetTag(new DistributionSetTag("B"));
tag.setName("A"); tag.setName("A");
tagManagement.updateDistributionSetTag(tag); try {
tagManagement.updateDistributionSetTag(tag);
fail("should not have worked as tag already exists");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test @Test

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.repository; package org.eclipse.hawkbit.repository;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.eclipse.hawkbit.AbstractIntegrationTest; import org.eclipse.hawkbit.AbstractIntegrationTest;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException; import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
@@ -37,15 +38,20 @@ public class TargetFilterQueryManagenmentTest extends AbstractIntegrationTest {
targetFilterQueryManagement.findTargetFilterQueryByName(filterName)); targetFilterQueryManagement.findTargetFilterQueryByName(filterName));
} }
@Test(expected = EntityAlreadyExistsException.class) @Test
@Description("Checks if the EntityAlreadyExistsException is thrown if a targetfilterquery with the same name are created more than once.") @Description("Checks if the EntityAlreadyExistsException is thrown if a targetfilterquery with the same name are created more than once.")
public void createDuplicateTargetFilterQuery() { public void createDuplicateTargetFilterQuery() {
final String filterName = "new target filter duplicate"; final String filterName = "new target filter duplicate";
targetFilterQueryManagement targetFilterQueryManagement
.createTargetFilterQuery(new TargetFilterQuery(filterName, "name==PendingTargets001")); .createTargetFilterQuery(new TargetFilterQuery(filterName, "name==PendingTargets001"));
targetFilterQueryManagement try {
.createTargetFilterQuery(new TargetFilterQuery(filterName, "name==PendingTargets001")); targetFilterQueryManagement
.createTargetFilterQuery(new TargetFilterQuery(filterName, "name==PendingTargets001"));
fail("should not have worked as query already exists");
} catch (final EntityAlreadyExistsException e) {
}
} }
@Test @Test

View File

@@ -54,11 +54,16 @@ import ru.yandex.qatools.allure.annotations.Stories;
@Stories("Target Management") @Stories("Target Management")
public class TargetManagementTest extends AbstractIntegrationTest { public class TargetManagementTest extends AbstractIntegrationTest {
@Test(expected = TenantNotExistException.class) @Test
@Description("Ensures that targets cannot be created e.g. in plug'n play scenarios when tenant does not exists.") @Description("Ensures that targets cannot be created e.g. in plug'n play scenarios when tenant does not exists.")
@WithUser(tenantId = "tenantWhichDoesNotExists", allSpPermissions = true, autoCreateTenant = false) @WithUser(tenantId = "tenantWhichDoesNotExists", allSpPermissions = true, autoCreateTenant = false)
public void createTargetForTenantWhichDoesNotExistThrowsTenantNotExistException() { public void createTargetForTenantWhichDoesNotExistThrowsTenantNotExistException() {
targetManagement.createTarget(new Target("targetId123")); try {
targetManagement.createTarget(new Target("targetId123"));
fail("should not be possible as the tenant does not exist");
} catch (final TenantNotExistException e) {
// ok
}
} }
@Test @Test

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.repository; package org.eclipse.hawkbit.repository;
import static org.fest.assertions.api.Assertions.assertThat; import static org.fest.assertions.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import java.time.Duration; import java.time.Duration;
import java.util.Arrays; import java.util.Arrays;
@@ -91,13 +92,19 @@ public class TenantConfigurationManagementTest extends AbstractIntegrationTestWi
.isEqualTo(value2); .isEqualTo(value2);
} }
@Test(expected = TenantConfigurationValidatorException.class) @Test
@Description("Tests that the get configuration throws exception in case the value cannot be automatically converted from String to Boolean") @Description("Tests that the get configuration throws exception in case the value cannot be automatically converted from String to Boolean")
public void wrongTenantConfigurationValueTypeThrowsException() { public void wrongTenantConfigurationValueTypeThrowsException() {
final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_HEADER_ENABLED; final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_HEADER_ENABLED;
final String value1 = "thisIsNotABoolean"; final String value1 = "thisIsNotABoolean";
// add value as String // add value as String
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, value1); try {
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, value1);
fail("should not have worked as string is not a boolean");
} catch (final TenantConfigurationValidatorException e) {
}
} }
@Test @Test
@@ -128,46 +135,71 @@ public class TenantConfigurationManagementTest extends AbstractIntegrationTestWi
assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, String.class).getValue()).isNull(); assertThat(tenantConfigurationManagement.getConfigurationValue(configKey, String.class).getValue()).isNull();
} }
@Test(expected = TenantConfigurationValidatorException.class) @Test
@Description("Test that an Exception is thrown, when an integer is stored but a string expected.") @Description("Test that an Exception is thrown, when an integer is stored but a string expected.")
public void storesIntegerWhenStringIsExpected() { public void storesIntegerWhenStringIsExpected() {
final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_NAME; final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_NAME;
final Integer wrongDataype = 123; final Integer wrongDataype = 123;
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataype); try {
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataype);
fail("should not have worked as integer is not a string");
} catch (final TenantConfigurationValidatorException e) {
}
} }
@Test(expected = TenantConfigurationValidatorException.class) @Test
@Description("Test that an Exception is thrown, when an integer is stored but a boolean expected.") @Description("Test that an Exception is thrown, when an integer is stored but a boolean expected.")
public void storesIntegerWhenBooleanIsExpected() { public void storesIntegerWhenBooleanIsExpected() {
final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_ENABLED; final TenantConfigurationKey configKey = TenantConfigurationKey.AUTHENTICATION_MODE_GATEWAY_SECURITY_TOKEN_ENABLED;
final Integer wrongDataype = 123; final Integer wrongDataype = 123;
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataype); try {
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataype);
fail("should not have worked as integer is not a boolean");
} catch (final TenantConfigurationValidatorException e) {
}
} }
@Test(expected = TenantConfigurationValidatorException.class) @Test
@Description("Test that an Exception is thrown, when an integer is stored as PollingTime.") @Description("Test that an Exception is thrown, when an integer is stored as PollingTime.")
public void storesIntegerWhenPollingIntervalIsExpected() { public void storesIntegerWhenPollingIntervalIsExpected() {
final TenantConfigurationKey configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL; final TenantConfigurationKey configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL;
final Integer wrongDataype = 123; final Integer wrongDataype = 123;
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataype); try {
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongDataype);
fail("should not have worked as integer is not a time field");
} catch (final TenantConfigurationValidatorException e) {
}
} }
@Test(expected = TenantConfigurationValidatorException.class) @Test
@Description("Test that an Exception is thrown, when an invalid formatted string is stored as PollingTime.") @Description("Test that an Exception is thrown, when an invalid formatted string is stored as PollingTime.")
public void storesWrongFormattedStringAsPollingInterval() { public void storesWrongFormattedStringAsPollingInterval() {
final TenantConfigurationKey configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL; final TenantConfigurationKey configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL;
final String wrongFormatted = "wrongFormatted"; final String wrongFormatted = "wrongFormatted";
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongFormatted); try {
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, wrongFormatted);
fail("should not have worked as string is not a time field");
} catch (final TenantConfigurationValidatorException e) {
}
} }
@Test(expected = TenantConfigurationValidatorException.class) @Test
@Description("Test that an Exception is thrown, when an invalid formatted string is stored as PollingTime.") @Description("Test that an Exception is thrown, when an invalid formatted string is stored as PollingTime.")
public void storesTooSmallDurationAsPollingInterval() { public void storesTooSmallDurationAsPollingInterval() {
final TenantConfigurationKey configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL; final TenantConfigurationKey configKey = TenantConfigurationKey.POLLING_TIME_INTERVAL;
final String tooSmallDuration = DurationHelper final String tooSmallDuration = DurationHelper
.durationToFormattedString(DurationHelper.getDurationByTimeValues(0, 0, 1)); .durationToFormattedString(DurationHelper.getDurationByTimeValues(0, 0, 1));
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, tooSmallDuration); try {
tenantConfigurationManagement.addOrUpdateConfiguration(configKey, tooSmallDuration);
fail("should not have worked as string has an invalid format");
} catch (final TenantConfigurationValidatorException e) {
}
} }
@Test @Test

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.rest.resource.model; package org.eclipse.hawkbit.rest.resource.model;
import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.fail;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -23,10 +24,14 @@ import ru.yandex.qatools.allure.annotations.Stories;
@Stories("Paged List Handling") @Stories("Paged List Handling")
public class PagedListTest { public class PagedListTest {
@Test(expected = NullPointerException.class) @Test
@Description("Ensures that a null payload entitiy throws an exception.") @Description("Ensures that a null payload entitiy throws an exception.")
public void createListWithNullContentThrowsException() { public void createListWithNullContentThrowsException() {
new PagedList<>(null, 0); try {
new PagedList<>(null, 0);
fail("as content is null");
} catch (final NullPointerException e) {
}
} }
@Test @Test