Java tutorial
/* * Copyright (C) 2016 FormKiQ Inc. * * 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.formkiq.core.dao; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.UUID; import javax.persistence.EntityManager; import org.hibernate.Session; import org.hibernate.metadata.ClassMetadata; import org.hibernate.persister.entity.AbstractEntityPersister; import org.springframework.beans.factory.annotation.Autowired; import com.formkiq.core.domain.Asset; import com.formkiq.core.domain.Folder; import com.formkiq.core.domain.FolderAccess; import com.formkiq.core.domain.User; import com.formkiq.core.domain.type.FolderPermission; import com.formkiq.core.domain.type.FolderStatus; import com.formkiq.core.domain.type.UserRole; import com.formkiq.core.domain.type.UserStatus; /** * Dao Helper Tests. * */ public abstract class DaoTests { /** Default EMAIL. */ private static final String EMAIL = "test@"; /** Default PASS. */ private static final String PASS = "pass"; /** EntityManager. */ @Autowired private EntityManager entityManager; /** FolderDao. */ @Autowired private FolderDao folderDao; /** AssetDao. */ @Autowired private AssetDao assetDao; /** UserDao. */ @Autowired private UserDao userDao; /** * Creates Asset. * @param s String * @return Asset */ protected Asset createAsset(final String s) { UUID uuid = UUID.randomUUID(); this.assetDao.saveAsset(uuid, s.getBytes(StandardCharsets.UTF_8)); return this.assetDao.findAsset(uuid); } /** * createDefaultUser(). * @return User */ protected User createDefaultUserAndSave() { UserStatus status = UserStatus.ACTIVE; User user = new User(EMAIL, PASS, status, UserRole.ROLE_USER); user.setClientid("clientid"); user = this.userDao.saveUser(user); return user; } /** * Create Folder. * @param user {@link User} * @param name {@link String} * @return UserClient */ protected Folder createFolder(final User user, final String name) { Folder folder = new Folder(); folder.setName(name); folder = this.folderDao.saveFolder(folder); FolderAccess access = new FolderAccess(); access.setStatus(FolderStatus.ACTIVE); access.setFolderid(folder.getFolderid()); access.setUserid(user.getUserid()); access.setPermissions(Arrays.asList(FolderPermission.PERM_FORM_ADMIN)); this.folderDao.saveFolderAccess(access); return folder; } /** * Create User. * @param client {@link String} * @param role {@link UserRole} * @return {@link User} */ protected User createUser(final String client, final UserRole role) { User user = new User("test", "", UserStatus.ACTIVE, role); user.setClientid(client); this.userDao.saveUser(user); return user; } /** * @return {@link EntityManager} */ public EntityManager getEntityManager() { return this.entityManager; } /** * List of User Tables. * @return {@link List} */ private List<String> getUserTables() { return Arrays.asList("users", "folders_access", "folders", "oauth_client_details", "oauth_access_token", "oauth_code", "oauth_refresh_token"); } /** * Truncate database. * @param em EntityManager */ @SuppressWarnings("resource") protected void truncateDatabase(final EntityManager em) { List<String> tableNames = new ArrayList<>(); Session session = em.unwrap(Session.class); Map<String, ClassMetadata> hibernateMetadata = session.getSessionFactory().getAllClassMetadata(); List<String> userTables = getUserTables(); for (ClassMetadata metadata : hibernateMetadata.values()) { AbstractEntityPersister aep = (AbstractEntityPersister) metadata; if (!userTables.contains(aep.getTableName())) { tableNames.add(aep.getTableName()); } } truncateTables(em, tableNames); truncateTables(em, getUserTables()); } /** * Truncate List of tables. * @param em {@link EntityManager} * @param tableNames {@link List} */ protected void truncateTables(final EntityManager em, final List<String> tableNames) { for (String tn : tableNames) { em.createNativeQuery("TRUNCATE TABLE " + tn).executeUpdate(); } em.flush(); } }