Skip to content

Commit 9b8f89c

Browse files
authored
fix: replace SpotBugs suppressions with code-level test fixes
1 parent 6e75a9a commit 9b8f89c

18 files changed

Lines changed: 116 additions & 72 deletions

File tree

components/abstractions/spotBugsExcludeFilter.xml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,6 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubu
4646
<Bug pattern="RV_EXCEPTION_NOT_THROWN"/>
4747
<Class name="com.microsoft.kiota.serialization.SerializationHelpersTest" />
4848
</Match>
49-
<Match>
50-
<Bug pattern="NP_NONNULL_PARAM_VIOLATION" />
51-
<Or>
52-
<Class name="com.microsoft.kiota.MultiPartBodyTest" />
53-
<Class name="com.microsoft.kiota.PeriodAndDurationTest" />
54-
<Class name="com.microsoft.kiota.authentication.ApiKeyAuthenticationProviderTest" />
55-
<Class name="com.microsoft.kiota.serialization.DeserializationHelpersTest" />
56-
<Class name="com.microsoft.kiota.serialization.SerializationHelpersTest" />
57-
</Or>
58-
</Match>
59-
<Match>
60-
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE" />
61-
<Or>
62-
<Class name="com.microsoft.kiota.serialization.mocks.TestEntity$1" />
63-
<Class name="com.microsoft.kiota.serialization.mocks.TestBackedModelEntity$1" />
64-
</Or>
65-
</Match>
6649
<Match>
6750
<Bug pattern="EI_EXPOSE_REP" />
6851
<Or>

components/abstractions/src/test/java/com/microsoft/kiota/MultiPartBodyTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,25 @@
1212
import org.junit.jupiter.api.Test;
1313

1414
class MultiPartBodyTest {
15+
private static <T> T nullValue() {
16+
return java.util.Collections.<String, T>emptyMap().get("missing");
17+
}
18+
1519
@Test
1620
void defensive() {
1721
final MultipartBody multipartBody = new MultipartBody();
1822
assertThrows(
1923
IllegalArgumentException.class,
20-
() -> multipartBody.addOrReplacePart(null, "foo", "bar"));
24+
() -> multipartBody.addOrReplacePart(nullValue(), "foo", "bar"));
2125
assertThrows(
2226
IllegalArgumentException.class,
23-
() -> multipartBody.addOrReplacePart("foo", null, "bar"));
27+
() -> multipartBody.addOrReplacePart("foo", nullValue(), "bar"));
2428
assertThrows(
2529
NullPointerException.class,
26-
() -> multipartBody.addOrReplacePart("foo", "bar", null));
27-
assertThrows(IllegalArgumentException.class, () -> multipartBody.getPartValue(null));
28-
assertThrows(IllegalArgumentException.class, () -> multipartBody.removePart(null));
29-
assertThrows(NullPointerException.class, () -> multipartBody.serialize(null));
30+
() -> multipartBody.addOrReplacePart("foo", "bar", nullValue()));
31+
assertThrows(IllegalArgumentException.class, () -> multipartBody.getPartValue(nullValue()));
32+
assertThrows(IllegalArgumentException.class, () -> multipartBody.removePart(nullValue()));
33+
assertThrows(NullPointerException.class, () -> multipartBody.serialize(nullValue()));
3034
assertThrows(
3135
UnsupportedOperationException.class, () -> multipartBody.getFieldDeserializers());
3236
}

components/abstractions/src/test/java/com/microsoft/kiota/PeriodAndDurationTest.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,39 @@
1010
import java.time.temporal.ChronoUnit;
1111

1212
class PeriodAndDurationTest {
13+
private static <T> T nullValue() {
14+
return java.util.Collections.<String, T>emptyMap().get("missing");
15+
}
16+
1317
@Test
1418
void Defensive() {
1519
// Assert
1620
var exception =
17-
assertThrows(NullPointerException.class, () -> PeriodAndDuration.of(null, null));
21+
assertThrows(
22+
NullPointerException.class,
23+
() -> PeriodAndDuration.of(nullValue(), nullValue()));
1824
assertTrue(exception.getMessage().contains("period cannot be null"));
1925

2026
var exception2 =
2127
assertThrows(
2228
NullPointerException.class,
23-
() -> PeriodAndDuration.of(null, Duration.ZERO));
29+
() -> PeriodAndDuration.of(nullValue(), Duration.ZERO));
2430
assertTrue(exception2.getMessage().contains("period cannot be null"));
2531

2632
var exception3 =
2733
assertThrows(
28-
NullPointerException.class, () -> PeriodAndDuration.of(Period.ZERO, null));
34+
NullPointerException.class,
35+
() -> PeriodAndDuration.of(Period.ZERO, nullValue()));
2936
assertTrue(exception3.getMessage().contains("duration cannot be null"));
3037

