Skip to content

Commit 7791cf4

Browse files
authored
Updated Test Coverage (#38)
* Updated Test Coverage * fix path check
1 parent 70dd35b commit 7791cf4

File tree

2 files changed

+213
-6
lines changed

2 files changed

+213
-6
lines changed

src/test/java/io/nats/NatsClusterTest.java

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
import org.junit.jupiter.api.Test;
1717
import org.junit.jupiter.api.parallel.Isolated;
1818

19+
import java.nio.file.Paths;
1920
import java.util.Collections;
2021
import java.util.List;
2122

2223
import static io.nats.NatsRunnerUtils.*;
23-
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
import static org.junit.jupiter.api.Assertions.*;
2425

2526
@Isolated
2627
public class NatsClusterTest extends TestBase {
@@ -75,4 +76,102 @@ private void _testCreateCluster(List<ClusterInsert> clusterInserts, boolean js)
7576
}
7677
}
7778
}
79+
80+
@Test
81+
public void testClusterNodeConstruction() {
82+
ClusterNode cn = new ClusterNode("name", "server", 1234, 5678);
83+
assertEquals("name", cn.clusterName);
84+
assertEquals("server", cn.serverName);
85+
assertNull(cn.host);
86+
assertEquals(1234, cn.port);
87+
assertEquals(5678, cn.listen);
88+
assertNull(cn.monitor);
89+
assertNull(cn.jsStoreDir);
90+
91+
cn = new ClusterNode("name", "server", 1234, 5678, 9999);
92+
assertEquals("name", cn.clusterName);
93+
assertEquals("server", cn.serverName);
94+
assertNull(cn.host);
95+
assertEquals(1234, cn.port);
96+
assertEquals(5678, cn.listen);
97+
assertNotNull(cn.monitor);
98+
assertEquals(9999, cn.monitor);
99+
assertNull(cn.jsStoreDir);
100+
101+
cn = new ClusterNode("name", "server", 1234, 5678, Paths.get("path"));
102+
assertEquals("name", cn.clusterName);
103+
assertEquals("server", cn.serverName);
104+
assertNull(cn.host);
105+
assertEquals(1234, cn.port);
106+
assertEquals(5678, cn.listen);
107+
assertNull(cn.monitor);
108+
assertNotNull(cn.jsStoreDir);
109+
assertEquals("path", cn.jsStoreDir.toString());
110+
111+
cn = new ClusterNode("name", "server", "host", 1234, 5678, 9999, Paths.get("path"));
112+
assertEquals("name", cn.clusterName);
113+
assertEquals("server", cn.serverName);
114+
assertEquals("host", cn.host);
115+
assertEquals(1234, cn.port);
116+
assertEquals(5678, cn.listen);
117+
assertNotNull(cn.monitor);
118+
assertEquals(9999, cn.monitor);
119+
assertNotNull(cn.jsStoreDir);
120+
assertEquals("path", cn.jsStoreDir.toString());
121+
122+
cn = ClusterNode.builder()
123+
.clusterName("name")
124+
.serverName("server")
125+
.host("host")
126+
.port(1234)
127+
.listen(5678)
128+
.monitor(9999)
129+
.jsStoreDir(Paths.get("path"))
130+
.build()
131+
;
132+
assertEquals("name", cn.clusterName);
133+
assertEquals("server", cn.serverName);
134+
assertEquals("host", cn.host);
135+
assertEquals(1234, cn.port);
136+
assertEquals(5678, cn.listen);
137+
assertNotNull(cn.monitor);
138+
assertEquals(9999, cn.monitor);
139+
assertNotNull(cn.jsStoreDir);
140+
assertEquals("path", cn.jsStoreDir.toString());
141+
}
142+
143+
@Test
144+
public void testClusterInsertCoverage() {
145+
146+
ClusterNode cn = ClusterNode.builder()
147+
.clusterName("name")
148+
.serverName("server")
149+
.host("host")
150+
.port(1234)
151+
.listen(5678)
152+
.monitor(9999)
153+
.jsStoreDir(Paths.get("path"))
154+
.build()
155+
;
156+
157+
ClusterInsert ci = new ClusterInsert(cn, null);
158+
assertEquals("name", ci.node.clusterName);
159+
assertEquals("server", ci.node.serverName);
160+
assertEquals("host", ci.node.host);
161+
assertEquals(1234, ci.node.port);
162+
assertEquals(5678, ci.node.listen);
163+
assertNotNull(ci.node.monitor);
164+
assertEquals(9999, ci.node.monitor);
165+
assertNotNull(ci.node.jsStoreDir);
166+
assertEquals("path", ci.node.jsStoreDir.toString());
167+
168+
assertNull(ci.configInserts);
169+
170+
ci = new ClusterInsert(cn, new String[0]);
171+
assertNull(ci.configInserts);
172+
173+
ci = new ClusterInsert(cn, new String[]{"insert"});
174+
assertNotNull(ci.configInserts);
175+
assertEquals("insert", ci.configInserts[0]);
176+
}
78177
}

