### Question:
MapSelectionControl extends AbstractCanvasHandlerControl<H> implements SelectionControl<H, Element>,
                   CanvasRegistrationControl<H, Element> { @Override public Optional<Object> getSelectedItemDefinition() { String selectedItemUUID = null; final Collection<String> selectedItems = getSelectedItems(); if (null != selectedItems && !selectedItems.isEmpty()) { selectedItemUUID = selectedItems.iterator().next(); } if (Objects.isNull(selectedItemUUID)) { final AbstractCanvasHandler canvasHandler = getCanvasHandler(); if (!Objects.isNull(canvasHandler)) { final Diagram<?, ?> diagram = getCanvasHandler().getDiagram(); if (!Objects.isNull(diagram)) { final String cRoot = diagram.getMetadata().getCanvasRootUUID(); if (!StringUtils.isEmpty(cRoot)) { selectedItemUUID = cRoot; } } } } if (!Objects.isNull(selectedItemUUID)) { final Element<? extends Definition<?>> element = CanvasLayoutUtils.getElement(getCanvasHandler(), selectedItemUUID); return Optional.ofNullable(element); } else { return Optional.empty(); } } MapSelectionControl(final Consumer<CanvasSelectionEvent> selectionEventConsumer,
                        final Consumer<CanvasClearSelectionEvent> clearSelectionEventConsumer); static MapSelectionControl<H> build(final Consumer<CanvasSelectionEvent> selectionEventConsumer,
                                                                                 final Consumer<CanvasClearSelectionEvent> clearSelectionEventConsumer); @Override void clear(); @Override void register(final Element element); @Override void deregister(final Element element); @Override SelectionControl<H, Element> select(final String uuid); @Override SelectionControl<H, Element> deselect(final String uuid); @Override boolean isSelected(final Element element); @Override Collection<String> getSelectedItems(); @Override SelectionControl<H, Element> clearSelection(); @Override Optional<Object> getSelectedItemDefinition(); boolean isReadonly(); void setReadonly(boolean readonly); SelectionControl<H, Element> select(final Collection<String> uuids); SelectionControl<H, Element> deselect(final Collection<String> uuids); boolean isSelected(final String uuid); void onShapeRemoved(final CanvasShapeRemovedEvent shapeRemovedEvent); void onCanvasElementSelected(final CanvasSelectionEvent event); void onCanvasClearSelection(final CanvasClearSelectionEvent event); AbstractCanvasHandler getCanvasHandler(); Predicate<String> itemsRegistered(); AbstractCanvas getCanvas();  }### Answer:
@Test public void testGetSelectedItemDefinitionWithNoItemsSelected() { tested.init(canvasHandler); when(index.get(ROOT_UUID)).thenReturn(rootElement); final Optional<Object> selectedItemDefinition = tested.getSelectedItemDefinition(); assertTrue(selectedItemDefinition.isPresent()); assertEquals(rootElement, selectedItemDefinition.get()); }
@Test public void testGetSelectedItemDefinitionWithNoItemsSelectedAndNoDiagram() { tested.init(canvasHandler); when(canvasHandler.getDiagram()).thenReturn(null); final Optional<Object> selectedItemDefinition = tested.getSelectedItemDefinition(); assertFalse(selectedItemDefinition.isPresent()); }
@Test public void testDestroy() { tested.init(canvasHandler); tested.destroy(); assertFalse(tested.getSelectedItemDefinition().isPresent()); verify(canvas).removeHandler(any(MouseClickHandler.class)); }