protected Permission toPermission(final Node node) {
        final Permission permission = new Permission();
        if(node.getPermissions() != null) {
            switch(node.getType()) {
                case FOLDER:
                case ROOM:
                    if(node.getPermissions().isCreate()
                            // For existing files the delete role is also required to overwrite
                            && node.getPermissions().isDelete()) {
                        permission.setUser(Permission.Action.all);
                    }
                    else {
                        permission.setUser(Permission.Action.read.or(Permission.Action.execute));
                    }
                    break;
                case FILE:
                    if(node.isIsEncrypted() != null && node.isIsEncrypted()) {
                        try {
                            if(null != session.keyPair()) {
                                permission.setUser(Permission.Action.none.or(Permission.Action.read));
                            }
                            else {
                                log.warn(String.format("Missing read permission for node %s with missing key pair", node));
                            }
                        }
                        catch(BackgroundException e) {
                            log.warn(String.format("Ignore failure %s retrieving key pair", e));
                        }
                    }
                    else {
                        if(node.getPermissions().isRead()) {
                            permission.setUser(Permission.Action.read);
                        }
                    }
                    if(node.getPermissions().isChange() && node.getPermissions().isDelete()) {
                        permission.setUser(permission.getUser().or(Permission.Action.write));
                    }
                    break;
            }
            if(log.isDebugEnabled()) {
                log.debug(String.format("Map node permissions %s to %s", node.getPermissions(), permission));
            }
        }
        return permission;
    }
@Test
    public void testPermissionsFolder() throws Exception {
        final SDSAttributesAdapter f = new SDSAttributesAdapter(session);
        final Node node = new Node();
        node.setIsEncrypted(false);
        node.setType(Node.TypeEnum.FOLDER);
        final NodePermissions permissions = new NodePermissions().read(false).delete(false).change(false).create(false);
        node.setPermissions(permissions);
        assertTrue(f.toPermission(node).isReadable());
        assertFalse(f.toPermission(node).isWritable());
        assertTrue(f.toPermission(node).isExecutable());
        permissions.setRead(true);
        assertTrue(f.toPermission(node).isReadable());
        assertFalse(f.toPermission(node).isWritable());
        assertTrue(f.toPermission(node).isExecutable());
        permissions.setChange(true);
        assertTrue(f.toPermission(node).isReadable());
        assertFalse(f.toPermission(node).isWritable());
        assertTrue(f.toPermission(node).isExecutable());
        permissions.setDelete(true);
        assertTrue(f.toPermission(node).isReadable());
        assertFalse(f.toPermission(node).isWritable());
        assertTrue(f.toPermission(node).isExecutable());
        permissions.setCreate(true);
        assertTrue(f.toPermission(node).isReadable());
        assertTrue(f.toPermission(node).isWritable());
        assertTrue(f.toPermission(node).isExecutable());
    }