Skip to content

Commit

Permalink
Merge pull request #489 from Cognifide/feature/cleaner-integration-test
Browse files Browse the repository at this point in the history
Cleaner integration tests
  • Loading branch information
tkaik authored Jul 12, 2019
2 parents 791ff47 + 2f6bb98 commit 41cba3e
Show file tree
Hide file tree
Showing 36 changed files with 2,930 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to AET will be documented in this file.
- [PR-479](https://github.com/Cognifide/aet/pull/479) Added Secure and HttpOnly flags for cookies. ([#477](https://github.com/Cognifide/aet/issues/477))
- [PR-506](https://github.com/Cognifide/aet/pull/506) About tab ([#475](https://github.com/Cognifide/aet/issues/475))
- [PR-462](https://github.com/Cognifide/aet/pull/462) Popup window unification([#367](https://github.com/Cognifide/aet/issues/367))
- [PR-489](https://github.com/Cognifide/aet/pull/489) Cleaner integration tests

## Version 3.2.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package com.cognifide.aet.cleaner;


import com.cognifide.aet.cleaner.camel.CamelContextCreator;
import com.cognifide.aet.cleaner.context.CleanerContext;
import com.cognifide.aet.cleaner.route.MetadataCleanerRouteBuilder;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.DefaultCamelContext;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
Expand All @@ -46,6 +46,8 @@ public class CleanerJob implements Job {

static final String KEY_DRY_RUN = "dryRun";

static final String KEY_CAMEL_CONTEXT_CREATOR = "camelContextCreator";

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
CamelContext camelContext = null;
Expand All @@ -59,8 +61,10 @@ public void execute(JobExecutionContext context) throws JobExecutionException {
final String companyFilter = (String) jobDataMap.get(KEY_COMPANY_FILTER);
final String projectFilter = (String) jobDataMap.get(KEY_PROJECT_FILTER);
final Boolean dryRun = (Boolean) jobDataMap.get(KEY_DRY_RUN);
final CamelContextCreator contextCreator = (CamelContextCreator) jobDataMap
.get(KEY_CAMEL_CONTEXT_CREATOR);

camelContext = new DefaultCamelContext();
camelContext = contextCreator.create();
camelContext.setAllowUseOriginalMessage(false);
camelContext.addRoutes(cleanerRouteBuilder);
camelContext.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.cognifide.aet.cleaner;

import com.cognifide.aet.cleaner.camel.CamelContextCreator;
import com.cognifide.aet.cleaner.configuration.CleanerSchedulerConf;
import com.cognifide.aet.cleaner.route.MetadataCleanerRouteBuilder;
import com.cognifide.aet.cleaner.validation.CleanerSchedulerValidator;
Expand Down Expand Up @@ -58,6 +59,9 @@ public class CleanerScheduler {
@Reference
private MetadataCleanerRouteBuilder metadataCleanerRouteBuilder;

@Reference
private CamelContextCreator camelContextCreator;

@Reference
private ValidationResultBuilderFactory validationResultBuilderFactory;

Expand Down Expand Up @@ -118,6 +122,7 @@ private String registerCleaningJob() throws SchedulerException {

final ImmutableMap<String, Object> jobData = ImmutableMap.<String, Object>builder()
.put(CleanerJob.KEY_ROUTE_BUILDER, metadataCleanerRouteBuilder)
.put(CleanerJob.KEY_CAMEL_CONTEXT_CREATOR, camelContextCreator)
.put(CleanerJob.KEY_KEEP_N_VERSIONS, config.keepNVersions())
.put(CleanerJob.KEY_REMOVE_OLDER_THAN, config.removeOlderThan())
.put(CleanerJob.KEY_COMPANY_FILTER, config.companyName())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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.camel;

import org.apache.camel.CamelContext;

public interface CamelContextCreator {

CamelContext create();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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.camel;

import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.osgi.service.component.annotations.Component;

@Component(service = CamelContextCreator.class)
public class DefaultCamelContextCreator implements CamelContextCreator {

@Override
public CamelContext create() {
return new DefaultCamelContext();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.cognifide.aet.cleaner.processors.exchange.AllSuiteVersionsMessageBody;
import com.cognifide.aet.cleaner.processors.exchange.SuiteMessageBody;
import com.cognifide.aet.cleaner.processors.filters.SuiteRemoveCondition;
import com.cognifide.aet.cleaner.time.LocalDateTimeProvider;
import com.cognifide.aet.communication.api.metadata.Suite;
import com.cognifide.aet.vs.DBKey;
import com.google.common.base.Function;
Expand All @@ -29,6 +30,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -38,6 +40,9 @@ public class SuitesRemovePredicateProcessor implements Processor {
private static final Logger LOGGER = LoggerFactory
.getLogger(SuitesRemovePredicateProcessor.class);

@Reference
private LocalDateTimeProvider dateTimeProvider;

@Override
@SuppressWarnings("unchecked")
public void process(Exchange exchange) throws Exception {
Expand All @@ -53,7 +58,7 @@ public void process(Exchange exchange) throws Exception {
suiteVersions.size(), dbKey);

final SuiteRemoveCondition removeCondition = new SuiteRemoveCondition(suiteVersions,
cleanerContext);
cleanerContext, dateTimeProvider);

final ImmutableList<SuiteMessageBody> body =
FluentIterable.from(suiteVersions).transform(new Function<Suite, SuiteMessageBody>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.cognifide.aet.cleaner.processors.filters;

import com.cognifide.aet.cleaner.context.CleanerContext;
import com.cognifide.aet.cleaner.time.LocalDateTimeProvider;
import com.cognifide.aet.communication.api.metadata.Suite;
import com.google.common.collect.Ordering;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -45,9 +46,9 @@ public int compare(Suite o1, Suite o2) {
private final Long minKeepVersion;

public SuiteRemoveCondition(final Collection<Suite> suiteRunVersions,
final CleanerContext cleanerContext) {
final CleanerContext cleanerContext, LocalDateTimeProvider dateTimeProvider) {
minKeepVersion = getMinKeepVersion(suiteRunVersions, cleanerContext);
removeTimestamp = getRemoveTimestamp(cleanerContext);
removeTimestamp = getRemoveTimestamp(cleanerContext, dateTimeProvider);
}

public boolean evaluate(final Suite suite) {
Expand Down Expand Up @@ -86,10 +87,10 @@ private Long getMinKeepVersion(final Collection<Suite> suiteRunVersions,
return minVersionToKeep;
}

private Long getRemoveTimestamp(CleanerContext cleanerContext) {
private Long getRemoveTimestamp(CleanerContext cleanerContext, LocalDateTimeProvider dateTimeProvider) {
Long removeBeforeTimestamp = null;
if (cleanerContext.getRemoveOlderThan() != null) {
LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
LocalDateTime now = dateTimeProvider.now(ZoneOffset.UTC);
int daysToKeep = cleanerContext.getRemoveOlderThan().intValue();
LocalDateTime then = now.minusDays(daysToKeep);
removeBeforeTimestamp = then.toInstant(ZoneOffset.UTC).toEpochMilli();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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.time;

import java.time.LocalDateTime;
import java.time.ZoneId;

public interface LocalDateTimeProvider {

LocalDateTime now(ZoneId zone);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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.time;

import java.time.LocalDateTime;
import java.time.ZoneId;
import org.osgi.service.component.annotations.Component;

@Component(service = LocalDateTimeProvider.class)
public class SystemLocalDateTime implements LocalDateTimeProvider {

@Override
public LocalDateTime now(ZoneId zone) {
return LocalDateTime.now(zone);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.mockito.Mockito.when;

import com.cognifide.aet.cleaner.context.CleanerContext;
import com.cognifide.aet.cleaner.time.SystemLocalDateTime;
import com.cognifide.aet.communication.api.metadata.Suite;
import com.cognifide.aet.communication.api.metadata.Suite.Timestamp;
import com.googlecode.zohhak.api.Configure;
Expand Down Expand Up @@ -52,7 +53,7 @@ public void evaluate_whenOnlyOneSuite_expectFalseNoMatterConditions(String allSu

Suite suite = mockSuiteVersionAndCreatedDate(evaluatedSuite, createdDaysAgo);
SuiteRemoveCondition condition = new SuiteRemoveCondition(suites,
mockRemoveConditions(removeOlderThan, keepNVersions));
mockRemoveConditions(removeOlderThan, keepNVersions), new SystemLocalDateTime());
final boolean remove = condition.evaluate(suite);
assertFalse(remove);
}
Expand Down Expand Up @@ -82,7 +83,7 @@ public void evaluate_when8SuitesVersionsRemoveOldVersions_expectTrue(String allS

Suite suite = mockSuiteVersionAndCreatedDate(evaluatedSuite, createdDaysAgo);
SuiteRemoveCondition condition = new SuiteRemoveCondition(suites,
mockRemoveConditions(removeOlderThan, keepNVersions));
mockRemoveConditions(removeOlderThan, keepNVersions), new SystemLocalDateTime());
final boolean remove = condition.evaluate(suite);
assertTrue(remove);
}
Expand All @@ -108,7 +109,7 @@ public void evaluate_when8SuitesVersionsKeepLastVersions_expectFalse(String allS

Suite suite = mockSuiteVersionAndCreatedDate(evaluatedSuite, createdDaysAgo);
SuiteRemoveCondition condition = new SuiteRemoveCondition(suites,
mockRemoveConditions(removeOlderThan, keepNVersions));
mockRemoveConditions(removeOlderThan, keepNVersions), new SystemLocalDateTime());
final boolean remove = condition.evaluate(suite);
assertFalse(remove);
}
Expand Down Expand Up @@ -136,7 +137,7 @@ public void evaluate_when8SuitesRemoveOlderThanButAlwaysKeepLatestVersion_expect

Suite suite = mockSuiteVersionAndCreatedDate(evaluatedSuite, createdDaysAgo);
SuiteRemoveCondition condition = new SuiteRemoveCondition(suites,
mockRemoveConditions(removeOlderThan, keepNVersions));
mockRemoveConditions(removeOlderThan, keepNVersions), new SystemLocalDateTime());
final boolean remove = condition.evaluate(suite);
assertTrue(remove);
}
Expand All @@ -155,7 +156,7 @@ public void evaluate_when8SuitesKeepNewerThan_expectFalse(String allSuitesVersio

Suite suite = mockSuiteVersionAndCreatedDate(evaluatedSuite, createdDaysAgo);
SuiteRemoveCondition condition = new SuiteRemoveCondition(suites,
mockRemoveConditions(removeOlderThan, keepNVersions));
mockRemoveConditions(removeOlderThan, keepNVersions), new SystemLocalDateTime());
final boolean remove = condition.evaluate(suite);
assertFalse(remove);
}
Expand All @@ -174,7 +175,7 @@ public void evaluate_when8SuitesKeepNewerThanAndAtLeastXVersions_expectTrue(

Suite suite = mockSuiteVersionAndCreatedDate(evaluatedSuite, createdDaysAgo);
SuiteRemoveCondition condition = new SuiteRemoveCondition(suites,
mockRemoveConditions(removeOlderThan, keepNVersions));
mockRemoveConditions(removeOlderThan, keepNVersions), new SystemLocalDateTime());
final boolean remove = condition.evaluate(suite);
assertTrue(remove);
}
Expand All @@ -193,7 +194,7 @@ public void evaluate_when8SuitesKeepNewerThanAndAtLeastXVersions_expectFalse(

Suite suite = mockSuiteVersionAndCreatedDate(evaluatedSuite, createdDaysAgo);
SuiteRemoveCondition condition = new SuiteRemoveCondition(suites,
mockRemoveConditions(removeOlderThan, keepNVersions));
mockRemoveConditions(removeOlderThan, keepNVersions), new SystemLocalDateTime());
final boolean remove = condition.evaluate(suite);
assertFalse(remove);
}
Expand All @@ -208,7 +209,7 @@ public void evaluate_whenDuplicatedVersion_expectRemoveOnlyOldVersions(String al

Suite suite = mockSuiteVersionAndCreatedDate(evaluatedSuite, createdDaysAgo);
SuiteRemoveCondition condition = new SuiteRemoveCondition(suites,
mockRemoveConditions(removeOlderThan, keepNVersions));
mockRemoveConditions(removeOlderThan, keepNVersions), new SystemLocalDateTime());
final boolean remove = condition.evaluate(suite);
assertTrue(remove);
}
Expand All @@ -224,7 +225,7 @@ public void evaluate_whenDuplicatedVersion_expectKeepNewestVersion(String allSui

Suite suite = mockSuiteVersionAndCreatedDate(evaluatedSuite, createdDaysAgo);
SuiteRemoveCondition condition = new SuiteRemoveCondition(suites,
mockRemoveConditions(removeOlderThan, keepNVersions));
mockRemoveConditions(removeOlderThan, keepNVersions), new SystemLocalDateTime());
final boolean remove = condition.evaluate(suite);
assertFalse(remove);
}
Expand Down
7 changes: 7 additions & 0 deletions integration-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ or in `.../config/common/webdriver.properties` file.
To start the Bobcat tests, run `mvn clean test` from the `sanity-functional` directory level

[Chromedriver]: https://sites.google.com/a/chromium.org/chromedriver/

### cleaner-test

Cleaner Integration Tests are using [mocked OSGi context](https://sling.apache.org/documentation/development/osgi-mock.html)
and an [in-memory mock of MongoDB server](https://github.com/bwaldvogel/mongo-java-server).
Tests check various combinations of Cleaner parameters (versions to keep and suite max age)
and verify whether correct metadata and artifacts documents have been removed from database.
Loading

0 comments on commit 41cba3e

Please sign in to comment.