diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/TestUtils.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/TestUtils.java index b477cd2a53..a787f0b30e 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/TestUtils.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/TestUtils.java @@ -106,8 +106,17 @@ public class TestUtils { } public static Tag getTag2() { + return new Tag( 2, "tag2", "This is tag2"); + } + + public static Tag getTag1Dao() { + return new Tag("tag1", "This is tag1"); + } + + public static Tag getTag2Dao() { return new Tag( "tag2", "This is tag2"); } + public static Tag getTag3() { return new Tag("tag3", "This is tag3"); } @@ -125,6 +134,13 @@ public class TestUtils { return tagList; } + public static List createTagList3() { + List tagList = new ArrayList<>(); + tagList.add(getTag1Dao()); + tagList.add(getTag2Dao()); + return tagList; + } + public static GroupPaginationRequest createPaginationRequest(){ GroupPaginationRequest request = new GroupPaginationRequest(0, 5); return request; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/common/TestDataHolder.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/common/TestDataHolder.java index cffbb3389a..579678ec97 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/common/TestDataHolder.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/common/TestDataHolder.java @@ -41,6 +41,8 @@ public class TestDataHolder { public final static String TEST_DEVICE_TYPE = "TEST-DEVICE-TYPE"; public final static Integer SUPER_TENANT_ID = -1234; + public final static Integer ALTERNATE_TENANT_ID = -1235; + public final static Integer ALTERNATE_TENANT_ID_1 = -1236; public final static String SUPER_TENANT_DOMAIN = "carbon.super"; public final static String initialDeviceIdentifier = "12345"; public final static String initialDeviceName = "TEST-DEVICE"; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/TagPersistTests.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/TagPersistTests.java new file mode 100644 index 0000000000..a18049fa3f --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/java/io/entgra/device/mgt/core/device/mgt/core/dao/TagPersistTests.java @@ -0,0 +1,216 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you 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 io.entgra.device.mgt.core.device.mgt.core.dao; + +import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException; +import io.entgra.device.mgt.core.device.mgt.common.tag.mgt.Tag; +import io.entgra.device.mgt.core.device.mgt.core.TestUtils; +import io.entgra.device.mgt.core.device.mgt.core.common.BaseDeviceManagementTest; +import io.entgra.device.mgt.core.device.mgt.core.common.TestDataHolder; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import java.util.List; + +public class TagPersistTests extends BaseDeviceManagementTest { + + private TagDAO tagDAO; + private static final Log log = LogFactory.getLog(TagPersistTests.class); + + @BeforeClass + @Override + public void init() throws Exception { + initDataSource(); + tagDAO = DeviceManagementDAOFactory.getTagDAO(); + } + + @Test + public void addTag() { + try { + DeviceManagementDAOFactory.beginTransaction(); + tagDAO.addTags(TestUtils.createTagList3(), TestDataHolder.ALTERNATE_TENANT_ID); + log.debug("Tags added to the database"); + Tag tag = tagDAO.getTagByName("tag1", TestDataHolder.ALTERNATE_TENANT_ID); + DeviceManagementDAOFactory.commitTransaction(); + Assert.assertNotNull(tag, "Tag should be added and retrieved."); + Assert.assertEquals(tag.getName(), "tag1", "Tag name mismatch."); + } catch (TagManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while adding tag list type."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "addTag") + public void updateTag() { + try { + DeviceManagementDAOFactory.beginTransaction(); + String updatedDescription = "Updated Description"; + Tag tag = tagDAO.getTagByName("tag1", TestDataHolder.ALTERNATE_TENANT_ID); + Tag tagToUpdate = new Tag(tag.getId(), tag.getName(), updatedDescription); + tagDAO.updateTag(tagToUpdate, TestDataHolder.ALTERNATE_TENANT_ID); + log.debug("Tag updated in the database"); + tag = tagDAO.getTagByName("tag1", TestDataHolder.ALTERNATE_TENANT_ID); + DeviceManagementDAOFactory.commitTransaction(); + Assert.assertEquals(tag.getDescription(), updatedDescription, "Tag description mismatch."); + } catch (TagManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while updating tag."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "updateTag") + public void deleteTag() { + try { + DeviceManagementDAOFactory.beginTransaction(); + tagDAO.deleteTag(1, TestDataHolder.ALTERNATE_TENANT_ID); + log.debug("Tag deleted from the database"); + Tag deletedTag = tagDAO.getTagById(1, TestDataHolder.ALTERNATE_TENANT_ID); + DeviceManagementDAOFactory.commitTransaction(); + Assert.assertNull(deletedTag, "Tag should be deleted."); + } catch (TagManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while deleting tag."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"deleteTag"}) + public void getTags() { + try { + DeviceManagementDAOFactory.beginTransaction(); + List tags = tagDAO.getTags(TestDataHolder.ALTERNATE_TENANT_ID); + DeviceManagementDAOFactory.commitTransaction(); + log.debug("Tags retrieved successfully."); + Assert.assertEquals(tags.size(), 2); + } catch (TagManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while retrieving tags."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = {"getTags"}) + public void getTagByName() { + try { + String tagName = "tag2"; + DeviceManagementDAOFactory.beginTransaction(); + Tag tag = tagDAO.getTagByName(tagName, TestDataHolder.ALTERNATE_TENANT_ID); + DeviceManagementDAOFactory.commitTransaction(); + log.debug("Tag " + tagName + " retrieved successfully."); + Assert.assertEquals(tag.getName(), "tag2"); + } catch (TagManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while retrieving tag by Id."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "getTagByName") + public void addTagsForAlternateTenant() { + try { + DeviceManagementDAOFactory.beginTransaction(); + //Here, adding a same tag name for a separate tenant is tested. + tagDAO.addTags(TestUtils.createTagList3(), TestDataHolder.ALTERNATE_TENANT_ID_1); + log.debug("Tags added for a alternate tenant"); + List tagList = tagDAO.getTags(TestDataHolder.ALTERNATE_TENANT_ID_1); + DeviceManagementDAOFactory.commitTransaction(); + Assert.assertEquals(tagList.size(), 2, "Tag count mismatch."); + } catch (TagManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while adding tags for a different tenant."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Test(dependsOnMethods = "addTagsForAlternateTenant") + public void getTagsOfAlternateTenant() { + try { + DeviceManagementDAOFactory.beginTransaction(); + List tagList = tagDAO.getTags(TestDataHolder.ALTERNATE_TENANT_ID_1); + log.debug("Tags retrieved for a alternate tenant " + TestDataHolder.ALTERNATE_TENANT_ID_1); + DeviceManagementDAOFactory.commitTransaction(); + Assert.assertEquals(tagList.size(), 2, "Tag count mismatch."); + } catch (TagManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while adding tags for a different tenant."; + log.error(msg, e); + Assert.fail(msg, e); + } catch (TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + Assert.fail(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/mssql-testng.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/mssql-testng.xml index 2015c5eb5e..51d60a2aee 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/mssql-testng.xml +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/mssql-testng.xml @@ -33,6 +33,7 @@ + diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/mysql-testng.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/mysql-testng.xml index 190802eb51..3959510cbd 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/mysql-testng.xml +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/mysql-testng.xml @@ -33,6 +33,7 @@ + diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/oracle-testng.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/oracle-testng.xml index e4b774a06f..8650e43b96 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/oracle-testng.xml +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/oracle-testng.xml @@ -33,6 +33,7 @@ + diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/postgre-testng.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/postgre-testng.xml index a4839a1a0d..b4bfc29da1 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/postgre-testng.xml +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/postgre-testng.xml @@ -33,6 +33,7 @@ + diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql index 6ed4f5fddd..9fded58e81 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql @@ -693,7 +693,7 @@ CREATE TABLE IF NOT EXISTS DM_TAG ( DESCRIPTION VARCHAR(255) NULL, TENANT_ID INTEGER NOT NULL, PRIMARY KEY (ID), - CONSTRAINT DM_TAG_NAME_UNIQUE UNIQUE (NAME) + CONSTRAINT DM_TAG_NAME_TENANT_UNIQUE UNIQUE (NAME, TENANT_ID) ); -- END OF DM_TAG TABLE -- diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/testng.xml index 58789e4fd9..a51df0bc38 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/testng.xml @@ -33,6 +33,7 @@ + diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 02baa647b5..446eba4794 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -882,7 +882,7 @@ CREATE TABLE IF NOT EXISTS DM_TAG ( DESCRIPTION VARCHAR(255) NULL, TENANT_ID INTEGER NOT NULL, PRIMARY KEY (ID), - CONSTRAINT DM_TAG_NAME_UNIQUE UNIQUE (NAME) + CONSTRAINT DM_TAG_NAME_TENANT_UNIQUE UNIQUE (NAME, TENANT_ID) ); -- END OF DM_TAG TABLE -- diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 4f5bd193ea..dc39bb4d16 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -958,7 +958,7 @@ CREATE TABLE IF NOT EXISTS DM_TAG ( NAME NVARCHAR(255) NOT NULL, DESCRIPTION NVARCHAR(255) NULL, TENANT_ID INT NOT NULL, - CONSTRAINT DM_TAG_NAME_UNIQUE UNIQUE (NAME) + CONSTRAINT DM_TAG_NAME_TENANT_UNIQUE UNIQUE (NAME, TENANT_ID) ); -- END OF DM_TAG TABLE -- diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 89f43bad0d..a943265bdb 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -957,7 +957,7 @@ CREATE TABLE IF NOT EXISTS DM_TAG ( NAME VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255) NULL, TENANT_ID INT NOT NULL, - CONSTRAINT DM_TAG_NAME_UNIQUE UNIQUE (NAME) + CONSTRAINT DM_TAG_NAME_TENANT_UNIQUE UNIQUE (NAME, TENANT_ID) ); -- END OF DM_TAG TABLE -- diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index fed865804a..2fa51b9d49 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -1262,7 +1262,7 @@ CREATE TABLE DM_TAG ( NAME VARCHAR2(255) NOT NULL, DESCRIPTION VARCHAR2(255) NULL, TENANT_ID NUMBER(10) NOT NULL, - CONSTRAINT DM_TAG_NAME_UNIQUE UNIQUE (NAME) + CONSTRAINT DM_TAG_NAME_TENANT_UNIQUE UNIQUE (NAME, TENANT_ID) ); -- END OF DM_TAG TABLE -- diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index baeee662a5..d9bce9fd04 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -888,7 +888,7 @@ CREATE TABLE IF NOT EXISTS DM_TAG ( NAME VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255) NULL, TENANT_ID INTEGER NOT NULL, - CONSTRAINT DM_TAG_NAME_UNIQUE UNIQUE (NAME) + CONSTRAINT DM_TAG_NAME_TENANT_UNIQUE UNIQUE (NAME, TENANT_ID) ); -- END OF DM_TAG TABLE --