/*
 * Copyright 2012 Splunk, Inc.
 *
 * 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.splunk;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ConfigurationTest extends SDKTestCase {

    private String applicationName;
    private Application application;

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();
        
        // We cannot straightforwardly delete confs,
        // so instead we create a temporary application,
        // reconnect using its namespace, then delete it
        // when we're done.
        applicationName = createTemporaryName();
        service.getApplications().create(applicationName);
        
        service.logout();
        
        ConnectionArgs applicationArgs = new ConnectionArgs();
        applicationArgs.putAll(connectionArgs);
        applicationArgs.put("sharing", "app");
        applicationArgs.put("owner", "nobody");
        applicationArgs.put("app", applicationName);
        service = Service.connect(applicationArgs);
        
        application = service.getApplications().get(applicationName);
    }

    @After
    @Override
    public void tearDown() throws Exception {
        application.remove();
        service.app = null;
        clearRestartMessage();
        
        super.tearDown();
    }

    @Test
    public void testStandardFilesExist() {
        ConfCollection confs = service.getConfs();
        assertTrue(confs.containsKey("eventtypes"));
        assertTrue(confs.containsKey("indexes"));
        assertTrue(confs.containsKey("inputs"));
        assertTrue(confs.containsKey("props"));
        assertTrue(confs.containsKey("transforms"));
        assertTrue(confs.containsKey("savedsearches"));

        for (Entity stanza : confs.get("indexes").values()) {
            assertNotNull(stanza);
            assertNotNull(stanza.getName());
            assertNotNull(stanza.getPath());
        }
    }

    @Test
    public void testCreateConfWorks() {
        ConfCollection confs = service.getConfs();
        String confName = createTemporaryName();

        assertFalse("New configuration name already used.", confs.containsKey(confName));
        EntityCollection<Entity> conf = confs.create(confName);
        assertTrue("New configuration doesn't show up after creation.", confs.containsKey(confName));
        assertEquals("New configuration is not empty.", 0, conf.size());
    }

    @Test
    public void testCreateAndDeleteStanzaWorks() {
        ConfCollection confs = service.getConfs();
        String confName = createTemporaryName();
        EntityCollection<Entity> conf = confs.create(confName);

        String stanzaName = createTemporaryName();
        assertFalse("Stanza already exists.", conf.containsKey(stanzaName));
        Entity stanza = conf.create(stanzaName);

        assertTrue("Stanza doesn't show up after creation.", conf.containsKey(stanzaName));
        assertEquals("Stanza contains something besides eai: and disabled attributes when first created.", 5, stanza.size());

        stanza.remove();
        conf.refresh();
        assertFalse("Stanza not deleted by remove()", conf.containsKey(stanzaName));
    }

    @Test
    public void testWriteToStanza() {
        ConfCollection confs = service.getConfs();
        String confName = createTemporaryName();
        EntityCollection<Entity> conf = confs.create(confName);
        String stanzaName = createTemporaryName();
        Entity stanza = conf.create(stanzaName);

        assertEquals("Stanza begins with only eai:* and disabled in it.", 5, stanza.size());
        Args args = new Args();
        args.put("test-key", "42");
        stanza.update(args);
        assertEquals("Stanza has right size after write.", 6, stanza.size());
        assertEquals("Read from key gives right value.", "42", stanza.get("test-key"));
    }
}

@@ -737,16 +737,47 @@
 tCase %7B%0A
+    private Service appService;
 %0A    pri
@@ -1139,16 +1139,24 @@
 e done.%0A
+        
         
@@ -1255,24 +1255,25 @@
 nName);%0A
+%0A
         
 %0A       
@@ -1268,43 +1268,496 @@
     
-%0A        service.logout();%0A        
+// We cannot easily change the namespace of service,%0A        // so rather than fight with it, we create a new%0A        // service with the right namespace and work from it%0A        // instead. Then we'll return to service in tearDown%0A        // to clean up.%0A%0A        // NOTE: If you ever have to put a restart into%0A        // the configuration tests, then you will need%0A        // to explicitly re-login appService, since it will%0A        // not be re-logged in by the call to restartSplunk.
 %0A   
@@ -2016,25 +2016,28 @@
 e);%0A        
-s
+appS
 ervice = Ser
@@ -2075,22 +2075,143 @@
     
+%7D%0A%0A
     
-%0A        a
+@After%0A    @Override%0A    public void tearDown() throws Exception %7B%0A        appService.logout();%0A        final EntityCollection%3CA
 ppli
@@ -2216,16 +2216,22 @@
 lication
+%3E apps
  = servi
@@ -2250,20 +2250,37 @@
 ations()
-.get
+;%0A        apps.remove
 (applica
@@ -2290,38 +2290,88 @@
 nName);%0A    
-%7D%0A%0A
     
-@After%0A
+assertEventuallyTrue(new EventuallyTrueBehavior() %7B%0A        
     @Overrid
@@ -2367,36 +2367,32 @@
    @Override
-%0A   
  public 
 void tearDow
@@ -2383,99 +2383,145 @@
 lic 
-void tearDown() throws Exception %7B%0A        application.remove();%0A        service.app = null
+boolean predicate() %7B%0A                apps.refresh();%0A                return !apps.containsKey(applicationName);%0A            %7D%0A        %7D)
 ;%0A  
@@ -2667,33 +2667,36 @@
 lection confs = 
-s
+appS
 ervice.getConfs(
@@ -3285,33 +3285,36 @@
 lection confs = 
-s
+appS
 ervice.getConfs(
@@ -3786,33 +3786,36 @@
 lection confs = 
-s
+appS
 ervice.getConfs(
@@ -4544,25 +4544,28 @@
 ion confs = 
-s
+appS
 ervice.getCo

Fix how configuration tests handle getting a new service with a new namespace.