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  
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 = Collections.singleton(clusterInstance);
49      Set<ObjectInstance> membership = Collections.singleton(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<psiprobe.tools.JmxTools> jmxToolsMock =
64            Mockito.mockStatic(psiprobe.tools.JmxTools.class)) {
65          jmxToolsMock.when(() -> psiprobe.tools.JmxTools.getStringAttr(any(), any(), any()))
66              .thenReturn("test");
67          jmxToolsMock.when(() -> psiprobe.tools.JmxTools.getLongAttr(any(), any(), any()))
68              .thenReturn(1L);
69          jmxToolsMock.when(() -> psiprobe.tools.JmxTools.getIntAttr(any(), any(), any()))
70              .thenReturn(1);
71          jmxToolsMock.when(() -> psiprobe.tools.JmxTools.getBooleanAttr(any(), any(), any()))
72              .thenReturn(true);
73          jmxToolsMock.when(() -> psiprobe.tools.JmxTools.getAttribute(any(), any(), any()))
74              .thenReturn(new ObjectName[0]);
75  
76          ClusterWrapperBean bean = new ClusterWrapperBean();
77  
78          // Act
79          Cluster cluster = bean.getCluster(serverName, hostName, loadMembers);
80  
81          // Assert
82          assertNotNull(cluster);
83          assertEquals("test", cluster.getName());
84        }
85      }
86    }
87  
88    @Test
89    void testGetClusterReturnsNullWhenNoCluster() throws Exception {
90      String serverName = "Catalina";
91      String hostName = "localhost";
92      boolean loadMembers = false;
93  
94      MBeanServer mbeanServer = mock(MBeanServer.class);
95  
96      when(mbeanServer.queryMBeans(new ObjectName("*:type=Cluster,host=" + hostName), null))
97          .thenReturn(Collections.emptySet());
98      when(mbeanServer
99          .queryMBeans(new ObjectName(serverName + ":type=ClusterMembership,host=" + hostName), null))
100         .thenReturn(Collections.emptySet());
101 
102     try (MockedStatic<ManagementFactory> mgmtFactoryMock =
103         Mockito.mockStatic(ManagementFactory.class)) {
104       mgmtFactoryMock.when(ManagementFactory::getPlatformMBeanServer).thenReturn(mbeanServer);
105 
106       ClusterWrapperBean bean = new ClusterWrapperBean();
107 
108       Cluster cluster = bean.getCluster(serverName, hostName, loadMembers);
109 
110       assertNull(cluster);
111     }
112   }
113 }