|
| 1 | +package root.core.repository.implement; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.BeforeAll; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import com.jcraft.jsch.JSchException; |
| 16 | + |
| 17 | +import root.common.server.implement.JschServer; |
| 18 | +import root.core.domain.AlertLog; |
| 19 | +import root.core.domain.AlertLogCommand; |
| 20 | +import root.core.repository.constracts.ServerMonitoringRepository; |
| 21 | + |
| 22 | +public class WindowServerMonitoringRepositoryTest { |
| 23 | + |
| 24 | + public JschServer jschServer; |
| 25 | + public ServerMonitoringRepository repo; |
| 26 | + public static String alertLogString = ""; |
| 27 | + public static String[] alertLogLines; |
| 28 | + |
| 29 | + @BeforeAll |
| 30 | + public static void before() { |
| 31 | + StringBuffer sb = new StringBuffer(); |
| 32 | + sb.append("2022-03-23T13:38:35.065184+09:00").append("\n"); |
| 33 | + sb.append("Thread 1 advanced to log sequence 7606 (LGWR switch)").append("\n"); |
| 34 | + sb.append(" Current log# 5 seq# 7606 mem# 0: +REDO/1003346093").append("\n"); |
| 35 | + sb.append("2022-03-24T13:39:00.180206+09:00").append("\n"); |
| 36 | + sb.append("Archived Log entry 13398 added for T-1.S-7605 ID 0x8155080b LAD:1").append("\n"); |
| 37 | + sb.append("2022-03-25T14:24:57.291344+09:00").append("\n"); |
| 38 | + sb.append("Session (223,56276): RECO logon successful: Inbound connection from client").append("\n"); |
| 39 | + sb.append("Session (223,56276): RECO logon successful: DB Logon User: RECO, ").append("\n"); |
| 40 | + sb.append("Session (223,56276): RECO logon successful: Client IP Address: -").append("\n"); |
| 41 | + sb.append("2022-03-26T18:04:53.572965+09:00").append("\n"); |
| 42 | + sb.append("Thread 1 advanced to log sequence 7607 (LGWR switch)").append("\n"); |
| 43 | + sb.append(" Current log# 1 seq# 7607 mem# 0: +REDO/DBERP/ONLINELOG/group_1.257.966360593").append("\n"); |
| 44 | + sb.append("2022-03-27T18:05:16.929231+09:00").append("\n"); |
| 45 | + sb.append("Archived Log entry 13400 added for T-1.S-7606 ID 0x8155080b LAD:1").append("\n"); |
| 46 | + sb.append("2022-03-28T00:02:21.629284+09:00").append("\n"); |
| 47 | + sb.append("TABLE SYS.WRI$_OPTSTAT_HISTHEAD_HISTORY: ADDED INTERVAL PARTITION SYS_P38333)").append("\n"); |
| 48 | + sb.append("TABLE SYS.WRI$_OPTSTAT_HISTGRM_HISTORY: ADDED INTERVAL PARTITION SYS_P38336)").append("\n"); |
| 49 | + sb.append("2022-03-28T01:00:18.971650+09:00").append("\n"); |
| 50 | + sb.append("ALTER SYSTEM ARCHIVE LOG").append("\n"); |
| 51 | + sb.append("2022-03-29T01:00:18.980968+09:00").append("\n"); |
| 52 | + sb.append("Thread 1 advanced to log sequence 7608 (LGWR switch)").append("\n"); |
| 53 | + sb.append(" Current log# 6 seq# 7608 mem# 0: +REDO/1003346037").append("\n"); |
| 54 | + sb.append("2022-03-30T01:00:38.903080+09:00").append("\n"); |
| 55 | + sb.append("Archived Log entry 13401 added for T-1.S-7607 ID 0x8155080b LAD:1").append("\n"); |
| 56 | + sb.append("2022-03-31T02:56:51.984291+09:00").append("\n"); |
| 57 | + sb.append("Starting control autobackup").append("\n"); |
| 58 | + alertLogString = sb.toString(); |
| 59 | + |
| 60 | + alertLogLines = alertLogString.split("\n"); |
| 61 | + } |
| 62 | + |
| 63 | + @BeforeEach |
| 64 | + public void setup() { |
| 65 | + jschServer = mock(JschServer.class); |
| 66 | + repo = new WindowServerMonitoringRepository(jschServer); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testGetServerName_ServerNameIsNull() { |
| 71 | + when(jschServer.getServerName()).thenReturn(null); |
| 72 | + String result = repo.getServerName(); |
| 73 | + assertNull(result); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testGetServerName_ServerNameIsNotNull() { |
| 78 | + when(jschServer.getServerName()).thenReturn("DKY SERVER"); |
| 79 | + String result = repo.getServerName(); |
| 80 | + assertNotNull(result); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testGetAlertLogFileLineCount() throws JSchException, IOException { |
| 85 | + // Arrange |
| 86 | + AlertLogCommand alc = new AlertLogCommand(); |
| 87 | + alc.setReadFilePath("C:\\alert_DKYDB.log"); |
| 88 | + |
| 89 | + String command = String.format("find /v /c \"\" %s", alc.getReadFilePath()); |
| 90 | + when(jschServer.executeCommand(command)).thenReturn(String.valueOf(alertLogLines.length)); |
| 91 | + |
| 92 | + // Act |
| 93 | + int lineCount = repo.getAlertLogFileLineCount(alc); |
| 94 | + |
| 95 | + // Assert |
| 96 | + assertEquals(lineCount, alertLogLines.length); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testCheckAlertLog() throws JSchException, IOException { |
| 101 | + // Arrange |
| 102 | + AlertLogCommand alc = new AlertLogCommand(); |
| 103 | + alc.setReadLine(10); |
| 104 | + alc.setReadFilePath("C:\\alert_DKYDB.log"); |
| 105 | + |
| 106 | + String command = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine()); |
| 107 | + when(jschServer.executeCommand(command)).thenReturn(alertLogString); |
| 108 | + |
| 109 | + // Act |
| 110 | + String result = repo.checkAlertLog(alc); |
| 111 | + |
| 112 | + // Assert |
| 113 | + assertEquals(result, alertLogString); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testCheckAlertLogDuringPeriod() throws JSchException, IOException { |
| 118 | + // Arrange |
| 119 | + AlertLogCommand alc = new AlertLogCommand(); |
| 120 | + alc.setReadLine(10); |
| 121 | + alc.setReadFilePath("C:\\alert_DKYDB.log"); |
| 122 | + |
| 123 | + String command1 = String.format("find /v /c \"\" %s", alc.getReadFilePath()); |
| 124 | + when(jschServer.executeCommand(command1)).thenReturn(String.valueOf(alertLogLines.length)); |
| 125 | + |
| 126 | + String command2 = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine()); |
| 127 | + when(jschServer.executeCommand(command2)).thenReturn(alertLogString); |
| 128 | + |
| 129 | + // Act |
| 130 | + AlertLog alertLog = repo.checkAlertLogDuringPeriod(alc, "2022-03-24", "2022-03-29"); |
| 131 | + |
| 132 | + // Assert |
| 133 | + assertEquals(alertLog.getTotalLineCount(), 12); |
| 134 | + assertEquals(alertLog.getAlertLogs().size(), 7); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testCheckAlertLogDuringPeriod_ReadLineBiggerThenTotalLineCnt() throws JSchException, IOException { |
| 139 | + // Arrange |
| 140 | + AlertLogCommand alc = new AlertLogCommand(); |
| 141 | + alc.setReadLine(20); |
| 142 | + alc.setReadFilePath("C:\\alert_DKYDB.log"); |
| 143 | + |
| 144 | + String command1 = String.format("find /v /c \"\" %s", alc.getReadFilePath()); |
| 145 | + when(jschServer.executeCommand(command1)).thenReturn("26"); |
| 146 | + |
| 147 | + String command2 = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine()); |
| 148 | + StringBuilder builder = new StringBuilder(); |
| 149 | + for (int i = 0; i < Math.min(alertLogLines.length, alc.getReadLine()); i++) { |
| 150 | + builder.append(alertLogLines[i]).append("\n"); |
| 151 | + } |
| 152 | + when(jschServer.executeCommand(command2)).thenReturn(builder.toString()); |
| 153 | + |
| 154 | + String command3 = String.format("Get-Content %s -Tail %d", alc.getReadFilePath(), alc.getReadLine() * 2); |
| 155 | + builder = new StringBuilder(); |
| 156 | + for (int i = 0; i < Math.min(alertLogLines.length, alc.getReadLine() * 2); i++) { |
| 157 | + builder.append(alertLogLines[i]).append("\n"); |
| 158 | + } |
| 159 | + when(jschServer.executeCommand(command3)).thenReturn(builder.toString()); |
| 160 | + |
| 161 | + // Act |
| 162 | + AlertLog alertLog = repo.checkAlertLogDuringPeriod(alc, "2022-03-23", "2022-03-24"); |
| 163 | + |
| 164 | + // Assert |
| 165 | + assertEquals(alertLog.getTotalLineCount(), 3); |
| 166 | + assertEquals(alertLog.getAlertLogs().size(), 2); |
| 167 | + } |
| 168 | +} |
0 commit comments