Fix tenant id constraint in DM_TAG table

master
Gimhan Wijayawardana 3 months ago
parent 295f1831d8
commit 8d2fb5fa76

@ -48,6 +48,4 @@ public class TagInfoList extends BasePaginatedResult {
sb.append("]}\n");
return sb.toString();
}
}

@ -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<Tag> createTagList3() {
List<Tag> tagList = new ArrayList<>();
tagList.add(getTag1Dao());
tagList.add(getTag2Dao());
return tagList;
}
public static GroupPaginationRequest createPaginationRequest(){
GroupPaginationRequest request = new GroupPaginationRequest(0, 5);
return request;

@ -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";

@ -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<Tag> 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<Tag> 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<Tag> 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();
}
}
}

@ -33,6 +33,7 @@
<class name="io.entgra.device.mgt.core.device.mgt.core.app.mgt.AppManagementConfigurationManagerTest"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.search.DeviceDetails"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.dao.GroupPersistTests"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.dao.TagPersistTests"/>
</classes>
</test>

@ -33,6 +33,7 @@
<class name="io.entgra.device.mgt.core.device.mgt.core.app.mgt.AppManagementConfigurationManagerTest"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.search.DeviceDetails"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.dao.GroupPersistTests"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.dao.TagPersistTests"/>
</classes>
</test>

@ -33,6 +33,7 @@
<class name="io.entgra.device.mgt.core.device.mgt.core.app.mgt.AppManagementConfigurationManagerTest"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.search.DeviceDetails"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.dao.GroupPersistTests"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.dao.TagPersistTests"/>
</classes>
</test>

@ -33,6 +33,7 @@
<class name="io.entgra.device.mgt.core.device.mgt.core.app.mgt.AppManagementConfigurationManagerTest"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.search.DeviceDetails"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.dao.GroupPersistTests"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.dao.TagPersistTests"/>
</classes>
</test>

@ -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 --

@ -33,6 +33,7 @@
<class name="io.entgra.device.mgt.core.device.mgt.core.app.mgt.AppManagementConfigurationManagerTest"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.search.DeviceDetails"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.dao.GroupPersistTests"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.dao.TagPersistTests"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.dao.EnrolmentPersistenceTests"/>
<class name="io.entgra.device.mgt.core.device.mgt.core.dao.DeviceStatusPersistenceTests"/>
</classes>

@ -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 --

@ -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 --

@ -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 --

@ -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 --

@ -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 --

Loading…
Cancel
Save