Skip to content

Commit 8d77e54

Browse files
committed
Added unit test specification for CommandOperationHelper
1 parent 673f6ce commit 8d77e54

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2015 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.operation
18+
19+
import com.mongodb.MongoCommandException
20+
import com.mongodb.ServerAddress
21+
import org.bson.BsonBoolean
22+
import org.bson.BsonDocument
23+
import org.bson.BsonInt32
24+
import org.bson.BsonString
25+
import spock.lang.Specification
26+
27+
import static com.mongodb.operation.CommandOperationHelper.isNamespaceError
28+
import static com.mongodb.operation.CommandOperationHelper.rethrowIfNotNamespaceError
29+
30+
class CommandOperationHelperSpecification extends Specification {
31+
32+
def 'should be a namespace error if Throwable is a MongoCommandException and error code is 26'() {
33+
expect:
34+
isNamespaceError(new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE)
35+
.append('code', new BsonInt32(26)),
36+
new ServerAddress()))
37+
}
38+
39+
def 'should be a namespace error if Throwable is a MongoCommandException and error message contains "ns not found"'() {
40+
expect:
41+
isNamespaceError(new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE)
42+
.append('errmsg', new BsonString('the ns not found here')),
43+
new ServerAddress()))
44+
}
45+
46+
def 'should not be a namespace error if Throwable is a MongoCommandException and error message does not contain "ns not found"'() {
47+
expect:
48+
!isNamespaceError(new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE)
49+
.append('errmsg', new BsonString('some other error')),
50+
new ServerAddress()))
51+
}
52+
53+
def 'should not be a namespace error should return false if Throwable is not a MongoCommandException'() {
54+
expect:
55+
!isNamespaceError(new NullPointerException())
56+
}
57+
58+
def 'should rethrow if not namespace error'() {
59+
when:
60+
rethrowIfNotNamespaceError(new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE)
61+
.append('errmsg', new BsonString('some other error')),
62+
new ServerAddress()))
63+
64+
then:
65+
thrown(MongoCommandException)
66+
67+
when:
68+
rethrowIfNotNamespaceError(new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE)
69+
.append('errmsg', new BsonString('some other error')),
70+
new ServerAddress()), 'some value')
71+
72+
then:
73+
thrown(MongoCommandException)
74+
}
75+
76+
def 'should not rethrow if namespace error'() {
77+
when:
78+
rethrowIfNotNamespaceError(new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE)
79+
.append('code', new BsonInt32(26)),
80+
new ServerAddress()))
81+
82+
then:
83+
true
84+
}
85+
86+
def 'should return default value if not namespace error'() {
87+
expect:
88+
rethrowIfNotNamespaceError(new MongoCommandException(new BsonDocument('ok', BsonBoolean.FALSE)
89+
.append('code', new BsonInt32(26)),
90+
new ServerAddress()), 'some value') == 'some value'
91+
}
92+
}

0 commit comments

Comments
 (0)