QQQ offers a fixed list (a possible-value source of an enum) in a form but accepts any value in a write: through the API a project could be saved with a status that is not on the list. Found by a live check in a host application. - CmsValueListCheck: a pre-insert and pre-update customizer on every table with a value-list field; a record with another value gets an error and is not written, and an update is checked only for the fields it names. - Changelog: check constraints on the same columns (as kof22 has for media_type and blog_post_version; the project status check is new), and kof22's checks on media_type.max_file_size_mb and the focal point of a project's image. They are part of the not yet released 1.0.0-constraints-<feature> changesets. - Reference DDL regenerated. Tests: 37, Checkstyle 0, guardrails and coverage checks pass. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
172 lines
9.0 KiB
Java
172 lines
9.0 KiB
Java
/*
|
|
* Copyright 2026 Kingsrook, LLC
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.kingsrook.qbits.cms;
|
|
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.ResultSet;
|
|
import java.sql.Statement;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import com.kingsrook.qbits.cms.security.CmsRole;
|
|
import com.kingsrook.qbits.cms.security.CmsSecurity;
|
|
import com.kingsrook.qqq.backend.core.actions.tables.GetAction;
|
|
import com.kingsrook.qqq.backend.core.actions.tables.InsertAction;
|
|
import com.kingsrook.qqq.backend.core.context.QContext;
|
|
import com.kingsrook.qqq.backend.core.model.actions.tables.get.GetInput;
|
|
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
|
|
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
|
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
|
import com.kingsrook.qqq.backend.module.postgres.model.metadata.PostgreSQLBackendMetaData;
|
|
import com.kingsrook.qqq.backend.module.rdbms.jdbc.ConnectionManager;
|
|
import org.junit.jupiter.api.AfterEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.testcontainers.containers.PostgreSQLContainer;
|
|
import org.testcontainers.junit.jupiter.Container;
|
|
import org.testcontainers.junit.jupiter.Testcontainers;
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
|
/*******************************************************************************
|
|
** The install on PostgreSQL (the production database): the changelog creates
|
|
** the kof22-shaped schema (identity ids, the same column types and
|
|
** nullability), the schema check passes, and QQQ reads and writes it.
|
|
** Skipped when Docker is not available.
|
|
*******************************************************************************/
|
|
@Testcontainers(disabledWithoutDocker = true)
|
|
class CmsPostgresInstallTest
|
|
{
|
|
@Container
|
|
private static final PostgreSQLContainer<?> POSTGRES = new PostgreSQLContainer<>("postgres:17-alpine");
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
**
|
|
*******************************************************************************/
|
|
@AfterEach
|
|
void afterEach()
|
|
{
|
|
QContext.clear();
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
** Fresh install on PostgreSQL with the default prefix.
|
|
*******************************************************************************/
|
|
@Test
|
|
void testInstallOnPostgres() throws Exception
|
|
{
|
|
ConnectionManager.resetConnectionProviders();
|
|
PostgreSQLBackendMetaData backend = new PostgreSQLBackendMetaData()
|
|
.withName(CmsTestSupport.BACKEND)
|
|
.withHostName(POSTGRES.getHost())
|
|
.withPort(POSTGRES.getFirstMappedPort())
|
|
.withDatabaseName(POSTGRES.getDatabaseName())
|
|
.withUsername(POSTGRES.getUsername())
|
|
.withPassword(POSTGRES.getPassword());
|
|
CmsTestSupport.migrate(backend, Map.of());
|
|
|
|
QInstance qInstance = CmsTestSupport.blankInstance(backend);
|
|
CmsQBitConfig config = new CmsQBitConfig().withBackendName(CmsTestSupport.BACKEND);
|
|
new CmsQBitProducer().withQBitConfig(config).produce(qInstance);
|
|
assertThat(config.getRuntime().getSchemaProblems()).isEmpty();
|
|
|
|
try(Connection connection = ConnectionManager.getConnection(backend); Statement statement = connection.createStatement())
|
|
{
|
|
assertThat(columns(statement, "cms_page")).contains(
|
|
"id bigint NO", "slug character varying(100) NO", "title character varying(200) NO", "content text YES", "is_published boolean NO",
|
|
"publish_date timestamp without time zone YES", "parent_page_id bigint YES", "create_date timestamp without time zone NO");
|
|
try(ResultSet resultSet = statement.executeQuery("SELECT pg_get_serial_sequence('cms_page', 'id'), "
|
|
+ "(SELECT COUNT(*) FROM information_schema.table_constraints WHERE table_name = 'cms_page' AND constraint_type = 'FOREIGN KEY')"))
|
|
{
|
|
assertThat(resultSet.next()).isTrue();
|
|
assertThat(resultSet.getString(1)).isEqualTo("public.cms_page_id_seq");
|
|
assertThat(resultSet.getInt(2)).isEqualTo(3);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////
|
|
// the content features: column types, and the links between two //
|
|
// features (versions and albums of projects) //
|
|
////////////////////////////////////////////////////////////////////
|
|
assertThat(columns(statement, "cms_media")).contains(
|
|
"id bigint NO", "name character varying(200) NO", "media_type_id bigint NO", "public_url character varying(500) YES", "file_size bigint YES",
|
|
"is_active boolean NO", "site_id bigint NO");
|
|
assertThat(columns(statement, "cms_media_type")).contains("thumbnail_format character varying(10) NO", "max_file_size_mb integer YES", "strip_metadata boolean NO");
|
|
assertThat(columns(statement, "cms_blog_post")).contains(
|
|
"title character varying(100) YES", "content text NO", "excerpt text YES", "is_published boolean YES", "is_featured boolean NO", "view_count bigint YES");
|
|
assertThat(columns(statement, "cms_photo_album")).contains("location_latitude double precision YES", "album_date timestamp without time zone YES");
|
|
assertThat(columns(statement, "cms_project_update")).contains("update_date timestamp without time zone NO", "content text NO");
|
|
try(ResultSet resultSet = statement.executeQuery("SELECT table_name || ':' || COUNT(*) FROM information_schema.table_constraints "
|
|
+ "WHERE constraint_type = 'FOREIGN KEY' AND table_name IN ('cms_blog_post', 'cms_blog_post_version', 'cms_photo_album', 'cms_media') "
|
|
+ "GROUP BY table_name ORDER BY table_name"))
|
|
{
|
|
List<String> foreignKeys = new ArrayList<>();
|
|
while(resultSet.next())
|
|
{
|
|
foreignKeys.add(resultSet.getString(1));
|
|
}
|
|
assertThat(foreignKeys).containsExactly("cms_blog_post:3", "cms_blog_post_version:3", "cms_media:2", "cms_photo_album:5");
|
|
}
|
|
try(ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM information_schema.table_constraints WHERE constraint_type = 'CHECK' "
|
|
+ "AND constraint_name LIKE 'ck_cms_%'"))
|
|
{
|
|
assertThat(resultSet.next()).isTrue();
|
|
assertThat(resultSet.getInt(1)).isEqualTo(11);
|
|
}
|
|
try(ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM cms_media_type"))
|
|
{
|
|
assertThat(resultSet.next()).isTrue();
|
|
assertThat(resultSet.getInt(1)).isEqualTo(5);
|
|
}
|
|
}
|
|
|
|
CmsTestSupport.session(qInstance, CmsSecurity.permissionsFor(config, Set.of(CmsRole.ADMIN)));
|
|
Integer siteId = new InsertAction().execute(new InsertInput("cms_site").withRecord(new QRecord().withValue("name", "Example").withValue("shortName", "ex")))
|
|
.getRecords().get(0).getValueInteger("id");
|
|
Integer pageId = new InsertAction().execute(new InsertInput("cms_page").withRecord(new QRecord().withValue("name", "Home").withValue("slug", "home")
|
|
.withValue("title", "Home").withValue("pageTypeId", 1).withValue("siteId", siteId)
|
|
.withAssociatedRecord("sections", new QRecord().withValue("name", "Hero").withValue("sectionTypeId", 2).withValue("content", "{}")))).getRecords().get(0).getValueInteger("id");
|
|
QRecord page = new GetAction().execute(new GetInput("cms_page").withPrimaryKey(pageId).withIncludeAssociations(true)).getRecord();
|
|
assertThat(page.getAssociatedRecords().get("sections")).hasSize(1);
|
|
assertThat(page.getValue("createDate")).isNotNull();
|
|
}
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
** "name type nullable" for each column of a table.
|
|
*******************************************************************************/
|
|
private static List<String> columns(Statement statement, String table) throws Exception
|
|
{
|
|
List<String> rs = new ArrayList<>();
|
|
try(ResultSet resultSet = statement.executeQuery("SELECT column_name, data_type, character_maximum_length, is_nullable FROM information_schema.columns "
|
|
+ "WHERE table_name = '" + table + "' ORDER BY ordinal_position"))
|
|
{
|
|
while(resultSet.next())
|
|
{
|
|
Object length = resultSet.getObject(3);
|
|
rs.add(resultSet.getString(1) + " " + resultSet.getString(2) + (length == null ? "" : "(" + length + ")") + " " + resultSet.getString(4));
|
|
}
|
|
}
|
|
return (rs);
|
|
}
|
|
}
|