Skip to content

Commit 9dfbc90

Browse files
committed
add unit testing
1 parent 326d1e5 commit 9dfbc90

File tree

6 files changed

+145
-17
lines changed

6 files changed

+145
-17
lines changed

lightblue-ldap-integration-test/pom.xml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@
2020

2121
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2222

23-
<modelVersion>4.0.0</modelVersion>
24-
25-
<parent>
26-
<groupId>com.redhat.lightblue.ldap</groupId>
27-
<artifactId>lightblue-ldap-pom</artifactId>
28-
<version>0.0.1-SNAPSHOT</version>
29-
</parent>
30-
31-
<artifactId>lightblue-ldap-integration-test</artifactId>
32-
33-
<properties>
34-
<lightblue.mongo.version>1.3.0-SNAPSHOT</lightblue.mongo.version>
35-
</properties>
36-
37-
<dependencies>
23+
<modelVersion>4.0.0</modelVersion>
24+
25+
<parent>
26+
<groupId>com.redhat.lightblue.ldap</groupId>
27+
<artifactId>lightblue-ldap-pom</artifactId>
28+
<version>0.0.1-SNAPSHOT</version>
29+
</parent>
30+
31+
<artifactId>lightblue-ldap-integration-test</artifactId>
32+
33+
<properties>
34+
<lightblue.mongo.version>1.3.0-SNAPSHOT</lightblue.mongo.version>
35+
</properties>
36+
37+
<dependencies>
3838
<dependency>
3939
<groupId>com.redhat.lightblue.ldap</groupId>
4040
<artifactId>lightblue-ldap-config</artifactId>

