// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.xml;

import com.yahoo.component.ComponentId;
import com.yahoo.config.model.builder.xml.test.DomBuilderTest;
import com.yahoo.container.core.AccessLogConfig;
import com.yahoo.container.logging.VespaAccessLog;
import com.yahoo.container.logging.YApacheAccessLog;
import com.yahoo.vespa.model.container.ContainerCluster;
import com.yahoo.vespa.model.container.component.Component;
import org.junit.Test;
import org.w3c.dom.Element;

import static com.yahoo.text.StringUtilities.quote;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;

/**
 * @author gjoranv
 * @since 5.5
 */
public class AccessLogTest extends ContainerModelBuilderTestBase {

    @Test
    public void default_access_log_is_only_added_when_search_is_present() throws Exception {
        Element cluster1Elem = DomBuilderTest.parse(
                "<jdisc id='cluster1' version='1.0'>",
                "<search />",
                nodesXml,
                "</jdisc>");
        Element cluster2Elem = DomBuilderTest.parse(
                "<jdisc id='cluster2' version='1.0'>",
                "  <nodes>",
                "    <node hostalias='mockhost' baseport='1234' />",
                "  </nodes>",
                "</jdisc>" );

        createModel(root, cluster1Elem, cluster2Elem);

        assertNotNull(getVespaAccessLog("cluster1"));
        assertNull(   getVespaAccessLog("cluster2"));
    }

    @Test
    public void default_search_access_log_can_be_disabled() throws Exception {
        final String jdiscClusterId = "jdisc-cluster";

        Element clusterElem = DomBuilderTest.parse(
                "<jdisc id=" + quote(jdiscClusterId) + " version='1.0'>" +
                        "  <search />" +
                        "  <accesslog type='disabled' />" +
                        "</jdisc>" );

        createModel(root, clusterElem);
        assertNull(getVespaAccessLog(jdiscClusterId));
    }

    private Component<?, ?> getVespaAccessLog(String clusterName) {
        ContainerCluster cluster = (ContainerCluster) root.getChildren().get(clusterName);
        return cluster.getComponentsMap().get(ComponentId.fromString((VespaAccessLog.class.getName())));

    }

    @Test
    public void access_log_can_be_configured() throws Exception {
        Element clusterElem = DomBuilderTest.parse(
                "<jdisc id='default' version='1.0'>",
                "  <accesslog type='yapache' ",
                "             fileNamePattern='pattern' rotationInterval='interval'",
                "             rotationScheme='date' />",
                nodesXml,
                "</jdisc>" );

        createModel(root, clusterElem);

        Component<?, ?> accessLogComponent = getContainerComponent("default", YApacheAccessLog.class.getName());
        assertNotNull(accessLogComponent);
        assertThat(accessLogComponent.getClassId().getName(), is(YApacheAccessLog.class.getName()));

        AccessLogConfig config = root.getConfig(AccessLogConfig.class, "default/component/com.yahoo.container.logging.YApacheAccessLog");
        AccessLogConfig.FileHandler fileHandlerConfig = config.fileHandler();
        assertThat(fileHandlerConfig.pattern(), is("pattern"));
        assertThat(fileHandlerConfig.rotation(), is("interval"));
        assertThat(fileHandlerConfig.rotateScheme(), is(AccessLogConfig.FileHandler.RotateScheme.DATE));

    }

}

@@ -305,16 +305,66 @@
 Config;%0A
+import com.yahoo.container.logging.JSONAccessLog;%0A
 import c
@@ -684,52 +684,8 @@
 te;%0A
-import static org.hamcrest.CoreMatchers.is;%0A
 impo
@@ -806,20 +806,22 @@
 t.assert
-That
+Equals
 ;%0A%0A/**%0A 
@@ -2813,24 +2813,212 @@
 'date' /%3E%22,%0A
+                %22  %3Caccesslog type='json' %22,%0A                %22             fileNamePattern='pattern' rotationInterval='interval'%22,%0A                %22             rotationScheme='date' /%3E%22,%0A
             
@@ -3095,32 +3095,57 @@
  clusterElem);%0A%0A
+        %7B // yapache%0A    
         Componen
@@ -3241,32 +3241,36 @@
 ame());%0A        
+    
 assertNotNull(ac
@@ -3296,27 +3296,67 @@
         
+    
 assert
-That(
+Equals(YApacheAccessLog.class.getName(), 
 accessLo
@@ -3390,19 +3390,16 @@
 Name(), 
-is(
 YApacheA
@@ -3423,20 +3423,22 @@
 tName())
-)
 ;%0A
-%0A
+    
         
@@ -3567,32 +3567,36 @@
 sLog%22);%0A        
+    
 AccessLogConfig.
@@ -3653,35 +3653,52 @@
 ();%0A        
+    
 assert
-That(
+Equals(%22pattern%22, 
 fileHandlerC
@@ -3716,22 +3716,75 @@
 rn()
-, is(%22pattern%22
+);%0A            assertEquals(%22interval%22, fileHandlerConfig.rotation(
 ));%0A
@@ -3795,63 +3795,646 @@
     
+  
+  
 assert
-That(fileHandlerConfig.rotation(), is(%22interval%22)
+Equals(AccessLogConfig.FileHandler.RotateScheme.DATE, fileHandlerConfig.rotateScheme());%0A        %7D%0A%0A        %7B // json%0A            Component%3C?, ?%3E accessLogComponent = getContainerComponent(%22default%22, JSONAccessLog.class.getName());%0A            assertNotNull(accessLogComponent);%0A            assertEquals(JSONAccessLog.class.getName(), accessLogComponent.getClassId().getName(), JSONAccessLog.class.getName());%0A            AccessLogConfig config = root.getConfig(AccessLogConfig.class, %22default/component/com.yahoo.container.logging.JSONAccessLog%22);%0A            AccessLogConfig.FileHandler fileHandlerConfig = config.fileHandler(
 );%0A 
@@ -4444,104 +4444,252 @@
     
+  
+  
 assert
-That(fileHandlerConfig.rotateScheme(), is(AccessLogConfig.FileHandler.RotateScheme.DATE));
+Equals(%22pattern%22, fileHandlerConfig.pattern());%0A            assertEquals(%22interval%22, fileHandlerConfig.rotation());%0A            assertEquals(AccessLogConfig.FileHandler.RotateScheme.DATE, fileHandlerConfig.rotateScheme());%0A        %7D%0A%0A
 %0A%0A  

Test json log configuring