Skip to content
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

Ticket7515 #1519

Merged
merged 13 commits into from
Feb 17, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* This file is part of the ISIS IBEX application.
* Copyright (C) 2012-2023 Science & Technology Facilities Council.
* All rights reserved.
*
* This program is distributed in the hope that it will be useful.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution.
* EXCEPT AS EXPRESSLY SET FORTH IN THE ECLIPSE PUBLIC LICENSE V1.0, THE PROGRAM
* AND ACCOMPANYING MATERIALS ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND. See the Eclipse Public License v1.0 for more details.
*
* You should have received a copy of the Eclipse Public License v1.0
* along with this program; if not, you can obtain a copy from
* https://www.eclipse.org/org/documents/epl-v10.php or
* http://opensource.org/licenses/eclipse-1.0.php
*/

package uk.ac.stfc.isis.ibex.configserver.tests.internal;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import org.junit.BeforeClass;
import org.junit.Test;

import uk.ac.stfc.isis.ibex.configserver.configuration.Block;
import uk.ac.stfc.isis.ibex.configserver.configuration.Configuration;
import uk.ac.stfc.isis.ibex.configserver.configuration.Group;
import uk.ac.stfc.isis.ibex.configserver.configuration.Ioc;
import uk.ac.stfc.isis.ibex.configserver.configuration.Macro;
import uk.ac.stfc.isis.ibex.configserver.configuration.Macro.HasDefault;
import uk.ac.stfc.isis.ibex.configserver.configuration.PVDefaultValue;
import uk.ac.stfc.isis.ibex.configserver.configuration.PVSet;
import uk.ac.stfc.isis.ibex.configserver.editing.EditableConfiguration;
import uk.ac.stfc.isis.ibex.configserver.editing.EditableIoc;
import uk.ac.stfc.isis.ibex.configserver.internal.ImportConverter;

public class ImportConverterTest {

private final static String SOURCE_PREFIX = "SOURCE";
private final static String DESTINATION_PREFIX = "DESTINATION";
private final static String COMPONENT_NAME = "COMP";
private final static String COMPONENT_DESC = "DESC";
private final static String IOC_NAME = "IOC_01";
private final static boolean IOC_AUTOSTART = true;
private final static boolean IOC_RESTART = true;

private final static Macro MACRO = new Macro("MACRO", "10", "DESC", "pattern", "1", HasDefault.YES);
private final static PVDefaultValue PV = new PVDefaultValue(SOURCE_PREFIX.concat(":PV"), "10");
private final static PVSet PV_SET = new PVSet(SOURCE_PREFIX.concat(":PV_SET"), true);
private final static Block BLOCK = new Block("BLOCK", SOURCE_PREFIX.concat(":PV"), true, true);
private final static Group GROUP = new Group("GROUP", Arrays.asList("BLOCK"), null);

private static Collection<EditableIoc> editableIocs;
private static Configuration source;
private static Configuration empty;
private static EditableConfiguration destination;


@BeforeClass
public static void setUp() {
editableIocs = new ArrayList<EditableIoc>();
EditableIoc editableIoc = new EditableIoc(IOC_NAME);
editableIoc.setAutostart(IOC_AUTOSTART);
editableIoc.setRestart(IOC_RESTART);
editableIoc.setMacros(Arrays.asList(MACRO));
editableIoc.setPvs(Arrays.asList(PV));
editableIoc.setPvSets(Arrays.asList(PV_SET));
editableIocs.add(editableIoc);

source = new Configuration(COMPONENT_NAME, COMPONENT_DESC, null, Arrays.asList(new Ioc(editableIoc)), Arrays.asList(BLOCK),
Arrays.asList(GROUP), Collections.emptyList(), Collections.emptyList(), false, false, false);
empty = new Configuration("EMPTY", "EMTPY DESC", null, Collections.emptyList(), Collections.emptyList(),
Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), false, false, false);
destination = new EditableConfiguration(empty, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());

ImportConverter.convert(source, destination, SOURCE_PREFIX, DESTINATION_PREFIX, editableIocs);
}

