Skip to content

Commit

Permalink
moves the code to key-value
Browse files Browse the repository at this point in the history
  • Loading branch information
otaviojava committed Oct 1, 2019
1 parent 1c2cea6 commit 177cafe
Show file tree
Hide file tree
Showing 12 changed files with 615 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2019 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/

package org.eclipse.jnosql.artemis.keyvalue.configuration;

import jakarta.nosql.keyvalue.BucketManager;
import jakarta.nosql.keyvalue.BucketManagerFactory;
import jakarta.nosql.keyvalue.KeyValueConfiguration;
import org.eclipse.jnosql.artemis.util.BeanManagers;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.Converter;

/**
* Converter the {@link String} to {@link BucketManager} it will use the {@link org.eclipse.jnosql.artemis.configuration.SettingsConverter} and
* find by the provider that should be an implementation of {@link KeyValueConfiguration}
*/
public class BucketManagerConverter implements Converter<BucketManager> {

@Override
public BucketManager convert(String value) {

Config config = BeanManagers.getInstance(Config.class);
final BucketManagerFactory managerFactory = config.getValue(value, BucketManagerFactory.class);
final String database = value + ".database";
final String entity = config.getValue(database, String.class);
return managerFactory.getBucketManager(entity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2019 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.artemis.keyvalue.configuration;

import jakarta.nosql.Settings;
import jakarta.nosql.keyvalue.BucketManagerFactory;
import jakarta.nosql.keyvalue.KeyValueConfiguration;
import jakarta.nosql.mapping.reflection.Reflections;
import org.eclipse.jnosql.artemis.configuration.ConfigurationException;
import org.eclipse.jnosql.artemis.configuration.SettingsConverter;
import org.eclipse.jnosql.artemis.util.BeanManagers;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.Converter;

/**
* Converter the {@link String} to {@link BucketManagerFactory} it will
* use the {@link SettingsConverter} and
* find by the provider that should be an implementation of {@link KeyValueConfiguration}
*/
public class BucketManagerFactoryConverter implements Converter<BucketManagerFactory> {

@Override
public BucketManagerFactory convert(String value) {
final SettingsConverter settingsConverter = BeanManagers.getInstance(SettingsConverter.class);
Config config = BeanManagers.getInstance(Config.class);
final Settings settings = settingsConverter.convert(value);
String provider = value + ".provider";
final Class<?> configurationClass = config.getValue(provider, Class.class);
if (KeyValueConfiguration.class.isAssignableFrom(configurationClass)) {
final Reflections reflections = BeanManagers.getInstance(Reflections.class);
final KeyValueConfiguration configuration = (KeyValueConfiguration) reflections.newInstance(configurationClass);
return configuration.get(settings);

}
throw new ConfigurationException("The class " + configurationClass + " is not valid to " + KeyValueConfiguration.class);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2019 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.artemis.keyvalue.configuration;

import jakarta.nosql.keyvalue.BucketManager;
import jakarta.nosql.keyvalue.KeyValueConfiguration;
import jakarta.nosql.mapping.keyvalue.KeyValueTemplate;
import jakarta.nosql.mapping.keyvalue.KeyValueTemplateProducer;
import org.eclipse.jnosql.artemis.util.BeanManagers;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.Converter;

/**
* Converter the {@link String} to {@link KeyValueTemplate} it will use the
* {@link org.eclipse.jnosql.artemis.configuration.SettingsConverter} and
* find by the provider that should be an implementation of {@link KeyValueConfiguration}
*/
public class KeyValueTemplateConverter implements Converter<KeyValueTemplate> {

@Override
public KeyValueTemplate convert(String value) {
Config config = BeanManagers.getInstance(Config.class);
final BucketManager manager = config.getValue(value, BucketManager.class);
KeyValueTemplateProducer producer = BeanManagers.getInstance(KeyValueTemplateProducer.class);

return producer.get(manager);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2019 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.artemis.configuration;

import jakarta.nosql.keyvalue.BucketManager;
import org.eclipse.jnosql.artemis.CDIExtension;
import org.eclipse.jnosql.artemis.configuration.KeyValueConfigurationMock.BucketManagerMock;
import org.eclipse.microprofile.config.Config;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import javax.inject.Inject;
import java.util.NoSuchElementException;
import java.util.UUID;

@ExtendWith(CDIExtension.class)
class BucketManagerConverterTest {

@Inject
private Config config;

@Test
public void shouldReturnErrorWhenThereIsNoProvider() {
final String prefix = UUID.randomUUID().toString();
System.setProperty(prefix, prefix);
System.setProperty(prefix + ".settings.key", "value");
System.setProperty(prefix + ".settings.key2", "value2");
Assertions.assertThrows(NoSuchElementException.class, () -> config.getValue(prefix, BucketManager.class) );

System.clearProperty(prefix);
System.clearProperty(prefix + ".settings.key");
System.clearProperty(prefix + ".settings.key2");
}

@Test
public void shouldReturnErrorWhenThereIsInvalidProvider() {
final String prefix = UUID.randomUUID().toString();
System.setProperty(prefix, prefix);
System.setProperty(prefix + ".settings.key", "value");
System.setProperty(prefix + ".settings.key2", "value2");
System.setProperty(prefix + ".provider", "java.lang.String");
Assertions.assertThrows(ConfigurationException.class, () -> config.getValue(prefix, BucketManager.class) );

System.clearProperty(prefix);
System.clearProperty(prefix + ".settings.key");
System.clearProperty(prefix + ".settings.key2");
System.clearProperty(prefix + ".provider");
}

@Test
public void shouldReturnErrorWhenTableIsMissing() {
final String prefix = UUID.randomUUID().toString();
System.setProperty(prefix, prefix);
System.setProperty(prefix + ".settings.key", "value");
System.setProperty(prefix + ".settings.key2", "value2");
System.setProperty(prefix + ".provider", KeyValueConfigurationMock.class.getName());
Assertions.assertThrows(NoSuchElementException.class, () -> config.getValue(prefix, BucketManager.class) );

System.clearProperty(prefix);
System.clearProperty(prefix + ".settings.key");
System.clearProperty(prefix + ".settings.key2");
System.clearProperty(prefix + ".provider");
}

@Test
public void shouldReturnBucket() {
final String prefix = UUID.randomUUID().toString();
System.setProperty(prefix, prefix);
System.setProperty(prefix + ".settings.key", "value");
System.setProperty(prefix + ".settings.key2", "value2");
System.setProperty(prefix + ".provider", KeyValueConfigurationMock.class.getName());
System.setProperty(prefix + ".database", "bucket");
final BucketManager bucketManager = config.getValue(prefix, BucketManager.class);
final BucketManagerMock bucket = BucketManagerMock.class.cast(bucketManager);
Assertions.assertEquals("bucket", bucket.getBucketName());
System.clearProperty(prefix);
System.clearProperty(prefix + ".settings.key");
System.clearProperty(prefix + ".settings.key2");
System.clearProperty(prefix + ".provider");
System.clearProperty(prefix + ".database");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2019 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.artemis.configuration;

import jakarta.nosql.Settings;
import jakarta.nosql.keyvalue.BucketManagerFactory;
import org.eclipse.jnosql.artemis.CDIExtension;
import org.eclipse.jnosql.artemis.configuration.KeyValueConfigurationMock.BucketManagerFactoryMock;
import org.eclipse.microprofile.config.Config;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import javax.inject.Inject;
import java.util.NoSuchElementException;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@ExtendWith(CDIExtension.class)
class BucketManagerFactoryConverterTest {

@Inject
private Config config;

@Test
public void shouldReturnErrorWhenThereIsNoProvider() {
final String prefix = UUID.randomUUID().toString();
System.setProperty(prefix, prefix);
System.setProperty(prefix + ".settings.key", "value");
System.setProperty(prefix + ".settings.key2", "value2");
Assertions.assertThrows(NoSuchElementException.class, () -> config.getValue(prefix, BucketManagerFactory.class) );

System.clearProperty(prefix);
System.clearProperty(prefix + ".settings.key");
System.clearProperty(prefix + ".settings.key2");
}


@Test
public void shouldReturnErrorWhenThereIsInvalidProvider() {
final String prefix = UUID.randomUUID().toString();
System.setProperty(prefix, prefix);
System.setProperty(prefix + ".settings.key", "value");
System.setProperty(prefix + ".settings.key2", "value2");
System.setProperty(prefix + ".provider", "java.lang.String");
Assertions.assertThrows(ConfigurationException.class, () -> config.getValue(prefix, BucketManagerFactory.class) );

System.clearProperty(prefix);
System.clearProperty(prefix + ".settings.key");
System.clearProperty(prefix + ".settings.key2");
System.clearProperty(prefix + ".provider");
}

@Test
public void shouldReturnManagerFactory() {
final String prefix = UUID.randomUUID().toString();
System.setProperty(prefix, prefix);
System.setProperty(prefix + ".settings.key", "value");
System.setProperty(prefix + ".settings.key2", "value2");
System.setProperty(prefix + ".provider", KeyValueConfigurationMock.class.getName());
final BucketManagerFactory managerFactory = config.getValue(prefix, BucketManagerFactory.class);

final BucketManagerFactoryMock factoryMock = BucketManagerFactoryMock.class.cast(managerFactory);
final Settings settings = factoryMock.getSettings();

assertEquals(2, settings.size());
assertEquals(settings.get("key").get(), "value");
assertEquals(settings.get("key2").get(), "value2");

assertNotNull(managerFactory);
System.clearProperty(prefix);
System.clearProperty(prefix + ".settings.key");
System.clearProperty(prefix + ".settings.key2");
System.clearProperty(prefix + ".provider");
}
}
Loading

0 comments on commit 177cafe

Please sign in to comment.