Skip to content

Commit

Permalink
Fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
georgweiss committed May 17, 2024
1 parent 6b6166c commit 16ed203
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.phoebus.applications.saveandrestore.ui.VNoData;
import org.phoebus.applications.saveandrestore.ui.VTypePair;
import org.phoebus.core.vtypes.VDisconnectedData;
import org.phoebus.core.vtypes.VTypeHelper;
import org.phoebus.framework.jobs.JobManager;
import org.phoebus.ui.dialog.DialogHelper;
import org.phoebus.util.time.TimestampFormats;
Expand All @@ -58,6 +59,7 @@
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -402,6 +404,65 @@ public void restore(Snapshot snapshot, Consumer<List<RestoreResult>> completion)
});
}

/**
* Restores a snapshot from client.
*
* @param snapshot The {@link Snapshot} object subject to restore
* @param completion A handler for the outcome of the restore operation
*/
@SuppressWarnings("unused")
public void restoreFromClient(Snapshot snapshot, Consumer<List<String>> completion) {
new Thread(() -> {
List<String> restoreFailedPVNames = new ArrayList<>();
CountDownLatch countDownLatch = new CountDownLatch(snapshot.getSnapshotData().getSnapshotItems().size());
snapshot.getSnapshotData().getSnapshotItems()
.forEach(e -> pvs.get(getPVKey(e.getConfigPv().getPvName(), e.getConfigPv().isReadOnly())).setCountDownLatch(countDownLatch));

for (SnapshotItem entry : snapshot.getSnapshotData().getSnapshotItems()) {
TableEntry e = tableEntryItems.get(getPVKey(entry.getConfigPv().getPvName(), entry.getConfigPv().isReadOnly()));

boolean restorable = e.selectedProperty().get() &&
!e.readOnlyProperty().get() &&
entry.getValue() != null &&
!entry.getValue().equals(VNoData.INSTANCE);

if (restorable) {
final SaveAndRestorePV pv = pvs.get(getPVKey(e.pvNameProperty().get(), e.readOnlyProperty().get()));
if (entry.getValue() != null) {
try {
pv.getPv().write(VTypeHelper.toObject(entry.getValue()));
} catch (Exception writeException) {
restoreFailedPVNames.add(entry.getConfigPv().getPvName());
} finally {
pv.countDown();
}
}
} else {
countDownLatch.countDown();
}
}

try {
countDownLatch.await(10, TimeUnit.MINUTES);
} catch (InterruptedException e) {
LOGGER.log(Level.INFO, "Encountered InterruptedException", e);
}

if (restoreFailedPVNames.isEmpty()) {
LOGGER.log(Level.FINE, "Restored snapshot {0}", snapshot.getSnapshotNode().getName());
} else {
Collections.sort(restoreFailedPVNames);
StringBuilder sb = new StringBuilder(restoreFailedPVNames.size() * 200);
restoreFailedPVNames.forEach(e -> sb.append(e).append('\n'));
LOGGER.log(Level.WARNING,
"Not all PVs could be restored for {0}: {1}. The following errors occurred:\n{2}",
new Object[]{snapshot.getSnapshotNode().getName(), snapshot.getSnapshotNode(), sb.toString()});
}
completion.accept(restoreFailedPVNames);
}).start();
}


