/*
 * (c) 2015 CenturyLink. 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.centurylink.cloud.sdk.servers.services.groups.stats;

import com.centurylink.cloud.sdk.common.management.services.domain.datacenters.refs.DataCenter;
import com.centurylink.cloud.sdk.servers.AbstractServersSdkTest;
import com.centurylink.cloud.sdk.servers.client.domain.group.GroupMetadata;
import com.centurylink.cloud.sdk.servers.client.domain.group.SamplingEntry;
import com.centurylink.cloud.sdk.servers.client.domain.group.ServerMonitoringStatistics;
import com.centurylink.cloud.sdk.servers.client.domain.server.metadata.ServerMetadata;
import com.centurylink.cloud.sdk.servers.services.GroupService;
import com.centurylink.cloud.sdk.servers.services.domain.group.ServerMonitoringFilter;
import com.centurylink.cloud.sdk.servers.services.domain.group.filters.GroupFilter;
import com.centurylink.cloud.sdk.servers.services.domain.group.refs.Group;
import com.centurylink.cloud.sdk.servers.services.domain.group.refs.GroupByIdRef;
import com.google.inject.Inject;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.time.Duration;
import java.time.OffsetDateTime;
import java.util.List;

import static com.centurylink.cloud.sdk.tests.TestGroups.INTEGRATION;
import static com.centurylink.cloud.sdk.tests.TestGroups.LONG_RUNNING;
import static java.util.stream.Collectors.toList;

@Test(groups = {INTEGRATION, LONG_RUNNING})
public class GetServerMonitoringStatsTest extends AbstractServersSdkTest {

    @Inject
    GroupService groupService;

    GroupByIdRef group;

    @BeforeMethod
    public void setUp() {
        List<GroupMetadata> metadata = groupService.find(new GroupFilter()
            .dataCenters(DataCenter.DE_FRANKFURT)
            .nameContains(Group.DEFAULT_GROUP));

        assertTrue(metadata.size() > 0);
        group = metadata.get(0).asRefById();
    }

    @Test
    public void testServerStats() {
        Duration sampleInterval = Duration.ofHours(1);
        GroupFilter groupFilter = new GroupFilter().groups(group);
        List<ServerMonitoringStatistics> result = groupService.getMonitoringStats(
            groupFilter,
            new ServerMonitoringFilter()
                .from(OffsetDateTime.now().minusDays(2))
        );

        List<GroupMetadata> m = groupService.find(groupFilter);

        assertNotNull(result);

        List<String> allServers = m.stream()
            .map(GroupMetadata::getServers)
            .flatMap(List::stream)
            .map(ServerMetadata::getId)
            .collect(toList());

        result.stream()
            .map(ServerMonitoringStatistics::getName)
            .forEach(serverId ->
                assertTrue(allServers.contains(serverId), "check that server exists")
            );

        result.stream()
                .forEach(metadata -> {

                    for (int i = 0; i < metadata.getStats().size() - 1; i++) {
                        SamplingEntry curStats = metadata.getStats().get(i);
                        SamplingEntry nextStats = metadata.getStats().get(i + 1);
                        assertEquals(Duration.between(curStats.getTimestamp(), nextStats.getTimestamp()), sampleInterval);

                        checkStats(curStats);
                    }
                });
    }

    private void checkStats(SamplingEntry stats) {
        boolean isEmpty = stats.getCpu().equals(0);

        checkStat(stats.getCpuPercent(), isEmpty);
        checkStat(stats.getMemoryPercent(), isEmpty);
        checkStat(stats.getNetworkReceivedKbps(), isEmpty);
        checkStat(stats.getNetworkTransmittedKbps(), isEmpty);
        checkStat(stats.getDiskUsageTotalCapacityMB(), isEmpty);
        checkStat(stats.getMemoryMB(), isEmpty);
        checkStat(stats.getDiskUsage().size(), isEmpty);
        checkStat(stats.getGuestDiskUsage().size(), isEmpty);
    }

    private void checkStat(Double value, boolean isEmpty) {
        Double zero = 0d;
        if (isEmpty) {
            assertTrue(value == zero);
        }
    }

    private void checkStat(Integer value, boolean isEmpty) {
        Integer zero = 0;
        assertTrue(isEmpty ? value == zero : value >=zero);
    }
}

@@ -1070,95 +1070,8 @@
 cs;%0A
-import com.centurylink.cloud.sdk.servers.client.domain.server.metadata.ServerMetadata;%0A
 impo
@@ -1800,58 +1800,8 @@
 ING;
-%0Aimport static java.util.stream.Collectors.toList;
 %0A%0A@T
@@ -2697,535 +2697,56 @@
     
-List%3CGroupMetadata%3E m = groupService.find(groupFilter);%0A%0A        assertNotNull(result);%0A%0A        List%3CString%3E allServers = m.stream()%0A            .map(GroupMetadata::getServers)%0A            .flatMap(List::stream)%0A            .map(ServerMetadata::getId)%0A            .collect(toList());%0A%0A        result.stream()%0A            .map(ServerMonitoringStatistics::getName)%0A            .forEach(serverId -%3E%0A                assertTrue(allServers.contains(serverId), %22check that server exists%22)%0A            );%0A%0A        result.stream()%0A    
+assertNotNull(result);%0A%0A        result.stream()%0A
     
@@ -2776,21 +2776,16 @@
 ta -%3E %7B%0A
-%0A    
         
@@ -2863,36 +2863,32 @@
                 
- 
-   
 SamplingEntry cu
@@ -2924,20 +2924,16 @@
 get(i);%0A
-    
         
@@ -3018,28 +3018,24 @@
             
- 
-   
 assertEquals
@@ -3138,28 +3138,24 @@
             
-    
 checkStats(c
@@ -3180,26 +3180,18 @@
         
-    %7D%0A    
+%7D%0A
         

fix test