diff --git a/src/main/java/com/kingsrook/qqq/backend/core/callbacks/NoopCallback.java b/src/main/java/com/kingsrook/qqq/backend/core/callbacks/NoopCallback.java new file mode 100644 index 00000000..4c3fa2c5 --- /dev/null +++ b/src/main/java/com/kingsrook/qqq/backend/core/callbacks/NoopCallback.java @@ -0,0 +1,58 @@ +/* + * QQQ - Low-code Application Framework for Engineers. + * Copyright (C) 2021-2022. Kingsrook, LLC + * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States + * contact@kingsrook.com + * https://github.com/Kingsrook/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.kingsrook.qqq.backend.core.callbacks; + + +import java.io.Serializable; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import com.kingsrook.qqq.backend.core.model.actions.query.QQueryFilter; +import com.kingsrook.qqq.backend.core.model.metadata.QFieldMetaData; + + +/******************************************************************************* + ** Simple implementation of a callback, that does no-op (returns empty objects). + ** Useful for scaffolding, perhaps tests. + *******************************************************************************/ +public class NoopCallback implements QProcessCallback +{ + /******************************************************************************* + ** Get the filter query for this callback. + *******************************************************************************/ + @Override + public QQueryFilter getQueryFilter() + { + return (new QQueryFilter()); + } + + + + /******************************************************************************* + ** Get the field values for this callback. + *******************************************************************************/ + @Override + public Map getFieldValues(List fields) + { + return (Collections.emptyMap()); + } +} diff --git a/src/main/java/com/kingsrook/qqq/backend/core/model/metadata/QTableMetaData.java b/src/main/java/com/kingsrook/qqq/backend/core/model/metadata/QTableMetaData.java index 78c0ff84..f479e463 100644 --- a/src/main/java/com/kingsrook/qqq/backend/core/model/metadata/QTableMetaData.java +++ b/src/main/java/com/kingsrook/qqq/backend/core/model/metadata/QTableMetaData.java @@ -22,6 +22,7 @@ package com.kingsrook.qqq.backend.core.model.metadata; +import java.io.Serializable; import java.util.LinkedHashMap; import java.util.Map; @@ -30,7 +31,7 @@ import java.util.Map; ** Meta-Data to define a table in a QQQ instance. ** *******************************************************************************/ -public class QTableMetaData +public class QTableMetaData implements Serializable { private String name; private String label; diff --git a/src/main/java/com/kingsrook/qqq/backend/core/modules/defaults/FullyAnonymousAuthenticationModule.java b/src/main/java/com/kingsrook/qqq/backend/core/modules/defaults/FullyAnonymousAuthenticationModule.java index ace5057a..1698f2d2 100644 --- a/src/main/java/com/kingsrook/qqq/backend/core/modules/defaults/FullyAnonymousAuthenticationModule.java +++ b/src/main/java/com/kingsrook/qqq/backend/core/modules/defaults/FullyAnonymousAuthenticationModule.java @@ -27,18 +27,14 @@ import java.util.UUID; import com.kingsrook.qqq.backend.core.model.session.QSession; import com.kingsrook.qqq.backend.core.model.session.QUser; import com.kingsrook.qqq.backend.core.modules.interfaces.QAuthenticationModuleInterface; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; /******************************************************************************* - ** + ** An authentication module with no actual backing system - all users are treated + ** as anonymous, and all sessions are always valid. *******************************************************************************/ public class FullyAnonymousAuthenticationModule implements QAuthenticationModuleInterface { - private static final Logger logger = LogManager.getLogger(FullyAnonymousAuthenticationModule.class); - - /******************************************************************************* ** @@ -51,7 +47,7 @@ public class FullyAnonymousAuthenticationModule implements QAuthenticationModule qUser.setFullName("Anonymous"); QSession qSession = new QSession(); - if (context.get("sessionId") != null) + if(context != null && context.get("sessionId") != null) { qSession.setIdReference(context.get("sessionId")); } @@ -72,6 +68,10 @@ public class FullyAnonymousAuthenticationModule implements QAuthenticationModule @Override public boolean isSessionValid(QSession session) { + if(session == null) + { + return (false); + } return (true); } } diff --git a/src/main/java/com/kingsrook/qqq/backend/core/state/TempFileStateProvider.java b/src/main/java/com/kingsrook/qqq/backend/core/state/TempFileStateProvider.java index d4c092b9..0c6e41d3 100644 --- a/src/main/java/com/kingsrook/qqq/backend/core/state/TempFileStateProvider.java +++ b/src/main/java/com/kingsrook/qqq/backend/core/state/TempFileStateProvider.java @@ -23,6 +23,7 @@ package com.kingsrook.qqq.backend.core.state; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.Serializable; import com.kingsrook.qqq.backend.core.utils.JsonUtils; @@ -30,7 +31,7 @@ import org.apache.commons.io.FileUtils; /******************************************************************************* - ** Singleton class that provides a (non-persistent!!) in-memory state provider. + ** State provider that uses files in the /tmp/ directory. *******************************************************************************/ public class TempFileStateProvider implements StateProviderInterface { @@ -92,9 +93,9 @@ public class TempFileStateProvider implements StateProviderInterface String json = FileUtils.readFileToString(new File("/tmp/" + key.toString())); return JsonUtils.toObject(json, type); } - catch(ClassCastException cce) + catch(FileNotFoundException fnfe) { - throw new RuntimeException("Stored state value could not be cast to desired type", cce); + return (null); } catch(IOException ie) { diff --git a/src/test/java/com/kingsrook/qqq/backend/core/modules/defaults/FullyAnonymousAuthenticationModuleTest.java b/src/test/java/com/kingsrook/qqq/backend/core/modules/defaults/FullyAnonymousAuthenticationModuleTest.java new file mode 100644 index 00000000..f6a11a16 --- /dev/null +++ b/src/test/java/com/kingsrook/qqq/backend/core/modules/defaults/FullyAnonymousAuthenticationModuleTest.java @@ -0,0 +1,53 @@ +/* + * QQQ - Low-code Application Framework for Engineers. + * Copyright (C) 2021-2022. Kingsrook, LLC + * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States + * contact@kingsrook.com + * https://github.com/Kingsrook/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.kingsrook.qqq.backend.core.modules.defaults; + + +import com.kingsrook.qqq.backend.core.model.session.QSession; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + + +/******************************************************************************* + ** Unit test for the FullyAnonymousAuthenticationModule + *******************************************************************************/ +public class FullyAnonymousAuthenticationModuleTest +{ + + @Test + public void test() + { + FullyAnonymousAuthenticationModule fullyAnonymousAuthenticationModule = new FullyAnonymousAuthenticationModule(); + + QSession session = fullyAnonymousAuthenticationModule.createSession(null); + + assertNotNull(session, "Session should not be null"); + assertNotNull(session.getIdReference(), "Session id ref should not be null"); + assertNotNull(session.getUser(), "Session User should not be null"); + assertNotNull(session.getUser().getIdReference(), "Session User id ref should not be null"); + assertTrue(fullyAnonymousAuthenticationModule.isSessionValid(session), "Any session should be valid"); + assertFalse(fullyAnonymousAuthenticationModule.isSessionValid(null), "null should be not valid"); + } + +} \ No newline at end of file diff --git a/src/test/java/com/kingsrook/qqq/backend/core/state/InMemoryStateProviderTest.java b/src/test/java/com/kingsrook/qqq/backend/core/state/InMemoryStateProviderTest.java new file mode 100644 index 00000000..193305db --- /dev/null +++ b/src/test/java/com/kingsrook/qqq/backend/core/state/InMemoryStateProviderTest.java @@ -0,0 +1,89 @@ +/* + * QQQ - Low-code Application Framework for Engineers. + * Copyright (C) 2021-2022. Kingsrook, LLC + * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States + * contact@kingsrook.com + * https://github.com/Kingsrook/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.kingsrook.qqq.backend.core.state; + + +import java.util.UUID; +import com.kingsrook.qqq.backend.core.model.data.QRecord; +import com.kingsrook.qqq.backend.core.model.metadata.QTableMetaData; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + + +/******************************************************************************* + ** Unit test for InMemoryStateProvider + *******************************************************************************/ +public class InMemoryStateProviderTest +{ + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + public void testStateNotFound() + { + InMemoryStateProvider stateProvider = InMemoryStateProvider.getInstance(); + UUIDStateKey key = new UUIDStateKey(); + + Assertions.assertNull(stateProvider.get(QRecord.class, key), "Key not found in state should return null"); + } + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + public void testSimpleStateFound() + { + InMemoryStateProvider stateProvider = InMemoryStateProvider.getInstance(); + UUIDStateKey key = new UUIDStateKey(); + + String uuid = UUID.randomUUID().toString(); + QRecord qRecord = new QRecord().withValue("uuid", uuid); + stateProvider.put(key, qRecord); + + QRecord qRecordFromState = stateProvider.get(QRecord.class, key); + Assertions.assertEquals(uuid, qRecordFromState.getValueString("uuid"), "Should read value from state persistence"); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + public void testWrongTypeOnGet() + { + InMemoryStateProvider stateProvider = InMemoryStateProvider.getInstance(); + UUIDStateKey key = new UUIDStateKey(); + + String uuid = UUID.randomUUID().toString(); + QRecord qRecord = new QRecord().withValue("uuid", uuid); + stateProvider.put(key, qRecord); + + Assertions.assertThrows(Exception.class, () -> + { + stateProvider.get(QTableMetaData.class, key); + }); + } + +} \ No newline at end of file diff --git a/src/test/java/com/kingsrook/qqq/backend/core/state/TempFileStateProviderTest.java b/src/test/java/com/kingsrook/qqq/backend/core/state/TempFileStateProviderTest.java new file mode 100644 index 00000000..4f361afd --- /dev/null +++ b/src/test/java/com/kingsrook/qqq/backend/core/state/TempFileStateProviderTest.java @@ -0,0 +1,89 @@ +/* + * QQQ - Low-code Application Framework for Engineers. + * Copyright (C) 2021-2022. Kingsrook, LLC + * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States + * contact@kingsrook.com + * https://github.com/Kingsrook/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.kingsrook.qqq.backend.core.state; + + +import java.util.UUID; +import com.kingsrook.qqq.backend.core.model.data.QRecord; +import com.kingsrook.qqq.backend.core.model.metadata.QTableMetaData; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + + +/******************************************************************************* + ** Unit test for TempFileStateProvider + *******************************************************************************/ +public class TempFileStateProviderTest +{ + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + public void testStateNotFound() + { + TempFileStateProvider stateProvider = TempFileStateProvider.getInstance(); + UUIDStateKey key = new UUIDStateKey(); + + Assertions.assertNull(stateProvider.get(QRecord.class, key), "Key not found in state should return null"); + } + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + public void testSimpleStateFound() + { + TempFileStateProvider stateProvider = TempFileStateProvider.getInstance(); + UUIDStateKey key = new UUIDStateKey(); + + String uuid = UUID.randomUUID().toString(); + QRecord qRecord = new QRecord().withValue("uuid", uuid); + stateProvider.put(key, qRecord); + + QRecord qRecordFromState = stateProvider.get(QRecord.class, key); + Assertions.assertEquals(uuid, qRecordFromState.getValueString("uuid"), "Should read value from state persistence"); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + public void testWrongTypeOnGet() + { + TempFileStateProvider stateProvider = TempFileStateProvider.getInstance(); + UUIDStateKey key = new UUIDStateKey(); + + String uuid = UUID.randomUUID().toString(); + QRecord qRecord = new QRecord().withValue("uuid", uuid); + stateProvider.put(key, qRecord); + + Assertions.assertThrows(Exception.class, () -> + { + stateProvider.get(QTableMetaData.class, key); + }); + } + +} \ No newline at end of file