Skip to content

Commit 207837d

Browse files
committed
Write serverMonitoringRepository test code
1 parent 513589b commit 207837d

File tree

2 files changed

+192
-102
lines changed

2 files changed

+192
-102
lines changed

Repository/src/test/java/root/repository/implement/WindowServerMonitoringRepositoryTest.java

Lines changed: 192 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -3,163 +3,253 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertNotNull;
55
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
import static org.junit.jupiter.api.Assertions.fail;
68
import static org.mockito.Mockito.mock;
79
import static org.mockito.Mockito.when;
810

11+
import java.io.ByteArrayInputStream;
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.nio.file.Files;
15+
import java.nio.file.Paths;
16+
import java.util.List;
17+
18+
import org.junit.jupiter.api.AfterAll;
919
import org.junit.jupiter.api.BeforeAll;
1020
import org.junit.jupiter.api.BeforeEach;
1121
import org.junit.jupiter.api.Test;
1222

23+
import com.jcraft.jsch.Channel;
24+
import com.jcraft.jsch.ChannelSftp;
25+
import com.jcraft.jsch.Session;
26+
1327
import root.common.server.implement.AlertLogCommand;
28+
import root.common.server.implement.JschConnectionInfo;
1429
import root.common.server.implement.JschServer;
30+
import root.common.server.implement.ServerOS;
31+
import root.core.domain.OSDiskUsage;
1532
import root.core.repository.constracts.ServerMonitoringRepository;
1633

