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

        MastershipRole role = getRole(networkId, nodeId, deviceId);
        switch (role) {
            case MASTER:
                // no-op
                return CompletableFuture.completedFuture(null);
            case STANDBY:
            case NONE:
                NodeId prevMaster = masterMap.put(deviceId, nodeId);
                incrementTerm(networkId, deviceId);
                removeFromBackups(networkId, deviceId, nodeId);
                addToBackup(networkId, deviceId, prevMaster);
                break;
            default:
                log.warn("unknown Mastership Role {}", role);
                return null;
        }

        return CompletableFuture.completedFuture(
                new MastershipEvent(MASTER_CHANGED, deviceId, getMastership(networkId, deviceId)));
    }
@Test
    public void setMaster() {
        put(VNID1, VDID1, N1, false, false);
        assertEquals("wrong event", MASTER_CHANGED,
                     Futures.getUnchecked(sms.setMaster(VNID1, N1, VDID1)).type());
        assertEquals("wrong role", MASTER, sms.getRole(VNID1, N1, VDID1));
        //set node that's already master - should be ignored
        assertNull("wrong event",
                   Futures.getUnchecked(sms.setMaster(VNID1, N1, VDID1)));

        //set STANDBY to MASTER
        put(VNID1, VDID2, N1, false, true);
        assertEquals("wrong role", STANDBY, sms.getRole(VNID1, N1, VDID2));
        assertEquals("wrong event", MASTER_CHANGED,
                     Futures.getUnchecked(sms.setMaster(VNID1, N1, VDID2)).type());
        assertEquals("wrong role", MASTER, sms.getRole(VNID1, N1, VDID2));
    }