View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.beans;
12  
13  import static org.junit.jupiter.api.Assertions.assertEquals;
14  import static org.junit.jupiter.api.Assertions.assertNotNull;
15  import static org.junit.jupiter.api.Assertions.assertNull;
16  import static org.mockito.Mockito.any;
17  import static org.mockito.Mockito.mock;
18  import static org.mockito.Mockito.when;
19  
20  import java.lang.management.ManagementFactory;
21  import java.util.Set;
22  
23  import javax.management.MBeanServer;
24  import javax.management.ObjectInstance;
25  import javax.management.ObjectName;
26  
27  import org.junit.jupiter.api.Test;
28  import org.mockito.MockedStatic;
29  import org.mockito.Mockito;
30  
31  import psiprobe.model.jmx.Cluster;
32  import psiprobe.tools.JmxTools;
33  
34  class ClusterWrapperBeanTest {
35  
36    @Test
37    void testGetClusterReturnsClusterWhenJmxPresent() throws Exception {
38      // Arrange
39      String serverName = "Catalina";
40      String hostName = "localhost";
41      boolean loadMembers = false;
42  
43      MBeanServer mbeanServer = mock(MBeanServer.class);
44      ObjectName clusterObjName = new ObjectName(serverName + ":type=Cluster,host=" + hostName);
45      ObjectInstance clusterInstance = mock(ObjectInstance.class);
46      when(clusterInstance.getObjectName()).thenReturn(clusterObjName);
47  
48      Set<ObjectInstance> clusters = Set.of(clusterInstance);
49      Set<ObjectInstance> membership = Set.of(clusterInstance);
50  
51      when(mbeanServer.queryMBeans(new ObjectName("*:type=Cluster,host=" + hostName), null))
52          .thenReturn(clusters);
53      when(mbeanServer
54          .queryMBeans(new ObjectName(serverName + ":type=ClusterMembership,host=" + hostName), null))
55          .thenReturn(membership);
56  
57      // Mock JmxTools static methods
58      try (MockedStatic<ManagementFactory> mgmtFactoryMock =
59          Mockito.mockStatic(ManagementFactory.class)) {
60        mgmtFactoryMock.when(ManagementFactory::getPlatformMBeanServer).thenReturn(mbeanServer);
61  
62        // Mock JmxTools methods as needed (example for getStringAttr)
63        try (MockedStatic<JmxTools> jmxToolsMock = Mockito.mockStatic(JmxTools.class)) {
64          jmxToolsMock.when(() -> JmxTools.getStringAttr(any(), any(), any())).thenReturn("test");
65          jmxToolsMock.when(() -> JmxTools.getLongAttr(any(), any(), any())).thenReturn(1L);
66          jmxToolsMock.when(() -> JmxTools.getIntAttr(any(), any(), any())).thenReturn(1);
67          jmxToolsMock.when(() -> JmxTools.getBooleanAttr(any(), any(), any())).thenReturn(true);
68          jmxToolsMock.when(() -> JmxTools.getAttribute(any(), any(), any()))
69              .thenReturn(new ObjectName[0]);
70  
71          ClusterWrapperBean bean = new ClusterWrapperBean();
72  
73          // Act
74          Cluster cluster = bean.getCluster(serverName, hostName, loadMembers);
75  
76          // Assert
77          assertNotNull(cluster);
78          assertEquals("test", cluster.getName());
79        }
80      }
81    }
82  
83    @Test
84    void testGetClusterReturnsNullWhenNoCluster() throws Exception {
85      String serverName = "Catalina";
86      String hostName = "localhost";
87      boolean loadMembers = false;
88  
89      MBeanServer mbeanServer = mock(MBeanServer.class);
90  
91      when(mbeanServer.queryMBeans(new ObjectName("*:type=Cluster,host=" + hostName), null))
92          .thenReturn(Set.of());
93      when(mbeanServer
94          .queryMBeans(new ObjectName(serverName + ":type=ClusterMembership,host=" + hostName), null))
95          .thenReturn(Set.of());
96  
97      try (MockedStatic<ManagementFactory> mgmtFactoryMock =
98          Mockito.mockStatic(ManagementFactory.class)) {
99        mgmtFactoryMock.when(ManagementFactory::getPlatformMBeanServer).thenReturn(mbeanServer);
100 
101       ClusterWrapperBean bean = new ClusterWrapperBean();
102 
103       Cluster cluster = bean.getCluster(serverName, hostName, loadMembers);
104 
105       assertNull(cluster);
106     }
107   }
108 }