@Test
public void GIVEN_ioc_WHEN_component_converted_THEN_ioc_values_converted_correctly() {
var ioc = destination.getAddedIocs().stream().findFirst().get();
assertTrue(ioc.getName().equals(IOC_NAME));
assertTrue(ioc.getAutostart() == IOC_AUTOSTART);
assertTrue(ioc.getRestart() == IOC_RESTART);
assertTrue(ioc.getMacros().contains(MACRO));

var pv = ioc.getPvs().stream().findFirst().get();
assertTrue(pv.getName().equals(DESTINATION_PREFIX.concat(":PV")));
assertTrue(pv.getValue().equals(PV.getValue()));

var pvSet = ioc.getPvSets().stream().findFirst().get();
assertTrue(pvSet.getName().equals(DESTINATION_PREFIX.concat(":PV_SET")));
assertTrue(pvSet.getEnabled() == PV_SET.getEnabled());
}

@Test
public void GIVEN_block_WHEN_component_converted_THEN_block_values_converted_correctly() {
var block = destination.getAllBlocks().stream().findFirst().get();
assertTrue(block.getName().equals(BLOCK.getName()));
assertTrue(block.getPV().equals(DESTINATION_PREFIX.concat(":PV")));
assertTrue(block.getIsLocal());
assertTrue(block.getIsVisible());
}

@Test
public void GIVEN_group_with_block_WHEN_component_converted_THEN_group_values_converted_correctly() {
var group = destination.getEditableGroups().stream().findFirst().get();
assertTrue(group.getName().equals(GROUP.getName()));
assertTrue(group.getBlocks().stream().findFirst().get().equals(BLOCK.getName()));

var block = group.getSelectedBlocks().stream().findFirst().get();
assertTrue(block.getName().equals(block.getName()));
assertTrue(block.getPV().equals(DESTINATION_PREFIX.concat(":PV")));
}
}
3 changes: 2 additions & 1 deletion base/uk.ac.stfc.isis.ibex.configserver/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Export-Package: uk.ac.stfc.isis.ibex.configserver;
uk.ac.stfc.isis.ibex.configserver,
uk.ac.stfc.isis.ibex.configserver.configuration,
uk.ac.stfc.isis.ibex.epics.observing",
uk.ac.stfc.isis.ibex.configserver.internal
uk.ac.stfc.isis.ibex.configserver.internal,
uk.ac.stfc.isis.ibex.configserver.json
Import-Package: org.eclipse.wb.swt
Automatic-Module-Name: uk.ac.stfc.isis.ibex.configserver
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* This file is part of the ISIS IBEX application.
* Copyright (C) 2012-2023 Science & Technology Facilities Council.
* All rights reserved.
*
* This program is distributed in the hope that it will be useful.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution.
* EXCEPT AS EXPRESSLY SET FORTH IN THE ECLIPSE PUBLIC LICENSE V1.0, THE PROGRAM
* AND ACCOMPANYING MATERIALS ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND. See the Eclipse Public License v1.0 for more details.
*
* You should have received a copy of the Eclipse Public License v1.0
* along with this program; if not, you can obtain a copy from
* https://www.eclipse.org/org/documents/epl-v10.php or
* http://opensource.org/licenses/eclipse-1.0.php
*/

package uk.ac.stfc.isis.ibex.configserver.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;

import uk.ac.stfc.isis.ibex.configserver.configuration.Configuration;
import uk.ac.stfc.isis.ibex.configserver.configuration.PVDefaultValue;
import uk.ac.stfc.isis.ibex.configserver.configuration.PVSet;
import uk.ac.stfc.isis.ibex.configserver.editing.DuplicateBlockNameException;
import uk.ac.stfc.isis.ibex.configserver.editing.EditableBlock;
import uk.ac.stfc.isis.ibex.configserver.editing.EditableConfiguration;
import uk.ac.stfc.isis.ibex.configserver.editing.EditableIoc;
import uk.ac.stfc.isis.ibex.logger.IsisLog;
import uk.ac.stfc.isis.ibex.logger.LoggerUtils;