src/test/java/io/nats/NatsRunnerUtilsTest.java

Lines changed: 113 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,47 @@
1515

1616
import org.junit.jupiter.api.Test;
1717

18-
import static io.nats.NatsRunnerUtils.DEFAULT_NATS_SERVER;
19-
import static io.nats.NatsRunnerUtils.getResolvedServerPath;
18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.nio.file.Path;
21+
import java.nio.file.Paths;
22+
import java.util.List;
23+
24+
import static io.nats.NatsRunnerUtils.*;
2025
import static org.junit.jupiter.api.Assertions.*;
2126

2227
public class NatsRunnerUtilsTest extends TestBase {
2328

2429
@Test
25-
public void testGetUriForPort() throws Exception {
30+
public void testGetUriForPort() {
2631
//noinspection deprecation
2732
assertEquals("nats://localhost:1234", NatsRunnerUtils.getURIForPort(1234));
2833
}
2934

3035
@Test
31-
public void testGetNatsServerVersionString() throws Exception {
36+
public void testGetNatsLocalhostUri() {
37+
assertEquals("nats://localhost:1234", NatsRunnerUtils.getNatsLocalhostUri(1234));
38+
}
39+
40+
@Test
41+
public void testGetNatsUri() {
42+
assertEquals("nats://host:1234", NatsRunnerUtils.getNatsUri("host", 1234));
43+
}
44+
45+
@Test
46+
public void testGetLocalhostUri() {
47+
assertEquals("schema://localhost:1234", NatsRunnerUtils.getLocalhostUri("schema", 1234));
48+
}
49+
50+
@Test
51+
public void testGetNatsServerVersionString() {
3252
String v = NatsRunnerUtils.getNatsServerVersionString();
3353
assertNotNull(v);
3454
assertTrue(v.startsWith("nats-server: v"));
3555
}
3656

3757
@Test
38-
public void testGetResolvedServerPath() throws Exception {
58+
public void testGetResolvedServerPath() {
3959
assertEquals(DEFAULT_NATS_SERVER, getResolvedServerPath());
4060

4161
//noinspection deprecation
@@ -46,4 +66,92 @@ public void testGetResolvedServerPath() throws Exception {
4666
NatsRunnerUtils.clearServerPath();
4767
assertEquals(DEFAULT_NATS_SERVER, getResolvedServerPath());
4868
}
69+
70+
@Test
71+
public void testCreateClusterInserts() throws IOException {
72+
Path t = Paths.get("path");
73+
int p = 4220;
74+
int l = 4230;
75+
int m = 4280;
76+
String c = "cluster";
77+
String s = "server";
78+
String h = "127.0.0.1";
79+
80+
validateClusterInserts(3, p, l, c, s, h, null, null, createClusterInserts());
81+
validateClusterInserts(2, p, l, c, s, h, null, null, createClusterInserts(2));
82+
validateClusterInserts(3, p, l, c, s, h, null, t, createClusterInserts(t));
83+
validateClusterInserts(2, p, l, c, s, h, null, t, createClusterInserts(2, t));
84+
85+
c = "clstr";
86+
s = "srvr";
87+
validateClusterInserts(2, p, l, c, s, h, null, null, createClusterInserts(2, c, s));
88+
validateClusterInserts(2, p, l, c, s, h, null, t, createClusterInserts(2, c, s, t));
89+
validateClusterInserts(2, p, l, c, s, h, null, null, createClusterInserts(2, c, s, false));
90+
validateClusterInserts(2, p, l, c, s, h, m, null, createClusterInserts(2, c, s, true));
91+
validateClusterInserts(2, p, l, c, s, h, null, t, createClusterInserts(2, c, s, false, t));
92+
validateClusterInserts(2, p, l, c, s, h, m, t, createClusterInserts(2, c, s, true, t));
93+
94+
p = 5220;
95+
l = 5230;
96+
m = 5280;
97+
h = "host";
98+
c = "cluster";
99+
s = "server";
100+
101+
defaultHost(h);
102+
defaultPortStart(p);
103+
defaultListenStart(l);
104+
defaultMonitorStart(m);
105+
106+
validateClusterInserts(3, p, l, c, s, h, null, null, createClusterInserts());
107+
validateClusterInserts(2, p, l, c, s, h, null, null, createClusterInserts(2));
108+
validateClusterInserts(3, p, l, c, s, h, null, t, createClusterInserts(t));
109+
validateClusterInserts(2, p, l, c, s, h, null, t, createClusterInserts(2, t));
110+
111+
c = "clstr";
112+
s = "srvr";
113+
validateClusterInserts(2, p, l, c, s, h, null, null, createClusterInserts(2, c, s));
114+
validateClusterInserts(2, p, l, c, s, h, null, t, createClusterInserts(2, c, s, t));
115+
validateClusterInserts(2, p, l, c, s, h, null, null, createClusterInserts(2, c, s, false));
116+
validateClusterInserts(2, p, l, c, s, h, m, null, createClusterInserts(2, c, s, true));
117+
validateClusterInserts(2, p, l, c, s, h, null, t, createClusterInserts(2, c, s, false, t));
118+
validateClusterInserts(2, p, l, c, s, h, m, t, createClusterInserts(2, c, s, true, t));
119+
}
120+
121+
private static void validateClusterInserts(int count,
122+
int basePort,
123+
int baseListen,
124+
String cluster,
125+
String server,
126+
String host,
127+
Integer baseMonitor,
128+
Path jsStoreDir,
129+
List<ClusterInsert> list) {
130+
assertEquals(count, list.size());
131+
for (int x = 0; x < list.size(); x++) {
132+
int port = basePort + x;
133+
int listen = baseListen + x;
134+
ClusterInsert ci = list.get(x);
135+
assertEquals(cluster, ci.node.clusterName);
136+
assertEquals(server + x, ci.node.serverName);
137+
assertEquals(port, ci.node.port);
138+
assertEquals(listen, ci.node.listen);
139+
assertEquals(host, ci.node.host);
140+
if (baseMonitor == null) {
141+
assertNull(ci.node.monitor);
142+
}
143+
else {
144+
assertEquals(baseMonitor + x, ci.node.monitor);
145+
}
146+
if (jsStoreDir == null) {
147+
assertNull(ci.node.jsStoreDir);
148+
}
149+
else if (File.separatorChar == '\\') {
150+
assertEquals(jsStoreDir + "\\" + port, ci.node.jsStoreDir.toString());
151+
}
152+
else {
153+
assertEquals(jsStoreDir + "/" + port, ci.node.jsStoreDir.toString());
154+
}
155+
}
156+
}
49157
}

0 commit comments

Comments
 (0)