Repository: apache/flink
File: flink-kubernetes/src/test/java/org/apache/flink/kubernetes/kubeclient/decorators/ExternalServiceDecoratorTest.java (Java)

Code before:
import io.fabric8.kubernetes.api.model.ServicePortBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

/** General tests for the {@link ExternalServiceDecorator}. */
class ExternalServiceDecoratorTest extends KubernetesJobManagerTestBase {

    private ExternalServiceDecorator externalServiceDecorator;

    private Map<String, String> customizedAnnotations =
            new HashMap<String, String>() {
                {
                    put("annotation1", "annotation-value1");
                    put("annotation2", "annotation-value2");
                }
            };

    private Map<String, String> customizedLabels =
            new HashMap<String, String>() {
                {
                    put("label1", "label-value1");
                    put("label2", "label-value2");
                }
            };

    @Override
    protected void onSetup() throws Exception {
        super.onSetup();

        this.flinkConfig.set(
                KubernetesConfigOptions.REST_SERVICE_ANNOTATIONS, customizedAnnotations);
        this.flinkConfig.set(KubernetesConfigOptions.REST_SERVICE_LABELS, customizedLabels);
        this.externalServiceDecorator =
                new ExternalServiceDecorator(this.kubernetesJobManagerParameters);
    }

    @Test
    void testBuildAccompanyingKubernetesResources() throws IOException {
        final List<HasMetadata> resources =
                this.externalServiceDecorator.buildAccompanyingKubernetesResources();
        assertThat(resources).hasSize(1);

        final Service restService = (Service) resources.get(0);


Review comment (suggestion):
I know it's a copy of the existing class level field, but let's use `Map.of(...)` instead for both.

Code after:
import io.fabric8.kubernetes.api.model.ServicePortBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

/** General tests for the {@link ExternalServiceDecorator}. */
class ExternalServiceDecoratorTest extends KubernetesJobManagerTestBase {

    private ExternalServiceDecorator externalServiceDecorator;

    private Map<String, String> customizedAnnotations =
            Map.of(
                    "annotation1", "annotation-value1",
                    "annotation2", "annotation-value2");

    private Map<String, String> customizedLabels =
            Map.of(
                    "label1", "label-value1",
                    "label2", "label-value2");

    @Override
    protected void onSetup() throws Exception {
        super.onSetup();

        this.flinkConfig.set(
                KubernetesConfigOptions.REST_SERVICE_ANNOTATIONS, customizedAnnotations);
        this.flinkConfig.set(KubernetesConfigOptions.REST_SERVICE_LABELS, customizedLabels);
        this.externalServiceDecorator =
                new ExternalServiceDecorator(this.kubernetesJobManagerParameters);
    }

    @Test
    void testBuildAccompanyingKubernetesResources() throws IOException {
        final List<HasMetadata> resources =
                this.externalServiceDecorator.buildAccompanyingKubernetesResources();
        assertThat(resources).hasSize(1);

        final Service restService = (Service) resources.get(0);

        assertThat(restService.getApiVersion()).isEqualTo(Constants.API_VERSION);

        assertThat(restService.getMetadata().getName())
                .isEqualTo(ExternalServiceDecorator.getExternalServiceName(CLUSTER_ID));

        final Map<String, String> expectedLabels = getCommonLabels();
        expectedLabels.putAll(customizedLabels);