/**
* Handles the changes to be done when importing a component from another instrument.
*/
public final class ImportConverter {

private ImportConverter() { }

/**
* Adds the IOCs, blocks, and groups of the imported component to the local editable component.
*
* @param source The imported component.
* @param destination The editable component that will be added.
* @param sourcePrefix The remote instrument prefix.
* @param destinationPrefix The local instrument prefix.
* @param editableIocs The local IOCs.
*/
public static void convert(Configuration source, EditableConfiguration destination, String sourcePrefix, String destinationPrefix, Collection<EditableIoc> editableIocs) {
destination.setName(source.getName());
destination.setDescription(source.description());
destination.setIsProtected(source.isProtected());
destination.setIsDynamic(source.isDynamic());

convertIocs(source, destination, sourcePrefix, destinationPrefix, editableIocs);
convertBlocks(source, destination, sourcePrefix, destinationPrefix);
convertGroups(source, destination);
}

private static void convertIocs(Configuration source, EditableConfiguration destination, String sourcePrefix, String destinationPrefix, Collection<EditableIoc> editableIocs) {
for (var ioc : source.getIocs()) {
for (var editableIoc : editableIocs) {
if (ioc.getName().equals(editableIoc.getName())) {
editableIoc.setAutostart(ioc.getAutostart());
editableIoc.setRestart(ioc.getRestart());

editableIoc.setMacros(ioc.getMacros());

editableIoc.setPvs(ioc.getPvs().stream()
.map(e -> new PVDefaultValue(e.getName().replace(sourcePrefix, destinationPrefix), e.getValue()))
.collect(Collectors.toList()));

editableIoc.setPvSets(ioc.getPvSets().stream()
.map(e -> new PVSet(e.getName().replace(sourcePrefix, destinationPrefix), e.getEnabled()))
.collect(Collectors.toList()));

destination.addIoc(editableIoc);
break;
}
}
}
}

private static void convertBlocks(Configuration source, EditableConfiguration destination, String sourcePrefix, String destinationPrefix) {
for (var block : source.getBlocks()) {
var editableBlock = new EditableBlock(block);

if (editableBlock.getIsLocal()) {
editableBlock.setPV(editableBlock.getPV().replace(sourcePrefix, destinationPrefix));
}

try {
destination.addNewBlock(editableBlock);
} catch (DuplicateBlockNameException e) {
LoggerUtils.logErrorWithStackTrace(IsisLog.getLogger(ImportConverter.class), e.getMessage(), e);
new MessageDialog(Display.getCurrent().getActiveShell(), "Error", null,
"Failed to add block " + block.getName() + ":\nBlock with this name already exists.",
MessageDialog.ERROR, new String[] {"OK"}, 0).open();
}
}
}

private static void convertGroups(Configuration source, EditableConfiguration destination) {
// After blocks have been added to the destination configuration, add them to their respective groups.
// We need to use the newly added Editable Blocks when we toggle the selection for each new Editable Group.

var destinationBlocks = destination.getAllBlocks();
var sourceGroups = source.getGroups()
.stream()
.filter(g -> DisplayUtils.filterNoneGroup(g.getName()))
.collect(Collectors.toList());

// Create the destination groups.
for (var sourceGroup : sourceGroups) {
var newGroup = destination.addNewGroup();
newGroup.setName(sourceGroup.getName());

// Get the toggled blocks.
var blocksToToggle = new ArrayList<EditableBlock>();
for (var sourceBlockName : sourceGroup.getBlocks()) {
for (var destinationBlock : destinationBlocks) {
if (destinationBlock.getName().equals(sourceBlockName)) {
blocksToToggle.add(destinationBlock);
break;
}
}
}

newGroup.toggleSelection(blocksToToggle);
}
}
}
3 changes: 3 additions & 0 deletions base/uk.ac.stfc.isis.ibex.e4.client/Application.e4xmi
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</children>
<children xsi:type="menu:Menu" xmi:id="_W44gEGcTEeeM7uzkT_ViHQ" elementId="uk.ac.stfc.isis.ibex.e4.client.menu.components" label="Components" mnemonics="p">
<children xsi:type="menu:HandledMenuItem" xmi:id="_bjqNIGcTEeeM7uzkT_ViHQ" elementId="uk.ac.stfc.isis.ibex.e4.client.handledmenuitem.component.new" label="New" tooltip="Create a new component" mnemonics="N" command="_jpCRYHtiEeeIzM7lvAxMoQ"/>
<children xsi:type="menu:HandledMenuItem" xmi:id="_KHTXoH-pEe2cVZ02cKzrYw" elementId="uk.ac.stfc.isis.ibex.e4.client.handledmenuitem.component.import" label="Import" tooltip="Import a component from an instrument" mnemonics="I" command="_zZZcUH-oEe2cVZ02cKzrYw"/>
<children xsi:type="menu:DynamicMenuContribution" xmi:id="_dRsXgJDeEem0bruZliKGOw" elementId="uk.ac.stfc.isis.ibex.e4.client.dynamicmenucontribution.2" label="View or Edit Component (set by menu item)" contributionURI="bundleclass://uk.ac.stfc.isis.ibex.ui.configserver/uk.ac.stfc.isis.ibex.ui.configserver.commands.EditComponentHandler"/>
<children xsi:type="menu:HandledMenuItem" xmi:id="_cpZGIGcTEeeM7uzkT_ViHQ" elementId="uk.ac.stfc.isis.ibex.e4.client.handledmenuitem.component.delete" label="Delete" tooltip="Delete an existing component" mnemonics="D" command="_keEb8HtiEeeIzM7lvAxMoQ"/>
</children>
Expand Down Expand Up @@ -98,6 +99,7 @@
<handlers xmi:id="_Nm2tQHtaEeeIzM7lvAxMoQ" elementId="uk.ac.stfc.isis.ibex.e4.client.handler.configuration.edit" contributionURI="bundleclass://uk.ac.stfc.isis.ibex.ui.configserver/uk.ac.stfc.isis.ibex.ui.configserver.commands.EditConfigHandler" command="_FgXuUHtaEeeIzM7lvAxMoQ"/>
<handlers xmi:id="_vbnBMHtaEeeIzM7lvAxMoQ" elementId="uk.ac.stfc.isis.ibex.e4.client.handler.configuration.delete" contributionURI="bundleclass://uk.ac.stfc.isis.ibex.ui.configserver/uk.ac.stfc.isis.ibex.ui.configserver.commands.DeleteConfigsHandler" command="_kcuRwHtaEeeIzM7lvAxMoQ"/>
<handlers xmi:id="_gHbWYHtjEeeIzM7lvAxMoQ" elementId="uk.ac.stfc.isis.ibex.e4.client.handler.component.new" contributionURI="bundleclass://uk.ac.stfc.isis.ibex.ui.configserver/uk.ac.stfc.isis.ibex.ui.configserver.commands.NewComponentHandler" command="_jpCRYHtiEeeIzM7lvAxMoQ"/>
<handlers xmi:id="_km9iwH-oEe2cVZ02cKzrYw" elementId="uk.ac.stfc.isis.ibex.e4.client.handler.component.import" contributionURI="bundleclass://uk.ac.stfc.isis.ibex.ui.configserver/uk.ac.stfc.isis.ibex.ui.configserver.commands.ImportComponentHandler" command="_zZZcUH-oEe2cVZ02cKzrYw"/>
<handlers xmi:id="_lXgI4HtjEeeIzM7lvAxMoQ" elementId="uk.ac.stfc.isis.ibex.e4.client.handler.component.edit" contributionURI="bundleclass://uk.ac.stfc.isis.ibex.ui.configserver/uk.ac.stfc.isis.ibex.ui.configserver.commands.EditComponentHandler" command="_kNSN8HtiEeeIzM7lvAxMoQ"/>
<handlers xmi:id="_lxEfcHtjEeeIzM7lvAxMoQ" elementId="uk.ac.stfc.isis.ibex.e4.client.handler.component.delete" contributionURI="bundleclass://uk.ac.stfc.isis.ibex.ui.configserver/uk.ac.stfc.isis.ibex.ui.configserver.commands.DeleteComponentsHandler" command="_keEb8HtiEeeIzM7lvAxMoQ"/>
<handlers xmi:id="_TJHrkJCyEeiuzpdmZmmjoA" elementId="uk.ac.stfc.isis.ibex.e4.client.handler.configuration.recent" contributionURI="bundleclass://uk.ac.stfc.isis.ibex.ui.configserver/uk.ac.stfc.isis.ibex.ui.configserver.commands.RecentConfigsHandler" command="_A3_dkJCyEeiuzpdmZmmjoA"/>
Expand Down Expand Up @@ -612,6 +614,7 @@
<commands xmi:id="_FgXuUHtaEeeIzM7lvAxMoQ" elementId="uk.ac.stfc.isis.ibex.e4.client.configuration.edit" commandName="Edit or View configuration" description="Select and edit an existing configuration" category="_La_dMHtbEeeIzM7lvAxMoQ"/>
<commands xmi:id="_kcuRwHtaEeeIzM7lvAxMoQ" elementId="uk.ac.stfc.isis.ibex.e4.client.configuration.delete" commandName="Delete configuration" description="Delete an existing configuration" category="_La_dMHtbEeeIzM7lvAxMoQ"/>
<commands xmi:id="_jpCRYHtiEeeIzM7lvAxMoQ" elementId="uk.ac.stfc.isis.ibex.e4.client.component.new" commandName="New component" description="Create a new component" category="_DlkfcHtjEeeIzM7lvAxMoQ"/>
<commands xmi:id="_zZZcUH-oEe2cVZ02cKzrYw" elementId="uk.ac.stfc.isis.ibex.e4.client.component.import" commandName="Import component" description="Import a component from another instrument" category="_DlkfcHtjEeeIzM7lvAxMoQ"/>
<commands xmi:id="_kNSN8HtiEeeIzM7lvAxMoQ" elementId="uk.ac.stfc.isis.ibex.e4.client.component.edit" commandName="Edit or View component" description="Select and edit an existing component" category="_DlkfcHtjEeeIzM7lvAxMoQ"/>
<commands xmi:id="_keEb8HtiEeeIzM7lvAxMoQ" elementId="uk.ac.stfc.isis.ibex.e4.client.component.delete" commandName="Delete component" description="Delete an existing component" category="_DlkfcHtjEeeIzM7lvAxMoQ"/>
<commands xmi:id="_bjIjUHz5EeeSXZfE9ugy5A" elementId="uk.ac.stfc.isis.ibex.e4.client.command.waitfor" commandName="Wait for Instrument" description="Command indicating that the client is waiting for the instrument"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* This file is part of the ISIS IBEX application.
* Copyright (C) 2012-2023 Science & Technology Facilities Council.
* All rights reserved.
*
* This program is distributed in the hope that it will be useful.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution.
* EXCEPT AS EXPRESSLY SET FORTH IN THE ECLIPSE PUBLIC LICENSE V1.0, THE PROGRAM
* AND ACCOMPANYING MATERIALS ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND. See the Eclipse Public License v1.0 for more details.
*
* You should have received a copy of the Eclipse Public License v1.0
* along with this program; if not, you can obtain a copy from
* https://www.eclipse.org/org/documents/epl-v10.php or
* http://opensource.org/licenses/eclipse-1.0.php
*/

