/*
 * 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.jackrabbit.oak.plugins.document;

import java.util.Map;

import org.apache.jackrabbit.oak.api.jmx.ConsolidatedDataStoreCacheStatsMBean;
import org.apache.jackrabbit.oak.plugins.blob.AbstractSharedCachingDataStore;
import org.apache.jackrabbit.oak.plugins.blob.ConsolidatedDataStoreCacheStats;
import org.apache.jackrabbit.oak.spi.blob.BlobStore;
import org.apache.jackrabbit.oak.spi.state.NodeStore;
import org.apache.jackrabbit.oak.stats.StatisticsProvider;
import org.apache.sling.testing.mock.osgi.ReferenceViolationException;
import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.osgi.framework.ServiceRegistration;

import static com.google.common.collect.Maps.newHashMap;
import static org.apache.sling.testing.mock.osgi.MockOsgi.deactivate;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;

/**
 * Tests the registration of the {@link ConsolidatedDataStoreCacheStatsMBean}.
 */
public class DocumentCachingDataStoreStatsTest {

    @Rule
    public OsgiContext context = new OsgiContext();

    @Rule
    public ExpectedException expectedEx = ExpectedException.none();

    @BeforeClass
    public static void checkMongoDbAvailable() {
        Assume.assumeTrue(MongoUtils.isAvailable());
    }

    @Before
    public void setUp() {
        context.registerService(StatisticsProvider.class, StatisticsProvider.NOOP);
    }

    @Test
    public void testUseCachingBlobStore() {
        ServiceRegistration delegateReg =
            context.bundleContext().registerService(AbstractSharedCachingDataStore.class.getName(),
                mock(AbstractSharedCachingDataStore.class), null);
        assertNotNull(context.getService(AbstractSharedCachingDataStore.class));
        registerBlobStore();

        registerDocumentNodeStoreService(true);
        assertServiceActivated();

        ConsolidatedDataStoreCacheStats dataStoreStats =
            context.registerInjectActivateService(new ConsolidatedDataStoreCacheStats(), null);
        assertNotNull(context.getService(ConsolidatedDataStoreCacheStatsMBean.class));

        deactivate(dataStoreStats);
        unregisterDocumentNodeStoreService();
        unregisterBlobStore();
        delegateReg.unregister();
    }

    @Test
    public void testNoCachingBlobStore() {
        expectedEx.expect(ReferenceViolationException.class);

        registerBlobStore();

        registerDocumentNodeStoreService(true);
        assertServiceActivated();

        ConsolidatedDataStoreCacheStats dataStoreStats =
            context.registerInjectActivateService(new ConsolidatedDataStoreCacheStats(), null);
        assertNull(context.getService(ConsolidatedDataStoreCacheStatsMBean.class));

        unregisterDocumentNodeStoreService();
        unregisterBlobStore();
    }

    private DocumentNodeStoreService documentNodeStoreService;

    private void registerDocumentNodeStoreService(boolean customBlobStore) {
        Map<String, Object> properties = newHashMap();

        properties.put("mongouri", MongoUtils.URL);
        properties.put("db", MongoUtils.DB);
        properties.put(DocumentNodeStoreService.CUSTOM_BLOB_STORE, customBlobStore);
        documentNodeStoreService =
            context.registerInjectActivateService(new DocumentNodeStoreService(), properties);
    }

    private void unregisterDocumentNodeStoreService() {
        deactivate(documentNodeStoreService);
    }

    private ServiceRegistration blobStore;

    private void registerBlobStore() {
        blobStore = context.bundleContext()
            .registerService(BlobStore.class.getName(), mock(BlobStore.class), null);
    }

    private void unregisterBlobStore() {
        blobStore.unregister();
    }

    private void assertServiceActivated() {
        assertNotNull(context.getService(NodeStore.class));
    }
}

@@ -854,16 +854,65 @@
 ument;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0A
 import j
@@ -925,16 +925,16 @@
 il.Map;%0A
-
 %0Aimport 
@@ -1620,32 +1620,72 @@
 ectedException;%0A
+import org.junit.rules.TemporaryFolder;%0A
 import org.osgi.
@@ -2248,24 +2248,148 @@
 on.none();%0A%0A
+    @Rule%0A    public final TemporaryFolder target = new TemporaryFolder(new File(%22target%22));%0A%0A    private String repoHome;%0A%0A
     @BeforeC
@@ -2537,16 +2537,35 @@
  setUp()
+ throws IOException
  %7B%0A     
@@ -2643,16 +2643,73 @@
 .NOOP);%0A
+        repoHome = target.newFolder().getAbsolutePath();%0A
     %7D%0A%0A 
@@ -3780,32 +3780,50 @@
 ceActivated();%0A%0A
+        try %7B%0A    
         Consolid
@@ -3855,32 +3855,36 @@
 ataStoreStats =%0A
+    
             cont
@@ -3967,24 +3967,28 @@
 l);%0A        
+  
+  
 assertNull(c
@@ -4043,33 +4043,56 @@
 sMBean.class));%0A
-%0A
+        %7D finally %7B%0A    
         unregist
@@ -4121,32 +4121,36 @@
 vice();%0A        
+    
 unregisterBlobSt
@@ -4152,24 +4152,34 @@
 lobStore();%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -4422,16 +4422,16 @@
 s.URL);%0A
-
         
@@ -4467,16 +4467,69 @@
 ls.DB);%0A
+        properties.put(%22repository.home%22, repoHome);%0A
         

Implement caching for S3DataStore