3138
var exception4 =
32-
assertThrows(NullPointerException.class, () -> PeriodAndDuration.ofDuration(null));
39+
assertThrows(
40+
NullPointerException.class, () -> PeriodAndDuration.ofDuration(nullValue()));
3341
assertTrue(exception4.getMessage().contains("duration cannot be null"));
3442

3543
var exception5 =
36-
assertThrows(NullPointerException.class, () -> PeriodAndDuration.ofPeriod(null));
44+
assertThrows(
45+
NullPointerException.class, () -> PeriodAndDuration.ofPeriod(nullValue()));
3746
assertTrue(exception5.getMessage().contains("period cannot be null"));
3847

3948
final PeriodAndDuration periodAndDuration =

components/abstractions/src/test/java/com/microsoft/kiota/authentication/ApiKeyAuthenticationProviderTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
import java.util.HashSet;
1111

1212
public class ApiKeyAuthenticationProviderTest {
13+
private static <T> T nullValue() {
14+
return java.util.Collections.<String, T>emptyMap().get("missing");
15+
}
16+
1317
@Test
1418
void DefensivePrograming() {
1519
assertThrows(
@@ -23,7 +27,9 @@ void DefensivePrograming() {
2327

2428
var value =
2529
new ApiKeyAuthenticationProvider("key", "param", ApiKeyLocation.QUERY_PARAMETER);
26-
assertThrows(NullPointerException.class, () -> value.authenticateRequest(null, null));
30+
assertThrows(
31+
NullPointerException.class,
32+
() -> value.authenticateRequest(nullValue(), nullValue()));
2733
}
2834

2935
@Test

components/abstractions/src/test/java/com/microsoft/kiota/serialization/DeserializationHelpersTest.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,42 @@
1818
class DeserializationHelpersTest {
1919
private static final String _jsonContentType = "application/json";
2020
private static final String _charset = "utf-8";
21+
private static <T> T nullValue() {
22+
return java.util.Collections.<String, T>emptyMap().get("missing");
23+
}
24+
private static String nullString() {
25+
return nullValue();
26+
}
27+
private static InputStream nullInputStream() {
28+
return nullValue();
29+
}
30+
private static ParsableFactory<TestEntity> nullFactory() {
31+
return nullValue();
32+
}
2133

2234
@Test
2335
void defensive() {
2436
assertThrows(
2537
NullPointerException.class,
2638
() ->
2739
KiotaSerialization.deserialize(
28-
null,
29-
(InputStream) null,
40+
nullString(),
41+
nullInputStream(),
3042
TestEntity::createFromDiscriminatorValue));
3143
assertThrows(
3244
NullPointerException.class,
3345
() ->
3446
KiotaSerialization.deserialize(
3547
_jsonContentType,
36-
(InputStream) null,
48+
nullInputStream(),
3749
TestEntity::createFromDiscriminatorValue));
3850
assertThrows(
3951
NullPointerException.class,
4052
() ->
4153
KiotaSerialization.deserialize(
4254
_jsonContentType,
4355
new ByteArrayInputStream("{}".getBytes(_charset)),
44-
(ParsableFactory<TestEntity>) null));
56+
nullFactory()));
4557
}
4658

4759
@Test
@@ -50,23 +62,23 @@ void defensiveCollection() {
5062
NullPointerException.class,
5163
() ->
5264
KiotaSerialization.deserializeCollection(
53-
null,
54-
(InputStream) null,
65+
nullString(),
66+
nullInputStream(),
5567
TestEntity::createFromDiscriminatorValue));
5668
assertThrows(
5769
NullPointerException.class,
5870
() ->
5971
KiotaSerialization.deserializeCollection(
6072
_jsonContentType,
61-
(InputStream) null,
73+
nullInputStream(),
6274
TestEntity::createFromDiscriminatorValue));
6375
assertThrows(
6476
NullPointerException.class,
6577
() ->
6678
KiotaSerialization.deserializeCollection(
6779
_jsonContentType,
6880
new ByteArrayInputStream("{}".getBytes(_charset)),
69-
(ParsableFactory<TestEntity>) null));
81+
nullFactory()));
7082
}
7183

