-
Notifications
You must be signed in to change notification settings - Fork 49
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
Cleaner will remove artifacts without reference in suites e.g after rerun one test #336
Changes from 15 commits
a8de89b
ba2cd9d
53d9d97
ee4c2ad
b5e85cb
f8d728d
903b24b
8f07000
cd9f968
130a069
94ded09
03b8ea3
2d03479
05dbffd
3e0303d
3115a95
4bfedf0
1e367f6
af28aa4
47acf66
c1e494b
dfd2e0d
e5f88e5
72aa7fa
278227b
05a60cd
f20cf28
87fede7
1878579
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,11 +21,15 @@ public class SuiteAggregationCounter { | |
|
||
private final int suitesToAggregate; | ||
|
||
public SuiteAggregationCounter(int suitesToAggregate) { | ||
this.suitesToAggregate = suitesToAggregate; | ||
public SuiteAggregationCounter() { | ||
this.suitesToAggregate = 0; | ||
} | ||
|
||
public int getSuitesToAggregate() { | ||
return suitesToAggregate; | ||
} | ||
|
||
public int addSuitesToAggregate(int suitesToAggregate) { | ||
return this.suitesToAggregate + suitesToAggregate; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't be here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,11 @@ | |
import com.cognifide.aet.cleaner.processors.exchange.ReferencedArtifactsMessageBody; | ||
import com.cognifide.aet.vs.ArtifactsDAO; | ||
import com.google.common.collect.Sets; | ||
import com.google.common.collect.Sets.SetView; | ||
import java.util.ArrayList; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.apache.camel.Exchange; | ||
import org.apache.camel.Processor; | ||
import org.osgi.service.component.annotations.Component; | ||
|
@@ -34,6 +39,15 @@ public class RemoveArtifactsProcessor implements Processor { | |
@Reference | ||
private ArtifactsDAO artifactsDAO; | ||
|
||
public RemoveArtifactsProcessor() { | ||
//default constructor | ||
} | ||
|
||
// for unit tests | ||
public RemoveArtifactsProcessor(ArtifactsDAO artifactsDAO) { | ||
this.artifactsDAO = artifactsDAO; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public void process(Exchange exchange) throws Exception { | ||
|
@@ -42,8 +56,7 @@ public void process(Exchange exchange) throws Exception { | |
final ReferencedArtifactsMessageBody messageBody = exchange.getIn() | ||
.getBody(ReferencedArtifactsMessageBody.class); | ||
|
||
final Sets.SetView<String> artifactsToRemove = | ||
Sets.difference(messageBody.getArtifactsToRemove(), messageBody.getArtifactsToKeep()); | ||
Set<String> artifactsToRemove = getArtifactsIdsToRemove(artifactsDAO, messageBody); | ||
|
||
LOGGER.debug("Artifacts that will be removed: {}", artifactsToRemove); | ||
if (!cleanerContext.isDryRun()) { | ||
|
@@ -57,4 +70,11 @@ public void process(Exchange exchange) throws Exception { | |
artifactsToRemove.size(), messageBody.getDbKey(), messageBody.getData()); | ||
} | ||
} | ||
|
||
public static Set<String> getArtifactsIdsToRemove(ArtifactsDAO artifactsDAO, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't have to be |
||
ReferencedArtifactsMessageBody messageBody) { | ||
Set<String> artifactsToRemove = artifactsDAO.getArtifactsId(messageBody.getDbKey()); | ||
artifactsToRemove.removeAll(messageBody.getArtifactsToKeep()); | ||
return artifactsToRemove; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,8 +32,8 @@ public SuiteMessageBody(Suite data, DBKey dbKey, boolean toRemove) { | |
this.toRemove = toRemove; | ||
} | ||
|
||
public boolean shouldBeRemoved() { | ||
return toRemove; | ||
public boolean shouldBeKeeped() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return !toRemove; | ||
} | ||
|
||
public Set<String> getSuiteArtifacts() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** | ||
* AET | ||
* | ||
* Copyright (C) 2013 Cognifide Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.cognifide.aet.cleaner.processors; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.cognifide.aet.cleaner.context.CleanerContext; | ||
import com.cognifide.aet.cleaner.processors.exchange.ReferencedArtifactsMessageBody; | ||
import com.cognifide.aet.vs.ArtifactsDAO; | ||
import com.cognifide.aet.vs.DBKey; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import org.apache.camel.CamelContext; | ||
import org.apache.camel.Exchange; | ||
import org.apache.camel.Message; | ||
import org.apache.camel.impl.DefaultCamelContext; | ||
import org.apache.camel.impl.DefaultExchange; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class RemoveArtifactsProcessorTest { | ||
|
||
private static final Set<String> ONE_TO_SEVEN_ARTIFACTS_ID_SET = new HashSet<>(Arrays | ||
.asList("1", "2", "3", "4", "5", "6", "7")); | ||
|
||
private static final Set<String> ONE_TO_FIVE_ARTIFACTS_ID_SET = new HashSet<>(Arrays | ||
.asList("1", "2", "3", "4", "5")); | ||
|
||
private static final Set<String> SIX_TO_SEVEN_ARTIFACTS_ID_SET = new HashSet<>(Arrays | ||
.asList("6", "7")); | ||
|
||
private static final Set<String> EMPTY_ARTIFACTS_ID_SET = new HashSet<>(); | ||
|
||
private Exchange exchange; | ||
|
||
@Mock | ||
private ArtifactsDAO artifactDAO; | ||
|
||
@Mock | ||
private CleanerContext cleanerContext; | ||
|
||
@Mock | ||
private DBKey dbKey; | ||
|
||
private RemoveArtifactsProcessor removeArtifactsProcessor; | ||
|
||
@Before | ||
public void setUp() { | ||
when(cleanerContext.isDryRun()).thenReturn(false); | ||
|
||
CamelContext ctx = new DefaultCamelContext(); | ||
exchange = new DefaultExchange(ctx); | ||
|
||
Message in = exchange.getIn(); | ||
in.setBody(new ReferencedArtifactsMessageBody("", dbKey)); | ||
in.setHeader(CleanerContext.KEY_NAME, cleanerContext); | ||
} | ||
|
||
@Test | ||
public void check_ifRemoveArtifactsWasCalled_expectTrue() throws Exception { | ||
when(artifactDAO.getArtifactsId(any(DBKey.class))) | ||
.thenReturn(new HashSet<>(ONE_TO_SEVEN_ARTIFACTS_ID_SET)); | ||
setArtifactsIdToKeep(ONE_TO_FIVE_ARTIFACTS_ID_SET); | ||
|
||
removeArtifactsProcessor = new RemoveArtifactsProcessor(artifactDAO); | ||
removeArtifactsProcessor.process(exchange); | ||
|
||
verify(artifactDAO, times(1)).removeArtifacts(any(DBKey.class), any(Set.class)); | ||
verify(artifactDAO, times(1)).getArtifactsId(any(DBKey.class)); | ||
} | ||
|
||
@Test | ||
public void check_ifRemoveArtifactsWasCalled_expectFalse() throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are testing |
||
when(cleanerContext.isDryRun()).thenReturn(true); | ||
when(artifactDAO.getArtifactsId(any(DBKey.class))) | ||
.thenReturn(new HashSet<>(ONE_TO_SEVEN_ARTIFACTS_ID_SET)); | ||
setArtifactsIdToKeep(ONE_TO_FIVE_ARTIFACTS_ID_SET); | ||
|
||
removeArtifactsProcessor = new RemoveArtifactsProcessor(artifactDAO); | ||
removeArtifactsProcessor.process(exchange); | ||
|
||
verify(artifactDAO, times(0)).removeArtifacts(any(DBKey.class), any(Set.class)); | ||
verify(artifactDAO, times(1)).getArtifactsId(any(DBKey.class)); | ||
} | ||
|
||
@Test | ||
public void check_substractArtifactsSets_expectSetOfTwoVariables() throws Exception { | ||
when(artifactDAO.getArtifactsId(any(DBKey.class))) | ||
.thenReturn(new HashSet<>(ONE_TO_SEVEN_ARTIFACTS_ID_SET)); | ||
setArtifactsIdToKeep(ONE_TO_FIVE_ARTIFACTS_ID_SET); | ||
ReferencedArtifactsMessageBody messageBody = (ReferencedArtifactsMessageBody) exchange.getIn() | ||
.getBody(); | ||
assertEquals(SIX_TO_SEVEN_ARTIFACTS_ID_SET, | ||
removeArtifactsProcessor.getArtifactsIdsToRemove(artifactDAO, messageBody)); | ||
} | ||
|
||
@Test | ||
public void check_substractArtifactsSets_expectEmptySet() throws Exception { | ||
when(artifactDAO.getArtifactsId(any(DBKey.class))) | ||
.thenReturn(new HashSet<>(ONE_TO_FIVE_ARTIFACTS_ID_SET)); | ||
setArtifactsIdToKeep(ONE_TO_SEVEN_ARTIFACTS_ID_SET); | ||
ReferencedArtifactsMessageBody messageBody = (ReferencedArtifactsMessageBody) exchange.getIn() | ||
.getBody(); | ||
assertEquals(EMPTY_ARTIFACTS_ID_SET, | ||
removeArtifactsProcessor.getArtifactsIdsToRemove(artifactDAO, messageBody)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be easier to just check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm checking it there: https://github.com/Cognifide/aet/pull/336/files#diff-4b628417ba96f36a0a4eec9f4817ba27R144. Here I wanted to check substraction ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant: |
||
} | ||
|
||
@Test | ||
public void check_substractArtifactsSets_expectSetOfFiveVariables() throws Exception { | ||
when(artifactDAO.getArtifactsId(any(DBKey.class))) | ||
.thenReturn(new HashSet<>(ONE_TO_SEVEN_ARTIFACTS_ID_SET)); | ||
setArtifactsIdToKeep(SIX_TO_SEVEN_ARTIFACTS_ID_SET); | ||
|
||
ReferencedArtifactsMessageBody messageBody = (ReferencedArtifactsMessageBody) exchange.getIn() | ||
.getBody(); | ||
|
||
assertEquals(ONE_TO_FIVE_ARTIFACTS_ID_SET, | ||
removeArtifactsProcessor.getArtifactsIdsToRemove(artifactDAO, messageBody)); | ||
} | ||
|
||
@Test | ||
public void check_substractArtifactsSetsWhenDbIsEmpty_expectEmptySet() throws Exception { | ||
when(artifactDAO.getArtifactsId(any(DBKey.class))) | ||
.thenReturn(new HashSet<>(EMPTY_ARTIFACTS_ID_SET)); | ||
setArtifactsIdToKeep(SIX_TO_SEVEN_ARTIFACTS_ID_SET); | ||
ReferencedArtifactsMessageBody messageBody = (ReferencedArtifactsMessageBody) exchange.getIn() | ||
.getBody(); | ||
assertEquals(EMPTY_ARTIFACTS_ID_SET, | ||
removeArtifactsProcessor.getArtifactsIdsToRemove(artifactDAO, messageBody)); | ||
} | ||
|
||
private void setArtifactsIdToKeep(Set<String> artifactsIdToRemove) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is to keep or remove :) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be af28aa4 🎃 |
||
ReferencedArtifactsMessageBody body = (ReferencedArtifactsMessageBody) exchange.getIn() | ||
.getBody(); | ||
body.setArtifactsToKeep(artifactsIdToRemove); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getArtifactsIds
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4bfedf0