/*
 * Copyright Terracotta, 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 org.terracotta.dynamic_config.system_tests.activated;

import com.terracotta.diagnostic.Diagnostics;
import org.junit.Test;
import org.terracotta.connection.Connection;
import org.terracotta.connection.ConnectionFactory;
import org.terracotta.connection.ConnectionPropertyNames;
import org.terracotta.connection.entity.EntityRef;
import org.terracotta.dynamic_config.test_support.ClusterDefinition;
import org.terracotta.dynamic_config.test_support.DynamicConfigIT;

import java.util.Properties;

import static java.util.Collections.singletonList;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.terracotta.angela.client.support.hamcrest.AngelaMatchers.successful;

@ClusterDefinition (autoActivate = true)
public class DiagnosticsIT extends DynamicConfigIT {
    @Test
    public void testGetConfigByAddingOffheapResource() throws Exception {
        final String newOffheapName = "new-test-offheap-resource";

        Properties properties = new Properties();
        properties.setProperty(ConnectionPropertyNames.CONNECTION_TYPE, "diagnostic");

        try (Connection connection =
                     ConnectionFactory.connect(singletonList(getNodeAddress(1, 1)), properties)) {
            EntityRef<Diagnostics, Object, Void> ref = connection.getEntityRef(Diagnostics.class, 1, "root");
            Diagnostics diagnostics = ref.fetchEntity(null);
            assertThat(diagnostics.getConfig(), not(containsString(newOffheapName)));
        }


        assertThat(configToolInvocation("set", "-s", "localhost:" + getNodePort(), "-c", "offheap-resources=" + newOffheapName + ":1GB"), is(successful()));

        try (Connection connection =
                     ConnectionFactory.connect(singletonList(getNodeAddress(1, 1)), properties)) {
            EntityRef<Diagnostics, Object, Void> ref = connection.getEntityRef(Diagnostics.class, 1, "root");
            Diagnostics diagnostics = ref.fetchEntity(null);
            assertThat(diagnostics.getConfig(), containsString(newOffheapName));
        }
    }
}

@@ -1520,18 +1520,16 @@
 T %7B%0A
-  
   @Test%0A
     
@@ -1524,18 +1524,16 @@
   @Test%0A
-  
   public
@@ -1600,20 +1600,16 @@
 n %7B%0A    
-  
-  
 final St
@@ -1660,20 +1660,16 @@
 urce%22;%0A%0A
-    
     Prop
@@ -1706,20 +1706,16 @@
 ties();%0A
-    
     prop
@@ -1786,28 +1786,24 @@
 gnostic%22);%0A%0A
-    
     try (Con
@@ -1832,32 +1832,24 @@
             
-   
-     
 ConnectionFa
@@ -1906,38 +1906,32 @@
  properties)) %7B%0A
-      
       EntityRef%3C
@@ -2016,38 +2016,32 @@
  %22root%22);%0A      
- 
-     
 Diagnostics diag
@@ -2065,38 +2065,32 @@
 chEntity(null);%0A
-      
       assertThat
@@ -2161,20 +2161,11 @@
     
-    %7D%0A%0A%0A    
+%7D%0A%0A
     
@@ -2314,20 +2314,16 @@
 l()));%0A%0A
-    
     try 
@@ -2343,32 +2343,24 @@
 onnection =%0A
-        
             
@@ -2444,22 +2444,16 @@
 %7B%0A      
-  
-    
 EntityRe
@@ -2542,22 +2542,16 @@
 root%22);%0A
-      
       Di
@@ -2603,22 +2603,16 @@
 ;%0A      
- 
-     
 assertTh
@@ -2680,16 +2680,10 @@
     
-    %7D%0A  
+%7D%0A
   %7D%0A

fix indentation