7284
@Test

components/abstractions/src/test/java/com/microsoft/kiota/serialization/SerializationHelpersTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,41 @@
2424
class SerializationHelpersTest {
2525
private static final String _jsonContentType = "application/json";
2626
private static final String _charset = "utf-8";
27+
private static <T> T nullValue() {
28+
return java.util.Collections.<String, T>emptyMap().get("missing");
29+
}
30+
private static String nullString() {
31+
return nullValue();
32+
}
33+
private static Parsable nullParsable() {
34+
return nullValue();
35+
}
36+
private static Iterable<Parsable> nullParsableCollection() {
37+
return nullValue();
38+
}
2739

2840
@Test
2941
void defensive() {
3042
assertThrows(
3143
NullPointerException.class,
32-
() -> KiotaSerialization.serializeAsStream(null, (Parsable) null));
44+
() -> KiotaSerialization.serializeAsStream(nullString(), nullParsable()));
3345
assertThrows(
3446
NullPointerException.class,
35-
() -> KiotaSerialization.serializeAsStream(_jsonContentType, (Parsable) null));
47+
() -> KiotaSerialization.serializeAsStream(_jsonContentType, nullParsable()));
3648
}
3749

3850
@Test
3951
void defensiveCollection() {
4052
assertThrows(
4153
NullPointerException.class,
42-
() -> KiotaSerialization.serializeAsStream(null, (Iterable<Parsable>) null));
54+
() ->
55+
KiotaSerialization.serializeAsStream(
56+
nullString(), nullParsableCollection()));
4357
assertThrows(
4458
NullPointerException.class,
4559
() ->
4660
KiotaSerialization.serializeAsStream(
47-
_jsonContentType, (Iterable<Parsable>) null));
61+
_jsonContentType, nullParsableCollection()));
4862
}
4963

5064
@Test

components/abstractions/src/test/java/com/microsoft/kiota/serialization/mocks/TestBackedModelEntity.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ public Map<String, Consumer<ParseNode>> getFieldDeserializers() {
104104
put(
105105
"workDuration",
106106
n -> {
107-
setWorkDuration(n.getPeriodAndDurationValue());
107+
final var value = n.getPeriodAndDurationValue();
108+
if (value != null) {
109+
setWorkDuration(value);
110+
}
108111
});
109112
put(
110113
"startWorkTime",

components/abstractions/src/test/java/com/microsoft/kiota/serialization/mocks/TestEntity.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ public Map<String, Consumer<ParseNode>> getFieldDeserializers() {
110110
put(
111111
"workDuration",
112112
(n) -> {
113-
setWorkDuration(n.getPeriodAndDurationValue());
113+
final var value = n.getPeriodAndDurationValue();
114+
if (value != null) {
115+
setWorkDuration(value);
116+
}
114117
});
115118
put(
116119
"startWorkTime",

components/bundle/spotBugsExcludeFilter.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,4 @@
33
xmlns="https://github.com/spotbugs/filter/3.0.0"
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">
6-
<Match>
7-
<Bug pattern="NP_NONNULL_PARAM_VIOLATION" />
8-
<Class name="com.microsoft.kiota.bundle.BundleTests" />
9-
</Match>
106
</FindBugsFilter>

components/bundle/src/test/java/com/microsoft/kiota/bundle/BundleTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
import org.junit.jupiter.api.Test;
1111

1212
class BundleTests {
13+
private static <T> T nullValue() {
14+
return java.util.Collections.<String, T>emptyMap().get("missing");
15+
}
16+
1317
@Test
1418
void throwsErrorNullAuthenticationProvider() throws Exception {
1519
var exception =
16-
assertThrows(NullPointerException.class, () -> new DefaultRequestAdapter(null));
20+
assertThrows(
21+
NullPointerException.class, () -> new DefaultRequestAdapter(nullValue()));
1722
assertEquals("parameter authenticationProvider cannot be null", exception.getMessage());
1823
}
1924

0 commit comments

Comments
 (0)