1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.process.cluster.health;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Random;
import org.junit.Test;
import static org.apache.commons.lang3.RandomStringUtils.secure;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.sonar.process.cluster.health.NodeDetails.newNodeDetailsBuilder;
public class NodeDetailsTest {
private Random random = new Random();
private NodeDetailsTestSupport testSupport = new NodeDetailsTestSupport(random);
private NodeDetails.Type randomType = testSupport.randomType();
private NodeDetails.Builder builderUnderTest = newNodeDetailsBuilder();
@Test
public void setType_throws_NPE_if_arg_is_null() {
assertThatThrownBy(() -> builderUnderTest.setType(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("type can't be null");
}
@Test
public void setName_throws_NPE_if_arg_is_null() {
assertThatThrownBy(() -> builderUnderTest.setName(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("name can't be null");
}
@Test
public void setName_throws_IAE_if_arg_is_empty() {
assertThatThrownBy(() -> builderUnderTest.setName(""))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("name can't be empty");
}
@Test
public void setName_throws_IAE_if_arg_is_empty_after_trim() {
assertThatThrownBy(() -> builderUnderTest.setName(" "))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("name can't be empty");
}
@Test
public void setHost_throws_NPE_if_arg_is_null() {
assertThatThrownBy(() -> builderUnderTest.setHost(null))
.isInstanceOf(NullPointerException.class)
.hasMessage("host can't be null");
}
@Test
public void setHost_throws_IAE_if_arg_is_empty() {
assertThatThrownBy(() -> builderUnderTest.setHost(""))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("host can't be empty");
}
@Test
public void setHost_throws_IAE_if_arg_is_empty_after_trim() {
assertThatThrownBy(() -> builderUnderTest.setHost(" "))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("host can't be empty");
}
@Test
public void setPort_throws_IAE_if_arg_is_less_than_1() {
assertThatThrownBy(() -> builderUnderTest.setPort(-random.nextInt(5)))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("port must be > 0");
}
@Test
public void setStarted_throws_IAE_if_arg_is_less_than_1() {
assertThatThrownBy(() -> builderUnderTest.setStartedAt(-random.nextInt(5)))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("startedAt must be > 0");
}
@Test
public void build_throws_NPE_if_type_is_null() {
assertThatThrownBy(() -> builderUnderTest.build())
.isInstanceOf(NullPointerException.class)
.hasMessage("type can't be null");
}
@Test
public void build_throws_NPE_if_name_is_null() {
builderUnderTest
.setType(randomType);
assertThatThrownBy(() -> builderUnderTest.build())
.isInstanceOf(NullPointerException.class)
.hasMessage("name can't be null");
}
@Test
public void build_throws_NPE_if_host_is_null() {
builderUnderTest
.setType(randomType)
.setName(secure().nextAlphanumeric(2));
assertThatThrownBy(() -> builderUnderTest.build())
.isInstanceOf(NullPointerException.class)
.hasMessage("host can't be null");
}
@Test
public void build_throws_IAE_if_setPort_not_called() {
builderUnderTest
.setType(randomType)
.setName(secure().nextAlphanumeric(2))
.setHost(secure().nextAlphanumeric(3));
assertThatThrownBy(() -> builderUnderTest.build())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("port must be > 0");
}
@Test
public void build_throws_IAE_if_setStarted_not_called() {
builderUnderTest
.setType(randomType)
.setName(secure().nextAlphanumeric(2))
.setHost(secure().nextAlphanumeric(3))
.setPort(1 + random.nextInt(33));
assertThatThrownBy(() -> builderUnderTest.build())
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("startedAt must be > 0");
}
@Test
public void equals_is_based_on_content() {
NodeDetails.Builder builder = testSupport.randomNodeDetailsBuilder();
NodeDetails underTest = builder.build();
assertThat(underTest).isEqualTo(underTest);
assertThat(builder.build())
.isEqualTo(underTest)
.isNotSameAs(underTest);
assertThat(underTest)
.isNotNull()
.isNotEqualTo(new Object());
}
@Test
public void hashcode_is_based_on_content() {
NodeDetails.Builder builder = testSupport.randomNodeDetailsBuilder();
NodeDetails underTest = builder.build();
assertThat(builder.build().hashCode())
.isEqualTo(underTest.hashCode());
}
@Test
public void NodeDetails_is_Externalizable() throws IOException, ClassNotFoundException {
NodeDetails source = testSupport.randomNodeDetails();
byte[] byteArray = NodeDetailsTestSupport.serialize(source);
NodeDetails underTest = (NodeDetails) new ObjectInputStream(new ByteArrayInputStream(byteArray)).readObject();
assertThat(underTest).isEqualTo(source);
}
@Test
public void verify_toString() {
String name = secure().nextAlphanumeric(3);
String host = secure().nextAlphanumeric(10);
int port = 1 + random.nextInt(10);
long startedAt = 1 + random.nextInt(666);
NodeDetails underTest = builderUnderTest
.setType(randomType)
.setName(name)
.setHost(host)
.setPort(port)
.setStartedAt(startedAt)
.build();
assertThat(underTest.toString())
.isEqualTo("NodeDetails{type=" + randomType + ", name='" + name + "', host='" + host + "', port=" + port + ", startedAt=" + startedAt + "}");
}
@Test
public void verify_getters() {
String name = secure().nextAlphanumeric(3);
String host = secure().nextAlphanumeric(10);
int port = 1 + random.nextInt(10);
long startedAt = 1 + random.nextInt(666);
NodeDetails underTest = builderUnderTest
.setType(randomType)
.setName(name)
.setHost(host)
.setPort(port)
.setStartedAt(startedAt)
.build();
assertThat(underTest.getType()).isEqualTo(randomType);
assertThat(underTest.getName()).isEqualTo(name);
assertThat(underTest.getHost()).isEqualTo(host);
assertThat(underTest.getPort()).isEqualTo(port);
assertThat(underTest.getStartedAt()).isEqualTo(startedAt);
}
}
|