/*
 * Asset Share Commons
 *
 * Copyright (C) 2019 Adobe
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.adobe.aem.commons.assetshare.content.impl;

import com.adobe.aem.commons.assetshare.content.MetadataProperties;

import io.wcm.testing.mock.aem.junit.AemContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.util.*;

import static org.junit.Assert.assertEquals;

public class MetadataSchemaPropertiesImplTest {

    @Rule
    public final AemContext ctx = new AemContext();

    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void collectExtraMetadataProperties() {
        ctx.registerInjectActivateService(new MetadataSchemaPropertiesImpl(),
                Collections.unmodifiableMap(new HashMap<String, Object>() {{
                        put("extra.metadata.properties", new String[]{"jcr:content/foo=My Foo", "jcr:content/metadata/bar=My Bar", "./jcr:content/foo=My Foo 2"});
                    }}));
        MetadataSchemaPropertiesImpl metadataSchemaProperties = (MetadataSchemaPropertiesImpl) ctx.getService(MetadataProperties.class);

        Map<String, List<String>> collectedMetadata = new HashMap<>();
        collectedMetadata = metadataSchemaProperties.collectExtraMetadataProperties(collectedMetadata);

        assertEquals(2, collectedMetadata.size());

        assertEquals(2, collectedMetadata.get("./jcr:content/foo").size());
        assertEquals("My Foo", collectedMetadata.get("./jcr:content/foo").get(0));
        assertEquals("My Foo 2", collectedMetadata.get("./jcr:content/foo").get(1));

        assertEquals(1, collectedMetadata.get("./jcr:content/metadata/bar").size());
        assertEquals("My Bar", collectedMetadata.get("./jcr:content/metadata/bar").get(0));
    }

    @Test
    public void removeMetadataProperties() {
        ctx.registerInjectActivateService(new MetadataSchemaPropertiesImpl(),
                Collections.unmodifiableMap(new HashMap<String, Object>() {{
                    put("blacklisted.metadata.properties", new String[]{"jcr:content/foo"});
                }}));
        MetadataSchemaPropertiesImpl metadataSchemaProperties = (MetadataSchemaPropertiesImpl) ctx.getService(MetadataProperties.class);

        Map<String, List<String>> collectedMetadata = new HashMap<>();
        collectedMetadata.put("jcr:content/foo", Collections.singletonList("Blacklisted"));
        collectedMetadata.put("./jcr:content/foo", Collections.singletonList("Blacklisted Too"));
        collectedMetadata.put("./jcr:content/metadata/bar", Collections.singletonList("Not blacklisted"));

        collectedMetadata = metadataSchemaProperties.removeBlacklistedMetadataProperties(collectedMetadata);

        assertEquals(1, collectedMetadata.size());
        assertEquals(1, collectedMetadata.get("./jcr:content/metadata/bar").size());
        assertEquals("Not blacklisted", collectedMetadata.get("./jcr:content/metadata/bar").get(0));
    }

    @Test
    public void collectMetadataProperty() {
    }

}