Skip to content

Commit

Permalink
add unit test for ExternalRolePersistServiceImpl (#6193)
Browse files Browse the repository at this point in the history
  • Loading branch information
li-xiao-shuang authored Jun 30, 2021
1 parent 8dcf38f commit ccd6744
Showing 1 changed file with 102 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* 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.alibaba.nacos.config.server.auth;

import com.alibaba.nacos.config.server.model.Page;
import com.alibaba.nacos.config.server.service.repository.PaginationHelper;
import com.alibaba.nacos.config.server.service.repository.extrnal.ExternalStoragePersistServiceImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.jdbc.core.JdbcTemplate;

import java.lang.reflect.Field;
import java.util.List;

@RunWith(MockitoJUnitRunner.class)
public class ExternalRolePersistServiceImplTest {

@Mock
private ExternalStoragePersistServiceImpl persistService;

@Mock
private JdbcTemplate jt;

@Mock
private PaginationHelper paginationHelper;

private ExternalRolePersistServiceImpl externalRolePersistService;

@Before
public void setUp() throws Exception {
externalRolePersistService = new ExternalRolePersistServiceImpl();
Class<ExternalRolePersistServiceImpl> externalRolePersistServiceClass = ExternalRolePersistServiceImpl.class;
Field persistServiceClassDeclaredField = externalRolePersistServiceClass.getDeclaredField("persistService");
persistServiceClassDeclaredField.setAccessible(true);
persistServiceClassDeclaredField.set(externalRolePersistService, persistService);

Mockito.when(persistService.getJdbcTemplate()).thenReturn(jt);
Mockito.when(persistService.createPaginationHelper()).thenReturn(paginationHelper);

externalRolePersistService.init();
}

@Test
public void testGetRoles() {
Page<RoleInfo> roles = externalRolePersistService.getRoles(1, 10);

Assert.assertNotNull(roles);
}

@Test
public void testGetRolesByUserName() {
Page<RoleInfo> userName = externalRolePersistService.getRolesByUserName("userName", 1, 10);
Assert.assertNull(userName);
}

@Test
public void testAddRole() {
externalRolePersistService.addRole("role", "userName");

String sql = "INSERT into roles (role, username) VALUES (?, ?)";
Mockito.verify(jt).update(sql, "role", "userName");
}

@Test
public void testDeleteRole() {

externalRolePersistService.deleteRole("role");
String sql = "DELETE from roles WHERE role=?";
Mockito.verify(jt).update(sql, "role");

externalRolePersistService.deleteRole("role", "userName");
String sql2 = "DELETE from roles WHERE role=? and username=?";
Mockito.verify(jt).update(sql2, "role", "userName");

}

@Test
public void testFindRolesLikeRoleName() {
List<String> role = externalRolePersistService.findRolesLikeRoleName("role");

Assert.assertEquals(role.size(), 0);
}
}

0 comments on commit ccd6744

Please sign in to comment.