|
| 1 | +package root.core.repository.implement; |
| 2 | + |
| 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 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import java.io.ByteArrayInputStream; |
| 10 | +import java.io.InputStream; |
| 11 | + |
| 12 | +import org.apache.commons.io.IOUtils; |
| 13 | +import org.junit.jupiter.api.BeforeAll; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.mockito.Mock; |
| 16 | + |
| 17 | +import com.jcraft.jsch.Channel; |
| 18 | +import com.jcraft.jsch.Session; |
| 19 | + |
| 20 | +import root.common.server.implement.JschServer; |
| 21 | +import root.core.domain.AlertLog; |
| 22 | +import root.core.domain.AlertLogCommand; |
| 23 | +import root.core.repository.constracts.ServerCheckRepository; |
| 24 | + |
| 25 | +public class ServerCheckRepositoryImplTest { |
| 26 | + |
| 27 | + @Mock |
| 28 | + public static JschServer jsch = mock(JschServer.class); |
| 29 | + |
| 30 | + public static ServerCheckRepository repo; |
| 31 | + |
| 32 | + public static String alertLogString = ""; |
| 33 | + |
| 34 | + @BeforeAll |
| 35 | + public static void before() { |
| 36 | + repo = new ServerCheckRepositoryImpl(jsch); |
| 37 | + StringBuffer sb = new StringBuffer(); |
| 38 | + sb.append("2022-03-24T13:38:35.065184+09:00").append("\n"); |
| 39 | + sb.append("Thread 1 advanced to log sequence 7606 (LGWR switch)").append("\n"); |
| 40 | + sb.append(" Current log# 5 seq# 7606 mem# 0: +REDO/1003346093").append("\n"); |
| 41 | + sb.append("2022-03-24T13:39:00.180206+09:00").append("\n"); |
| 42 | + sb.append("Archived Log entry 13398 added for T-1.S-7605 ID 0x8155080b LAD:1").append("\n"); |
| 43 | + sb.append("2022-03-25T14:24:57.291344+09:00").append("\n"); |
| 44 | + sb.append("Session (223,56276): RECO logon successful: Inbound connection from client").append("\n"); |
| 45 | + sb.append("Session (223,56276): RECO logon successful: DB Logon User: RECO, ").append("\n"); |
| 46 | + sb.append("Session (223,56276): RECO logon successful: Client IP Address: -").append("\n"); |
| 47 | + sb.append("2022-03-26T18:04:53.572965+09:00").append("\n"); |
| 48 | + sb.append("Thread 1 advanced to log sequence 7607 (LGWR switch)").append("\n"); |
| 49 | + sb.append(" Current log# 1 seq# 7607 mem# 0: +REDO/DBERP/ONLINELOG/group_1.257.966360593").append("\n"); |
| 50 | + sb.append("2022-03-27T18:05:16.929231+09:00").append("\n"); |
| 51 | + sb.append("Archived Log entry 13400 added for T-1.S-7606 ID 0x8155080b LAD:1").append("\n"); |
| 52 | + sb.append("2022-03-28T00:02:21.629284+09:00").append("\n"); |
| 53 | + sb.append("TABLE SYS.WRI$_OPTSTAT_HISTHEAD_HISTORY: ADDED INTERVAL PARTITION SYS_P38333)").append("\n"); |
| 54 | + sb.append("TABLE SYS.WRI$_OPTSTAT_HISTGRM_HISTORY: ADDED INTERVAL PARTITION SYS_P38336)").append("\n"); |
| 55 | + sb.append("2022-03-28T01:00:18.971650+09:00").append("\n"); |
| 56 | + sb.append("ALTER SYSTEM ARCHIVE LOG").append("\n"); |
| 57 | + sb.append("2022-03-29T01:00:18.980968+09:00").append("\n"); |
| 58 | + sb.append("Thread 1 advanced to log sequence 7608 (LGWR switch)").append("\n"); |
| 59 | + sb.append(" Current log# 6 seq# 7608 mem# 0: +REDO/1003346037").append("\n"); |
| 60 | + sb.append("2022-03-30T01:00:38.903080+09:00").append("\n"); |
| 61 | + sb.append("Archived Log entry 13401 added for T-1.S-7607 ID 0x8155080b LAD:1").append("\n"); |
| 62 | + sb.append("2022-03-31T02:56:51.984291+09:00").append("\n"); |
| 63 | + sb.append("Starting control autobackup").append("\n"); |
| 64 | + alertLogString = sb.toString(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void checkAlertLogTest() { |
| 69 | + Session session = mock(Session.class); |
| 70 | + Channel channel = mock(Channel.class); |
| 71 | + AlertLogCommand alc = mock(AlertLogCommand.class); |
| 72 | + InputStream in = new ByteArrayInputStream(alertLogString.getBytes()); |
| 73 | + |
| 74 | + when(repo.getSession()).thenReturn(session); |
| 75 | + when(jsch.openExecChannel(session, alc.getCommand())).thenReturn(channel); |
| 76 | + when(jsch.connectChannel(channel)).thenReturn(in); |
| 77 | + doNothing().when(jsch).disConnectChannel(channel); |
| 78 | + doNothing().when(channel).disconnect(); |
| 79 | + |
| 80 | + String result = repo.checkAlertLog(alc); |
| 81 | + assertEquals(result, alertLogString); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void getAlertLogFileLineCountTest() { |
| 86 | + Session session = mock(Session.class); |
| 87 | + Channel channel = mock(Channel.class); |
| 88 | + AlertLogCommand alc = mock(AlertLogCommand.class); |
| 89 | + InputStream in = new ByteArrayInputStream("26".getBytes()); |
| 90 | + |
| 91 | + when(repo.getSession()).thenReturn(session); |
| 92 | + when(jsch.openExecChannel(session, "cat " + alc.getReadFilePath() + " | wc -l")).thenReturn(channel); |
| 93 | + when(jsch.connectChannel(channel)).thenReturn(in); |
| 94 | + doNothing().when(jsch).disConnectChannel(channel); |
| 95 | + doNothing().when(channel).disconnect(); |
| 96 | + |
| 97 | + try { |
| 98 | + String result = IOUtils.toString(in, "UTF-8"); |
| 99 | + int fileLineCnt = Integer.parseInt(result.trim()); |
| 100 | + assertEquals(fileLineCnt, 26); |
| 101 | + } catch (Exception e) { |
| 102 | + fail(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void checkAlertLogDuringPeriod() { |
| 108 | + Session session = mock(Session.class); |
| 109 | + Channel channel = mock(Channel.class); |
| 110 | + Channel channel2 = mock(Channel.class); |
| 111 | + AlertLogCommand alc = mock(AlertLogCommand.class); |
| 112 | + InputStream in = new ByteArrayInputStream(alertLogString.getBytes()); |
| 113 | + InputStream in2 = new ByteArrayInputStream("26".getBytes()); |
| 114 | + |
| 115 | + when(repo.getSession()).thenReturn(session); |
| 116 | + when(jsch.openExecChannel(session, "cat " + alc.getReadFilePath() + " | wc -l")).thenReturn(channel2); |
| 117 | + when(jsch.connectChannel(channel2)).thenReturn(in2); |
| 118 | + doNothing().when(jsch).disConnectChannel(channel2); |
| 119 | + doNothing().when(channel2).disconnect(); |
| 120 | + |
| 121 | + when(jsch.openExecChannel(session, alc.getCommand())).thenReturn(channel); |
| 122 | + when(jsch.connectChannel(channel)).thenReturn(in); |
| 123 | + doNothing().when(jsch).disConnectChannel(channel); |
| 124 | + doNothing().when(channel).disconnect(); |
| 125 | + |
| 126 | + AlertLog alertLog = repo.checkAlertLogDuringPeriod(alc, "2022-03-24", "2022-03-29"); |
| 127 | + |
| 128 | + assertEquals(alertLog.getTotalLineCount(), 14); |
| 129 | + assertEquals(alertLog.getAlertLogs().size(), 8); |
| 130 | + } |
| 131 | +} |
0 commit comments