001/* 002 * (C) Copyright 2018 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Kevin Leturc <[email protected]> 018 */ 019package org.nuxeo.ecm.core.test; 020 021import static org.nuxeo.runtime.migration.MigrationServiceImpl.KEYVALUE_STORE_NAME; 022import static org.nuxeo.runtime.migration.MigrationServiceImpl.PING_TIME; 023import static org.nuxeo.runtime.migration.MigrationServiceImpl.PROGRESS_MESSAGE; 024import static org.nuxeo.runtime.migration.MigrationServiceImpl.PROGRESS_NUM; 025import static org.nuxeo.runtime.migration.MigrationServiceImpl.PROGRESS_TOTAL; 026import static org.nuxeo.runtime.migration.MigrationServiceImpl.START_TIME; 027import static org.nuxeo.runtime.migration.MigrationServiceImpl.STEP; 028 029import java.util.HashSet; 030import java.util.Objects; 031import java.util.Set; 032import java.util.stream.Stream; 033 034import org.nuxeo.runtime.api.Framework; 035import org.nuxeo.runtime.kv.KeyValueService; 036import org.nuxeo.runtime.kv.KeyValueStore; 037import org.nuxeo.runtime.test.runner.Deploy; 038import org.nuxeo.runtime.test.runner.FeaturesRunner; 039import org.nuxeo.runtime.test.runner.RunnerFeature; 040 041/** 042 * Feature for migration service. 043 * 044 * @since 10.2 045 */ 046@Deploy("org.nuxeo.runtime.kv") 047@Deploy("org.nuxeo.runtime.migration") 048public class MigrationFeature implements RunnerFeature { 049 050 protected static final String[] KEYS = { STEP, START_TIME, PING_TIME, PROGRESS_MESSAGE, PROGRESS_NUM, 051 PROGRESS_TOTAL }; 052 053 protected final Set<String> migrationIds = new HashSet<>(); 054 055 @Override 056 public void afterTeardown(FeaturesRunner runner) { 057 KeyValueStore kv = getKeyValueStore(); 058 // clear the changed migration states 059 migrationIds.stream() // 060 .flatMap(id -> Stream.of(KEYS).map(id::concat)) 061 .forEach(key -> kv.put(key, (String) null)); 062 migrationIds.clear(); 063 } 064 065 public void changeStatus(String migrationId, String state) { 066 KeyValueStore kv = getKeyValueStore(); 067 // get the initial state - allow to call simulateRunning several times 068 migrationIds.add(migrationId); 069 kv.put(migrationId + STEP, state); 070 kv.put(migrationId + START_TIME, String.valueOf(System.currentTimeMillis())); 071 kv.put(migrationId + PING_TIME, String.valueOf(System.currentTimeMillis())); 072 kv.put(migrationId + PROGRESS_MESSAGE, "Set from MigrationFeature"); 073 kv.put(migrationId + PROGRESS_NUM, String.valueOf(0)); 074 kv.put(migrationId + PROGRESS_TOTAL, String.valueOf(0)); 075 } 076 077 protected static KeyValueStore getKeyValueStore() { 078 KeyValueService service = Framework.getService(KeyValueService.class); 079 Objects.requireNonNull(service, "Missing KeyValueService"); 080 return service.getKeyValueStore(KEYVALUE_STORE_NAME); 081 } 082 083}