Skip to content

Commit

Permalink
test: update test in the microprofile settings
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
  • Loading branch information
otaviojava committed Oct 1, 2023
1 parent a40fe51 commit 05a7673
Showing 1 changed file with 33 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package org.eclipse.jnosql.mapping.config;

import org.eclipse.jnosql.communication.Settings;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -33,18 +35,28 @@
import static org.junit.jupiter.api.Assertions.*;

class MicroProfileSettingsTest {
@BeforeEach
public void beforeEach() {
System.clearProperty("jnosql.jnosql.key");
System.clearProperty("jnosql.jnosql.host");
System.clearProperty("jnosql.host.1");
System.clearProperty("jnosql.host.2");
System.clearProperty("jnosql.host.3");
System.clearProperty("jnosql.server");
System.clearProperty("jnosql.server.1");
System.clearProperty("jnosql.server.2");
@AfterAll
public static void afterAll() {

}

@BeforeAll
public static void beforeAll() {
System.setProperty("jnosql.jnosql.key", "value");
System.setProperty("jnosql.jnosql.host", "host");
System.setProperty("key.jnosql", "value");
System.setProperty("jnosql.key", "value");
System.setProperty("jnosql.key-number", "12");
System.setProperty("jnosql.host", "host");
System.setProperty("jnosql.host.1", "host-1");
System.setProperty("jnosql.host.2", "host-2");
System.setProperty("jnosql.host.3", "host-3");
System.setProperty("jnosql.server", "server");
System.setProperty("jnosql.server.1", "server-1");
System.setProperty("jnosql.server.2", "server-2");
}


@Test
public void shouldReturnNPEWhenInstanceIsNull() {
assertThrows(NullPointerException.class, () -> Settings.of((Map<String, Object>) null));
Expand All @@ -65,7 +77,6 @@ public void shouldCreateFromMap() {

@Test
public void shouldContainsKeys() {
setProperty(singletonMap("jnosql.key", "value"));
Settings settings = MicroProfileSettings.INSTANCE;
assertTrue(settings.containsKey("jnosql.key"));
assertFalse(settings.containsKey("key2"));
Expand All @@ -74,7 +85,6 @@ public void shouldContainsKeys() {

@Test
public void shouldGetKeys() {
setProperty(singletonMap("key.jnosql", "value"));
Settings settings = MicroProfileSettings.INSTANCE;
assertThat(settings.keySet()).contains("jnosql.key");
}
Expand All @@ -95,114 +105,92 @@ public void shouldIsEmpty() {

@Test
public void shouldGet() {
setProperty(singletonMap("jnosql.key", "12"));
Settings settings = MicroProfileSettings.INSTANCE;
Optional<Object> value = settings.get("jnosql.key");
Optional<Object> value = settings.get("jnosql.key-number");
Assertions.assertNotNull(value);
Assertions.assertEquals("12", value.get());
}

@Test
public void shouldGetSupplier() {
setProperty(singletonMap("jnosql.key", "12"));
Settings settings = MicroProfileSettings.INSTANCE;
Optional<Object> value = settings.get(() -> "jnosql.key");
Optional<Object> value = settings.get(() -> "jnosql.key-number");
Assertions.assertNotNull(value);
Assertions.assertEquals("12", value.get());
}

@Test
public void shouldNPEGet() {
setProperty(singletonMap("jnosql.key", "12"));
Settings settings = MicroProfileSettings.INSTANCE;
Assertions.assertThrows(NullPointerException.class, () -> settings.get((String) null));
Assertions.assertThrows(NullPointerException.class, () -> settings.get((Supplier<String>) null));
}

@Test
public void shouldGetIterable() {
setProperty(singletonMap("jnosql.key", "12"));
Settings settings = MicroProfileSettings.INSTANCE;
Optional<Object> value = settings.get(Collections.singleton("jnosql.key"));
Optional<Object> value = settings.get(Collections.singleton("jnosql.key-number"));
Assertions.assertNotNull(value);
Assertions.assertEquals("12", value.get());
}

@Test
public void shouldGetIterableSupplier() {
setProperty(singletonMap("jnosql.key", "12"));
Settings settings = MicroProfileSettings.INSTANCE;
Optional<Object> value = settings.getSupplier(Collections.singleton(() -> "jnosql.key"));
Optional<Object> value = settings.getSupplier(Collections.singleton(() -> "jnosql.key-number"));
Assertions.assertNotNull(value);
Assertions.assertEquals("12", value.get());
}

@Test
public void shouldNPEGetIterable() {
setProperty(singletonMap("jnosql.key", "12"));
Settings settings = MicroProfileSettings.INSTANCE;
Assertions.assertThrows(NullPointerException.class, () -> settings.get((Iterable<String>) null));
Assertions.assertThrows(NullPointerException.class, () -> settings.getSupplier(null));
}

@Test
public void shouldGetValueClass() {
setProperty(singletonMap("jnosql.key", "12"));
Settings settings = MicroProfileSettings.INSTANCE;

Integer value = settings.get("jnosql.key", Integer.class).get();
Integer value = settings.get("jnosql.key-number", Integer.class).get();
assertEquals(Integer.valueOf(12), value);
assertFalse(settings.get("jnosql.key2", Integer.class).isPresent());
}

@Test
public void shouldGetValueClassSupplier() {
setProperty(singletonMap("jnosql.key", "12"));
Settings settings = MicroProfileSettings.INSTANCE;

Integer value = settings.get(() -> "jnosql.key", Integer.class).get();
Integer value = settings.get(() -> "jnosql.key-number", Integer.class).get();
assertEquals(Integer.valueOf(12), value);
assertFalse(settings.get(() -> "key2", Integer.class).isPresent());
}

@Test
public void shouldGetOrDefault() {
setProperty(singletonMap("jnosql.key", "12"));
Settings settings = MicroProfileSettings.INSTANCE;
assertEquals("12", settings.getOrDefault("jnosql.key", "13"));
assertEquals("12", settings.getOrDefault("jnosql.key-number", "13"));
assertEquals("13", settings.getOrDefault("key-1", "13"));
}

@Test
public void shouldGetOrDefaultSupplier() {
setProperty(singletonMap("jnosql.key", "12"));
Settings settings = MicroProfileSettings.INSTANCE;
assertEquals("12", settings.getOrDefault(() -> "jnosql.key", "13"));
assertEquals("12", settings.getOrDefault(() -> "jnosql.key-number", "13"));
assertEquals("13", settings.getOrDefault(() -> "key-1", "13"));
}

@Test
public void shouldReturnErrorWhenPrefixIsNull() {

Map<String, Object> params = Map.of(
"jnosql.host", "host",
"jnosql.host.1", "host-1",
"jnosql.host.2", "host-2",
"jnosql.host.3", "host-3");
setProperty(params);
Settings settings = MicroProfileSettings.INSTANCE;

assertThrows(NullPointerException.class, () -> settings.prefix((String) null));
}

@Test
public void shouldFindPrefix() {
Map<String, Object> params = Map.of(
"jnosql.host", "host",
"jnosql.host.1", "host-1",
"jnosql.host.2", "host-2",
"jnosql.host.3", "host-3");
setProperty(params);
Settings settings = MicroProfileSettings.INSTANCE;

List<Object> hosts = settings.prefix("jnosql.host");
Expand All @@ -213,12 +201,6 @@ public void shouldFindPrefix() {

@Test
public void shouldFindPrefixSupplier() {
Map<String, Object> params = Map.of(
"jnosql.host", "host",
"jnosql.host.1", "host-1",
"jnosql.host.2", "host-2",
"jnosql.host.3", "host-3");
setProperty(params);
Settings settings = MicroProfileSettings.INSTANCE;

List<Object> hosts = settings.prefix(() -> "jnosql.host");
Expand All @@ -229,12 +211,6 @@ public void shouldFindPrefixSupplier() {

@Test
public void shouldFindPrefixWithOrder() {
Map<String, Object> params = Map.of(
"jnosql.host", "host",
"jnosql.host.1", "host-1",
"jnosql.host.2", "host-2",
"jnosql.host.3", "host-3");
setProperty(params);
Settings settings = MicroProfileSettings.INSTANCE;
List<Object> hosts = settings.prefix("jnosql.host");
assertThat(hosts).hasSize(4).contains("host", "host-1", "host-2", "host-3");
Expand All @@ -243,12 +219,6 @@ public void shouldFindPrefixWithOrder() {

@Test
public void shouldReturnErrorWhenPrefixesIsNull() {
Map<String, Object> params = Map.of(
"jnosql.host", "host",
"jnosql.host.1", "host-1",
"jnosql.host.2", "host-2",
"jnosql.host.3", "host-3");
setProperty(params);
Settings settings = MicroProfileSettings.INSTANCE;
assertThrows(NullPointerException.class, () -> settings.prefix((Collection<String>) null));

Expand All @@ -257,50 +227,29 @@ public void shouldReturnErrorWhenPrefixesIsNull() {
@Test
public void shouldFindPrefixes() {

Map<String, Object> params = Map.of(
"jnosql.host", "host",
"jnosql.host.1", "host-1",
"jnosql.server", "server",
"jnosql.server.1", "server-1");
setProperty(params);
Settings settings = MicroProfileSettings.INSTANCE;

List<Object> hosts = settings.prefix(Arrays.asList("jnosql.host", "jnosql.server"));
assertThat(hosts).hasSize(4).contains("host", "host-1", "server", "server-1");
assertThat(hosts).hasSize(7).contains("host", "host-1", "server", "server-1");
}

@Test
public void shouldFindPrefixesSupplier() {
Map<String, Object> params = Map.of(
"jnosql.host", "host",
"jnosql.host.1", "host-1",
"jnosql.server", "server",
"jnosql.server.1", "server-1");
setProperty(params);

Settings settings = MicroProfileSettings.INSTANCE;
List<Object> hosts = settings.prefixSupplier(Arrays.asList(() -> "jnosql.host", () -> "jnosql.server"));
assertThat(hosts).hasSize(4).contains("host", "host-1", "server", "server-1");
assertThat(hosts).hasSize(7).contains("host", "host-1", "server", "server-1");
}

@Test
public void shouldFindPrefixesSort() {
Map<String, Object> params = Map.of(
"jnosql.host", "host",
"jnosql.host.1", "host-1",
"jnosql.server", "server",
"jnosql.server.1", "server-1");
setProperty(params);

Settings settings = MicroProfileSettings.INSTANCE;

List<Object> hosts = settings.prefix(Arrays.asList("jnosql.host", "jnosql.server"));
assertThat(hosts).hasSize(4).contains("host", "host-1", "server", "server-1");
assertThat(hosts).hasSize(7).contains("host", "host-1", "server", "server-1");
}



private void setProperty(Map<String, Object> params) {
params.forEach((k, v) -> System.setProperty(k, v.toString()));
}
}

0 comments on commit 05a7673

Please sign in to comment.