package uk.ac.stfc.isis.ibex.ui.configserver.editing.components.tests;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;

import uk.ac.stfc.isis.ibex.configserver.configuration.Configuration;
import uk.ac.stfc.isis.ibex.ui.configserver.editing.components.ComponentNameSearch;

public class ComponentNameSearchTest {

private ComponentNameSearch search;

private final String COMPONENT_NAME = "COMP_1";
private final Configuration component = mockComponent(COMPONENT_NAME);

private Configuration mockComponent(String name) {
Configuration component = mock(Configuration.class);
when(component.getName()).thenReturn(name);
return component;
}


@Before
public void setUp() {
search = new ComponentNameSearch();
}

@Test
public void GIVEN_empty_match_WHEN_search_THEN_match() {
search.setSearchText("");
assertTrue(search.select(null, null, component));
}

@Test
public void GIVEN_full_match_WHEN_search_THEN_match() {
search.setSearchText(COMPONENT_NAME);
assertTrue(search.select(null, null, component));
}

@Test
public void GIVEN_partial_match_WHEN_search_THEN_match() {
search.setSearchText(COMPONENT_NAME.substring(0, 2));
assertTrue(search.select(null, null, component));
}

@Test
public void GIVEN_no_match_WHEN_search_THEN_no_match() {
search.setSearchText("CONFIG_1");
assertFalse(search.select(null, null, component));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ Require-Bundle: org.eclipse.ui,
uk.ac.stfc.isis.ibex.logger,
uk.ac.stfc.isis.ibex.managermode,
org.csstudio.csdata;bundle-version="3.2.0",
org.csstudio.ui.util;bundle-version="4.0.2"
org.csstudio.ui.util;bundle-version="4.0.2",
uk.ac.stfc.isis.ibex.instrument
Bundle-RequiredExecutionEnvironment: JavaSE-17
Bundle-ActivationPolicy: lazy
Automatic-Module-Name: uk.ac.stfc.isis.ibex.ui.configserver
Import-Package: uk.ac.stfc.isis.ibex.ui.ioccontrol
Import-Package: uk.ac.stfc.isis.ibex.ui.ioccontrol,
uk.ac.stfc.isis.ibex.ui.mainmenu.instrument
Loading