Commit e34026d3 authored by Rustam Lotsmanenko (EPAM)'s avatar Rustam Lotsmanenko (EPAM)
Browse files

GONRG-1638 unit tests + minor fixes + logs

parent 0abad538
......@@ -16,13 +16,18 @@
*/
package org.opengroup.osdu.backup.exception;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
@Slf4j
......@@ -33,6 +38,19 @@ public class AppExceptionHandler {
return this.getErrorResponse(e);
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, String> handleValidationExceptions(
MethodArgumentNotValidException ex) {
Map<String, String> errors = new HashMap<>();
ex.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return errors;
}
private ResponseEntity<Object> getErrorResponse(AppException e) {
String exceptionMsg = Objects.nonNull(e.getOriginalException())
......
......@@ -55,7 +55,7 @@ public class BackupCollection {
@NotNull(groups = CreateCollection.class)
@Positive(groups = {CreateCollection.class, UpdateCollection.class})
private Integer backupPeriod;
private Integer interval;
@NotNull(groups = CreateCollection.class)
@Positive(groups = {CreateCollection.class, UpdateCollection.class})
......
......@@ -50,7 +50,9 @@ public class BackupCollectionServiceImpl implements BackupCollectionService {
@Override
public BackupCollection saveCollection(BackupCollection collection) {
BackupCollection savedCollection = collectionRepository.submitCollection(collection);
schedulerService.addCollectionToScheduler(savedCollection);
if (Boolean.TRUE.equals(collection.isActive())) {
schedulerService.addCollectionToScheduler(collection);
}
return savedCollection;
}
......
......@@ -63,7 +63,11 @@ public class SchedulerServiceImpl implements SchedulerService {
@Override
public void addCollectionToScheduler(BackupCollection collection) {
PeriodicTrigger periodicTrigger = new PeriodicTrigger(collection.getBackupPeriod(),
if (Boolean.FALSE.equals(collection.isActive())) {
log.info("Collection : {} not activated, task won't be added to scheduler", collection.getCollectionName());
return;
}
PeriodicTrigger periodicTrigger = new PeriodicTrigger(collection.getInterval(),
schedulersProperties.getBackupTimeUnit());
periodicTrigger.setInitialDelay(schedulersProperties.getInitialDelay());
periodicTrigger.setFixedRate(true);
......@@ -73,16 +77,18 @@ public class SchedulerServiceImpl implements SchedulerService {
ScheduledFuture<?> schedule = poolTaskScheduler.schedule(collectionScheduledTask, periodicTrigger);
collectionTaskMap.put(collection, schedule);
log.info("Task for collection : {} added to scheduler", collection.getCollectionName());
log.info("Task for collection : {} added to scheduler, backup interval : {} , backup lifetime {}", collection.getCollectionName(),
collection.getInterval(), collection.getLifetime());
}
@Override
public void updateCollectionTask(BackupCollection collection) {
cancelCollectionTask(collection);
if (collection.isActive()) {
if (Boolean.TRUE.equals(collection.isActive())) {
addCollectionToScheduler(collection);
}
log.info("Task for collection : {} updated for scheduler", collection.getCollectionName());
log.info("Task for collection : {} updated in scheduler, task active : {}", collection.getCollectionName(),
collection.isActive());
}
@Override
......@@ -92,7 +98,8 @@ public class SchedulerServiceImpl implements SchedulerService {
if (Objects.nonNull(scheduledFuture)) {
cancel = scheduledFuture.cancel(false);
collectionTaskMap.remove(collection);
log.info("Task for collection : {} was canceled, no more backups will be produced further",collection.getCollectionName());
log.info("Task for collection : {} was canceled",
collection.getCollectionName());
}
return cancel;
}
......
/*
* Copyright 2020 Google LLC
* Copyright 2020 EPAM Systems, Inc
*
* 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
*
* https://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 org.opengroup.osdu.backup.service.impl;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.backup.locator.Asset;
import org.opengroup.osdu.backup.locator.AssetManagerFactory;
import org.opengroup.osdu.backup.manager.AssetBackupManager;
import org.opengroup.osdu.backup.model.BackupCollection;
import org.opengroup.osdu.backup.model.BackupStamp;
import org.opengroup.osdu.backup.model.CollectionState;
import org.opengroup.osdu.backup.repository.BackupCollectionRepository;
import org.opengroup.osdu.backup.repository.CollectionStateRepository;
import org.opengroup.osdu.backup.shedulers.SchedulerService;
@RunWith(MockitoJUnitRunner.class)
public class BackupCollectionServiceImplTest {
@Mock
BackupCollectionRepository collectionRepository;
@Mock
CollectionStateRepository collectionStateRepository;
@Mock
AssetManagerFactory assetManagerFactory;
@Mock
AssetBackupManager backupManager;
@Mock
SchedulerService schedulerService;
BackupCollection backupCollection;
CollectionState collectionState;
@InjectMocks
BackupCollectionServiceImpl backupCollectionService;
@Before
public void setUp() {
when(assetManagerFactory.getAssetManager(any())).thenReturn(backupManager);
}
@Test
public void startUpSchedulers() {
}
@Test
public void testSavedActiveCollectionShouldBeAddedToScheduler() {
backupCollection = BackupCollection.builder().active(true).build();
when(collectionRepository.submitCollection(backupCollection)).thenReturn(backupCollection);
BackupCollection collection = backupCollectionService.saveCollection(backupCollection);
assertEquals(backupCollection, collection);
verify(schedulerService, times(1)).addCollectionToScheduler(backupCollection);
}
@Test
public void testSavedActiveCollectionShouldNotBeAddedToScheduler() {
backupCollection = BackupCollection.builder().active(false).build();
when(collectionRepository.submitCollection(backupCollection)).thenReturn(backupCollection);
BackupCollection collection = backupCollectionService.saveCollection(backupCollection);
assertEquals(backupCollection, collection);
verify(schedulerService, never()).addCollectionToScheduler(backupCollection);
}
@Test
public void testRestoreCollectionState() {
BackupStamp backupStamp1 = BackupStamp.builder().stampId("1").assetType(Asset.DATASTORE).build();
BackupStamp backupStamp2 = BackupStamp.builder().stampId("2").assetType(Asset.DATASTORE).build();
collectionState = CollectionState.builder().backupStamps(Arrays.asList(backupStamp1, backupStamp2)).build();
when(collectionStateRepository.getStateById(any()))
.thenReturn(collectionState);
backupCollectionService.restoreCollection("stateId");
verify(backupManager, times(2)).importBackup(any());
}
}
\ No newline at end of file
/*
* Copyright 2020 Google LLC
* Copyright 2020 EPAM Systems, Inc
*
* 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
*
* https://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 org.opengroup.osdu.backup.shedulers;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.opengroup.osdu.backup.shedulers.TestConstants.COLLECTION_ID;
import static org.opengroup.osdu.backup.shedulers.TestConstants.COLLECTION_NAME;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.backup.locator.Asset;
import org.opengroup.osdu.backup.locator.AssetManagerFactory;
import org.opengroup.osdu.backup.manager.AssetBackupManager;
import org.opengroup.osdu.backup.model.BackupCollection;
import org.opengroup.osdu.backup.model.BackupSchedule;
import org.opengroup.osdu.backup.model.BackupStamp;
import org.opengroup.osdu.backup.model.CollectionState;
import org.opengroup.osdu.backup.repository.CollectionStateRepository;
@RunWith(MockitoJUnitRunner.class)
public class BackupCollectionScheduledTaskTest {
@Mock
CollectionStateRepository stateRepository;
@Mock
AssetManagerFactory assetManagerFactory;
@Mock
AssetBackupManager assetBackupManager;
BackupCollection backupCollection;
BackupSchedule backupSchedule;
BackupStamp backupStamp;
BackupCollectionScheduledTask scheduledTask;
@Before
public void setUp() {
backupSchedule = BackupSchedule.builder()
.assetType(Asset.DATASTORE)
.build();
backupCollection = BackupCollection.builder()
.collectionId(COLLECTION_ID)
.active(true)
.interval(1)
.collectionName(COLLECTION_NAME)
.lifetime(1)
.scheduleSet(Collections.singleton(backupSchedule))
.build();
backupStamp = BackupStamp
.builder()
.success(true)
.build();
scheduledTask = new BackupCollectionScheduledTask(assetManagerFactory, backupCollection, stateRepository);
when(assetManagerFactory.getAssetManager(any())).thenReturn(assetBackupManager);
when(assetBackupManager.exportBackupWithResult(any())).thenReturn(backupStamp);
}
@Test
public void testSubmittingCollectionStateWhenTaskFinished() {
scheduledTask.run();
ArgumentCaptor<CollectionState> captor = ArgumentCaptor.forClass(CollectionState.class);
verify(stateRepository, atLeast(1)).submitCollectionState(captor.capture());
CollectionState value = captor.getValue();
assertNotNull(value.getCollectionId());
}
}
\ No newline at end of file
/*
* Copyright 2020 Google LLC
* Copyright 2020 EPAM Systems, Inc
*
* 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
*
* https://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 org.opengroup.osdu.backup.shedulers;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.opengroup.osdu.backup.shedulers.TestConstants.COLLECTION_ID;
import static org.opengroup.osdu.backup.shedulers.TestConstants.COLLECTION_NAME;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.backup.config.property.SchedulersProperties;
import org.opengroup.osdu.backup.locator.Asset;
import org.opengroup.osdu.backup.locator.AssetManagerFactory;
import org.opengroup.osdu.backup.manager.AssetBackupManager;
import org.opengroup.osdu.backup.model.BackupCollection;
import org.opengroup.osdu.backup.model.BackupStamp;
import org.opengroup.osdu.backup.model.CollectionState;
import org.opengroup.osdu.backup.repository.BackupCollectionRepository;
import org.opengroup.osdu.backup.repository.CollectionStateRepository;
@RunWith(MockitoJUnitRunner.class)
public class TearDownBackupScheduledTaskTest {
@Mock
SchedulersProperties schedulersProperties;
@Mock
AssetManagerFactory assetManagerFactory;
@Mock
AssetBackupManager backupManager;
@Mock
CollectionStateRepository collectionStateRepository;
@Mock
BackupCollectionRepository collectionRepository;
BackupCollection backupCollection;
CollectionState outDatedState;
CollectionState notOutDatedState;
BackupStamp backupStamp;
@InjectMocks
TearDownBackupScheduledTask tearDownBackupScheduledTask;
@Before
public void setUp() {
backupCollection = BackupCollection.builder()
.collectionId(COLLECTION_ID)
.active(true)
.interval(1)
.collectionName(COLLECTION_NAME)
.lifetime(1)
.build();
backupStamp = BackupStamp.builder().assetType(Asset.DATASTORE).build();
outDatedState = CollectionState.builder().creationDate(LocalDateTime.MIN)
.backupStamps(Collections.singletonList(backupStamp)).build();
notOutDatedState = CollectionState.builder().creationDate(LocalDateTime.MAX)
.backupStamps(Collections.singletonList(backupStamp)).build();
when(schedulersProperties.getTearDownTimeUnit()).thenReturn(TimeUnit.MINUTES);
when(assetManagerFactory.getAssetManager(any())).thenReturn(backupManager);
when(collectionRepository.listBackupCollections()).thenReturn(Collections.singletonList(backupCollection));
when(collectionStateRepository.getCollectionsStatesByCollectionId(backupCollection.getCollectionId()))
.thenReturn(Arrays.asList(outDatedState, notOutDatedState));
}
@Test
public void shouldDeleteOnlyOutDatedCollectionState() {
tearDownBackupScheduledTask.run();
verify(collectionStateRepository,times(1)).deleteCollectionStates(Collections.singletonList(outDatedState));
verify(collectionStateRepository,never()).deleteCollectionStates(Collections.singletonList(notOutDatedState));
}
}
\ No newline at end of file
/*
* Copyright 2020 Google LLC
* Copyright 2020 EPAM Systems, Inc
*
* 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
*
* https://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 org.opengroup.osdu.backup.shedulers;
public class TestConstants {
public static final String COLLECTION_ID = "1";
public static final String COLLECTION_NAME = "new-collection";
}
/*
* Copyright 2020 Google LLC
* Copyright 2020 EPAM Systems, Inc
*
* 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
*
* https://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 org.opengroup.osdu.backup.shedulers.impl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.opengroup.osdu.backup.shedulers.TestConstants.COLLECTION_ID;
import static org.opengroup.osdu.backup.shedulers.TestConstants.COLLECTION_NAME;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.opengroup.osdu.backup.config.property.SchedulersProperties;
import org.opengroup.osdu.backup.locator.Asset;
import org.opengroup.osdu.backup.locator.AssetManagerFactory;
import org.opengroup.osdu.backup.manager.AssetBackupManager;
import org.opengroup.osdu.backup.model.BackupCollection;
import org.opengroup.osdu.backup.model.BackupSchedule;
import org.opengroup.osdu.backup.model.BackupStamp;
import org.opengroup.osdu.backup.model.CollectionState;
import org.opengroup.osdu.backup.model.StateStatus;
import org.opengroup.osdu.backup.repository.CollectionStateRepository;
@RunWith(MockitoJUnitRunner.class)
public class SchedulerServiceImplTest {
@Mock
SchedulersProperties schedulersProperties;
@Mock
AssetManagerFactory assetManagerFactory;
@Mock
CollectionStateRepository stateRepository;
@Mock
AssetBackupManager assetBackupManager;
BackupCollection backupCollection;
BackupStamp backupStamp;
BackupSchedule backupSchedule;
SchedulerServiceImpl schedulerService;
@Before
public void setUp() {
when(schedulersProperties.getBackupTimeUnit()).thenReturn(TimeUnit.SECONDS);
when(schedulersProperties.getInitialDelay()).thenReturn(0);
when(assetManagerFactory.getAssetManager(any())).thenReturn(assetBackupManager);
schedulerService = Mockito
.spy(new SchedulerServiceImpl(schedulersProperties, assetManagerFactory, stateRepository));
schedulerService.initThreadPool();
backupSchedule = BackupSchedule.builder()
.assetType(Asset.DATASTORE)
.build();
backupCollection = BackupCollection.builder()
.collectionId(COLLECTION_ID)
.active(true)
.interval(1)
.collectionName(COLLECTION_NAME)
.lifetime(1)
.scheduleSet(Collections.singleton(backupSchedule))
.build();
backupStamp = BackupStamp
.builder()
.success(true)
.build();
}
@Test
public void addNotActiveCollectionToSchedulerVerifyNoActivity() {
backupCollection = BackupCollection.builder().active(false).build();
schedulerService.addCollectionToScheduler(backupCollection);
verifyZeroInteractions(assetBackupManager);
}
@Test