/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with this
 * work for additional information regarding copyright ownership. The ASF
 * licenses this file to You 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 org.apache.sling.feature.cpconverter.handlers;

import static org.mockito.Mockito.when;

import java.io.IOException;

import org.apache.jackrabbit.vault.fs.io.Archive;
import org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
import org.apache.sling.feature.cpconverter.artifacts.ArtifactsDeployer;
import org.apache.sling.feature.cpconverter.features.FeaturesManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;

/**
 * Base class for tests against BundleEntryHandler.
 * Must be executed with MockitoJUnitRunner.
 *
 */
public abstract class AbstractBundleEntryHandlerTest {

    @Rule
    public TemporaryFolder tmpFolder = new TemporaryFolder();

    @Mock 
    Archive.Entry entry;
    @Mock 
    Archive archive;
    @Spy 
    ContentPackage2FeatureModelConverter converter;
    @Spy 
    FeaturesManager featuresManager;
    
    BundleEntryHandler handler;

    protected void setUpArchive(String entryPath, String resourcePath) throws IOException {
        when(entry.getName()).thenReturn(entryPath);
        when(archive.openInputStream(entry)).thenReturn(BundleEntryHandlerGAVTest.class.getResourceAsStream(resourcePath));
    }

    @Before
    public void setUp() throws IOException {
        handler = new BundleEntryHandler();
        handler.setSlingInitialContentPolicy(ContentPackage2FeatureModelConverter.SlingInitialContentPolicy.KEEP);
        ArtifactsDeployer deployer = Mockito.spy(ArtifactsDeployer.class);
        when(converter.getArtifactsDeployer()).thenReturn(deployer);
        when(converter.getTempDirectory()).thenReturn(tmpFolder.getRoot());
        featuresManager = Mockito.spy(FeaturesManager.class);
        when(converter.getFeaturesManager()).thenReturn(featuresManager);
    }

    @After
    public void tearDown() throws IOException {
        converter.close();
    }

}
