Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moved action filtering to the database query level #12

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,21 @@ public void sendDownloadAndInstallStatusMessageDuringMaintenanceWindow() {
@Test
@Description("Verify that a distribution assignment multiple times send cancel and assign events with right softwaremodules")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetAssignDistributionSetEvent.class, count = 1),
@Expect(type = TargetAssignDistributionSetEvent.class, count = 2),
@Expect(type = CancelTargetAssignmentEvent.class, count = 1),
@Expect(type = ActionCreatedEvent.class, count = 2), @Expect(type = ActionUpdatedEvent.class, count = 1),
@Expect(type = SoftwareModuleCreatedEvent.class, count = 6),
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 6),
@Expect(type = SoftwareModuleUpdatedEvent.class, count = 12),
@Expect(type = DistributionSetCreatedEvent.class, count = 2),
@Expect(type = TargetUpdatedEvent.class, count = 2), @Expect(type = TargetPollEvent.class, count = 3) })
@Expect(type = TargetUpdatedEvent.class, count = 2), @Expect(type = TargetPollEvent.class, count = 2) })
public void assignDistributionSetMultipleTimes() {
final String controllerId = TARGET_PREFIX + "assignDistributionSetMultipleTimes";

final DistributionSetAssignmentResult assignmentResult = registerTargetAndAssignDistributionSet(controllerId);

final DistributionSet distributionSet2 = testdataFactory.createDistributionSet(UUID.randomUUID().toString());
registerTargetAndAssignDistributionSet(distributionSet2.getId(), TargetUpdateStatus.PENDING,
getDistributionSet().getModules(), controllerId);
testdataFactory.addSoftwareModuleMetadata(distributionSet2);
Copy link
Author

@a-sayyed a-sayyed Jun 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peerreviewer double check if the change doesn't change the test behaviour.

assignDistributionSet(distributionSet2.getId(), controllerId);
assertDownloadAndInstallMessage(distributionSet2.getModules(), controllerId);
assertCancelActionMessage(assignmentResult.getActionIds().get(0), controllerId);

createAndSendThingCreated(controllerId, TENANT_EXIST);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,50 +57,27 @@ public SoftwareModulesMatcher(final List<DmfSoftwareModule> expectedModules) {
this.expectedModules = expectedModules;
}

static boolean containsExactly(final Object actual, final List<DmfSoftwareModule> expected) {
boolean containsExactly(final Object actual) {
if (actual == null) {
return expected == null;
return expectedModules == null;
}

@SuppressWarnings("unchecked")
final Collection<SoftwareModule> modules = (Collection<SoftwareModule>) actual;

if (modules.size() != expected.size()) {
return false;
}

for (final SoftwareModule repoSoftwareModule : modules) {
boolean containsElement = false;

for (final DmfSoftwareModule jsonSoftwareModule : expected) {
if (!jsonSoftwareModule.getModuleId().equals(repoSoftwareModule.getId())) {
continue;
}
containsElement = true;

if (!jsonSoftwareModule.getModuleType().equals(repoSoftwareModule.getType().getKey())) {
return false;
}
if (!jsonSoftwareModule.getModuleVersion().equals(repoSoftwareModule.getVersion())) {
return false;
}
if (jsonSoftwareModule.getArtifacts().size() != repoSoftwareModule.getArtifacts().size()) {
return false;
}
}

if (!containsElement) {
return false;
}

}
return expectedModules.stream().allMatch(e -> existsIn(e, modules));
}

return true;
private static boolean existsIn(final DmfSoftwareModule module, final Collection<SoftwareModule> actual) {
return actual.stream()
.anyMatch(e -> module.getModuleType().equals(e.getType().getKey())
&& module.getModuleVersion().equals(e.getVersion())
&& module.getArtifacts().size() == e.getArtifacts().size());
}

@Override
public boolean matches(final Object actualValue) {
return containsExactly(actualValue, expectedModules);
return containsExactly(actualValue);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ public boolean equals(final Object obj) {
return true;
}

@Override
public String toString() {
return String.format("DmfMetadata [key='%s', value='%s']", key, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,11 @@ public void setMetadata(final List<DmfMetadata> metadata) {
}
}

@Override
public String toString() {
return String.format(
"DmfSoftwareModule [moduleId=%d, moduleType='%s', moduleVersion='%s', artifacts=%s, metadata=%s]",
moduleId, moduleType, moduleVersion, artifacts, metadata);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ public interface ActionRepository extends BaseEntityRepository<JpaAction, Long>,
@Query("SELECT CASE WHEN COUNT(a)>0 THEN 'true' ELSE 'false' END FROM JpaAction a JOIN a.target t WHERE t.controllerId=:controllerId AND a.active=1")
boolean activeActionExistsForControllerId(@Param("controllerId") String controllerId);

/**
* Check if any active actions with given action status and given controller
* ID exist.
*
* @param controllerId
* of the target to check for actions
* @param currentStatus
* of the active action to look for
*
* @return <code>true</code> if one or more active actions for the given
* controllerId and action status are found
*/
boolean existsByTargetControllerIdAndStatusAndActiveIsTrue(String controllerId, Action.Status currentStatus);

/**
* Retrieves latest {@link Action} for given target and
* {@link SoftwareModule}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ private void sendTargetAssignDistributionSetEvent(final String tenant, final lon
}

private boolean hasPendingCancellations(final Target target) {
return actionRepository.findByActiveAndTarget(ACTION_PAGE_REQUEST, target.getControllerId(), true).getContent()
.stream().anyMatch(action -> action.getStatus() == Status.CANCELING);
return actionRepository.existsByTargetControllerIdAndStatusAndActiveIsTrue(target.getControllerId(),
Status.CANCELING);
}

/**
Expand Down
Loading