/*
 * #%L
 * wcm.io
 * %%
 * Copyright (C) 2014 wcm.io
 * %%
 * 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.
 * #L%
 */
package io.wcm.testing.mock.aem.context;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import io.wcm.testing.mock.aem.junit.AemContext;

import org.apache.sling.testing.mock.sling.loader.ContentLoader;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.day.cq.wcm.api.Page;

public class AemContextImplTest {

  @Rule
  public AemContext context = TestAemContext.newAemContextIncludingJackrabbit();

  private String contentRoot;
  private String appsRoot;

  @Before
  public void setUp() throws Exception {
    contentRoot = context.uniqueRoot().content() + "/sample";
    appsRoot = context.uniqueRoot().apps() + "/sample";

    ContentLoader contentLoader = this.context.load();
    contentLoader.json("/json-import-samples/application.json", appsRoot);
    contentLoader.json("/json-import-samples/content.json", contentRoot);
  }

  @After
  public void tearDown() throws Exception {
    this.context.tearDown();
  }

  @Test
  public void testContextObjects() {
    assertNotNull(context.pageManager());
  }

  @Test
  public void testSetCurrentPage() {
    context.currentPage(contentRoot + "/toolbar/profiles");
    assertEquals(contentRoot + "/toolbar/profiles", context.currentPage().getPath());
    assertEquals(contentRoot + "/toolbar/profiles/jcr:content", context.currentResource().getPath());

    context.currentPage(context.pageManager().getPage(contentRoot + "/toolbar"));
    assertEquals(contentRoot + "/toolbar", context.currentPage().getPath());
    assertEquals(contentRoot + "/toolbar/jcr:content", context.currentResource().getPath());

    context.currentPage((Page)null);
    assertNull(context.currentPage());

    context.currentPage((String)null);
    assertNull(context.currentPage());
  }

  @Test(expected = IllegalArgumentException.class)
  public void testSetCurrentPageNonExisting() {
    context.currentPage("/non/existing");
  }

}

@@ -843,24 +843,141 @@
 emContext;%0A%0A
+import org.apache.sling.api.resource.ResourceResolver;%0Aimport org.apache.sling.api.resource.ResourceResolverFactory;%0A
 import org.a
@@ -2752,10 +2752,321 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void testServiceLogin() throws Exception %7B%0A    ResourceResolverFactory resourceResolverFactory = context.getService(ResourceResolverFactory.class);%0A    ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(null);%0A    assertNotNull(resolver);%0A    resolver.close();%0A  %7D%0A%0A
 %7D%0A

add test case for service user login