/*
 * Copyright © 2017 Michael Weirauch (michael.weirauch@gmail.com)
 *
 * 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 io.github.mweirauch.micrometer.jvm.extras;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Test;

import com.google.common.testing.NullPointerTester;
import com.google.common.testing.NullPointerTester.Visibility;

import io.github.mweirauch.micrometer.jvm.extras.procfs.ProcfsSmaps;
import io.github.mweirauch.micrometer.jvm.extras.procfs.ProcfsSmaps.KEY;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;

public class ProcessMemoryMetricsTest {

    private final ProcfsSmaps smaps = mock(ProcfsSmaps.class);

    @Test
    public void testNullContract() {
        final ProcessMemoryMetrics uut = new ProcessMemoryMetrics(smaps);

        final NullPointerTester npt = new NullPointerTester();

        npt.testConstructors(uut.getClass(), Visibility.PACKAGE);
        npt.testStaticMethods(uut.getClass(), Visibility.PACKAGE);
        npt.testInstanceMethods(uut, Visibility.PACKAGE);
    }

    @SuppressWarnings("unused")
    @Test
    public void testInstantiation() {
        new ProcessMemoryMetrics();
    }

    @Test
    public void testGetMetrics() throws Exception {
        when(smaps.get(KEY.VSS)).thenReturn(1D);
        when(smaps.get(KEY.RSS)).thenReturn(2D);
        when(smaps.get(KEY.PSS)).thenReturn(3D);
        when(smaps.get(KEY.SWAP)).thenReturn(4D);
        when(smaps.get(KEY.SWAPPSS)).thenReturn(5D);

        final SimpleMeterRegistry registry = new SimpleMeterRegistry();
        final ProcessMemoryMetrics uut = new ProcessMemoryMetrics(smaps);

        uut.bindTo(registry);

        assertEquals(1.0, registry.find("process.memory.vss").gauge().get().value(), 0.0);
        assertEquals(2.0, registry.find("process.memory.rss").gauge().get().value(), 0.0);
        assertEquals(3.0, registry.find("process.memory.pss").gauge().get().value(), 0.0);
        assertEquals(4.0, registry.find("process.memory.swap").gauge().get().value(), 0.0);
        assertEquals(5.0, registry.find("process.memory.swappss").gauge().get().value(), 0.0);
    }

}

@@ -751,20 +751,211 @@
 ito.
-Mockito.mock
+ArgumentMatchers.any;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.times;%0Aimport static org.mockito.Mockito.verify;%0Aimport static org.mockito.Mockito.verifyNoMoreInteractions
 ;%0Aim
@@ -1275,16 +1275,60 @@
 ps.KEY;%0A
+import io.micrometer.core.instrument.Gauge;%0A
 import i
@@ -2500,25 +2500,25 @@
     
-assertEquals(1.0,
+final Gauge vss =
  reg
@@ -2555,32 +2555,63 @@
 %22).gauge().get()
+;%0A        assertEquals(1.0, vss
 .value(), 0.0);%0A
@@ -2635,12 +2635,71 @@
 als(
-2.0,
+%22bytes%22, vss.getId().getBaseUnit());%0A%0A        final Gauge rss =
  reg
@@ -2736,32 +2736,63 @@
 %22).gauge().get()
+;%0A        assertEquals(2.0, rss
 .value(), 0.0);%0A
@@ -2816,12 +2816,71 @@
 als(
-3.0,
+%22bytes%22, rss.getId().getBaseUnit());%0A%0A        final Gauge pss =
  reg
@@ -2917,32 +2917,63 @@
 %22).gauge().get()
+;%0A        assertEquals(3.0, pss
 .value(), 0.0);%0A
@@ -2997,12 +2997,72 @@
 als(
-4.0,
+%22bytes%22, pss.getId().getBaseUnit());%0A%0A        final Gauge swap =
  reg
@@ -3100,32 +3100,64 @@
 %22).gauge().get()
+;%0A        assertEquals(4.0, swap
 .value(), 0.0);%0A
@@ -3181,79 +3181,335 @@
 als(
-5.0, registry.find(%22process.memory.swappss%22).gauge().get().value(), 0.0
+%22bytes%22, swap.getId().getBaseUnit());%0A%0A        final Gauge swappss = registry.find(%22process.memory.swappss%22).gauge().get();%0A        assertEquals(5.0, swappss.value(), 0.0);%0A        assertEquals(%22bytes%22, swappss.getId().getBaseUnit());%0A%0A        verify(smaps, times(5)).get(any(KEY.class));%0A        verifyNoMoreInteractions(smaps
 );%0A 

verify baseUnit presence