/*
 * Copyright 2020 LINE Corporation
 *
 * LINE Corporation 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:
 *
 *   https://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 com.linecorp.armeria.spring.actuate;

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

import javax.inject.Inject;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.web.server.LocalManagementPort;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;

import com.linecorp.armeria.client.WebClient;
import com.linecorp.armeria.common.AggregatedHttpResponse;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.server.Server;

@SpringBootTest(classes = org.springframework.boot.test.context.TestConfiguration.class)
@ActiveProfiles({ "local", "managedMetricPath" })
@DirtiesContext
@AutoConfigureMetrics
@EnableAutoConfiguration
@ImportAutoConfiguration(ArmeriaSpringActuatorAutoConfiguration.class)
class PrometheusMetricExposureTest {

    @SpringBootApplication
    class TestConfiguration {}

    @Inject
    private Server server;

    @LocalManagementPort
    private int managementPort;

    private WebClient client;
    private WebClient managementClient;

    @BeforeEach
    void setUp() {
        client = WebClient.of("h2c://127.0.0.1:" + server.activeLocalPort());
        managementClient = WebClient.of("http://127.0.0.1:" + managementPort);
    }

    @Test
    void exposeMetricCollectingServiceOnManagementPort() throws Exception {
        AggregatedHttpResponse res = client.get("/internal/actuator/prometheus").aggregate().get();
        assertThat(res.status()).isEqualTo(HttpStatus.NOT_FOUND);
        res = managementClient.get("/internal/actuator/prometheus").aggregate().get();
        assertThat(res.status()).isEqualTo(HttpStatus.OK);

        // Make sure that the exposed results contain the metrics collected by MetricCollectingService
        assertThat(res.contentAscii()).contains("armeria_server_response_duration_seconds");
    }
}

@@ -731,16 +731,63 @@
 ertThat;
+%0Aimport static org.awaitility.Awaitility.await;
 %0A%0Aimport
@@ -2476,16 +2476,22 @@
         
+final 
 Aggregat
@@ -2508,18 +2508,37 @@
 onse res
+404 =%0A              
  
-=
  client.
@@ -2607,32 +2607,35 @@
   assertThat(res
+404
 .status()).isEqu
@@ -2662,24 +2662,25 @@
 FOUND);%0A
+%0A
         
 res = ma
@@ -2675,13 +2675,207 @@
     
-res =
+// Make sure that the exposed results contain the metrics collected by MetricCollectingService%0A        await().untilAsserted(() -%3E %7B%0A            final AggregatedHttpResponse res =%0A                   
  man
@@ -2944,32 +2944,36 @@
 .get();%0A        
+    
 assertThat(res.s
@@ -3011,112 +3011,12 @@
 K);%0A
-%0A
     
-    // Make sure that the exposed results contain the metrics collected by MetricCollectingService%0A
     
@@ -3104,16 +3104,28 @@
 onds%22);%0A
+        %7D);%0A
     %7D%0A%7D%0A

Fix flaky `PrometheusMetricExposureTest` (#3488)