lightblue-ldap-integration-test/src/test/resources/datasources.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"mongo" : {
1818
"type" : "com.redhat.lightblue.mongo.config.MongoConfiguration",
19-
"metadataBackendParser" : "com.redhat.lightblue.metadata.mongo.MongoBackendParser",
19+
"metadataBackendParser" : "com.redhat.lightblue.metadata.mongo.MongoBackendParser",
2020
"ssl" : false,
2121
"database" : "${mongo.database}",
2222
"servers" : [

lightblue-ldap-metadata/src/main/java/com/redhat/lightblue/metadata/ldap/parser/LdapDataStoreParser.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class LdapDataStoreParser<T> implements DataStoreParser<T> {
3737
private final static String BASEDN = "basedn";
3838
private final static String UNIQUE_FIELD = "uniqueattr";
3939

40-
public DataStore parse(String name, MetadataParser<T> p, T node) {
40+
@Override
41+
public LdapDataStore parse(String name, MetadataParser<T> p, T node) {
4142
if (!LdapConstant.BACKEND.equals(name)) {
4243
throw Error.get(MetadataConstants.ERR_ILL_FORMED_METADATA, name);
4344
}
@@ -50,6 +51,7 @@ public DataStore parse(String name, MetadataParser<T> p, T node) {
5051
return dataStore;
5152
}
5253

54+
@Override
5355
public void convert(MetadataParser<T> p, T emptyNode, DataStore store) {
5456
if(!(store instanceof LdapDataStore)){
5557
throw new IllegalArgumentException("DataStore of type " + store.getClass() + " is not supported.");
@@ -61,6 +63,7 @@ public void convert(MetadataParser<T> p, T emptyNode, DataStore store) {
6163
p.putString(emptyNode, UNIQUE_FIELD, ds.getUniqueField());
6264
}
6365

66+
@Override
6467
public String getDefaultName() {
6568
return LdapConstant.BACKEND;
6669
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Copyright 2015 Red Hat, Inc. and/or its affiliates.
3+
4+
This file is part of lightblue.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package com.redhat.lightblue.metadata.ldap.parser;
20+
21+
import static com.redhat.lightblue.util.JsonUtils.json;
22+
import static com.redhat.lightblue.util.test.AbstractJsonNodeTest.loadJsonNode;
23+
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertNotNull;
25+
26+
import java.io.IOException;
27+
28+
import org.json.JSONException;
29+
import org.junit.Rule;
30+
import org.junit.Test;
31+
import org.junit.rules.ExpectedException;
32+
import org.skyscreamer.jsonassert.JSONAssert;
33+
34+
import com.fasterxml.jackson.databind.JsonNode;
35+
import com.redhat.lightblue.common.ldap.LdapConstant;
36+
import com.redhat.lightblue.common.ldap.LdapDataStore;
37+
import com.redhat.lightblue.test.MetadataUtil;
38+
import com.redhat.lightblue.test.metadata.FakeDataStore;
39+
40+
public class LdapDataStoreParserTest {
41+
42+
private final static String DATABASE = "test";
43+
private final static String BASE_DN = "dc=example,dc=com";
44+
private final static String UNIQUE_ATTRIBUTE = "uid";
45+
46+
@Rule
47+
public ExpectedException expectedEx = ExpectedException.none();
48+
49+
public LdapDataStoreParserTest() {
50+
System.setProperty("ldap.database", DATABASE);
51+
System.setProperty("ldap.basedn", BASE_DN);
52+
System.setProperty("ldap.uniqueattr", UNIQUE_ATTRIBUTE);
53+
}
54+
55+
@Test
56+
public void testGetDefaultName(){
57+
assertEquals(LdapConstant.BACKEND, new LdapDataStoreParser<Object>().getDefaultName());
58+
}
59+
60+
@Test
61+
public void testParse() throws IOException{
62+
LdapDataStore store = new LdapDataStoreParser<JsonNode>().parse(
63+
LdapConstant.BACKEND,
64+
MetadataUtil.createJSONMetadataParser(LdapConstant.BACKEND, null),
65+
loadJsonNode("./entityinfo-segment-metadata.json").get("entityInfo").get("datastore"));
66+
67+
assertNotNull(store);
68+
assertEquals(DATABASE, store.getDatabase());
69+
assertEquals(BASE_DN, store.getBaseDN());
70+
assertEquals(UNIQUE_ATTRIBUTE, store.getUniqueField());
71+
}
72+
73+
@Test
74+
public void testParse_IncorrectBackend(){
75+
expectedEx.expect(com.redhat.lightblue.util.Error.class);
76+
expectedEx.expectMessage("{\"objectType\":\"error\",\"errorCode\":\"metadata:IllFormedMetadata\",\"msg\":\"fakebackend\"}");
77+
78+
new LdapDataStoreParser<JsonNode>().parse("fakebackend", null, null);
79+
}
80+
81+
@Test
82+
public void testConvert() throws IOException, JSONException{
83+
LdapDataStore store = new LdapDataStore();
84+
store.setDatabase(DATABASE);
85+
store.setBaseDN(BASE_DN);
86+
store.setUniqueField(UNIQUE_ATTRIBUTE);
87+
88+
JsonNode node = json("{}");
89+
90+
new LdapDataStoreParser<JsonNode>().convert(
91+
MetadataUtil.createJSONMetadataParser(LdapConstant.BACKEND, null),
92+
node,
93+
store);
94+
95+
JSONAssert.assertEquals("{\"database\":\"" + DATABASE + "\",\"basedn\":\"" + BASE_DN + "\",\"uniqueattr\":\"" + UNIQUE_ATTRIBUTE + "\"}",
96+
node.toString(), true);
97+
}
98+
99+
@Test(expected = IllegalArgumentException.class)
100+
public void testConvert_wrongStoreType(){
101+
new LdapDataStoreParser<JsonNode>().convert(null, null, new FakeDataStore("fake"));
102+
}
103+
104+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"entityInfo": {
3+
"name": "fakeEntity",
4+
"datastore": {
5+
"backend":"ldap",
6+
"database": "${ldap.database}",
7+
"basedn": "${ldap.basedn}",
8+
"uniqueattr": "${ldap.uniqueattr}"
9+
}
10+
}
11+
}

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@
114114
<groupId>org.mockito</groupId>
115115
<artifactId>mockito-core</artifactId>
116116
</dependency>
117+
<dependency>
118+
<groupId>com.redhat.lightblue</groupId>
119+
<artifactId>test</artifactId>
120+
</dependency>
117121
</dependencies>
118122

119123
<dependencyManagement>
@@ -199,6 +203,12 @@
199203
<version>${mockito.version}</version>
200204
<scope>test</scope>
201205
</dependency>
206+
<dependency>
207+
<groupId>com.redhat.lightblue</groupId>
208+
<artifactId>test</artifactId>
209+
<version>${lightblue-core.version}</version>
210+
<scope>test</scope>
211+
</dependency>
202212
</dependencies>
203213
</dependencyManagement>
204214

0 commit comments

Comments
 (0)