1734
public class WindowServerMonitoringRepositoryTest {
18-
19-
public JschServer jschServer;
20-
public ServerMonitoringRepository repo;
35+
36+
public static JschServer jschServer;
37+
public static ServerMonitoringRepository repo;
2138
public static String alertLogString = "";
22-
public static String[] alertLogLines;
39+
public static String[] alertLogLines;
2340

2441
@BeforeAll
2542
public static void before() {
26-
StringBuffer sb = new StringBuffer();
27-
sb.append("2022-03-23T13:38:35.065184+09:00").append("\n");
28-
sb.append("Thread 1 advanced to log sequence 7606 (LGWR switch)").append("\n");
29-
sb.append(" Current log# 5 seq# 7606 mem# 0: +REDO/1003346093").append("\n");
30-
sb.append("2022-03-24T13:39:00.180206+09:00").append("\n");
31-
sb.append("Archived Log entry 13398 added for T-1.S-7605 ID 0x8155080b LAD:1").append("\n");
32-
sb.append("2022-03-25T14:24:57.291344+09:00").append("\n");
33-
sb.append("Session (223,56276): RECO logon successful: Inbound connection from client").append("\n");
34-
sb.append("Session (223,56276): RECO logon successful: DB Logon User: RECO, ").append("\n");
35-
sb.append("Session (223,56276): RECO logon successful: Client IP Address: -").append("\n");
36-
sb.append("2022-03-26T18:04:53.572965+09:00").append("\n");
37-
sb.append("Thread 1 advanced to log sequence 7607 (LGWR switch)").append("\n");
38-
sb.append(" Current log# 1 seq# 7607 mem# 0: +REDO/DBERP/ONLINELOG/group_1.257.966360593").append("\n");
39-
sb.append("2022-03-27T18:05:16.929231+09:00").append("\n");
40-
sb.append("Archived Log entry 13400 added for T-1.S-7606 ID 0x8155080b LAD:1").append("\n");
41-
sb.append("2022-03-28T00:02:21.629284+09:00").append("\n");
42-
sb.append("TABLE SYS.WRI$_OPTSTAT_HISTHEAD_HISTORY: ADDED INTERVAL PARTITION SYS_P38333)").append("\n");
43-
sb.append("TABLE SYS.WRI$_OPTSTAT_HISTGRM_HISTORY: ADDED INTERVAL PARTITION SYS_P38336)").append("\n");
44-
sb.append("2022-03-28T01:00:18.971650+09:00").append("\n");
45-
sb.append("ALTER SYSTEM ARCHIVE LOG").append("\n");
46-
sb.append("2022-03-29T01:00:18.980968+09:00").append("\n");
47-
sb.append("Thread 1 advanced to log sequence 7608 (LGWR switch)").append("\n");
48-
sb.append(" Current log# 6 seq# 7608 mem# 0: +REDO/1003346037").append("\n");
49-
sb.append("2022-03-30T01:00:38.903080+09:00").append("\n");
50-
sb.append("Archived Log entry 13401 added for T-1.S-7607 ID 0x8155080b LAD:1").append("\n");
51-
sb.append("2022-03-31T02:56:51.984291+09:00").append("\n");
52-
sb.append("Starting control autobackup").append("\n");
53-
alertLogString = sb.toString();
54-
43+
// Load alert log test file
44+
try {
45+
alertLogString = Files.readString(Paths.get("src/test/resources/alertlog_sample.txt"));
46+
} catch (IOException e) {
47+
}
5548
alertLogLines = alertLogString.split("\n");
56-
}
57-
58-
@BeforeEach
59-
public void setup() {
60-
jschServer = mock(JschServer.class);
49+
50+
String serverName = "testServer";
51+
ServerOS serverOS = ServerOS.WINDOW;
52+
String host = "192.168.154.1";
53+
int port = 22;
54+
String id = "dky";
55+
String password = "ehruddbs1!";
56+
57+
jschServer = new JschServer(new JschConnectionInfo(serverName, serverOS, host, port, id, password));
58+
jschServer.init();
6159
repo = new WindowServerMonitoringRepository(jschServer);
60+
61+
// Send test alert log file to remote server
62+
try {
63+
Session session = jschServer.getSession();
64+
Channel channel = session.openChannel("sftp");
65+
channel.connect();
66+
ChannelSftp channelSftp = (ChannelSftp) channel;
67+
InputStream is = new ByteArrayInputStream(alertLogString.getBytes());
68+
channelSftp.put(is, "C:\\test.txt");
69+
channelSftp.disconnect();
70+
channel.disconnect();
71+
} catch (Exception e) {
72+
fail(e.getMessage());
73+
}
6274
}
6375

64-
@Test
65-
public void testGetServerName_ServerNameIsNull() {
66-
when(jschServer.getServerName()).thenReturn(null);
67-
String result = repo.getServerName();
68-
assertNull(result);
76+
@AfterAll
77+
public static void teardown() {
78+
try {
79+
// jschServer.executeCommand("rd /s /q C:\\test.txt");
80+
} catch (Exception e) {
81+
}
6982
}
7083

7184
@Test
72-
public void testGetServerName_ServerNameIsNotNull() {
73-
when(jschServer.getServerName()).thenReturn("DKY SERVER");
85+
public void testGetServerName() {
86+
// Act
7487
String result = repo.getServerName();
75-
assertNotNull(result);
88+
89+
// Assert
90+
assertEquals("testServer", result);
7691
}
7792

7893
@Test
79-
public void testGetAlertLogFileLineCount() throws Exception {
94+
public void testGetAlertLogFileLineCount() {
8095
// Arrange
81-
AlertLogCommand alc = new AlertLogCommand();
82-
alc.setReadFilePath("C:\\alert_DKYDB.log");
83-
84-
String command = String.format("find /v /c \"\" %s", alc.getReadFilePath());
85-
when(jschServer.executeCommand(command)).thenReturn(String.valueOf(alertLogLines.length));
96+
AlertLogCommand alc = new AlertLogCommand(10, "C:\\test.txt");
8697

8798
// Act
8899
int lineCount = repo.getAlertLogFileLineCount(alc);
89100

90101
// Assert
91-
assertEquals(lineCount, alertLogLines.length);
102+
assertEquals(alertLogLines.length, lineCount);
92103
}
93-
104+
94105
@Test
95106
public void testCheckAlertLog() throws Exception {
96107
// Arrange
97-
AlertLogCommand alc = new AlertLogCommand();
98-
alc.setReadLine(10);
99-
alc.setReadFilePath("C:\\alert_DKYDB.log");
100-
101-
String command = String.format("tail %d %s", alc.getReadLine(), alc.getReadFilePath());
102-
when(jschServer.executeCommand(command)).thenReturn(alertLogString);
108+
AlertLogCommand alc = new AlertLogCommand(alertLogLines.length, "C:\\test.txt");
103109

104110
// Act
105111
String result = repo.checkAlertLog(alc);
106112

107113
// Assert
108-
assertEquals(result, alertLogString);
114+
assertEquals(alertLogString, result);
109115
}
110116

111-
/*
112117
@Test
113-
public void testCheckAlertLogDuringPeriod() throws Exception {
118+
public void testCheckAlertLog_Last10Lines() throws Exception {
114119
// Arrange
115-
AlertLogCommand alc = new AlertLogCommand();
116-
alc.setReadLine(10);
117-
alc.setReadFilePath("C:\\alert_DKYDB.log");
118-
119-
String command1 = String.format("find /v /c \"\" %s", alc.getReadFilePath());
120-
when(jschServer.executeCommand(command1)).thenReturn(String.valueOf(alertLogLines.length));
121-
122-
String command2 = String.format("tail %d %s", alc.getReadLine(), alc.getReadFilePath());
123-
when(jschServer.executeCommand(command2)).thenReturn(alertLogString);
120+
AlertLogCommand alc = new AlertLogCommand(10, "C:\\test.txt");
124121

125122
// Act
126-
AlertLog alertLog = repo.checkAlertLogDuringPeriod(alc, "2022-03-24", "2022-03-29");
123+
String result = repo.checkAlertLog(alc);
127124

128125
// Assert
129-
assertEquals(alertLog.getTotalLineCount(), 12);
130-
assertEquals(alertLog.getAlertLogs().size(), 7);
126+
StringBuffer expected = new StringBuffer();
127+
for (int i = alertLogLines.length - 10; i < alertLogLines.length; i++) {
128+
expected.append(alertLogLines[i]).append("\n");
129+
}
130+
assertEquals(expected.toString(), result);
131131
}
132-
132+
133133
@Test
134-
public void testCheckAlertLogDuringPeriod_ReadLineBiggerThenTotalLineCnt() throws Exception {
134+
public void testCheckOSDiskUsage() {
135135
// Arrange
136-
AlertLogCommand alc = new AlertLogCommand();
137-
alc.setReadLine(20);
138-
alc.setReadFilePath("C:\\alert_DKYDB.log");
139-
140-
String command1 = String.format("find /v /c \"\" %s", alc.getReadFilePath());
141-
when(jschServer.executeCommand(command1)).thenReturn("26");
142-
143-
String command2 = String.format("tail %d %s", alc.getReadLine(), alc.getReadFilePath());
144-
StringBuilder builder = new StringBuilder();
145-
for (int i = 0; i < Math.min(alertLogLines.length, alc.getReadLine()); i++) {
146-
builder.append(alertLogLines[i]).append("\n");
147-
}
148-
when(jschServer.executeCommand(command2)).thenReturn(builder.toString());
149-
150-
String command3 = String.format("tail %d %s", alc.getReadLine() * 2, alc.getReadFilePath());
151-
builder = new StringBuilder();
152-
for (int i = 0; i < Math.min(alertLogLines.length, alc.getReadLine() * 2); i++) {
153-
builder.append(alertLogLines[i]).append("\n");
154-
}
155-
when(jschServer.executeCommand(command3)).thenReturn(builder.toString());
156-
157136
// Act
158-
AlertLog alertLog = repo.checkAlertLogDuringPeriod(alc, "2022-03-23", "2022-03-24");
137+
// List<OSDiskUsage> result = repo.checkOSDiskUsage();
159138

160139
// Assert
161-
assertEquals(alertLog.getTotalLineCount(), 3);
162-
assertEquals(alertLog.getAlertLogs().size(), 2);
140+
// assertTrue(result.size() != 0);
163141
}
164-
*/
142+
143+
/*
144+
* @Test
145+
* public void testGetServerName_ServerNameIsNull() {
146+
* when(jschServer.getServerName()).thenReturn(null);
147+
* String result = repo.getServerName();
148+
* assertNull(result);
149+
* }
150+
*
151+
* @Test
152+
* public void testGetServerName_ServerNameIsNotNull() {
153+
* when(jschServer.getServerName()).thenReturn("DKY SERVER");
154+
* String result = repo.getServerName();
155+
* assertNotNull(result);
156+
* }
157+
*
158+
* @Test
159+
* public void testGetAlertLogFileLineCount() throws Exception {
160+
* // Arrange
161+
* AlertLogCommand alc = new AlertLogCommand();
162+
* alc.setReadFilePath("C:\\alert_DKYDB.log");
163+
*
164+
* String command = String.format("find /v /c \"\" %s", alc.getReadFilePath());
165+
* when(jschServer.executeCommand(command)).thenReturn(String.valueOf(
166+
* alertLogLines.length));
167+
*
168+
* // Act
169+
* int lineCount = repo.getAlertLogFileLineCount(alc);
170+
*
171+
* // Assert
172+
* assertEquals(lineCount, alertLogLines.length);
173+
* }
174+
*
175+
* @Test
176+
* public void testCheckAlertLog() throws Exception {
177+
* // Arrange
178+
* AlertLogCommand alc = new AlertLogCommand();
179+
* alc.setReadLine(10);
180+
* alc.setReadFilePath("C:\\alert_DKYDB.log");
181+
*
182+
* String command = String.format("tail %d %s", alc.getReadLine(),
183+
* alc.getReadFilePath());
184+
* when(jschServer.executeCommand(command)).thenReturn(alertLogString);
185+
*
186+
* // Act
187+
* String result = repo.checkAlertLog(alc);
188+
*
189+
* // Assert
190+
* assertEquals(result, alertLogString);
191+
* }
192+
*/
193+
/*
194+
* @Test
195+
* public void testCheckAlertLogDuringPeriod() throws Exception {
196+
* // Arrange
197+
* AlertLogCommand alc = new AlertLogCommand();
198+
* alc.setReadLine(10);
199+
* alc.setReadFilePath("C:\\alert_DKYDB.log");
200+
*
201+
* String command1 = String.format("find /v /c \"\" %s", alc.getReadFilePath());
202+
* when(jschServer.executeCommand(command1)).thenReturn(String.valueOf(
203+
* alertLogLines.length));
204+
*
205+
* String command2 = String.format("tail %d %s", alc.getReadLine(),
206+
* alc.getReadFilePath());
207+
* when(jschServer.executeCommand(command2)).thenReturn(alertLogString);
208+
*
209+
* // Act
210+
* AlertLog alertLog = repo.checkAlertLogDuringPeriod(alc, "2022-03-24",
211+
* "2022-03-29");
212+
*
213+
* // Assert
214+
* assertEquals(alertLog.getTotalLineCount(), 12);
215+
* assertEquals(alertLog.getAlertLogs().size(), 7);
216+
* }
217+
*
218+
* @Test
219+
* public void testCheckAlertLogDuringPeriod_ReadLineBiggerThenTotalLineCnt()
220+
* throws Exception {
221+
* // Arrange
222+
* AlertLogCommand alc = new AlertLogCommand();
223+
* alc.setReadLine(20);
224+
* alc.setReadFilePath("C:\\alert_DKYDB.log");
225+
*
226+
* String command1 = String.format("find /v /c \"\" %s", alc.getReadFilePath());
227+
* when(jschServer.executeCommand(command1)).thenReturn("26");
228+
*
229+
* String command2 = String.format("tail %d %s", alc.getReadLine(),
230+
* alc.getReadFilePath());
231+
* StringBuilder builder = new StringBuilder();
232+
* for (int i = 0; i < Math.min(alertLogLines.length, alc.getReadLine()); i++) {
233+
* builder.append(alertLogLines[i]).append("\n");
234+
* }
235+
* when(jschServer.executeCommand(command2)).thenReturn(builder.toString());
236+
*
237+
* String command3 = String.format("tail %d %s", alc.getReadLine() * 2,
238+
* alc.getReadFilePath());
239+
* builder = new StringBuilder();
240+
* for (int i = 0; i < Math.min(alertLogLines.length, alc.getReadLine() * 2);
241+
* i++) {
242+
* builder.append(alertLogLines[i]).append("\n");
243+
* }
244+
* when(jschServer.executeCommand(command3)).thenReturn(builder.toString());
245+
*
246+
* // Act
247+
* AlertLog alertLog = repo.checkAlertLogDuringPeriod(alc, "2022-03-23",
248+
* "2022-03-24");
249+
*
250+
* // Assert
251+
* assertEquals(alertLog.getTotalLineCount(), 3);
252+
* assertEquals(alertLog.getAlertLogs().size(), 2);
253+
* }
254+
*/
165255
}
1.28 KB
Binary file not shown.

0 commit comments

Comments
 (0)