forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE alibaba#5092] Add unit tests for class ExternalUserPersistServ…
…iceImpl in nacos 2.0 (alibaba#6237) * add unit test for ExternalUserPersistServiceImpl * add unit test for ExternalUserPersistServiceImpl
- Loading branch information
1 parent
5fa6441
commit 4f0900b
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
...rc/test/java/com/alibaba/nacos/config/server/auth/ExternalUserPersistServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* 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.model.User; | ||
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 ExternalUserPersistServiceImplTest { | ||
|
||
@Mock | ||
private ExternalStoragePersistServiceImpl persistService; | ||
|
||
@Mock | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
@Mock | ||
private PaginationHelper paginationHelper; | ||
|
||
private ExternalUserPersistServiceImpl externalUserPersistService; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
externalUserPersistService = new ExternalUserPersistServiceImpl(); | ||
|
||
Class<ExternalUserPersistServiceImpl> externalUserPersistServiceClass = ExternalUserPersistServiceImpl.class; | ||
Field persistServiceClassDeclaredField = externalUserPersistServiceClass.getDeclaredField("persistService"); | ||
persistServiceClassDeclaredField.setAccessible(true); | ||
persistServiceClassDeclaredField.set(externalUserPersistService, persistService); | ||
|
||
Mockito.when(persistService.getJdbcTemplate()).thenReturn(jdbcTemplate); | ||
Mockito.when(persistService.createPaginationHelper()).thenReturn(paginationHelper); | ||
externalUserPersistService.init(); | ||
} | ||
|
||
@Test | ||
public void testCreateUser() { | ||
externalUserPersistService.createUser("username", "password"); | ||
|
||
String sql = "INSERT into users (username, password, enabled) VALUES (?, ?, ?)"; | ||
Mockito.verify(jdbcTemplate).update(sql, "username", "password", true); | ||
} | ||
|
||
@Test | ||
public void testDeleteUser() { | ||
externalUserPersistService.deleteUser("username"); | ||
|
||
String sql = "DELETE from users WHERE username=?"; | ||
Mockito.verify(jdbcTemplate).update(sql, "username"); | ||
} | ||
|
||
@Test | ||
public void testUpdateUserPassword() { | ||
externalUserPersistService.updateUserPassword("username", "password"); | ||
|
||
String sql = "UPDATE users SET password = ? WHERE username=?"; | ||
Mockito.verify(jdbcTemplate).update(sql, "password", "username"); | ||
} | ||
|
||
@Test | ||
public void testFindUserByUsername() { | ||
User username = externalUserPersistService.findUserByUsername("username"); | ||
|
||
Assert.assertNull(username); | ||
} | ||
|
||
@Test | ||
public void testGetUsers() { | ||
Page<User> users = externalUserPersistService.getUsers(1, 10); | ||
|
||
Assert.assertNotNull(users); | ||
} | ||
|
||
@Test | ||
public void testFindUserLikeUsername() { | ||
List<String> username = externalUserPersistService.findUserLikeUsername("username"); | ||
|
||
Assert.assertEquals(username.size(), 0); | ||
} | ||
} |