/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */

package org.elasticsearch.xpack.transform;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.license.XPackLicenseState;
import org.elasticsearch.test.ESTestCase;

import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.mock;

public class TransformTests extends ESTestCase {

    public void testNodeAttributes() {
        Settings.Builder builder = Settings.builder();
        boolean transformEnabled = true;
        boolean remoteEnabled = true;

        if (randomBoolean()) {
            transformEnabled = randomBoolean();
            if (randomBoolean()) {
                builder.put("node.transform", transformEnabled);
                if (randomBoolean()) {
                    // note: the case where node.transform: true and xpack.transform.enabled: false is benign
                    builder.put("xpack.transform.enabled", randomBoolean());
                }
            } else {
                builder.put("xpack.transform.enabled", transformEnabled);
            }
        }

        if (randomBoolean()) {
            remoteEnabled = randomBoolean();
            builder.put("cluster.remote.connect", remoteEnabled);
        }

        builder.put("node.attr.some_other_attrib", "value");
        Transform transform = createTransform(builder.build());
        assertNotNull(transform.additionalSettings());
        assertEquals(transformEnabled, Boolean.parseBoolean(transform.additionalSettings().get("node.attr.transform.node")));
        assertEquals(
            transformEnabled && remoteEnabled,
            Boolean.parseBoolean(transform.additionalSettings().get("node.attr.transform.remote_connect"))
        );
    }

    public void testNodeAttributesDirectlyGiven() {
        Settings.Builder builder = Settings.builder();

        if (randomBoolean()) {
            builder.put("node.attr.transform.node", randomBoolean());
        } else {
            builder.put("node.attr.transform.remote_connect", randomBoolean());
        }

        Transform transform = createTransform(builder.build());
        IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> transform.additionalSettings());
        assertThat(
            e.getMessage(),
            equalTo("Directly setting transform node attributes is not permitted, please use the documented node settings instead")
        );
    }

    private Transform createTransform(Settings settings) {
        XPackLicenseState licenseState = mock(XPackLicenseState.class);

        return new Transform(settings) {
            @Override
            protected XPackLicenseState getLicenseState() {
                return licenseState;
            }
        };
    }

}

@@ -691,20 +691,31 @@
 abled = 
-true
+randomBoolean()
 ;%0A      
@@ -728,43 +728,33 @@
 ean 
-remoteEnabled = true;%0A%0A        if (
+transformPluginEnabled = 
 rand
@@ -756,35 +756,33 @@
  randomBoolean()
-) %7B
+;
 %0A            tra
@@ -770,37 +770,38 @@
 ();%0A        
-    transform
+boolean remote
 Enabled = ra
@@ -811,32 +811,33 @@
 mBoolean();%0A
+%0A
         
     if (rand
@@ -820,32 +820,101 @@
 );%0A%0A        
-    if (
+// randomly use explicit or default setting%0A        if ((transformEnabled && 
 randomBoolea
@@ -910,38 +910,44 @@
 andomBoolean()) 
-%7B%0A    
+== false) %7B%0A
             buil
@@ -1003,207 +1003,92 @@
     
-        if (randomBoolean()) %7B%0A                    // note: the case where node.transform: true and xpack.transform.enabled: false is benign%0A                    builder.put(%22xpack.transform.e
+%7D%0A%0A        // randomly use explicit or default setting%0A        if ((remoteE
 nabled
-%22,
+ &&
  ran
@@ -1104,46 +1104,18 @@
 n())
-;%0A                %7D%0A            %7D e
+ == fa
 lse
+)
  %7B%0A 
@@ -1117,36 +1117,32 @@
 ) %7B%0A            
-   
- 
 builder.put(%22xpa
@@ -1142,66 +1142,48 @@
 ut(%22
-xpack.transform.enabled%22, transformEnabled);%0A            %7D
+cluster.remote.connect%22, remoteEnabled);
 %0A   
@@ -1206,128 +1206,109 @@
 if (
+t
 ran
-domBoolean()) %7B%0A            remoteEnabled = randomBoolean();%0A            builder.put(%22cluster.remote.connect%22, remote
+sformPluginEnabled == false) %7B%0A            builder.put(%22xpack.transform.enabled%22, transformPlugin
 Enab
@@ -1525,16 +1525,55 @@
 tEquals(
+%0A            transformPluginEnabled && 
 transfor
@@ -1581,16 +1581,28 @@
 Enabled,
+%0A           
  Boolean
@@ -1678,16 +1678,25 @@
 .node%22))
+%0A        
 );%0A     
@@ -1725,32 +1725,38 @@
        transform
+Plugin
 Enabled && remot

fix test logic in transform tests (#53002)