|
1 | 1 | package root.core.repository.implement; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | | -import static org.junit.jupiter.api.Assertions.fail; |
5 | | -import static org.mockito.Mockito.doNothing; |
6 | 4 | import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.mockStatic; |
7 | 6 | import static org.mockito.Mockito.when; |
8 | 7 |
|
9 | | -import java.io.ByteArrayInputStream; |
10 | | -import java.io.InputStream; |
| 8 | +import java.io.IOException; |
11 | 9 |
|
12 | 10 | import org.apache.commons.io.IOUtils; |
| 11 | +import org.junit.jupiter.api.AfterAll; |
13 | 12 | import org.junit.jupiter.api.BeforeAll; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
14 | 14 | import org.junit.jupiter.api.Test; |
15 | | -import org.mockito.Mock; |
| 15 | +import org.mockito.MockedStatic; |
16 | 16 |
|
17 | | -import com.jcraft.jsch.Channel; |
18 | | -import com.jcraft.jsch.Session; |
| 17 | +import com.jcraft.jsch.JSchException; |
19 | 18 |
|
20 | 19 | import root.common.server.implement.JschServer; |
21 | 20 | import root.core.domain.AlertLog; |
|
24 | 23 |
|
25 | 24 | public class LinuxServerMonitoringRepositoryTest { |
26 | 25 |
|
27 | | - @Mock |
28 | | - public static JschServer jsch = mock(JschServer.class); |
| 26 | + public JschServer jschServer; |
29 | 27 |
|
30 | | - public static ServerMonitoringRepository repo; |
| 28 | + public ServerMonitoringRepository repo; |
| 29 | + |
| 30 | + public static MockedStatic<IOUtils> ioUtilsMock; |
31 | 31 |
|
32 | 32 | public static String alertLogString = ""; |
| 33 | + |
| 34 | + public static String[] alertLogLines; |
33 | 35 |
|
34 | 36 | @BeforeAll |
35 | 37 | public static void before() { |
36 | | - repo = new LinuxServerMonitoringRepository(jsch); |
| 38 | + ioUtilsMock = mockStatic(IOUtils.class); |
37 | 39 | StringBuffer sb = new StringBuffer(); |
38 | 40 | sb.append("2022-03-24T13:38:35.065184+09:00").append("\n"); |
39 | 41 | sb.append("Thread 1 advanced to log sequence 7606 (LGWR switch)").append("\n"); |
@@ -62,71 +64,72 @@ public static void before() { |
62 | 64 | sb.append("2022-03-31T02:56:51.984291+09:00").append("\n"); |
63 | 65 | sb.append("Starting control autobackup").append("\n"); |
64 | 66 | alertLogString = sb.toString(); |
| 67 | + |
| 68 | + alertLogLines = alertLogString.split("\n"); |
| 69 | + } |
| 70 | + |
| 71 | + @BeforeEach |
| 72 | + public void setup() { |
| 73 | + jschServer = mock(JschServer.class); |
| 74 | + repo = new LinuxServerMonitoringRepository(jschServer); |
| 75 | + } |
| 76 | + |
| 77 | + @AfterAll |
| 78 | + public static void after() { |
| 79 | + ioUtilsMock.close(); |
65 | 80 | } |
66 | 81 |
|
67 | 82 | @Test |
68 | | - public void checkAlertLogTest() { |
69 | | - Session session = mock(Session.class); |
70 | | - Channel channel = mock(Channel.class); |
| 83 | + public void checkAlertLogTest() throws JSchException, IOException { |
| 84 | + // Arrange |
71 | 85 | AlertLogCommand alc = mock(AlertLogCommand.class); |
72 | | - InputStream in = new ByteArrayInputStream(alertLogString.getBytes()); |
73 | | - |
74 | | - when(jsch.getSession()).thenReturn(session); |
75 | | - String command = "tail -" + alc.getReadLine() + " " + alc.getReadFilePath(); |
76 | | - when(jsch.openExecChannel(session, command)).thenReturn(channel); |
77 | | - when(jsch.connectChannel(channel)).thenReturn(in); |
78 | | - doNothing().when(jsch).disConnectChannel(channel); |
79 | | - doNothing().when(channel).disconnect(); |
| 86 | + alc.setReadLine("10"); |
| 87 | + alc.setReadFilePath("/test/alert_DB.log"); |
| 88 | + |
| 89 | + String command = String.format("tail -%d %s", alc.getReadLine(), alc.getReadFilePath()); |
| 90 | + when(jschServer.executeCommand(command)).thenReturn(alertLogString); |
80 | 91 |
|
| 92 | + // Act |
81 | 93 | String result = repo.checkAlertLog(alc); |
| 94 | + |
| 95 | + // Assert |
82 | 96 | assertEquals(result, alertLogString); |
83 | 97 | } |
84 | 98 |
|
85 | 99 | @Test |
86 | | - public void getAlertLogFileLineCountTest() { |
87 | | - Session session = mock(Session.class); |
88 | | - Channel channel = mock(Channel.class); |
| 100 | + public void getAlertLogFileLineCountTest() throws IOException, JSchException { |
| 101 | + // Arrange |
89 | 102 | AlertLogCommand alc = mock(AlertLogCommand.class); |
90 | | - InputStream in = new ByteArrayInputStream("26".getBytes()); |
91 | | - |
92 | | - when(jsch.getSession()).thenReturn(session); |
93 | | - when(jsch.openExecChannel(session, "cat " + alc.getReadFilePath() + " | wc -l")).thenReturn(channel); |
94 | | - when(jsch.connectChannel(channel)).thenReturn(in); |
95 | | - doNothing().when(jsch).disConnectChannel(channel); |
96 | | - doNothing().when(channel).disconnect(); |
97 | | - |
98 | | - try { |
99 | | - String result = IOUtils.toString(in, "UTF-8"); |
100 | | - int fileLineCnt = Integer.parseInt(result.trim()); |
101 | | - assertEquals(fileLineCnt, 26); |
102 | | - } catch (Exception e) { |
103 | | - fail(); |
104 | | - } |
| 103 | + alc.setReadLine("10"); |
| 104 | + alc.setReadFilePath("/test/alert_DB.log"); |
| 105 | + |
| 106 | + String command = String.format("cat %s | wc -l", alc.getReadFilePath()); |
| 107 | + when(jschServer.executeCommand(command)).thenReturn(String.valueOf(alertLogLines.length)); |
| 108 | + |
| 109 | + // Act |
| 110 | + int lineCount = repo.getAlertLogFileLineCount(alc); |
| 111 | + |
| 112 | + // Assert |
| 113 | + assertEquals(lineCount, alertLogLines.length); |
105 | 114 | } |
106 | 115 |
|
107 | 116 | @Test |
108 | | - public void checkAlertLogDuringPeriod() { |
109 | | - Session session = mock(Session.class); |
110 | | - Channel channel = mock(Channel.class); |
111 | | - Channel channel2 = mock(Channel.class); |
| 117 | + public void checkAlertLogDuringPeriod() throws JSchException, IOException { |
| 118 | + // Arrange |
112 | 119 | AlertLogCommand alc = mock(AlertLogCommand.class); |
113 | | - InputStream in = new ByteArrayInputStream(alertLogString.getBytes()); |
114 | | - InputStream in2 = new ByteArrayInputStream("26".getBytes()); |
115 | | - |
116 | | - when(jsch.getSession()).thenReturn(session); |
117 | | - when(jsch.openExecChannel(session, "cat " + alc.getReadFilePath() + " | wc -l")).thenReturn(channel2); |
118 | | - when(jsch.connectChannel(channel2)).thenReturn(in2); |
119 | | - doNothing().when(jsch).disConnectChannel(channel2); |
120 | | - doNothing().when(channel2).disconnect(); |
121 | | - |
122 | | - String command = "tail -" + alc.getReadLine() + " " + alc.getReadFilePath(); |
123 | | - when(jsch.openExecChannel(session, command)).thenReturn(channel); |
124 | | - when(jsch.connectChannel(channel)).thenReturn(in); |
125 | | - doNothing().when(jsch).disConnectChannel(channel); |
126 | | - doNothing().when(channel).disconnect(); |
127 | | - |
| 120 | + alc.setReadLine("10"); |
| 121 | + alc.setReadFilePath("/test/alert_DB.log"); |
| 122 | + |
| 123 | + String command1 = String.format("cat %s | wc -l", alc.getReadFilePath()); |
| 124 | + when(jschServer.executeCommand(command1)).thenReturn(String.valueOf(alertLogLines.length)); |
| 125 | + |
| 126 | + String command2 = String.format("tail -%d %s", alc.getReadLine(), alc.getReadFilePath()); |
| 127 | + when(jschServer.executeCommand(command2)).thenReturn(alertLogString); |
| 128 | + |
| 129 | + // Act |
128 | 130 | AlertLog alertLog = repo.checkAlertLogDuringPeriod(alc, "2022-03-24", "2022-03-29"); |
129 | 131 |
|
| 132 | + // Assert |
130 | 133 | assertEquals(alertLog.getTotalLineCount(), 14); |
131 | 134 | assertEquals(alertLog.getAlertLogs().size(), 8); |
132 | 135 | } |
|
0 commit comments