Skip to content

Commit 1ea1ac6

Browse files
authored
improve test coverage of plugins load() methods (#4448)
1 parent c05ca2a commit 1ea1ac6

File tree

5 files changed

+105
-8
lines changed

5 files changed

+105
-8
lines changed

plugins/src/main/java/opengrok/auth/plugin/LdapFilterPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class LdapFilterPlugin extends AbstractLdapPlugin {
5656
protected static final String FILTER_PARAM = "filter";
5757
protected static final String TRANSFORMS_PARAM = "transforms";
5858
private static final String SESSION_ALLOWED_PREFIX = "opengrok-filter-plugin-allowed";
59-
private static final String INSTANCE = "instance";
59+
static final String INSTANCE = "instance";
6060
private String sessionAllowed = SESSION_ALLOWED_PREFIX;
6161

6262
/**

plugins/src/main/java/opengrok/auth/plugin/UserPlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package opengrok.auth.plugin;
2424

@@ -44,7 +44,7 @@ public class UserPlugin implements IAuthorizationPlugin {
4444

4545
private static final Logger LOGGER = Logger.getLogger(UserPlugin.class.getName());
4646

47-
private static final String DECODER_CLASS_PARAM = "decoder";
47+
static final String DECODER_CLASS_PARAM = "decoder";
4848

4949
public static final String REQUEST_ATTR = "opengrok-user-plugin-user";
5050

plugins/src/test/java/opengrok/auth/plugin/LdapAttrPluginTest.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package opengrok.auth.plugin;
2424

@@ -32,6 +32,7 @@
3232
import java.util.Collections;
3333
import java.util.HashMap;
3434
import java.util.Map;
35+
import java.util.Objects;
3536
import java.util.Set;
3637
import java.util.TreeMap;
3738
import java.util.TreeSet;
@@ -51,8 +52,11 @@
5152
import org.opengrok.indexer.configuration.Group;
5253
import org.opengrok.indexer.configuration.Project;
5354

55+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
5456
import static org.junit.jupiter.api.Assertions.assertFalse;
5557
import static org.junit.jupiter.api.Assertions.assertNotNull;
58+
import static org.junit.jupiter.api.Assertions.assertNull;
59+
import static org.junit.jupiter.api.Assertions.assertThrows;
5660
import static org.junit.jupiter.api.Assertions.assertTrue;
5761
import static org.mockito.ArgumentMatchers.any;
5862
import static org.mockito.ArgumentMatchers.anyString;
@@ -199,4 +203,44 @@ void testAttrLookup() throws LdapException {
199203
assertTrue((Boolean) request.getSession().getAttribute(plugin.getSessionAllowedAttrName()));
200204
assertTrue(ldapUser.getAttribute(attr_to_get).contains(mail_attr_value));
201205
}
206+
207+
private Map<String, Object> getParamsMap() {
208+
Map<String, Object> params = new TreeMap<>();
209+
params.put(AbstractLdapPlugin.CONFIGURATION_PARAM,
210+
Objects.requireNonNull(getClass().getResource("config.xml")).getFile());
211+
212+
return params;
213+
}
214+
215+
@Test
216+
void loadTestNegativeNoAttrParam() {
217+
Map<String, Object> params = getParamsMap();
218+
assertNull(params.get(LdapAttrPlugin.ATTR_PARAM));
219+
assertThrows(NullPointerException.class, () -> plugin.load(params));
220+
}
221+
222+
@Test
223+
void loadTestNegativeNoFileParam() {
224+
Map<String, Object> params = getParamsMap();
225+
params.put(LdapAttrPlugin.ATTR_PARAM, "mail");
226+
assertNull(params.get(LdapAttrPlugin.FILE_PARAM));
227+
assertThrows(NullPointerException.class, () -> plugin.load(params));
228+
}
229+
230+
@Test
231+
void loadTestNegativeInvalidFileParam() {
232+
Map<String, Object> params = getParamsMap();
233+
params.put(LdapAttrPlugin.ATTR_PARAM, "mail");
234+
params.put(LdapAttrPlugin.FILE_PARAM, "nonexistent");
235+
assertThrows(IllegalArgumentException.class, () -> plugin.load(params));
236+
}
237+
238+
@Test
239+
void loadTestPositive() {
240+
Map<String, Object> params = getParamsMap();
241+
params.put(LdapAttrPlugin.FILE_PARAM, whitelistFile.getAbsolutePath());
242+
params.put(LdapAttrPlugin.ATTR_PARAM, "mail");
243+
params.put(LdapAttrPlugin.INSTANCE_PARAM, "42");
244+
assertDoesNotThrow(() -> plugin.load(params));
245+
}
202246
}

plugins/src/test/java/opengrok/auth/plugin/LdapFilterPluginTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package opengrok.auth.plugin;
2424

2525
import java.util.Arrays;
2626
import java.util.Collections;
27+
import java.util.Map;
28+
import java.util.Objects;
29+
import java.util.TreeMap;
2730
import java.util.TreeSet;
2831
import opengrok.auth.entity.LdapUser;
2932
import opengrok.auth.plugin.entity.User;
@@ -32,6 +35,7 @@
3235

3336
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3437
import static org.junit.jupiter.api.Assertions.assertEquals;
38+
import static org.junit.jupiter.api.Assertions.assertNull;
3539
import static org.junit.jupiter.api.Assertions.assertThrows;
3640

3741
class LdapFilterPluginTest {
@@ -113,4 +117,27 @@ void testLoadTransformsNegative() {
113117
plugin.loadTransforms("foo:toUpperCase,ugly:nice")
114118
);
115119
}
120+
121+
private Map<String, Object> getParamsMap() {
122+
Map<String, Object> params = new TreeMap<>();
123+
params.put(AbstractLdapPlugin.CONFIGURATION_PARAM,
124+
Objects.requireNonNull(getClass().getResource("config.xml")).getFile());
125+
126+
return params;
127+
}
128+
129+
@Test
130+
void loadTestNegativeNoFilterParam() {
131+
Map<String, Object> params = getParamsMap();
132+
assertNull(params.get(LdapFilterPlugin.FILTER_PARAM));
133+
assertThrows(NullPointerException.class, () -> plugin.load(params));
134+
}
135+
136+
@Test
137+
void loadTestPositive() {
138+
Map<String, Object> params = getParamsMap();
139+
params.put(LdapFilterPlugin.FILTER_PARAM, "foo:toUpperCase");
140+
params.put(LdapFilterPlugin.INSTANCE, "42");
141+
assertDoesNotThrow(() -> plugin.load(params));
142+
}
116143
}

plugins/src/test/java/opengrok/auth/plugin/UserPluginTest.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package opengrok.auth.plugin;
2424

@@ -31,9 +31,14 @@
3131
import org.opengrok.indexer.configuration.Group;
3232
import org.opengrok.indexer.configuration.Project;
3333

34+
import java.util.Map;
35+
import java.util.TreeMap;
36+
37+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3438
import static org.junit.jupiter.api.Assertions.assertEquals;
3539
import static org.junit.jupiter.api.Assertions.assertFalse;
3640
import static org.junit.jupiter.api.Assertions.assertNull;
41+
import static org.junit.jupiter.api.Assertions.assertThrows;
3742
import static org.junit.jupiter.api.Assertions.assertTrue;
3843

3944
/**
@@ -97,15 +102,36 @@ HttpServletRequest createRequest(String email, Boolean timeout) {
97102
};
98103
}
99104

100-
Group createGroup(String name) {
105+
private Group createGroup(String name) {
101106
Group g = new Group();
102107
g.setName(name);
103108
return g;
104109
}
105110

106-
Project createProject(String name) {
111+
private Project createProject(String name) {
107112
Project g = new Project();
108113
g.setName(name);
109114
return g;
110115
}
116+
117+
@Test
118+
void loadTestNegativeNoDecoderParam() {
119+
Map<String, Object> params = new TreeMap<>();
120+
params.put("foo", "bar");
121+
assertThrows(NullPointerException.class, () -> plugin.load(params));
122+
}
123+
124+
@Test
125+
void loadTestNegativeInvalidDecoder() {
126+
Map<String, Object> params = new TreeMap<>();
127+
params.put(UserPlugin.DECODER_CLASS_PARAM, "foo");
128+
assertThrows(RuntimeException.class, () -> plugin.load(params));
129+
}
130+
131+
@Test
132+
void loadTestPositive() {
133+
Map<String, Object> params = new TreeMap<>();
134+
params.put(UserPlugin.DECODER_CLASS_PARAM, "opengrok.auth.plugin.decoders.MellonHeaderDecoder");
135+
assertDoesNotThrow(() -> plugin.load(params));
136+
}
111137
}

0 commit comments

Comments
 (0)