Skip to content

Commit 6d867fb

Browse files
committed
TDD: Write test code
1 parent d3952a3 commit 6d867fb

File tree

3 files changed

+76
-60
lines changed

3 files changed

+76
-60
lines changed

pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,21 @@
180180
<scope>test</scope>
181181
</dependency>
182182

183-
<!-- mockito -->
183+
<!-- mockito-core -->
184184
<dependency>
185185
<groupId>org.mockito</groupId>
186186
<artifactId>mockito-core</artifactId>
187187
<version>4.3.1</version>
188188
<scope>test</scope>
189189
</dependency>
190+
191+
<!-- mockito-inline -->
192+
<dependency>
193+
<groupId>org.mockito</groupId>
194+
<artifactId>mockito-inline</artifactId>
195+
<version>4.3.1</version>
196+
<scope>test</scope>
197+
</dependency>
190198

191199
<!-- logback -->
192200
<dependency>

src/test/java/root/common/server/implement/JschServerTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ public void testGetServerName() {
157157
String serverName = jsch.getServerName();
158158
assertEquals(serverName, "DKY SERVER");
159159
}
160+
@Test
161+
public void testExecuteCommand() throws JSchException, IOException {
162+
String result = jsch.executeCommand("echo 1");
163+
assertEquals(result, "1");
164+
}
160165

161166
@Test
162167
public void testValidateConn_Valid() {

src/test/java/root/core/repository/implement/LinuxServerMonitoringRepositoryTest.java

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
package root.core.repository.implement;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.fail;
5-
import static org.mockito.Mockito.doNothing;
64
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.mockStatic;
76
import static org.mockito.Mockito.when;
87

9-
import java.io.ByteArrayInputStream;
10-
import java.io.InputStream;
8+
import java.io.IOException;
119

1210
import org.apache.commons.io.IOUtils;
11+
import org.junit.jupiter.api.AfterAll;
1312
import org.junit.jupiter.api.BeforeAll;
13+
import org.junit.jupiter.api.BeforeEach;
1414
import org.junit.jupiter.api.Test;
15-
import org.mockito.Mock;
15+
import org.mockito.MockedStatic;
1616

17-
import com.jcraft.jsch.Channel;
18-
import com.jcraft.jsch.Session;
17+
import com.jcraft.jsch.JSchException;
1918

2019
import root.common.server.implement.JschServer;
2120
import root.core.domain.AlertLog;
@@ -24,16 +23,19 @@
2423

2524
public class LinuxServerMonitoringRepositoryTest {
2625

27-
@Mock
28-
public static JschServer jsch = mock(JschServer.class);
26+
public JschServer jschServer;
2927

30-
public static ServerMonitoringRepository repo;
28+
public ServerMonitoringRepository repo;
29+
30+
public static MockedStatic<IOUtils> ioUtilsMock;
3131

3232
public static String alertLogString = "";
33+
34+
public static String[] alertLogLines;
3335

3436
@BeforeAll
3537
public static void before() {
36-
repo = new LinuxServerMonitoringRepository(jsch);
38+
ioUtilsMock = mockStatic(IOUtils.class);
3739
StringBuffer sb = new StringBuffer();
3840
sb.append("2022-03-24T13:38:35.065184+09:00").append("\n");
3941
sb.append("Thread 1 advanced to log sequence 7606 (LGWR switch)").append("\n");
@@ -62,71 +64,72 @@ public static void before() {
6264
sb.append("2022-03-31T02:56:51.984291+09:00").append("\n");
6365
sb.append("Starting control autobackup").append("\n");
6466
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();
6580
}
6681

6782
@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
7185
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);
8091

92+
// Act
8193
String result = repo.checkAlertLog(alc);
94+
95+
// Assert
8296
assertEquals(result, alertLogString);
8397
}
8498

8599
@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
89102
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);
105114
}
106115

107116
@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
112119
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
128130
AlertLog alertLog = repo.checkAlertLogDuringPeriod(alc, "2022-03-24", "2022-03-29");
129131

132+
// Assert
130133
assertEquals(alertLog.getTotalLineCount(), 14);
131134
assertEquals(alertLog.getAlertLogs().size(), 8);
132135
}

0 commit comments

Comments
 (0)