public void setShowDeltaPercentage(boolean showDeltaPercentage) {
this.showDeltaPercentage.set(showDeltaPercentage);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
package org.phoebus.service.saveandrestore.web.controllers;
/*
* Copyright (C) 2023 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/

package org.phoebus.service.saveandrestore.epics;

import org.junit.jupiter.api.Assertions;

Expand All @@ -9,18 +28,26 @@
import org.epics.vtype.Alarm;
import org.epics.vtype.Display;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.phoebus.applications.saveandrestore.model.ConfigPv;
import org.phoebus.applications.saveandrestore.model.SnapshotItem;
import org.phoebus.core.vtypes.VTypeHelper;
import org.phoebus.pv.PV;
import org.phoebus.pv.PVPool;
import org.phoebus.service.saveandrestore.epics.SnapshotRestorer;
import org.phoebus.service.saveandrestore.web.config.ControllersTestConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SnapshotRestorerTestConfig.class)
public class SnapshotRestorerTest {

@Autowired
private SnapshotRestorer snapshotRestorer;

@Test
public void testRestorePVValues() throws Exception {
var snapshotRestorer = new SnapshotRestorer();
PV pv = PVPool.getPV("loc://x(42.0)");
var configPv = new ConfigPv();
configPv.setPvName("loc://x");
Expand All @@ -36,7 +63,6 @@ public void testRestorePVValues() throws Exception {

@Test
public void testCannotConnectPV() throws Exception {
var snapshotRestorer = new SnapshotRestorer();
var configPv = new ConfigPv();
configPv.setPvName("pva://x");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (C) 2018 European Spallation Source ERIC.
* <p>
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

package org.phoebus.service.saveandrestore.epics;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import org.mockito.Mockito;
import org.phoebus.service.saveandrestore.persistence.dao.NodeDAO;
import org.phoebus.service.saveandrestore.persistence.dao.impl.elasticsearch.ConfigurationDataRepository;
import org.phoebus.service.saveandrestore.persistence.dao.impl.elasticsearch.ElasticsearchTreeRepository;
import org.phoebus.service.saveandrestore.persistence.dao.impl.elasticsearch.FilterRepository;
import org.phoebus.service.saveandrestore.persistence.dao.impl.elasticsearch.SnapshotDataRepository;
import org.phoebus.service.saveandrestore.search.SearchUtil;
import org.phoebus.service.saveandrestore.web.config.AcceptHeaderResolver;
import org.phoebus.service.saveandrestore.web.config.WebSecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.Scope;
import org.springframework.util.Base64Utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SpringBootConfiguration
@ComponentScan(basePackages = "org.phoebus.service.saveandrestore.epics")
@SuppressWarnings("unused")
@Profile("!IT")
public class SnapshotRestorerTestConfig {

@Bean
public SnapshotRestorer snapshotRestorer(){
return new SnapshotRestorer();
}

@Bean
public ExecutorService executorService(){
return Executors.newCachedThreadPool();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import org.mockito.Mockito;
import org.phoebus.service.saveandrestore.epics.SnapshotRestorer;
import org.phoebus.service.saveandrestore.persistence.dao.NodeDAO;
import org.phoebus.service.saveandrestore.persistence.dao.impl.elasticsearch.ConfigurationDataRepository;
import org.phoebus.service.saveandrestore.persistence.dao.impl.elasticsearch.ElasticsearchTreeRepository;
Expand All @@ -32,8 +33,12 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.Scope;
import org.springframework.util.Base64Utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SpringBootConfiguration
@ComponentScan(basePackages = "org.phoebus.service.saveandrestore.web.controllers")
@Import(WebSecurityConfig.class)
Expand Down Expand Up @@ -115,4 +120,16 @@ public String adminAuthorization() {
public String readOnlyAuthorization() {
return "Basic " + Base64Utils.encodeToString((demoReadOnly + ":" + demoReadOnlyPassword).getBytes());
}

@SuppressWarnings("unused")
@Bean
@Scope("singleton")
public SnapshotRestorer snapshotRestorer(){
return new SnapshotRestorer();
}

@Bean
public ExecutorService executorService(){
return Executors.newCachedThreadPool();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testRestoreFromSnapshotNode() throws Exception {

when(nodeDAO.getSnapshotData("uniqueId")).thenReturn(snapshotData);

MockHttpServletRequestBuilder request = post("/restore/node?parentNodeId=uniqueId")
MockHttpServletRequestBuilder request = post("/restore/node?nodeId=uniqueId")
.header(HttpHeaders.AUTHORIZATION, userAuthorization);

MvcResult result = mockMvc.perform(request).andExpect(status().isOk()).andExpect(content().contentType(JSON))
Expand Down

0 comments on commit 16ed203

Please sign in to comment.