dybyte commented on code in PR #9968: URL: https://github.com/apache/seatunnel/pull/9968#discussion_r2495661084
########## seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/rest/RestApiHttpBasicTest.java: ########## @@ -0,0 +1,208 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.seatunnel.engine.server.rest; + +import org.apache.seatunnel.engine.common.config.SeaTunnelConfig; +import org.apache.seatunnel.engine.common.config.server.HttpConfig; +import org.apache.seatunnel.engine.common.runtime.ExecutionMode; +import org.apache.seatunnel.engine.common.utils.PassiveCompletableFuture; +import org.apache.seatunnel.engine.core.dag.logical.LogicalDag; +import org.apache.seatunnel.engine.core.job.JobImmutableInformation; +import org.apache.seatunnel.engine.server.AbstractSeaTunnelServerTest; +import org.apache.seatunnel.engine.server.SeaTunnelServer; +import org.apache.seatunnel.engine.server.SeaTunnelServerStarter; +import org.apache.seatunnel.engine.server.TestUtils; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import com.hazelcast.config.Config; +import com.hazelcast.internal.serialization.Data; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.util.Collections; +import java.util.stream.Collectors; + +/** Test for Rest API with Basic. */ +class RestApiHttpBasicTest extends AbstractSeaTunnelServerTest { + + private static final int HTTP_PORT = 18081; + private static final Long JOB_1 = System.currentTimeMillis() + 1L; + public static final String USER = "admin"; + public static final String PASS = "admin"; + + @BeforeAll + void setUp() { + String name = this.getClass().getName(); + Config hazelcastConfig = Config.loadFromString(getHazelcastConfig()); + hazelcastConfig.setClusterName( + TestUtils.getClusterName("RestApiServletHttpBasicTest_" + name)); + SeaTunnelConfig seaTunnelConfig = loadSeaTunnelConfig(); + seaTunnelConfig.setHazelcastConfig(hazelcastConfig); + seaTunnelConfig.getEngineConfig().setMode(ExecutionMode.LOCAL); + + HttpConfig httpConfig = seaTunnelConfig.getEngineConfig().getHttpConfig(); + httpConfig.setEnabled(Boolean.TRUE); + httpConfig.setPort(HTTP_PORT); + + httpConfig.setEnableBasicAuth(Boolean.TRUE); + httpConfig.setBasicAuthUsername(USER); + httpConfig.setBasicAuthPassword(PASS); + + instance = SeaTunnelServerStarter.createHazelcastInstance(seaTunnelConfig); + nodeEngine = instance.node.nodeEngine; + server = nodeEngine.getService(SeaTunnelServer.SERVICE_NAME); + LOGGER = nodeEngine.getLogger(AbstractSeaTunnelServerTest.class); + } + + @AfterAll + public void after() { + // Disable basic auth + // Because of the ConfigProvider.locateAndGetSeaTunnelConfig() single-case, + // if you change, other use cases will also change + // managed via org.apache.seatunnel.engine.common.config.YamlSeaTunnelDomConfigProcessor + SeaTunnelConfig seaTunnelConfig = loadSeaTunnelConfig(); + HttpConfig httpConfig = seaTunnelConfig.getEngineConfig().getHttpConfig(); + httpConfig.setEnableBasicAuth(Boolean.FALSE); + httpConfig.setBasicAuthUsername(""); + httpConfig.setBasicAuthPassword(""); + } + + @Test + public void testRestApiOverview() throws Exception { + HttpURLConnection conn = null; + try { + java.net.URL url = new java.net.URL("http://localhost:" + HTTP_PORT + "/overview"); + conn = (HttpURLConnection) url.openConnection(); + setBasicAuth(conn); + + Assertions.assertEquals(200, conn.getResponseCode()); + Assertions.assertTrue( + conn.getHeaderFields() + .get("Content-Type") + .toString() + .contains("charset=utf-8")); + } finally { + if (conn != null) { + conn.disconnect(); + } + } + } + + @Test + void testLogRestApiResponseFailure() throws IOException { + startJob(); + HttpURLConnection conn = null; + try { + java.net.URL url = + new java.net.URL("http://localhost:" + HTTP_PORT + "/logs?format=JSON"); Review Comment: I think we can reuse https://github.com/apache/seatunnel/blob/5681fe3bb7c7c5a99a275e2139e4d06b2d780b7b/seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/rest/RestConstant.java#L86 ########## seatunnel-engine/seatunnel-engine-server/src/main/java/org/apache/seatunnel/engine/server/rest/HttpBasic.java: ########## @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.seatunnel.engine.server.rest; + +import lombok.Data; + +@Data +public class HttpBasic { + public String basicUser; + public String basicPass; +} Review Comment: It seems this class duplicates `LogService.HttpBasic`. ########## seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/rest/RestApiHttpBasicTest.java: ########## @@ -0,0 +1,208 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.seatunnel.engine.server.rest; + +import org.apache.seatunnel.engine.common.config.SeaTunnelConfig; +import org.apache.seatunnel.engine.common.config.server.HttpConfig; +import org.apache.seatunnel.engine.common.runtime.ExecutionMode; +import org.apache.seatunnel.engine.common.utils.PassiveCompletableFuture; +import org.apache.seatunnel.engine.core.dag.logical.LogicalDag; +import org.apache.seatunnel.engine.core.job.JobImmutableInformation; +import org.apache.seatunnel.engine.server.AbstractSeaTunnelServerTest; +import org.apache.seatunnel.engine.server.SeaTunnelServer; +import org.apache.seatunnel.engine.server.SeaTunnelServerStarter; +import org.apache.seatunnel.engine.server.TestUtils; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import com.hazelcast.config.Config; +import com.hazelcast.internal.serialization.Data; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.util.Collections; +import java.util.stream.Collectors; + +/** Test for Rest API with Basic. */ +class RestApiHttpBasicTest extends AbstractSeaTunnelServerTest { + + private static final int HTTP_PORT = 18081; + private static final Long JOB_1 = System.currentTimeMillis() + 1L; + public static final String USER = "admin"; + public static final String PASS = "admin"; + + @BeforeAll + void setUp() { + String name = this.getClass().getName(); + Config hazelcastConfig = Config.loadFromString(getHazelcastConfig()); + hazelcastConfig.setClusterName( + TestUtils.getClusterName("RestApiServletHttpBasicTest_" + name)); + SeaTunnelConfig seaTunnelConfig = loadSeaTunnelConfig(); + seaTunnelConfig.setHazelcastConfig(hazelcastConfig); + seaTunnelConfig.getEngineConfig().setMode(ExecutionMode.LOCAL); + + HttpConfig httpConfig = seaTunnelConfig.getEngineConfig().getHttpConfig(); + httpConfig.setEnabled(Boolean.TRUE); + httpConfig.setPort(HTTP_PORT); + + httpConfig.setEnableBasicAuth(Boolean.TRUE); + httpConfig.setBasicAuthUsername(USER); + httpConfig.setBasicAuthPassword(PASS); + + instance = SeaTunnelServerStarter.createHazelcastInstance(seaTunnelConfig); + nodeEngine = instance.node.nodeEngine; + server = nodeEngine.getService(SeaTunnelServer.SERVICE_NAME); + LOGGER = nodeEngine.getLogger(AbstractSeaTunnelServerTest.class); + } + + @AfterAll + public void after() { + // Disable basic auth + // Because of the ConfigProvider.locateAndGetSeaTunnelConfig() single-case, + // if you change, other use cases will also change + // managed via org.apache.seatunnel.engine.common.config.YamlSeaTunnelDomConfigProcessor + SeaTunnelConfig seaTunnelConfig = loadSeaTunnelConfig(); + HttpConfig httpConfig = seaTunnelConfig.getEngineConfig().getHttpConfig(); + httpConfig.setEnableBasicAuth(Boolean.FALSE); + httpConfig.setBasicAuthUsername(""); + httpConfig.setBasicAuthPassword(""); + } + + @Test + public void testRestApiOverview() throws Exception { + HttpURLConnection conn = null; + try { + java.net.URL url = new java.net.URL("http://localhost:" + HTTP_PORT + "/overview"); + conn = (HttpURLConnection) url.openConnection(); + setBasicAuth(conn); + + Assertions.assertEquals(200, conn.getResponseCode()); + Assertions.assertTrue( + conn.getHeaderFields() + .get("Content-Type") + .toString() + .contains("charset=utf-8")); + } finally { + if (conn != null) { + conn.disconnect(); + } + } + } + + @Test + void testLogRestApiResponseFailure() throws IOException { + startJob(); + HttpURLConnection conn = null; + try { + java.net.URL url = + new java.net.URL("http://localhost:" + HTTP_PORT + "/logs?format=JSON"); + conn = (HttpURLConnection) url.openConnection(); + + Assertions.assertEquals(401, conn.getResponseCode()); + } finally { + if (conn != null) { + conn.disconnect(); + } + } + } + + @Test + void testLogRestApiResponseSuccess() throws IOException { + startJob(); + testLogRestApiResponse("JSON"); + } + + public void setBasicAuth(HttpURLConnection connection) { + // Basic Auth + String token = + java.util.Base64.getEncoder() + .encodeToString( + (USER + ":" + PASS) + .getBytes(java.nio.charset.StandardCharsets.UTF_8)); + connection.setRequestProperty("Authorization", "Basic " + token); + } + + public void testLogRestApiResponse(String format) throws IOException { + HttpURLConnection conn = null; + try { + java.net.URL url = + new java.net.URL("http://localhost:" + HTTP_PORT + "/logs?format=" + format); Review Comment: ditto -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
