mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-19 05:30:43 +00:00
Checkpoint
This commit is contained in:
@ -0,0 +1,36 @@
|
||||
package com.kingsrook.qqq.backend.core.actions;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.TableMetaDataRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.TableMetaDataResult;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QTableMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.frontend.QFrontendTableMetaData;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class TableMetaDataAction
|
||||
{
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public TableMetaDataResult execute(TableMetaDataRequest tableMetaDataRequest) throws QException
|
||||
{
|
||||
// todo pre-customization - just get to modify the request?
|
||||
TableMetaDataResult tableMetaDataResult = new TableMetaDataResult();
|
||||
|
||||
QTableMetaData table = tableMetaDataRequest.getInstance().getTable(tableMetaDataRequest.getTableName());
|
||||
if(table == null)
|
||||
{
|
||||
throw (new QUserFacingException("Table [" + tableMetaDataRequest.getTableName() + "] was not found."));
|
||||
}
|
||||
tableMetaDataResult.setTable(new QFrontendTableMetaData(table, true));
|
||||
|
||||
// todo post-customization - can do whatever w/ the result if you want
|
||||
|
||||
return tableMetaDataResult;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.kingsrook.qqq.backend.core.exceptions;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class QUserFacingException extends QException
|
||||
{
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public QUserFacingException(String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public QUserFacingException(String message, Throwable cause)
|
||||
{
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.kingsrook.qqq.backend.core.model.actions;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class TableMetaDataRequest extends AbstractQRequest
|
||||
{
|
||||
private String tableName;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public TableMetaDataRequest()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public TableMetaDataRequest(QInstance instance)
|
||||
{
|
||||
super(instance);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for tableName
|
||||
**
|
||||
*******************************************************************************/
|
||||
public String getTableName()
|
||||
{
|
||||
return tableName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for tableName
|
||||
**
|
||||
*******************************************************************************/
|
||||
public void setTableName(String tableName)
|
||||
{
|
||||
this.tableName = tableName;
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.kingsrook.qqq.backend.core.model.actions;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.frontend.QFrontendTableMetaData;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* Result for a metaData action
|
||||
*
|
||||
*******************************************************************************/
|
||||
public class TableMetaDataResult extends AbstractQResult
|
||||
{
|
||||
QFrontendTableMetaData table;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for table
|
||||
**
|
||||
*******************************************************************************/
|
||||
public QFrontendTableMetaData getTable()
|
||||
{
|
||||
return table;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for table
|
||||
**
|
||||
*******************************************************************************/
|
||||
public void setTable(QFrontendTableMetaData table)
|
||||
{
|
||||
this.table = table;
|
||||
}
|
||||
}
|
@ -23,8 +23,8 @@ public class QModuleDispatcher
|
||||
public QModuleDispatcher()
|
||||
{
|
||||
backendTypeToModuleClassNameMap = new HashMap<>();
|
||||
backendTypeToModuleClassNameMap.put("mock", "com.kingsrook.qqq.backend.core.modules.mock.MockModule");
|
||||
backendTypeToModuleClassNameMap.put("rdbms", "com.kingsrook.qqq.backend.module.rdbms.RDBSMModule");
|
||||
backendTypeToModuleClassNameMap.put("nosql", "com.kingsrook.qqq.backend.module.nosql.NoSQLModule");
|
||||
// todo - let user define custom type -> classes
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,39 @@
|
||||
package com.kingsrook.qqq.backend.core.modules.mock;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.InsertRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.InsertResult;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecordWithStatus;
|
||||
import com.kingsrook.qqq.backend.core.modules.interfaces.InsertInterface;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class MockInsertAction implements InsertInterface
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public InsertResult execute(InsertRequest insertRequest) throws QException
|
||||
{
|
||||
try
|
||||
{
|
||||
InsertResult rs = new InsertResult();
|
||||
|
||||
rs.setRecords(insertRequest.getRecords().stream().map(qRecord ->
|
||||
{
|
||||
return new QRecordWithStatus(qRecord);
|
||||
}).toList());
|
||||
|
||||
return rs;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
throw new QException("Error executing insert: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.kingsrook.qqq.backend.core.modules.mock;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.modules.interfaces.InsertInterface;
|
||||
import com.kingsrook.qqq.backend.core.modules.interfaces.QModuleInterface;
|
||||
import com.kingsrook.qqq.backend.core.modules.interfaces.QueryInterface;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class MockModule implements QModuleInterface
|
||||
{
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
public QueryInterface getQueryInterface()
|
||||
{
|
||||
return new MockQueryAction();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
public InsertInterface getInsertInterface()
|
||||
{
|
||||
return (new MockInsertAction());
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.kingsrook.qqq.backend.core.modules.mock;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.QueryRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.QueryResult;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QTableMetaData;
|
||||
import com.kingsrook.qqq.backend.core.modules.interfaces.QueryInterface;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class MockQueryAction implements QueryInterface
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public QueryResult execute(QueryRequest queryRequest) throws QException
|
||||
{
|
||||
try
|
||||
{
|
||||
QTableMetaData table = queryRequest.getTable();
|
||||
|
||||
QueryResult rs = new QueryResult();
|
||||
List<QRecord> records = new ArrayList<>();
|
||||
rs.setRecords(records);
|
||||
|
||||
QRecord record = new QRecord();
|
||||
records.add(record);
|
||||
record.setTableName(table.getName());
|
||||
|
||||
for(String field : table.getFields().keySet())
|
||||
{
|
||||
record.setValue(field, "1");
|
||||
}
|
||||
|
||||
return rs;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
throw new QException("Error executing query", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -251,7 +251,7 @@ public class CollectionUtils
|
||||
{
|
||||
List<List<T>> rs = new LinkedList<>();
|
||||
|
||||
if(values.isEmpty())
|
||||
if(values == null || values.isEmpty())
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// if there are no input values, return an empty list of lists. //
|
||||
|
@ -0,0 +1,34 @@
|
||||
package com.kingsrook.qqq.backend.core.utils;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class ExceptionUtils
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public static <T extends Throwable> T findClassInRootChain(Throwable e, Class<T> targetClass)
|
||||
{
|
||||
if (e == null)
|
||||
{
|
||||
return (null);
|
||||
}
|
||||
|
||||
if(e.getClass().equals(targetClass))
|
||||
{
|
||||
//noinspection unchecked
|
||||
return ((T) e);
|
||||
}
|
||||
|
||||
if(e.getCause() == null)
|
||||
{
|
||||
return (null);
|
||||
}
|
||||
|
||||
return findClassInRootChain(e.getCause(), targetClass);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.kingsrook.qqq.backend.core.actions;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.InsertRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.InsertResult;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.utils.TestUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
class InsertActionTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test() throws QException
|
||||
{
|
||||
InsertRequest request = new InsertRequest(TestUtils.defineInstance());
|
||||
request.setTableName("person");
|
||||
List<QRecord> records =new ArrayList<>();
|
||||
QRecord record = new QRecord();
|
||||
record.setValue("firstName", "James");
|
||||
records.add(record);
|
||||
request.setRecords(records);
|
||||
InsertResult result = new InsertAction().execute(request);
|
||||
assertNotNull(result);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.kingsrook.qqq.backend.core.actions;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.MetaDataRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.MetaDataResult;
|
||||
import com.kingsrook.qqq.backend.core.utils.TestUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
class MetaDataActionTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test() throws QException
|
||||
{
|
||||
MetaDataRequest request = new MetaDataRequest(TestUtils.defineInstance());
|
||||
MetaDataResult result = new MetaDataAction().execute(request);
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getTables());
|
||||
assertNotNull(result.getTables().get("person"));
|
||||
assertEquals("person", result.getTables().get("person").getName());
|
||||
assertEquals("Person", result.getTables().get("person").getLabel());
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.kingsrook.qqq.backend.core.actions;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.QueryRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.QueryResult;
|
||||
import com.kingsrook.qqq.backend.core.utils.TestUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
class QueryActionTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test() throws QException
|
||||
{
|
||||
QueryRequest request = new QueryRequest(TestUtils.defineInstance());
|
||||
request.setTableName("person");
|
||||
QueryResult result = new QueryAction().execute(request);
|
||||
assertNotNull(result);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.kingsrook.qqq.backend.core.actions;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.TableMetaDataRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.TableMetaDataResult;
|
||||
import com.kingsrook.qqq.backend.core.utils.TestUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
class TableMetaDataActionTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test() throws QException
|
||||
{
|
||||
TableMetaDataRequest request = new TableMetaDataRequest(TestUtils.defineInstance());
|
||||
request.setTableName("person");
|
||||
TableMetaDataResult result = new TableMetaDataAction().execute(request);
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getTable());
|
||||
assertEquals("person", result.getTable().getName());
|
||||
assertEquals("Person", result.getTable().getLabel());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test_notFound()
|
||||
{
|
||||
assertThrows(QUserFacingException.class, () -> {
|
||||
TableMetaDataRequest request = new TableMetaDataRequest(TestUtils.defineInstance());
|
||||
request.setTableName("willNotBeFound");
|
||||
new TableMetaDataAction().execute(request);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.kingsrook.qqq.backend.core.instances;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QFieldMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QTableMetaData;
|
||||
import com.kingsrook.qqq.backend.core.utils.TestUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
class QInstanceEnricherTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test_nullTableLabelComesFromName()
|
||||
{
|
||||
QInstance qInstance = TestUtils.defineInstance();
|
||||
QTableMetaData personTable = qInstance.getTable("person");
|
||||
personTable.setLabel(null);
|
||||
assertNull(personTable.getLabel());
|
||||
new QInstanceEnricher().enrich(qInstance);
|
||||
assertEquals("Person", personTable.getLabel());
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test_nullNameGivesNullLabel()
|
||||
{
|
||||
QInstance qInstance = TestUtils.defineInstance();
|
||||
QTableMetaData personTable = qInstance.getTable("person");
|
||||
personTable.setLabel(null);
|
||||
personTable.setName(null);
|
||||
assertNull(personTable.getLabel());
|
||||
assertNull(personTable.getName());
|
||||
new QInstanceEnricher().enrich(qInstance);
|
||||
assertNull(personTable.getLabel());
|
||||
assertNull(personTable.getName());
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test_nullFieldLabelComesFromName()
|
||||
{
|
||||
QInstance qInstance = TestUtils.defineInstance();
|
||||
QFieldMetaData idField = qInstance.getTable("person").getField("id");
|
||||
idField.setLabel(null);
|
||||
assertNull(idField.getLabel());
|
||||
new QInstanceEnricher().enrich(qInstance);
|
||||
assertEquals("Id", idField.getLabel());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.kingsrook.qqq.backend.core.modules;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QModuleDispatchException;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData;
|
||||
import com.kingsrook.qqq.backend.core.modules.interfaces.QModuleInterface;
|
||||
import com.kingsrook.qqq.backend.core.utils.TestUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
class QModuleDispatcherTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test_getQModule_valid() throws QModuleDispatchException
|
||||
{
|
||||
QModuleInterface qModule = new QModuleDispatcher().getQModule(TestUtils.defineBackend());
|
||||
assertNotNull(qModule);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test_getQModule_typeNotFound()
|
||||
{
|
||||
assertThrows(QModuleDispatchException.class, () ->
|
||||
{
|
||||
QBackendMetaData qBackendMetaData = TestUtils.defineBackend();
|
||||
qBackendMetaData.setType("aTypeThatWontEverExist");
|
||||
new QModuleDispatcher().getQModule(qBackendMetaData);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,391 @@
|
||||
package com.kingsrook.qqq.backend.core.utils;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
class CollectionUtilsTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_nullSafeIsEmpty_Map()
|
||||
{
|
||||
assertTrue(CollectionUtils.nullSafeIsEmpty(Collections.emptyMap()));
|
||||
assertTrue(CollectionUtils.nullSafeIsEmpty(new HashMap<>()));
|
||||
assertTrue(CollectionUtils.nullSafeIsEmpty((Map<?, ?>) null));
|
||||
Map<String, Integer> myMap = new HashMap<>();
|
||||
assertTrue(CollectionUtils.nullSafeIsEmpty(myMap));
|
||||
myMap.put("A", 1);
|
||||
assertFalse(CollectionUtils.nullSafeIsEmpty(myMap));
|
||||
myMap.clear();
|
||||
assertTrue(CollectionUtils.nullSafeIsEmpty(myMap));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_nullSafeIsEmpty_Collection()
|
||||
{
|
||||
assertTrue(CollectionUtils.nullSafeIsEmpty(Collections.emptyList()));
|
||||
assertTrue(CollectionUtils.nullSafeIsEmpty(new ArrayList<>()));
|
||||
assertTrue(CollectionUtils.nullSafeIsEmpty((List<?>) null));
|
||||
List<String> myList = new ArrayList<>();
|
||||
assertTrue(CollectionUtils.nullSafeIsEmpty(myList));
|
||||
myList.add("A");
|
||||
assertFalse(CollectionUtils.nullSafeIsEmpty(myList));
|
||||
myList.clear();
|
||||
assertTrue(CollectionUtils.nullSafeIsEmpty(myList));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_nullSafeHasContents_Map()
|
||||
{
|
||||
assertFalse(CollectionUtils.nullSafeHasContents(Collections.emptyMap()));
|
||||
assertFalse(CollectionUtils.nullSafeHasContents(new HashMap<>()));
|
||||
assertFalse(CollectionUtils.nullSafeHasContents((Map<?, ?>) null));
|
||||
Map<String, Integer> myMap = new HashMap<>();
|
||||
assertFalse(CollectionUtils.nullSafeHasContents(myMap));
|
||||
myMap.put("A", 1);
|
||||
assertTrue(CollectionUtils.nullSafeHasContents(myMap));
|
||||
myMap.clear();
|
||||
assertFalse(CollectionUtils.nullSafeHasContents(myMap));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_nullSafeHasContents_Collection()
|
||||
{
|
||||
assertFalse(CollectionUtils.nullSafeHasContents(Collections.emptyList()));
|
||||
assertFalse(CollectionUtils.nullSafeHasContents(new ArrayList<>()));
|
||||
assertFalse(CollectionUtils.nullSafeHasContents((List<?>) null));
|
||||
List<String> myList = new ArrayList<>();
|
||||
assertFalse(CollectionUtils.nullSafeHasContents(myList));
|
||||
myList.add("A");
|
||||
assertTrue(CollectionUtils.nullSafeHasContents(myList));
|
||||
myList.clear();
|
||||
assertFalse(CollectionUtils.nullSafeHasContents(myList));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_nullSafeSize_Map()
|
||||
{
|
||||
assertEquals(0, CollectionUtils.nullSafeSize(Collections.emptyMap()));
|
||||
assertEquals(0, CollectionUtils.nullSafeSize(new HashMap<>()));
|
||||
assertEquals(0, CollectionUtils.nullSafeSize((Map<?, ?>) null));
|
||||
Map<String, Integer> myMap = new HashMap<>();
|
||||
assertEquals(0, CollectionUtils.nullSafeSize(myMap));
|
||||
myMap.put("A", 1);
|
||||
assertEquals(1, CollectionUtils.nullSafeSize(myMap));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_nullSafeSize_Collection()
|
||||
{
|
||||
assertEquals(0, CollectionUtils.nullSafeSize(Collections.emptyList()));
|
||||
assertEquals(0, CollectionUtils.nullSafeSize(new ArrayList<>()));
|
||||
assertEquals(0, CollectionUtils.nullSafeSize((List<?>) null));
|
||||
List<String> myList = new ArrayList<>();
|
||||
assertEquals(0, CollectionUtils.nullSafeSize(myList));
|
||||
myList.add("A");
|
||||
assertEquals(1, CollectionUtils.nullSafeSize(myList));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Test
|
||||
void test_addAllToMap()
|
||||
{
|
||||
Map<String, Integer> to = new HashMap<>();
|
||||
Map<String, Integer> from = new HashMap<>();
|
||||
|
||||
assertThrows(NullPointerException.class, () -> CollectionUtils.addAllToMap(null, null));
|
||||
assertThrows(NullPointerException.class, () -> CollectionUtils.addAllToMap(to, null));
|
||||
|
||||
// this case does not currently throw - capture that fact here in case we change it.
|
||||
CollectionUtils.addAllToMap(null, from);
|
||||
|
||||
CollectionUtils.addAllToMap(to, from);
|
||||
assertEquals(0, to.size());
|
||||
|
||||
from.put("A", 1);
|
||||
CollectionUtils.addAllToMap(to, from);
|
||||
assertEquals(1, to.size());
|
||||
assertEquals(1, to.get("A"));
|
||||
|
||||
from.put("B", 2);
|
||||
CollectionUtils.addAllToMap(to, from);
|
||||
assertEquals(2, to.size());
|
||||
assertEquals(1, to.get("A"));
|
||||
assertEquals(2, to.get("B"));
|
||||
|
||||
from.put("B", 3);
|
||||
CollectionUtils.addAllToMap(to, from);
|
||||
assertEquals(2, to.size());
|
||||
assertEquals(1, to.get("A"));
|
||||
assertEquals(3, to.get("B"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_listToMap()
|
||||
{
|
||||
assertNull(CollectionUtils.listToMap(null, null));
|
||||
|
||||
List<String> myList = new ArrayList<>();
|
||||
myList.add("Apple");
|
||||
myList.add("Banana");
|
||||
assertThrows(NullPointerException.class, () -> CollectionUtils.listToMap(myList, null));
|
||||
|
||||
Map<String, String> myMap = CollectionUtils.listToMap(myList, first());
|
||||
assertEquals(2, myMap.size());
|
||||
assertEquals("Apple", myMap.get("A"));
|
||||
assertEquals("Banana", myMap.get("B"));
|
||||
|
||||
// confirm what a clobbered key does
|
||||
myList.add("Airplane");
|
||||
myMap = CollectionUtils.listToMap(myList, first());
|
||||
assertEquals(2, myMap.size());
|
||||
assertEquals("Airplane", myMap.get("A"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_listToMap_valueFunction()
|
||||
{
|
||||
assertNull(CollectionUtils.listToMap(null, null, null));
|
||||
|
||||
List<String> myList = new ArrayList<>();
|
||||
myList.add("Apple");
|
||||
myList.add("Banana");
|
||||
assertThrows(NullPointerException.class, () -> CollectionUtils.listToMap(myList, null, null));
|
||||
|
||||
Map<String, String> myMap = CollectionUtils.listToMap(myList, first(), rest());
|
||||
assertEquals(2, myMap.size());
|
||||
assertEquals("pple", myMap.get("A"));
|
||||
assertEquals("anana", myMap.get("B"));
|
||||
|
||||
// confirm what a clobbered key does
|
||||
myList.add("Airplane");
|
||||
myMap = CollectionUtils.listToMap(myList, first(), rest());
|
||||
assertEquals(2, myMap.size());
|
||||
assertEquals("irplane", myMap.get("A"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_listToListingHash()
|
||||
{
|
||||
assertNull(CollectionUtils.listToListingHash(null, null));
|
||||
|
||||
List<String> myList = new ArrayList<>();
|
||||
myList.add("Apple");
|
||||
myList.add("Banana");
|
||||
assertThrows(NullPointerException.class, () -> CollectionUtils.listToListingHash(myList, null));
|
||||
|
||||
ListingHash<String, String> myListingHash = CollectionUtils.listToListingHash(myList, first());
|
||||
assertEquals(2, myListingHash.size());
|
||||
assertEquals(1, myListingHash.get("A").size());
|
||||
assertEquals("Apple", myListingHash.get("A").get(0));
|
||||
assertEquals(1, myListingHash.get("B").size());
|
||||
assertEquals("Banana", myListingHash.get("B").get(0));
|
||||
|
||||
myList.add("Airplane");
|
||||
myListingHash = CollectionUtils.listToListingHash(myList, first());
|
||||
assertEquals(2, myListingHash.size());
|
||||
assertEquals(2, myListingHash.get("A").size());
|
||||
assertEquals("Apple", myListingHash.get("A").get(0));
|
||||
assertEquals("Airplane", myListingHash.get("A").get(1));
|
||||
assertEquals(1, myListingHash.get("B").size());
|
||||
assertEquals("Banana", myListingHash.get("B").get(0));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_listToListingHash_valueFunction()
|
||||
{
|
||||
assertNull(CollectionUtils.listToListingHash(null, null, null));
|
||||
|
||||
List<String> myList = new ArrayList<>();
|
||||
myList.add("Apple");
|
||||
myList.add("Banana");
|
||||
assertThrows(NullPointerException.class, () -> CollectionUtils.listToListingHash(myList, null, null));
|
||||
|
||||
ListingHash<String, String> myListingHash = CollectionUtils.listToListingHash(myList, first(), rest());
|
||||
assertEquals(2, myListingHash.size());
|
||||
assertEquals(1, myListingHash.get("A").size());
|
||||
assertEquals("pple", myListingHash.get("A").get(0));
|
||||
assertEquals(1, myListingHash.get("B").size());
|
||||
assertEquals("anana", myListingHash.get("B").get(0));
|
||||
|
||||
myList.add("Airplane");
|
||||
myListingHash = CollectionUtils.listToListingHash(myList, first(), rest());
|
||||
assertEquals(2, myListingHash.size());
|
||||
assertEquals(2, myListingHash.get("A").size());
|
||||
assertEquals("pple", myListingHash.get("A").get(0));
|
||||
assertEquals("irplane", myListingHash.get("A").get(1));
|
||||
assertEquals(1, myListingHash.get("B").size());
|
||||
assertEquals("anana", myListingHash.get("B").get(0));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_listTo2LevelMap()
|
||||
{
|
||||
assertNull(CollectionUtils.listTo2LevelMap(null, null, null));
|
||||
|
||||
List<String> myList = new ArrayList<>();
|
||||
myList.add("Apple");
|
||||
myList.add("Banana");
|
||||
myList.add("Airplane");
|
||||
assertThrows(NullPointerException.class, () -> CollectionUtils.listTo2LevelMap(myList, null, null));
|
||||
assertThrows(NullPointerException.class, () -> CollectionUtils.listTo2LevelMap(myList, first(), null));
|
||||
assertThrows(NullPointerException.class, () -> CollectionUtils.listTo2LevelMap(myList, null, second()));
|
||||
|
||||
Map<String, Map<String, String>> myMap = CollectionUtils.listTo2LevelMap(myList, first(), second());
|
||||
assertEquals(2, myMap.size());
|
||||
assertEquals(2, myMap.get("A").size());
|
||||
assertEquals("Apple", myMap.get("A").get("p"));
|
||||
assertEquals("Airplane", myMap.get("A").get("i"));
|
||||
assertEquals(1, myMap.get("B").size());
|
||||
assertEquals("Banana", myMap.get("B").get("a"));
|
||||
|
||||
// demonstrate clobbering behavior
|
||||
myList.add("Ape");
|
||||
myMap = CollectionUtils.listTo2LevelMap(myList, first(), second());
|
||||
assertEquals(2, myMap.get("A").size());
|
||||
assertEquals("Ape", myMap.get("A").get("p"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** helper method to get rest of string (unsafely)
|
||||
*******************************************************************************/
|
||||
private Function<String, String> rest()
|
||||
{
|
||||
return s -> s.substring(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** helper method to get first char of string (unsafely)
|
||||
*******************************************************************************/
|
||||
private Function<String, String> first()
|
||||
{
|
||||
return s -> s.substring(0, 1);
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
** helper method to get second char of string (unsafely)
|
||||
*******************************************************************************/
|
||||
private Function<String, String> second()
|
||||
{
|
||||
return s -> s.substring(1, 2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_getPages()
|
||||
{
|
||||
List<List<Integer>> pages = CollectionUtils.getPages(null, 5);
|
||||
assertEquals(0, pages.size());
|
||||
|
||||
pages = CollectionUtils.getPages(Collections.emptyList(), 5);
|
||||
assertEquals(0, pages.size());
|
||||
|
||||
pages = CollectionUtils.getPages(List.of(1, 2, 3), 5);
|
||||
assertEquals(1, pages.size());
|
||||
assertEquals(3, pages.get(0).size());
|
||||
|
||||
pages = CollectionUtils.getPages(List.of(1, 2, 3, 4, 5), 5);
|
||||
assertEquals(1, pages.size());
|
||||
assertEquals(5, pages.get(0).size());
|
||||
|
||||
pages = CollectionUtils.getPages(List.of(1, 2, 3, 4, 5, 6, 7), 5);
|
||||
assertEquals(2, pages.size());
|
||||
assertEquals(5, pages.get(0).size());
|
||||
assertEquals(2, pages.get(1).size());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void test_getQuestionMarks()
|
||||
{
|
||||
assertEquals("", CollectionUtils.getQuestionMarks(null));
|
||||
assertEquals("", CollectionUtils.getQuestionMarks(Collections.emptyList()));
|
||||
assertEquals("?", CollectionUtils.getQuestionMarks(List.of(1)));
|
||||
assertEquals("?,?,?", CollectionUtils.getQuestionMarks(List.of(1, 2, 3)));
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.kingsrook.qqq.backend.core.utils;
|
||||
|
||||
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
class ExceptionUtilsTest
|
||||
{
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void findClassInRootChain()
|
||||
{
|
||||
assertNull(ExceptionUtils.findClassInRootChain(null, QUserFacingException.class));
|
||||
QUserFacingException target = new QUserFacingException("target");
|
||||
|
||||
assertSame(target, ExceptionUtils.findClassInRootChain(target, QUserFacingException.class));
|
||||
assertSame(target, ExceptionUtils.findClassInRootChain(new QException("decoy", target), QUserFacingException.class));
|
||||
assertNull(ExceptionUtils.findClassInRootChain(new QException("decoy", target), IllegalArgumentException.class));
|
||||
}
|
||||
}
|
@ -67,6 +67,24 @@ class JsonUtilsTest
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test_toJSONObject_malformed()
|
||||
{
|
||||
// todo - what do we want to throw here?
|
||||
assertThrows(JSONException.class, () ->
|
||||
{
|
||||
JsonUtils.toJSONObject("""
|
||||
{
|
||||
"Foo": "Bar",
|
||||
""");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
@ -33,12 +33,7 @@ public class TestUtils
|
||||
{
|
||||
return new QBackendMetaData()
|
||||
.withName("default")
|
||||
.withType("rdbms")
|
||||
.withValue("vendor", "h2")
|
||||
.withValue("hostName", "mem")
|
||||
.withValue("databaseName", "test_database")
|
||||
.withValue("username", "sa")
|
||||
.withValue("password", "");
|
||||
.withType("mock");
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user