SbloodyS commented on code in PR #10442: URL: https://github.com/apache/dolphinscheduler/pull/10442#discussion_r897561502
########## dolphinscheduler-api-test/dolphinscheduler-api-test-case/src/test/java/org/apache/dolphinscheduler/api.test/utils/RequestClient.java: ########## @@ -0,0 +1,171 @@ +/* + * Licensed to 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. Apache Software Foundation (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.dolphinscheduler.api.test.utils; + +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.apache.dolphinscheduler.api.test.core.Constants; +import org.apache.dolphinscheduler.api.test.entity.HttpResponse; +import org.apache.dolphinscheduler.api.test.entity.HttpResponseBody; +import org.testcontainers.shaded.okhttp3.FormBody; +import org.testcontainers.shaded.okhttp3.Headers; +import org.testcontainers.shaded.okhttp3.MediaType; +import org.testcontainers.shaded.okhttp3.OkHttpClient; +import org.testcontainers.shaded.okhttp3.Request; +import org.testcontainers.shaded.okhttp3.RequestBody; +import org.testcontainers.shaded.okhttp3.Response; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +@Slf4j +public class RequestClient { + + private OkHttpClient httpClient = null; + + public RequestClient() { + this.httpClient = new OkHttpClient(); + } + + @SneakyThrows + public HttpResponse get(String url, Map<String, String> headers, Map<String, Object> params) { + String requestUrl = String.format("%s%s%s", Constants.DOLPHINSCHEDULER_API_URL, url, getParams(params)); + + Headers headersBuilder = new Headers.Builder().build(); + if (headers != null) { + headersBuilder = Headers.of(headers); + } + + LOGGER.info("GET request to {}, Headers: {}", requestUrl, headersBuilder); + Request request = new Request.Builder() + .url(requestUrl) + .headers(headersBuilder) + .get() + .build(); + + Response response = this.httpClient.newCall(request).execute(); + + HttpResponseBody responseData = null; + int responseCode = response.code(); + if (response.body() != null) { + responseData = JSONUtils.parseObject(response.body().string(), HttpResponseBody.class); + } + response.close(); + + HttpResponse httpResponse = new HttpResponse(responseCode, responseData); + + LOGGER.info("GET response: {}", httpResponse); + + return httpResponse; + } + + public static String getParams(Map<String, Object> params) { + StringBuilder sb = new StringBuilder("?"); + if (params.size() > 0) { + for (Map.Entry<String, Object> item : params.entrySet()) { + Object value = item.getValue(); + if (Objects.nonNull(value)) { + sb.append("&"); + sb.append(item.getKey()); + sb.append("="); Review Comment: Fixed. ########## dolphinscheduler-api-test/dolphinscheduler-api-test-core/src/main/java/org/apache/dolphinscheduler/api/test/core/DolphinSchedulerExtension.java: ########## @@ -0,0 +1,79 @@ +/* + * Licensed to 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. Apache Software Foundation (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.dolphinscheduler.api.test.core; + +import java.io.File; +import java.net.URL; +import java.time.Duration; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.jupiter.api.extension.AfterAllCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.testcontainers.containers.DockerComposeContainer; +import org.testcontainers.containers.wait.strategy.Wait; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +final class DolphinSchedulerExtension implements BeforeAllCallback, AfterAllCallback { + private final boolean LOCAL_MODE = Objects.equals(System.getProperty("local"), "true"); + + private final String SERVICE_NAME = "dolphinscheduler_1"; + + private DockerComposeContainer<?> compose; + + @Override + public void beforeAll(ExtensionContext context) { + if (!LOCAL_MODE) { + compose = createDockerCompose(context); + compose.start(); + } + } + + @Override + public void afterAll(ExtensionContext context) { + if (compose != null) { + compose.stop(); + } + } + + private DockerComposeContainer<?> createDockerCompose(ExtensionContext context) { + final Class<?> clazz = context.getRequiredTestClass(); + final DolphinScheduler annotation = clazz.getAnnotation(DolphinScheduler.class); + final List<File> files = Stream.of(annotation.composeFiles()) + .map(it -> DolphinScheduler.class.getClassLoader().getResource(it)) + .filter(Objects::nonNull) + .map(URL::getPath) + .map(File::new) + .collect(Collectors.toList()); + + compose = new DockerComposeContainer<>(files) + .withPull(true) + .withTailChildContainers(true) + .withLogConsumer(SERVICE_NAME, outputFrame -> LOGGER.info(outputFrame.getUtf8String())) + .waitingFor(SERVICE_NAME, Wait.forHealthcheck().withStartupTimeout(Duration.ofSeconds(180))); Review Comment: Fixed. -- 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]
