21 changed files with 837 additions and 235 deletions
@ -0,0 +1,140 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* 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 org.thingsboard.server.dao.sql.plugin; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
|
import org.springframework.data.repository.CrudRepository; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.thingsboard.server.common.data.id.PluginId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.page.TextPageLink; |
||||
|
import org.thingsboard.server.common.data.plugin.PluginMetaData; |
||||
|
import org.thingsboard.server.dao.DaoUtil; |
||||
|
import org.thingsboard.server.dao.model.ModelConstants; |
||||
|
import org.thingsboard.server.dao.model.sql.PluginMetaDataEntity; |
||||
|
import org.thingsboard.server.dao.plugin.PluginDao; |
||||
|
import org.thingsboard.server.dao.sql.JpaAbstractDao; |
||||
|
|
||||
|
import java.util.Arrays; |
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 5/1/2017. |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Component |
||||
|
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) |
||||
|
public class JpaBasePluginDao extends JpaAbstractDao<PluginMetaDataEntity, PluginMetaData> implements PluginDao { |
||||
|
|
||||
|
@Autowired |
||||
|
private PluginMetaDataRepository pluginMetaDataRepository; |
||||
|
|
||||
|
@Override |
||||
|
protected Class<PluginMetaDataEntity> getEntityClass() { |
||||
|
return PluginMetaDataEntity.class; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected String getColumnFamilyName() { |
||||
|
return ModelConstants.PLUGIN_COLUMN_FAMILY_NAME; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected CrudRepository<PluginMetaDataEntity, UUID> getCrudRepository() { |
||||
|
return pluginMetaDataRepository; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected boolean isSearchTextDao() { |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public PluginMetaData findById(PluginId pluginId) { |
||||
|
log.debug("Search plugin meta-data entity by id [{}]", pluginId); |
||||
|
PluginMetaData pluginMetaData = super.findById(pluginId.getId()); |
||||
|
if (log.isTraceEnabled()) { |
||||
|
log.trace("Search result: [{}] for plugin entity [{}]", pluginMetaData != null, pluginMetaData); |
||||
|
} else { |
||||
|
log.debug("Search result: [{}]", pluginMetaData != null); |
||||
|
} |
||||
|
return pluginMetaData; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public PluginMetaData findByApiToken(String apiToken) { |
||||
|
log.debug("Search plugin meta-data entity by api token [{}]", apiToken); |
||||
|
PluginMetaDataEntity entity = pluginMetaDataRepository.findByApiToken(apiToken); |
||||
|
if (log.isTraceEnabled()) { |
||||
|
log.trace("Search result: [{}] for plugin entity [{}]", entity != null, entity); |
||||
|
} else { |
||||
|
log.debug("Search result: [{}]", entity != null); |
||||
|
} |
||||
|
return DaoUtil.getData(entity); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void deleteById(UUID id) { |
||||
|
log.debug("Delete plugin meta-data entity by id [{}]", id); |
||||
|
pluginMetaDataRepository.delete(id); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void deleteById(PluginId pluginId) { |
||||
|
deleteById(pluginId.getId()); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<PluginMetaData> findByTenantIdAndPageLink(TenantId tenantId, TextPageLink pageLink) { |
||||
|
log.debug("Try to find здгпшты by tenantId [{}] and pageLink [{}]", tenantId, pageLink); |
||||
|
List<PluginMetaDataEntity> entities; |
||||
|
if (pageLink.getIdOffset() == null) { |
||||
|
entities = pluginMetaDataRepository |
||||
|
.findByTenantIdAndPageLinkFirstPage(pageLink.getLimit(), tenantId.getId(), pageLink.getTextSearch()); |
||||
|
} else { |
||||
|
entities = pluginMetaDataRepository |
||||
|
.findByTenantIdAndPageLinkNextPage(pageLink.getLimit(), tenantId.getId(), pageLink.getTextSearch(), pageLink.getIdOffset()); |
||||
|
} |
||||
|
if (log.isTraceEnabled()) { |
||||
|
log.trace("Search result: [{}]", Arrays.toString(entities.toArray())); |
||||
|
} else { |
||||
|
log.debug("Search result: [{}]", entities.size()); |
||||
|
} |
||||
|
return DaoUtil.convertDataList(entities); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<PluginMetaData> findAllTenantPluginsByTenantId(UUID tenantId, TextPageLink pageLink) { |
||||
|
log.debug("Try to find all tenant plugins by tenantId [{}] and pageLink [{}]", tenantId, pageLink); |
||||
|
List<PluginMetaDataEntity> entities; |
||||
|
if (pageLink.getIdOffset() == null) { |
||||
|
entities = pluginMetaDataRepository |
||||
|
.findAllTenantPluginsByTenantIdFirstPage(pageLink.getLimit(), tenantId, pageLink.getTextSearch()); |
||||
|
} else { |
||||
|
entities = pluginMetaDataRepository |
||||
|
.findAllTenantPluginsByTenantIdNextPage(pageLink.getLimit(), tenantId, pageLink.getTextSearch(), pageLink.getIdOffset()); |
||||
|
} |
||||
|
if (log.isTraceEnabled()) { |
||||
|
log.trace("Search result: [{}]", Arrays.toString(entities.toArray())); |
||||
|
} else { |
||||
|
log.debug("Search result: [{}]", entities.size()); |
||||
|
} |
||||
|
return DaoUtil.convertDataList(entities); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,55 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* 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 org.thingsboard.server.dao.sql.plugin; |
||||
|
|
||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.data.repository.CrudRepository; |
||||
|
import org.thingsboard.server.common.data.plugin.PluginMetaData; |
||||
|
import org.thingsboard.server.dao.model.sql.PluginMetaDataEntity; |
||||
|
import org.thingsboard.server.dao.model.sql.RuleMetaDataEntity; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 5/1/2017. |
||||
|
*/ |
||||
|
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) |
||||
|
public interface PluginMetaDataRepository extends CrudRepository<PluginMetaDataEntity, UUID> { |
||||
|
|
||||
|
PluginMetaDataEntity findByApiToken(String apiToken); |
||||
|
|
||||
|
@Query(nativeQuery = true, value = "SELECT * FROM PLUGIN WHERE TENANT_ID = ?2 " + |
||||
|
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + |
||||
|
"ORDER BY ID LIMIT ?1") |
||||
|
List<PluginMetaDataEntity> findByTenantIdAndPageLinkFirstPage(int limit, UUID tenantId, String textSearch); |
||||
|
|
||||
|
@Query(nativeQuery = true, value = "SELECT * FROM PLUGIN WHERE TENANT_ID = ?2 " + |
||||
|
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + |
||||
|
"AND ID > ?4 ORDER BY ID LIMIT ?1") |
||||
|
List<PluginMetaDataEntity> findByTenantIdAndPageLinkNextPage(int limit, UUID tenantId, String textSearch, UUID idOffset); |
||||
|
|
||||
|
@Query(nativeQuery = true, value = "SELECT * FROM PLUGIN WHERE (TENANT_ID = ?2 OR TENANT_ID IS NULL) " + |
||||
|
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + |
||||
|
"ORDER BY ID LIMIT ?1") |
||||
|
List<PluginMetaDataEntity> findAllTenantPluginsByTenantIdFirstPage(int limit, UUID tenantId, String textSearch); |
||||
|
|
||||
|
@Query(nativeQuery = true, value = "SELECT * FROM PLUGIN WHERE (TENANT_ID = ?2 OR TENANT_ID IS NULL) " + |
||||
|
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + |
||||
|
"AND ID > ?4 ORDER BY ID LIMIT ?1") |
||||
|
List<PluginMetaDataEntity> findAllTenantPluginsByTenantIdNextPage(int limit, UUID tenantId, String textSearch, UUID idOffset); |
||||
|
} |
||||
@ -0,0 +1,126 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* 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 org.thingsboard.server.dao.sql.rule; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
|
import org.springframework.data.repository.CrudRepository; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
import org.thingsboard.server.common.data.id.RuleId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.page.TextPageLink; |
||||
|
import org.thingsboard.server.common.data.rule.RuleMetaData; |
||||
|
import org.thingsboard.server.dao.DaoUtil; |
||||
|
import org.thingsboard.server.dao.model.ModelConstants; |
||||
|
import org.thingsboard.server.dao.model.sql.RuleMetaDataEntity; |
||||
|
import org.thingsboard.server.dao.rule.RuleDao; |
||||
|
import org.thingsboard.server.dao.sql.JpaAbstractDao; |
||||
|
|
||||
|
import java.util.Arrays; |
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 4/30/2017. |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Component |
||||
|
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) |
||||
|
public class JpaBaseRuleDao extends JpaAbstractDao<RuleMetaDataEntity, RuleMetaData> implements RuleDao { |
||||
|
|
||||
|
@Autowired |
||||
|
private RuleMetaDataRepository ruleMetaDataRepository; |
||||
|
|
||||
|
@Override |
||||
|
protected Class<RuleMetaDataEntity> getEntityClass() { |
||||
|
return RuleMetaDataEntity.class; |
||||
|
} |
||||
|
@Override |
||||
|
protected String getColumnFamilyName() { |
||||
|
return ModelConstants.RULE_COLUMN_FAMILY_NAME; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected CrudRepository<RuleMetaDataEntity, UUID> getCrudRepository() { |
||||
|
return ruleMetaDataRepository; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected boolean isSearchTextDao() { |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public RuleMetaData findById(RuleId ruleId) { |
||||
|
return findById(ruleId.getId()); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<RuleMetaData> findRulesByPlugin(String pluginToken) { |
||||
|
log.debug("Search rules by api token [{}]", pluginToken); |
||||
|
return DaoUtil.convertDataList(ruleMetaDataRepository.findByPluginToken(pluginToken)); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<RuleMetaData> findByTenantIdAndPageLink(TenantId tenantId, TextPageLink pageLink) { |
||||
|
log.debug("Try to find rules by tenantId [{}] and pageLink [{}]", tenantId, pageLink); |
||||
|
List<RuleMetaDataEntity> entities; |
||||
|
if (pageLink.getIdOffset() == null) { |
||||
|
entities = ruleMetaDataRepository |
||||
|
.findByTenantIdAndPageLinkFirstPage(pageLink.getLimit(), tenantId.getId(), pageLink.getTextSearch()); |
||||
|
} else { |
||||
|
entities = ruleMetaDataRepository |
||||
|
.findByTenantIdAndPageLinkNextPage(pageLink.getLimit(), tenantId.getId(), pageLink.getTextSearch(), pageLink.getIdOffset()); |
||||
|
} |
||||
|
if (log.isTraceEnabled()) { |
||||
|
log.trace("Search result: [{}]", Arrays.toString(entities.toArray())); |
||||
|
} else { |
||||
|
log.debug("Search result: [{}]", entities.size()); |
||||
|
} |
||||
|
return DaoUtil.convertDataList(entities); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public List<RuleMetaData> findAllTenantRulesByTenantId(UUID tenantId, TextPageLink pageLink) { |
||||
|
log.debug("Try to find all tenant rules by tenantId [{}] and pageLink [{}]", tenantId, pageLink); |
||||
|
List<RuleMetaDataEntity> entities; |
||||
|
if (pageLink.getIdOffset() == null) { |
||||
|
entities = ruleMetaDataRepository |
||||
|
.findAllTenantRulesByTenantIdFirstPage(pageLink.getLimit(), tenantId, pageLink.getTextSearch()); |
||||
|
} else { |
||||
|
entities = ruleMetaDataRepository |
||||
|
.findAllTenantRulesByTenantIdNextPage(pageLink.getLimit(), tenantId, pageLink.getTextSearch(), pageLink.getIdOffset()); |
||||
|
} |
||||
|
if (log.isTraceEnabled()) { |
||||
|
log.trace("Search result: [{}]", Arrays.toString(entities.toArray())); |
||||
|
} else { |
||||
|
log.debug("Search result: [{}]", entities.size()); |
||||
|
} |
||||
|
return DaoUtil.convertDataList(entities); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void deleteById(UUID id) { |
||||
|
log.debug("Delete rule meta-data entity by id [{}]", id); |
||||
|
ruleMetaDataRepository.delete(id); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void deleteById(RuleId ruleId) { |
||||
|
deleteById(ruleId.getId()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,56 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* 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 org.thingsboard.server.dao.sql.rule; |
||||
|
|
||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
|
import org.springframework.data.jpa.repository.Query; |
||||
|
import org.springframework.data.repository.CrudRepository; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.dao.model.ToData; |
||||
|
import org.thingsboard.server.dao.model.sql.RuleMetaDataEntity; |
||||
|
|
||||
|
import java.util.Collection; |
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 4/30/2017. |
||||
|
*/ |
||||
|
@ConditionalOnProperty(prefix = "sql", value = "enabled", havingValue = "true", matchIfMissing = false) |
||||
|
public interface RuleMetaDataRepository extends CrudRepository<RuleMetaDataEntity, UUID> { |
||||
|
|
||||
|
List<RuleMetaDataEntity> findByPluginToken(String pluginToken); |
||||
|
|
||||
|
@Query(nativeQuery = true, value = "SELECT * FROM RULE WHERE TENANT_ID = ?2 " + |
||||
|
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + |
||||
|
"ORDER BY ID LIMIT ?1") |
||||
|
List<RuleMetaDataEntity> findByTenantIdAndPageLinkFirstPage(int limit, UUID tenantId, String textSearch); |
||||
|
|
||||
|
@Query(nativeQuery = true, value = "SELECT * FROM RULE WHERE TENANT_ID = ?2 " + |
||||
|
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + |
||||
|
"AND ID > ?4 ORDER BY ID LIMIT ?1") |
||||
|
List<RuleMetaDataEntity> findByTenantIdAndPageLinkNextPage(int limit, UUID tenantId, String textSearch, UUID idOffset); |
||||
|
|
||||
|
@Query(nativeQuery = true, value = "SELECT * FROM RULE WHERE (TENANT_ID = ?2 OR TENANT_ID IS NULL) " + |
||||
|
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + |
||||
|
"ORDER BY ID LIMIT ?1") |
||||
|
List<RuleMetaDataEntity> findAllTenantRulesByTenantIdFirstPage(int limit, UUID tenantId, String textSearch); |
||||
|
|
||||
|
@Query(nativeQuery = true, value = "SELECT * FROM RULE WHERE (TENANT_ID = ?2 OR TENANT_ID IS NULL) " + |
||||
|
"AND LOWER(SEARCH_TEXT) LIKE LOWER(CONCAT(?3, '%')) " + |
||||
|
"AND ID > ?4 ORDER BY ID LIMIT ?1") |
||||
|
List<RuleMetaDataEntity> findAllTenantRulesByTenantIdNextPage(int limit, UUID tenantId, String textSearch, UUID idOffset); |
||||
|
} |
||||
@ -0,0 +1,104 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* 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 org.thingsboard.server.dao.sql.plugin; |
||||
|
|
||||
|
import com.datastax.driver.core.utils.UUIDs; |
||||
|
import com.github.springtestdbunit.annotation.DatabaseSetup; |
||||
|
import org.junit.Test; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.thingsboard.server.common.data.id.PluginId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.page.TextPageLink; |
||||
|
import org.thingsboard.server.common.data.plugin.PluginMetaData; |
||||
|
import org.thingsboard.server.common.data.rule.RuleMetaData; |
||||
|
import org.thingsboard.server.dao.AbstractJpaDaoTest; |
||||
|
import org.thingsboard.server.dao.plugin.PluginDao; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
import static org.junit.Assert.assertEquals; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 5/1/2017. |
||||
|
*/ |
||||
|
public class JpaBasePluginDaoTest extends AbstractJpaDaoTest { |
||||
|
|
||||
|
@Autowired |
||||
|
private PluginDao pluginDao; |
||||
|
|
||||
|
@Test |
||||
|
@DatabaseSetup("classpath:dbunit/empty_dataset.xml") |
||||
|
public void testFindByTenantIdAndPageLink() { |
||||
|
UUID tenantId1 = UUIDs.timeBased(); |
||||
|
UUID tenantId2 = UUIDs.timeBased(); |
||||
|
createPluginsTwoTenants(tenantId1, tenantId2, "plugin_"); |
||||
|
assertEquals(60, pluginDao.find().size()); |
||||
|
List<PluginMetaData> rules1 = pluginDao.findByTenantIdAndPageLink( |
||||
|
new TenantId(tenantId1), new TextPageLink(20, "plugin_")); |
||||
|
assertEquals(20, rules1.size()); |
||||
|
|
||||
|
List<PluginMetaData> rules2 = pluginDao.findByTenantIdAndPageLink(new TenantId(tenantId1), |
||||
|
new TextPageLink(20, "plugin_", rules1.get(19).getId().getId(), null)); |
||||
|
assertEquals(10, rules2.size()); |
||||
|
|
||||
|
List<PluginMetaData> rules3 = pluginDao.findByTenantIdAndPageLink(new TenantId(tenantId1), |
||||
|
new TextPageLink(20, "plugin_", rules2.get(9).getId().getId(), null)); |
||||
|
assertEquals(0, rules3.size()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
@DatabaseSetup("classpath:dbunit/empty_dataset.xml") |
||||
|
public void testFindAllTenantRulesByTenantId() { |
||||
|
UUID tenantId1 = UUIDs.timeBased(); |
||||
|
UUID tenantId2 = UUIDs.timeBased(); |
||||
|
createTenantsAndSystemPlugins(tenantId1, tenantId2, "name_"); |
||||
|
List<PluginMetaData> rules1 = pluginDao.findAllTenantPluginsByTenantId( |
||||
|
tenantId1, new TextPageLink(40, "name_")); |
||||
|
assertEquals(40, rules1.size()); |
||||
|
|
||||
|
List<PluginMetaData> rules2 = pluginDao.findAllTenantPluginsByTenantId(tenantId1, |
||||
|
new TextPageLink(40, "name_", rules1.get(39).getId().getId(), null)); |
||||
|
assertEquals(20, rules2.size()); |
||||
|
|
||||
|
List<PluginMetaData> rules3 = pluginDao.findAllTenantPluginsByTenantId(tenantId1, |
||||
|
new TextPageLink(40, "name_", rules2.get(19).getId().getId(), null)); |
||||
|
assertEquals(0, rules3.size()); |
||||
|
} |
||||
|
|
||||
|
private void createTenantsAndSystemPlugins(UUID tenantId1, UUID tenantId2, String namePrefix) { |
||||
|
for (int i = 0; i < 30; i++) { |
||||
|
createPlugin(tenantId1, namePrefix, i); |
||||
|
createPlugin(tenantId2, namePrefix, i); |
||||
|
createPlugin(null, namePrefix, i); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void createPluginsTwoTenants(UUID tenantId1, UUID tenantId2, String namePrefix) { |
||||
|
for (int i = 0; i < 30; i++) { |
||||
|
createPlugin(tenantId1, namePrefix, i); |
||||
|
createPlugin(tenantId2, namePrefix, i); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void createPlugin(UUID tenantId, String namePrefix, int i) { |
||||
|
PluginMetaData plugin = new PluginMetaData(); |
||||
|
plugin.setId(new PluginId(UUIDs.timeBased())); |
||||
|
plugin.setTenantId(new TenantId(tenantId)); |
||||
|
plugin.setName(namePrefix + i); |
||||
|
pluginDao.save(plugin); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,110 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* 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 org.thingsboard.server.dao.sql.rule; |
||||
|
|
||||
|
import com.datastax.driver.core.utils.UUIDs; |
||||
|
import com.github.springtestdbunit.annotation.DatabaseSetup; |
||||
|
import org.junit.Test; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.thingsboard.server.common.data.id.RuleId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.page.TextPageLink; |
||||
|
import org.thingsboard.server.common.data.rule.RuleMetaData; |
||||
|
import org.thingsboard.server.dao.AbstractJpaDaoTest; |
||||
|
import org.thingsboard.server.dao.rule.RuleDao; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
import static org.junit.Assert.assertEquals; |
||||
|
import static org.junit.Assert.assertNotNull; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 4/30/2017. |
||||
|
*/ |
||||
|
public class JpaBaseRuleDaoTest extends AbstractJpaDaoTest { |
||||
|
|
||||
|
@Autowired |
||||
|
private RuleDao jpaBaseRuleDao; |
||||
|
|
||||
|
@Test |
||||
|
@DatabaseSetup("classpath:dbunit/rule.xml") |
||||
|
public void testFindRulesByPlugin() { |
||||
|
assertEquals(3, jpaBaseRuleDao.findRulesByPlugin("token_1").size()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
@DatabaseSetup("classpath:dbunit/empty_dataset.xml") |
||||
|
public void testFindByTenantIdAndPageLink() { |
||||
|
UUID tenantId1 = UUIDs.timeBased(); |
||||
|
UUID tenantId2 = UUIDs.timeBased(); |
||||
|
createRulesTwoTenants(tenantId1, tenantId2, "name_", "token"); |
||||
|
List<RuleMetaData> rules1 = jpaBaseRuleDao.findByTenantIdAndPageLink( |
||||
|
new TenantId(tenantId1), new TextPageLink(20, "name_")); |
||||
|
assertEquals(20, rules1.size()); |
||||
|
|
||||
|
List<RuleMetaData> rules2 = jpaBaseRuleDao.findByTenantIdAndPageLink(new TenantId(tenantId1), |
||||
|
new TextPageLink(20, "name_", rules1.get(19).getId().getId(), null)); |
||||
|
assertEquals(10, rules2.size()); |
||||
|
|
||||
|
List<RuleMetaData> rules3 = jpaBaseRuleDao.findByTenantIdAndPageLink(new TenantId(tenantId1), |
||||
|
new TextPageLink(20, "name_", rules2.get(9).getId().getId(), null)); |
||||
|
assertEquals(0, rules3.size()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
@DatabaseSetup("classpath:dbunit/empty_dataset.xml") |
||||
|
public void testFindAllTenantRulesByTenantId() { |
||||
|
UUID tenantId1 = UUIDs.timeBased(); |
||||
|
UUID tenantId2 = UUIDs.timeBased(); |
||||
|
createTenantsAndSystemRules(tenantId1, tenantId2, "name_", "token"); |
||||
|
List<RuleMetaData> rules1 = jpaBaseRuleDao.findAllTenantRulesByTenantId( |
||||
|
tenantId1, new TextPageLink(40, "name_")); |
||||
|
assertEquals(40, rules1.size()); |
||||
|
|
||||
|
List<RuleMetaData> rules2 = jpaBaseRuleDao.findAllTenantRulesByTenantId(tenantId1, |
||||
|
new TextPageLink(40, "name_", rules1.get(39).getId().getId(), null)); |
||||
|
assertEquals(20, rules2.size()); |
||||
|
|
||||
|
List<RuleMetaData> rules3 = jpaBaseRuleDao.findAllTenantRulesByTenantId(tenantId1, |
||||
|
new TextPageLink(40, "name_", rules2.get(19).getId().getId(), null)); |
||||
|
assertEquals(0, rules3.size()); |
||||
|
} |
||||
|
|
||||
|
private void createRulesTwoTenants(UUID tenantId1, UUID tenantId2, String namePrefix, String pluginToken) { |
||||
|
for (int i = 0; i < 30; i++) { |
||||
|
createRule(tenantId1, namePrefix, pluginToken, i); |
||||
|
createRule(tenantId2, namePrefix, pluginToken, i); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void createTenantsAndSystemRules(UUID tenantId1, UUID tenantId2, String namePrefix, String pluginToken) { |
||||
|
for (int i = 0; i < 30; i++) { |
||||
|
createRule(tenantId1, namePrefix, pluginToken, i); |
||||
|
createRule(tenantId2, namePrefix, pluginToken, i); |
||||
|
createRule(null, namePrefix, pluginToken, i); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void createRule(UUID tenantId, String namePrefix, String pluginToken, int i) { |
||||
|
RuleMetaData ruleMetaData = new RuleMetaData(); |
||||
|
ruleMetaData.setId(new RuleId(UUIDs.timeBased())); |
||||
|
ruleMetaData.setTenantId(new TenantId(tenantId)); |
||||
|
ruleMetaData.setName(namePrefix + i); |
||||
|
ruleMetaData.setPluginToken(pluginToken); |
||||
|
jpaBaseRuleDao.save(ruleMetaData); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
<dataset> |
||||
|
<rule |
||||
|
id="uuid'8e834e11-2e0a-11e7-83ec-f7f6dbfb4833'" |
||||
|
tenant_id="uuid'8e834e10-2e0a-11e7-83ec-f7f6dbfb4833'" |
||||
|
name="RULE_11" |
||||
|
weight="1" |
||||
|
search_text="rule_11" |
||||
|
plugin_token="token_1" |
||||
|
/> |
||||
|
<rule |
||||
|
id="uuid'8e834e12-2e0a-11e7-83ec-f7f6dbfb4833'" |
||||
|
tenant_id="uuid'8e834e10-2e0a-11e7-83ec-f7f6dbfb4833'" |
||||
|
name="RULE_12" |
||||
|
weight="1" |
||||
|
search_text="rule_12" |
||||
|
plugin_token="token_1" |
||||
|
/> |
||||
|
<rule |
||||
|
id="uuid'8e834e13-2e0a-11e7-83ec-f7f6dbfb4833'" |
||||
|
tenant_id="uuid'8e834e10-2e0a-11e7-83ec-f7f6dbfb4833'" |
||||
|
name="RULE_13" |
||||
|
weight="1" |
||||
|
search_text="rule_13" |
||||
|
plugin_token="token_1" |
||||
|
/> |
||||
|
<rule |
||||
|
id="uuid'8e834e14-2e0a-11e7-83ec-f7f6dbfb4833'" |
||||
|
tenant_id="uuid'8e834e10-2e0a-11e7-83ec-f7f6dbfb4833'" |
||||
|
name="RULE_14" |
||||
|
weight="2" |
||||
|
search_text="rule_14" |
||||
|
plugin_token="token_2" |
||||
|
/> |
||||
|
</dataset> |
||||
Loading…
Reference in new issue