-
Notifications
You must be signed in to change notification settings - Fork 199
/
DocumentStoreConfigurationTest.java
194 lines (151 loc) · 6.59 KB
/
DocumentStoreConfigurationTest.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Hibernate OGM, Domain model persistence for NoSQL datastores
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.ogm.test.cfg;
import static org.fest.assertions.Assertions.assertThat;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.ogm.cfg.OgmProperties;
import org.hibernate.ogm.cfg.spi.DocumentStoreConfiguration;
import org.hibernate.ogm.cfg.spi.Hosts;
import org.hibernate.ogm.util.configurationreader.spi.ConfigurationPropertyReader;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
* Unit test for {@link DocumentStoreConfiguration}.
*
* @author Andrea Boriero <dreborier@gmail.com>
* @author Gunnar Morling
*/
public class DocumentStoreConfigurationTest {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private Map<String, String> configurationValues;
private DocumentStoreConfiguration configuration;
@Before
public void setUp() {
configurationValues = new HashMap<String, String>();
configurationValues.put( OgmProperties.DATABASE, "foobar" );
}
@Test
public void shouldReturnTheDefaultValueIfThePortKeyIsNotPresentAsAConfigurationValue() {
configuration = new TestDocumentStoreConfiguration( configurationValues );
Hosts hosts = configuration.getHosts();
assertThat( hosts.getFirst().getPort() ).isEqualTo( 1234 );
}
@Test
public void shouldReturnTheDefaultValueIfThePortConfigurationValueIsTheEmptyString() {
configurationValues.put( OgmProperties.PORT, "" );
configuration = new TestDocumentStoreConfiguration( configurationValues );
Hosts hosts = configuration.getHosts();
assertThat( hosts.getFirst().getPort() ).isEqualTo( 1234 );
}
@Test
public void shouldReturnThePortConfigured() {
final int configuredPortValue = 8080;
configurationValues.put( OgmProperties.PORT, String.valueOf( configuredPortValue ) );
configuration = new TestDocumentStoreConfiguration( configurationValues );
Hosts hosts = configuration.getHosts();
assertThat( hosts.getFirst().getPort() ).isEqualTo( configuredPortValue );
}
@Test
public void shouldReturnLocalhostIfTheHostNotPresentAsAConfigurationValue() {
configuration = new TestDocumentStoreConfiguration( configurationValues );
Hosts hosts = configuration.getHosts();
assertThat( hosts.getFirst().getHost() ).isEqualTo( "localhost" );
}
@Test
public void shouldReturnLocalhostIfTheHostConfigurationValueIsTheEmptyString() {
configurationValues.put( OgmProperties.HOST, " " );
configuration = new TestDocumentStoreConfiguration( configurationValues );
Hosts hosts = configuration.getHosts();
assertThat( hosts.getFirst().getHost() ).isEqualTo( "localhost" );
}
@Test
public void shouldReturnTheHostConfiguredValue() {
final String configuredHostValue = "192.168.2.2";
configurationValues.put( OgmProperties.HOST, configuredHostValue );
configuration = new TestDocumentStoreConfiguration( configurationValues );
Hosts hosts = configuration.getHosts();
assertThat( hosts.getFirst().getHost() ).isEqualTo( configuredHostValue );
}
@Test
public void shouldRaiseAnExceptionIfTheDatabaseNameConfigurationValueIsTheEmptyString() {
thrown.expect( HibernateException.class );
thrown.expectMessage( "OGM000052" );
configurationValues.put( OgmProperties.DATABASE, "" );
new TestDocumentStoreConfiguration( configurationValues );
}
@Test
public void shouldReturnTheDatabaseNameConfiguredValue() {
final String configuredDatabaseName = "test";
configurationValues.put( OgmProperties.DATABASE, configuredDatabaseName );
configuration = new TestDocumentStoreConfiguration( configurationValues );
assertThat( configuration.getDatabaseName() ).isEqualTo( configuredDatabaseName );
}
@Test
public void shouldReturnNullIfTheUsernameIsNotPresentAsAConfigurationValue() {
configuration = new TestDocumentStoreConfiguration( configurationValues );
assertThat( configuration.getUsername() ).isNull();
}
@Test
public void shouldReturnNullIfTheUsernameConfigurationValueIsTheEmptyString() {
configurationValues.put( OgmProperties.USERNAME, "" );
configuration = new TestDocumentStoreConfiguration( configurationValues );
assertThat( configuration.getUsername() ).isNull();
}
@Test
public void shouldReturnTheUsernameConfiguredValue() {
final String configuredUsername = "andrea";
configurationValues.put( OgmProperties.USERNAME, configuredUsername );
configuration = new TestDocumentStoreConfiguration( configurationValues );
assertThat( configuration.getUsername() ).isEqualTo( configuredUsername );
}
@Test
public void shouldReturnNullIfThePasswordIsNotPresentAsAConfigurationValue() {
configuration = new TestDocumentStoreConfiguration( configurationValues );
assertThat( configuration.getPassword() ).isNull();
}
@Test
public void shouldReturnNullIfThePasswordConfigurationValueIsTheEmptyString() {
configurationValues.put( OgmProperties.PASSWORD, "" );
configuration = new TestDocumentStoreConfiguration( configurationValues );
assertThat( configuration.getPassword() ).isNull();
}
@Test
public void shouldReturnThePasswordConfiguredValue() {
final String configuredPassword = "pwd";
configurationValues.put( OgmProperties.PASSWORD, configuredPassword );
configuration = new TestDocumentStoreConfiguration( configurationValues );
assertThat( configuration.getPassword() ).isEqualTo( configuredPassword );
}
@Test
public void shouldReturnFalseIfTheDatabaseCreationValueIsNotPresentAsAConfigurationValue() {
configuration = new TestDocumentStoreConfiguration( configurationValues );
assertThat( configuration.isCreateDatabase() ).isFalse();
}
@Test
public void shouldReturnFalseIfTheDatabaseCreationValueIsTheEmptyString() {
configurationValues.put( OgmProperties.CREATE_DATABASE, "" );
configuration = new TestDocumentStoreConfiguration( configurationValues );
assertThat( configuration.isCreateDatabase() ).isFalse();
}
@Test
public void shouldReturnTheDatabaseCreationConfiguredValue() {
final String configuredValue = "true";
configurationValues.put( OgmProperties.CREATE_DATABASE, configuredValue );
configuration = new TestDocumentStoreConfiguration( configurationValues );
assertThat( configuration.isCreateDatabase() ).isTrue();
}
private static class TestDocumentStoreConfiguration extends DocumentStoreConfiguration {
public TestDocumentStoreConfiguration(Map<?, ?> configurationValues) {
super( new ConfigurationPropertyReader( configurationValues ), 1234 );
}
}
}