-
Notifications
You must be signed in to change notification settings - Fork 215
/
DefaultOptionsConfigTest.java
113 lines (95 loc) · 4.83 KB
/
DefaultOptionsConfigTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.persistence.mongo.config;
import static org.mutabilitydetector.unittesting.AllowedReason.assumingFields;
import static org.mutabilitydetector.unittesting.AllowedReason.provided;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import org.assertj.core.api.JUnitSoftAssertions;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import com.mongodb.WriteConcern;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
/**
* Unit test for {@link DefaultOptionsConfig}.
*/
public final class DefaultOptionsConfigTest {
private static final String MONGODB_CONFIG_FILE_NAME = "mongodb_test.conf";
private Config rawMongoDbConfig;
@Rule
public final JUnitSoftAssertions softly = new JUnitSoftAssertions();
@Before
public void initMongoDbConfig() {
rawMongoDbConfig = ConfigFactory.parseResources(MONGODB_CONFIG_FILE_NAME).getConfig("mongodb");
}
@Test
public void assertImmutability() {
assertInstancesOf(DefaultOptionsConfig.class,
areImmutable(),
provided(ReadPreference.class, ReadConcern.class, WriteConcern.class).areAlsoImmutable(),
assumingFields("extraUriOptions")
.areSafelyCopiedUnmodifiableCollectionsWithImmutableElements());
}
@Test
public void testHashCodeAndEquals() {
EqualsVerifier.forClass(DefaultOptionsConfig.class)
.suppress(Warning.NULL_FIELDS)
.usingGetClass()
.verify();
}
@Test
public void toStringContainsExpected() {
final DefaultOptionsConfig underTest = DefaultOptionsConfig.of(rawMongoDbConfig);
softly.assertThat(underTest.toString()).contains(underTest.getClass().getSimpleName())
.contains("sslEnabled", "readPreference", "readConcern", "writeConcern",
"retryWrites");
}
@Test
public void defaultOptionsConfigContainsExactlyValuesOfResourceConfigFile() {
final DefaultOptionsConfig underTest = DefaultOptionsConfig.of(rawMongoDbConfig);
softly.assertThat(underTest.isSslEnabled()).isFalse();
softly.assertThat(underTest.readPreference()).isEqualTo(ReadPreference.SECONDARY_PREFERRED);
softly.assertThat(underTest.readConcern()).isEqualTo(ReadConcern.LINEARIZABLE);
softly.assertThat(underTest.writeConcern()).isEqualTo(WriteConcern.W3);
softly.assertThat(underTest.isRetryWrites()).isFalse();
}
@Test
public void defaultOptionsConfigContainsExactlyFallBackValuesIfEmptyResourceConfigFile() {
final DefaultOptionsConfig underTest = DefaultOptionsConfig.of(ConfigFactory.empty());
softly.assertThat(underTest.isSslEnabled())
.as(MongoDbConfig.OptionsConfig.OptionsConfigValue.SSL_ENABLED.getConfigPath())
.isEqualTo(MongoDbConfig.OptionsConfig.OptionsConfigValue.SSL_ENABLED.getDefaultValue());
softly.assertThat(underTest.readPreference())
.as(MongoDbConfig.OptionsConfig.OptionsConfigValue.READ_PREFERENCE.getConfigPath())
.isEqualTo(ReadPreference.ofReadPreference(
(String) MongoDbConfig.OptionsConfig.OptionsConfigValue.READ_PREFERENCE.getDefaultValue())
.orElseThrow());
softly.assertThat(underTest.readConcern())
.as(MongoDbConfig.OptionsConfig.OptionsConfigValue.READ_CONCERN.getConfigPath())
.isEqualTo(ReadConcern.ofReadConcern(
(String) MongoDbConfig.OptionsConfig.OptionsConfigValue.READ_CONCERN.getDefaultValue())
.orElseThrow());
softly.assertThat(underTest.writeConcern())
.as(MongoDbConfig.OptionsConfig.OptionsConfigValue.WRITE_CONCERN.getConfigPath())
.isEqualTo(WriteConcern.valueOf(
(String) MongoDbConfig.OptionsConfig.OptionsConfigValue.WRITE_CONCERN.getDefaultValue()));
softly.assertThat(underTest.isRetryWrites())
.as(MongoDbConfig.OptionsConfig.OptionsConfigValue.RETRY_WRITES.getConfigPath())
.isEqualTo(MongoDbConfig.OptionsConfig.OptionsConfigValue.RETRY_WRITES.getDefaultValue());
}
}