Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(object-search): persistence and service layer implementation #2155

Merged
merged 9 commits into from
Apr 15, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2023 OceanBase.
*
* 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.oceanbase.odc.metadb.dbobject;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.oceanbase.odc.ServiceTestEnv;
import com.oceanbase.odc.test.tool.TestRandom;

/**
* @author gaoda.xy
* @date 2024/3/27 19:11
*/
public class DBColumnRepositoryTest extends ServiceTestEnv {

@Autowired
private DBColumnRepository dbColumnRepository;

@Before
public void setUp() {
dbColumnRepository.deleteAll();
}

@After
public void tearDown() {
dbColumnRepository.deleteAll();
}

@Test
public void test_findAll() {
List<DBColumnEntity> entities = Arrays.asList(
TestRandom.nextObject(DBColumnEntity.class),
TestRandom.nextObject(DBColumnEntity.class),
TestRandom.nextObject(DBColumnEntity.class));
dbColumnRepository.saveAll(entities);
Assert.assertEquals(entities.size(), dbColumnRepository.findAll().size());
}

@Test
public void test_findTop1000ByDatabaseIdInAndNameLike() {
DBColumnEntity entity1 = TestRandom.nextObject(DBColumnEntity.class);
entity1.setDatabaseId(1L);
entity1.setName("database_for_test_1");
DBColumnEntity entity2 = TestRandom.nextObject(DBColumnEntity.class);
entity2.setDatabaseId(2L);
entity2.setName("database_for_test_2");
dbColumnRepository.saveAll(Arrays.asList(entity1, entity2));
List<DBColumnEntity> entities =
dbColumnRepository.findTop1000ByDatabaseIdInAndNameLike(Arrays.asList(1L, 2L, 3L), "%test_1");
Assert.assertEquals(1, entities.size());
entities = dbColumnRepository.findTop1000ByDatabaseIdInAndNameLike(Arrays.asList(1L, 2L, 3L), "%test%");
Assert.assertEquals(2, entities.size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2023 OceanBase.
*
* 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.oceanbase.odc.metadb.dbobject;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.oceanbase.odc.ServiceTestEnv;
import com.oceanbase.odc.test.tool.TestRandom;
import com.oceanbase.tools.dbbrowser.model.DBObjectType;

/**
* @author gaoda.xy
* @date 2024/3/27 19:15
*/
public class DBObjectRepositoryTest extends ServiceTestEnv {

@Autowired
private DBObjectRepository dbObjectRepository;

@Before
public void setUp() {
dbObjectRepository.deleteAll();
}

@After
public void tearDown() {
dbObjectRepository.deleteAll();
}

@Test
public void test_findAll() {
List<DBObjectEntity> entities = Arrays.asList(
TestRandom.nextObject(DBObjectEntity.class),
TestRandom.nextObject(DBObjectEntity.class),
TestRandom.nextObject(DBObjectEntity.class));
dbObjectRepository.saveAll(entities);
Assert.assertEquals(entities.size(), dbObjectRepository.findAll().size());
}

@Test
public void test_findTop1000ByDatabaseIdInAndNameLike() {
DBObjectEntity entity1 = TestRandom.nextObject(DBObjectEntity.class);
entity1.setDatabaseId(1L);
entity1.setName("object_for_test_1");
DBObjectEntity entity2 = TestRandom.nextObject(DBObjectEntity.class);
entity2.setDatabaseId(2L);
entity2.setName("object_for_test_2");
dbObjectRepository.saveAll(Arrays.asList(entity1, entity2));
dbObjectRepository.flush();
List<DBObjectEntity> entities =
dbObjectRepository.findTop1000ByDatabaseIdInAndNameLike(Arrays.asList(1L, 2L), "%test%");
Assert.assertEquals(2, entities.size());
}

@Test
public void test_findTop1000ByDatabaseIdInAndTypeAndNameLike() {
DBObjectEntity entity1 = TestRandom.nextObject(DBObjectEntity.class);
entity1.setDatabaseId(1L);
entity1.setType(DBObjectType.TABLE);
entity1.setName("table_for_test_1");
DBObjectEntity entity2 = TestRandom.nextObject(DBObjectEntity.class);
entity2.setDatabaseId(1L);
entity2.setType(DBObjectType.VIEW);
entity2.setName("view_for_test_1");
dbObjectRepository.saveAll(Arrays.asList(entity1, entity2));
List<DBObjectEntity> entities = dbObjectRepository.findTop1000ByDatabaseIdInAndTypeInAndNameLike(
Arrays.asList(1L, 2L), Collections.singletonList(DBObjectType.VIEW.name()), "%test%");
Assert.assertEquals(1, entities.size());
Assert.assertEquals("view_for_test_1", entities.get(0).getName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
package com.oceanbase.odc.service.collaboration;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang.time.DateUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -35,6 +38,8 @@
import com.oceanbase.odc.core.shared.exception.BadRequestException;
import com.oceanbase.odc.metadb.collaboration.ProjectEntity;
import com.oceanbase.odc.metadb.collaboration.ProjectRepository;
import com.oceanbase.odc.metadb.connection.DatabaseEntity;
import com.oceanbase.odc.metadb.connection.DatabaseRepository;
import com.oceanbase.odc.metadb.iam.UserEntity;
import com.oceanbase.odc.metadb.iam.resourcerole.ResourceRoleEntity;
import com.oceanbase.odc.metadb.iam.resourcerole.ResourceRoleRepository;
Expand All @@ -50,6 +55,7 @@
import com.oceanbase.odc.service.iam.auth.AuthenticationFacade;
import com.oceanbase.odc.service.iam.model.User;
import com.oceanbase.odc.service.iam.model.UserResourceRole;
import com.oceanbase.odc.test.tool.TestRandom;

/**
* @Author: Lebie
Expand All @@ -65,6 +71,9 @@ public class ProjectServiceTest extends ServiceTestEnv {
@Autowired
private ProjectRepository projectRepository;

@Autowired
private DatabaseRepository databaseRepository;

@MockBean
private UserService userService;

Expand All @@ -87,13 +96,14 @@ public void setUp() {
Mockito.when(authenticationFacade.currentOrganizationId()).thenReturn(1L);
Mockito.when(authenticationFacade.currentUser()).thenReturn(getUser());
Mockito.when(resourceRoleRepository.findAll()).thenReturn(listAllProjectResourceRoles());

databaseRepository.deleteAll();
projectRepository.deleteAll();

}

@After
public void tearDown() {
databaseRepository.deleteAll();
projectRepository.deleteAll();
}

Expand All @@ -115,6 +125,32 @@ public void testGetProject_Success() {
Assert.assertNotNull(actual);
}

@Test
public void testGetProject_syncTimeIsNull() {
Date syncTime = new Date();
Project saved = projectService.create(getProject());
Mockito.when(
resourceRoleService.listByResourceTypeAndId(Mockito.eq(ResourceType.ODC_PROJECT), Mockito.anyLong()))
.thenReturn(listUserResourceRole(saved.getId()));
createDatabase(saved.getId(), null);
createDatabase(saved.getId(), syncTime);
Project actual = projectService.detail(saved.getId());
Assert.assertNull(actual.getDbObjectLastSyncTime());
}

@Test
public void testGetProject_syncTimeNotNull() {
Date syncTime = new Date();
Project saved = projectService.create(getProject());
Mockito.when(
resourceRoleService.listByResourceTypeAndId(Mockito.eq(ResourceType.ODC_PROJECT), Mockito.anyLong()))
.thenReturn(listUserResourceRole(saved.getId()));
createDatabase(saved.getId(), syncTime);
createDatabase(saved.getId(), DateUtils.addDays(syncTime, 1));
Project actual = projectService.detail(saved.getId());
Assert.assertEquals(syncTime, actual.getDbObjectLastSyncTime());
}

@Test
public void testUpdateProject_Success() {
Mockito.when(resourceRoleService.saveAll(Mockito.any())).thenReturn(listUserResourceRole(1L));
Expand Down Expand Up @@ -304,4 +340,14 @@ private List<UserResourceRole> listUserResourceRole(Long resourceId) {

return userResourceRoles;
}

private void createDatabase(Long projectId, Date lastSyncTime) {
DatabaseEntity entity = TestRandom.nextObject(DatabaseEntity.class);
entity.setProjectId(projectId);
entity.setDatabaseId(UUID.randomUUID().toString());
entity.setExisted(true);
entity.setObjectLastSyncTime(lastSyncTime);
databaseRepository.save(entity);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.lang3.time.DateUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -54,6 +57,8 @@
import com.oceanbase.odc.core.shared.exception.BadRequestException;
import com.oceanbase.odc.core.shared.exception.NotFoundException;
import com.oceanbase.odc.metadb.connection.ConnectionConfigRepository;
import com.oceanbase.odc.metadb.connection.DatabaseEntity;
import com.oceanbase.odc.metadb.connection.DatabaseRepository;
import com.oceanbase.odc.metadb.iam.UserEntity;
import com.oceanbase.odc.metadb.resourcegroup.ResourceGroupRepository;
import com.oceanbase.odc.service.collaboration.environment.EnvironmentService;
Expand Down Expand Up @@ -87,6 +92,8 @@ public class ConnectionConfigServiceTest extends MockedAuthorityTestEnv {
private ConnectionConfigRepository repository;
@Autowired
private ResourceGroupRepository resourceGroupRepository;
@Autowired
private DatabaseRepository databaseRepository;
@MockBean
private AuthenticationFacade authenticationFacade;
@MockBean
Expand Down Expand Up @@ -129,8 +136,8 @@ public void setUp() throws Exception {
doNothing().when(userPermissionService).bindUserAndDataSourcePermission(eq(CREATOR_ID), eq(ORGANIZATION_ID),
any(Long.class), eq(Arrays.asList("read", "update", "delete")));


repository.deleteAll();
databaseRepository.deleteAll();
resourceGroupRepository.deleteAll();

grantAllPermissions(ResourceType.ODC_CONNECTION, ResourceType.ODC_RESOURCE_GROUP, ResourceType.ODC_USER,
Expand All @@ -140,10 +147,31 @@ public void setUp() throws Exception {
@After
public void tearDown() {
repository.deleteAll();
databaseRepository.deleteAll();
DefaultLoginSecurityManager.removeSecurityContext();
DefaultLoginSecurityManager.removeContext();
}

@Test
public void test_detail_syncTimeIsNull() {
Date syncTime = new Date();
ConnectionConfig connection = createConnection(ConnectionVisibleScope.ORGANIZATION, NAME);
createDatabase(connection.getId(), syncTime);
createDatabase(connection.getId(), null);
ConnectionConfig detail = service.detail(connection.getId());
Assert.assertNull(detail.getDbObjectLastSyncTime());
}

@Test
public void test_detail_syncTimeIsNotNull() {
Date syncTime = new Date();
ConnectionConfig connection = createConnection(ConnectionVisibleScope.ORGANIZATION, NAME);
createDatabase(connection.getId(), syncTime);
createDatabase(connection.getId(), DateUtils.addDays(syncTime, 1));
ConnectionConfig detail = service.detail(connection.getId());
Assert.assertEquals(syncTime, detail.getDbObjectLastSyncTime());
}

@Test
public void exists_HasConnection_MatchTrue() {
ConnectionConfig connection = createConnection(ConnectionVisibleScope.ORGANIZATION, NAME);
Expand Down Expand Up @@ -380,4 +408,14 @@ private Environment getEnvironment() {
environment.setName("fake_env");
return environment;
}

private void createDatabase(Long dataSourceId, Date lastSyncTime) {
DatabaseEntity entity = TestRandom.nextObject(DatabaseEntity.class);
entity.setConnectionId(dataSourceId);
entity.setDatabaseId(UUID.randomUUID().toString());
entity.setExisted(true);
entity.setObjectLastSyncTime(lastSyncTime);
databaseRepository.save(entity);
}

}
Loading
Loading