Skip to content

Commit 852dd59

Browse files
committed
refactor(driver) extract reading array into separate method and add specification
1 parent cbde3ff commit 852dd59

2 files changed

Lines changed: 103 additions & 7 deletions

File tree

driver/src/main/org/mongodb/command/MapReduceCommandResultCodec.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ public MapReduceCommandResultCodec(final PrimitiveCodecs primitiveCodecs, final
2121
@Override
2222
protected Object readValue(final BSONReader reader, final String fieldName) {
2323
if ("results".equals(fieldName)) {
24-
final List<T> list = new ArrayList<T>();
25-
reader.readStartArray();
26-
while (reader.readBSONType() != BSONType.END_OF_DOCUMENT) {
27-
list.add(decoder.decode(reader));
28-
}
29-
reader.readEndArray();
30-
return list;
24+
return readArray(reader);
3125
} else {
3226
return super.readValue(reader, fieldName);
3327
}
3428
}
29+
30+
private List<T> readArray(final BSONReader reader) {
31+
final List<T> list = new ArrayList<T>();
32+
reader.readStartArray();
33+
while (reader.readBSONType() != BSONType.END_OF_DOCUMENT) {
34+
list.add(decoder.decode(reader));
35+
}
36+
reader.readEndArray();
37+
return list;
38+
}
3539
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.mongodb.command
2+
3+
import org.mongodb.Document
4+
import org.mongodb.connection.ServerAddress
5+
import org.mongodb.operation.CommandResult
6+
import spock.lang.Specification
7+
import spock.lang.Subject
8+
9+
class MapReduceCommandResultSpecification extends Specification {
10+
11+
def setupSpec() {
12+
Map.metaClass.asType = { Class type ->
13+
if (type == Document) {
14+
return new Document(delegate)
15+
}
16+
}
17+
}
18+
19+
@Subject
20+
private MapReduceCommandResult<Document> commandResult
21+
22+
def 'should extract correct value from "results" field'() {
23+
when:
24+
commandResult = new MapReduceCommandResult<>(new CommandResult(
25+
[:] as Document,
26+
new ServerAddress(),
27+
['results': [new Document('a', 1), new Document('b', 2)]] as Document,
28+
0
29+
));
30+
31+
then:
32+
commandResult.isInline()
33+
commandResult.getValue() instanceof Iterable<Document>
34+
}
35+
36+
def 'should extract collectionName from result'() {
37+
when:
38+
commandResult = new MapReduceCommandResult<>(new CommandResult(
39+
[:] as Document,
40+
new ServerAddress(),
41+
['result': 'foo'] as Document,
42+
0
43+
));
44+
45+
then:
46+
commandResult.getTargetCollectionName() == 'foo'
47+
}
48+
49+
def 'should extract databaseName and collectionName from result'() {
50+
when:
51+
commandResult = new MapReduceCommandResult<>(new CommandResult(
52+
[:] as Document,
53+
new ServerAddress(),
54+
['result': ['collection': 'foo', 'db': 'bar'] as Document] as Document,
55+
0
56+
));
57+
58+
then:
59+
commandResult.getTargetCollectionName() == 'foo'
60+
commandResult.getTargetDatabaseName() == 'bar'
61+
}
62+
63+
def 'should return null if there is no database name in result'() {
64+
when:
65+
commandResult = new MapReduceCommandResult<>(new CommandResult(
66+
[:] as Document,
67+
new ServerAddress(),
68+
['result': ['collection': 'foo'] as Document] as Document,
69+
0
70+
));
71+
72+
then:
73+
commandResult.getTargetDatabaseName() == null
74+
}
75+
76+
def 'should throw on getting collectionName for result with inlined data'(){
77+
setup:
78+
commandResult = new MapReduceCommandResult<>(new CommandResult(
79+
[:] as Document,
80+
new ServerAddress(),
81+
['results': [new Document('a', 1), new Document('b', 2)]] as Document,
82+
0
83+
));
84+
85+
when:
86+
commandResult.getTargetCollectionName()
87+
88+
then:
89+
thrown(IllegalAccessError)
90+
}
91+
92+
}

0 commit comments

Comments
 (0)