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.Collections;
22  import java.util.Set;
23  
24  import javax.management.MBeanServer;
25  import javax.management.ObjectInstance;
26  import javax.management.ObjectName;
27  
28  import org.junit.jupiter.api.Test;
29  import org.mockito.MockedStatic;
30  import org.mockito.Mockito;
31  
32  import psiprobe.model.jmx.Cluster;
33  import psiprobe.tools.JmxTools;
34  
35  class ClusterWrapperBeanTest {
36  
37    @Test
38    void testGetClusterReturnsClusterWhenJmxPresent() throws Exception {
39      // Arrange
40      String serverName = "Catalina";
41      String hostName = "localhost";
42      boolean loadMembers = false;
43  
44      MBeanServer mbeanServer = mock(MBeanServer.class);
45      ObjectName clusterObjName = new ObjectName(serverName + ":type=Cluster,host=" + hostName);
46      ObjectInstance clusterInstance = mock(ObjectInstance.class);
47      when(clusterInstance.getObjectName()).thenReturn(clusterObjName);
48  
49      Set<ObjectInstance> clusters = Collections.singleton(clusterInstance);
50      Set<ObjectInstance> membership = Collections.singleton(clusterInstance);
51  
52      when(mbeanServer.queryMBeans(new ObjectName("*:type=Cluster,host=" + hostName), null))
53          .thenReturn(clusters);
54      when(mbeanServer
55          .queryMBeans(new ObjectName(serverName + ":type=ClusterMembership,host=" + hostName), null))
56          .thenReturn(membership);
57  
58      // Mock JmxTools static methods
59      try (MockedStatic<ManagementFactory> mgmtFactoryMock =
60          Mockito.mockStatic(ManagementFactory.class)) {
61        mgmtFactoryMock.when(ManagementFactory::getPlatformMBeanServer).thenReturn(mbeanServer);
62  
63        // Mock JmxTools methods as needed (example for getStringAttr)
64        try (MockedStatic<JmxTools> jmxToolsMock = Mockito.mockStatic(JmxTools.class)) {
65          jmxToolsMock.when(() -> JmxTools.getStringAttr(any(), any(), any())).thenReturn("test");
66          jmxToolsMock.when(() -> JmxTools.getLongAttr(any(), any(), any())).thenReturn(1L);
67          jmxToolsMock.when(() -> JmxTools.getIntAttr(any(), any(), any())).thenReturn(1);
68          jmxToolsMock.when(() -> JmxTools.getBooleanAttr(any(), any(), any())).thenReturn(true);
69          jmxToolsMock.when(() -> JmxTools.getAttribute(any(), any(), any()))
70              .thenReturn(new ObjectName[0]);
71  
72          ClusterWrapperBean bean = new ClusterWrapperBean();
73  
74          // Act
75          Cluster cluster = bean.getCluster(serverName, hostName, loadMembers);
76  
77          // Assert
78          assertNotNull(cluster);
79          assertEquals("test", cluster.getName());
80        }
81      }
82    }
83  
84    @Test
85    void testGetClusterReturnsNullWhenNoCluster() throws Exception {
86      String serverName = "Catalina";
87      String hostName = "localhost";
88      boolean loadMembers = false;
89  
90      MBeanServer mbeanServer = mock(MBeanServer.class);
91  
92      when(mbeanServer.queryMBeans(new ObjectName("*:type=Cluster,host=" + hostName), null))
93          .thenReturn(Collections.emptySet());
94      when(mbeanServer
95          .queryMBeans(new ObjectName(serverName + ":type=ClusterMembership,host=" + hostName), null))
96          .thenReturn(Collections.emptySet());
97  
98      try (MockedStatic<ManagementFactory> mgmtFactoryMock =
99          Mockito.mockStatic(ManagementFactory.class)) {
100       mgmtFactoryMock.when(ManagementFactory::getPlatformMBeanServer).thenReturn(mbeanServer);
101 
102       ClusterWrapperBean bean = new ClusterWrapperBean();
103 
104       Cluster cluster = bean.getCluster(serverName, hostName, loadMembers);
105 
106       assertNull(cluster);
107     }
108   }
109 }