@Override
    public CompletableFuture<MastershipEvent> setStandby(NetworkId networkId,
                                                         NodeId nodeId, DeviceId deviceId) {
        Map<DeviceId, NodeId> masterMap = getMasterMap(networkId);

        MastershipRole role = getRole(networkId, nodeId, deviceId);
        switch (role) {
            case MASTER:
                NodeId backup = reelect(networkId, deviceId, nodeId);
                if (backup == null) {
                    // no master alternative
                    masterMap.remove(deviceId);
                    // TODO: Should there be new event type for no MASTER?
                    return CompletableFuture.completedFuture(
                            new MastershipEvent(MASTER_CHANGED, deviceId,
                                getMastership(networkId, deviceId)));
                } else {
                    NodeId prevMaster = masterMap.put(deviceId, backup);
                    incrementTerm(networkId, deviceId);
                    addToBackup(networkId, deviceId, prevMaster);
                    return CompletableFuture.completedFuture(
                            new MastershipEvent(MASTER_CHANGED, deviceId,
                                getMastership(networkId, deviceId)));
                }

            case STANDBY:
            case NONE:
                boolean modified = addToBackup(networkId, deviceId, nodeId);
                if (modified) {
                    return CompletableFuture.completedFuture(
                            new MastershipEvent(BACKUPS_CHANGED, deviceId,
                                getMastership(networkId, deviceId)));
                }
                break;

            default:
                log.warn("unknown Mastership Role {}", role);
        }
        return null;
    }
@Test
    public void unsetMaster() {
        //NONE - record backup but take no other action
        put(VNID1, VDID1, N1, false, false);
        sms.setStandby(VNID1, N1, VDID1);
        assertTrue("not backed up", sms.backupsByNetwork.get(VNID1)
                .get(VDID1).contains(N1));
        int prev = sms.termMapByNetwork.get(VNID1).get(VDID1).get();
        sms.setStandby(VNID1, N1, VDID1);
        assertEquals("term should not change", prev, sms.termMapByNetwork.get(VNID1)
                .get(VDID1).get());

        //no backup, MASTER
        put(VNID1, VDID1, N1, true, false);
        assertNull("expect no MASTER event",
                   Futures.getUnchecked(sms.setStandby(VNID1, N1, VDID1)).roleInfo().master());
        assertNull("wrong node", sms.masterMapByNetwork.get(VNID1).get(VDID1));

        //backup, switch
        sms.masterMapByNetwork.get(VNID1).clear();
        put(VNID1, VDID1, N1, true, true);
        put(VNID1, VDID1, N2, false, true);
        put(VNID1, VDID2, N2, true, true);
        MastershipEvent event = Futures.getUnchecked(sms.setStandby(VNID1, N1, VDID1));
        assertEquals("wrong event", MASTER_CHANGED, event.type());
        assertEquals("wrong master", N2, event.roleInfo().master());
    }