/*
 * Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed 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 com.hazelcast.internal.management;

import com.hazelcast.client.test.TestHazelcastFactory;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.internal.json.Json;
import com.hazelcast.internal.json.JsonObject;
import com.hazelcast.internal.json.ParseException;
import com.hazelcast.internal.management.events.Event;
import com.hazelcast.internal.management.events.EventMetadata;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.ParallelJVMTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.util.List;

import static com.hazelcast.internal.util.StringUtil.isNullOrEmpty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;

@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelJVMTest.class})
public class ManagementCenterServiceIntegrationTest extends HazelcastTestSupport {

    private static final String CLUSTER_NAME = "mc-service-tests";

    private TestHazelcastFactory factory;
    private HazelcastInstance instance;
    private ManagementCenterService mcs;

    @Before
    public void setUp() {
        factory = new TestHazelcastFactory(1);
        instance = factory.newHazelcastInstance(getConfig().setClusterName(CLUSTER_NAME));

        assertTrueEventually(() -> {
            ManagementCenterService mcs = getNode(instance).getManagementCenterService();
            assertNotNull(mcs);
            this.mcs = mcs;
        });
    }

    @After
    public void tearDown() {
        factory.shutdownAll();
    }

    @Test
    public void testTimedMemberState_returnsNotNull() {
        String responseString = mcs.getTimedMemberStateJson().orElse(null);
        assertFalse(isNullOrEmpty(responseString));

        JsonObject object;
        try {
            object = Json.parse(responseString).asObject();
        } catch (ParseException e) {
            throw new AssertionError("Failed to parse JSON: " + responseString);
        }
        TimedMemberState memberState = new TimedMemberState();
        memberState.fromJson(object.get("timedMemberState").asObject());
        assertEquals(CLUSTER_NAME, memberState.getClusterName());
    }

    @Test
    public void testTimedMemberState_usesCache_shortTimeFrame() {
        String responseOne = mcs.getTimedMemberStateJson().orElse(null);
        String responseTwo = mcs.getTimedMemberStateJson().orElse(null);
        assertSame(responseOne, responseTwo);
    }

    @Test
    public void testTimedMemberState_ignoresCache_longTimeFrame() {
        String responseOne = mcs.getTimedMemberStateJson().orElse(null);
        sleepSeconds(2);
        String responseTwo = mcs.getTimedMemberStateJson().orElse(null);
        assertNotSame(responseOne, responseTwo);
    }

    @Test
    public void testMCEvents_storesEvents_recentPoll() {
        mcs.pollMCEvents();

        TestEvent expectedEvent = new TestEvent();
        mcs.log(expectedEvent);
        mcs.log(new TestEvent());
        mcs.log(new TestEvent());

        List<Event> actualEvents = mcs.pollMCEvents();
        assertEquals(3, actualEvents.size());
        assertEquals(expectedEvent, actualEvents.get(0));
    }

    @Test
    public void testMCEvents_storesEvents_noPollAtAll() {
        mcs.log(new TestEvent());
        mcs.log(new TestEvent());

        assertEquals(2, mcs.pollMCEvents().size());
    }

    @Test
    public void testMCEvents_clearsEventQueue_noRecentPoll() {
        mcs.pollMCEvents();
        mcs.log(new TestEvent());
        mcs.log(new TestEvent());

        mcs.onMCEventWindowExceeded();
        assertEquals(0, mcs.pollMCEvents().size());

        mcs.log(new TestEvent());
        assertEquals(1, mcs.pollMCEvents().size());
    }

    private static class TestEvent implements Event {

        @Override
        public EventMetadata.EventType getType() {
            return EventMetadata.EventType.WAN_SYNC_STARTED;
        }

        @Override
        public long getTimestamp() {
            return 42;
        }

        @Override
        public JsonObject toJson() {
            return null;
        }

    }

}

@@ -3236,32 +3236,73 @@
 rtTimeFrame() %7B%0A
+        assertTrueEventually(() -%3E %7B%0A    
         String r
@@ -3350,32 +3350,36 @@
 ).orElse(null);%0A
+    
         String r
@@ -3439,24 +3439,28 @@
 l);%0A        
+ 
+   
 assertSame(r
@@ -3477,32 +3477,44 @@
 , responseTwo);%0A
+        %7D);%0A
     %7D%0A%0A    @Test

Fix flaky ManagementCenterServiceIntegrationTest (#16563)