View Javadoc
1   package com.guinetik.hexafun.examples.sysmon;
2   
3   import java.util.Random;
4   
5   /**
6    * Mock implementation of MetricsProvider for testing and demos.
7    *
8    * <p>Returns configurable or random values. Useful for:
9    * <ul>
10   *   <li>Unit testing handlers without OS dependencies</li>
11   *   <li>Demos with controlled/interesting values</li>
12   *   <li>Simulating warning conditions</li>
13   * </ul>
14   */
15  public class MockMetricsProvider implements MetricsProvider {
16  
17      private final double cpu;
18      private final double memory;
19      private final double disk;
20  
21      /**
22       * Create with fixed values.
23       */
24      public MockMetricsProvider(double cpu, double memory, double disk) {
25          this.cpu = cpu;
26          this.memory = memory;
27          this.disk = disk;
28      }
29  
30      /**
31       * Create with random values within realistic ranges.
32       */
33      public static MockMetricsProvider random() {
34          Random r = new Random();
35          return new MockMetricsProvider(
36              20 + r.nextDouble() * 60,  // CPU: 20-80%
37              30 + r.nextDouble() * 50,  // Memory: 30-80%
38              40 + r.nextDouble() * 55   // Disk: 40-95%
39          );
40      }
41  
42      /**
43       * Create with values designed to show warnings.
44       */
45      public static MockMetricsProvider withWarnings() {
46          return new MockMetricsProvider(67.0, 52.0, 91.0);
47      }
48  
49      /**
50       * Create with all healthy values.
51       */
52      public static MockMetricsProvider healthy() {
53          return new MockMetricsProvider(35.0, 48.0, 62.0);
54      }
55  
56      @Override
57      public double getCpuUsage() {
58          return cpu;
59      }
60  
61      @Override
62      public double getMemoryUsage() {
63          return memory;
64      }
65  
66      @Override
67      public double getDiskUsage() {
68          return disk;
69      }
70  }