All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lordofthejars.nosqlunit.vault.VaultAssertion Maven / Gradle / Ivy

package com.lordofthejars.nosqlunit.vault;

import com.lordofthejars.nosqlunit.core.FailureHandler;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static com.lordofthejars.nosqlunit.util.DeepEquals.deepEquals;

public class VaultAssertion {

    private static final String TOKENS = "tokens";

    public static void strictAssertEquals(final InputStream dataset, final VaultConnection vault) {

        final Yaml yaml = new Yaml();
        final Object load = yaml.load(dataset);

        if (areOnlySecrets(load)) {
            final List>> secrets = (List) load;

            checkSecretsRegisteredByTokens(secrets, vault);
            assertSecrets(secrets, vault);
        }

    }

    private static void checkSecretsRegisteredByTokens(List>> data, VaultConnection vault) {
        final Optional>> tokens = data.stream()
                .flatMap(e -> e.entrySet().stream())
                .filter(e -> TOKENS.equals(e.getKey()))
                .findFirst();

        if (tokens.isPresent()) {
            final List> tokenDefinitions = (List>) tokens.get().getValue();

            for (final Map tokenDefinition : tokenDefinitions) {

                if (tokenDefinition.containsKey("uuid")) {
                    final String uuid = asString(tokenDefinition, "uuid");
                    if (tokenDefinition.containsKey("secrets")) {
                        List>> secrets = (List>>) tokenDefinition.get("secrets");
                        try {
                            vault.updateToken(uuid);
                            assertSecrets(secrets, vault);
                        } finally {
                            vault.reconnectToOriginal();
                        }
                    }
                }
            }
        }
    }

    private static void assertSecrets(List>> expectedSecrets, VaultConnection vaultConnection) {
        for (Map> secret : expectedSecrets) {

            final Set>> entries = secret.entrySet();

            for (Map.Entry> entry : entries) {
                String backend = entry.getKey();
                if (! TOKENS.equals(backend)) {
                    final Map real = vaultConnection.readLogical(backend);

                    final Map expected = toStringMap(entry.getValue());
                    if (!deepEquals(real, expected)) {
                        throw FailureHandler.createFailure(
                                "Expected element # %s # is not found but # %s # was found.",
                                expected, real);
                    }
                }
            }
        }
    }

    private static boolean areOnlySecrets(Object load) {
        return load instanceof List;
    }
    private static String asString(Map elements, String key) {
        if (elements.containsKey(key)) {
            return (String) elements.get(key);
        }

        return null;
    }

    private static Map toStringMap(Map elements) {
        return elements.entrySet()
                .stream()
                .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().toString()));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy