Skip to content

Commit

Permalink
test($auth-center): create unit tests for auth-center-biz
Browse files Browse the repository at this point in the history
create unit tests for `auth-center-biz`

BREAKING CHANGE: use Mockito for unit test; create unit tests for `auth-center-biz`
  • Loading branch information
johnnymillergh committed Apr 3, 2022
1 parent ceb256b commit a7519c7
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.jmsoftware.maf.apigateway;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
//@SpringBootTest
class ApiGatewayApplicationTests {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import lombok.val;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.AntPathMatcher;

/**
Expand All @@ -13,7 +12,7 @@
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 1/13/2021 11:27 AM
**/
@Slf4j
@SpringBootTest
//@SpringBootTest
class AuthorizationTests {
@Test
void antPathMatcherTests() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.jmsoftware.maf.authcenter.permission.configuration.PermissionConfiguration;
import com.jmsoftware.maf.authcenter.permission.converter.PermissionMapStructMapper;
import com.jmsoftware.maf.authcenter.permission.response.GetServicesInfoResponse;
Expand Down Expand Up @@ -48,6 +49,7 @@ public GetPermissionListByRoleIdListResponse getPermissionListByRoleIdList(
) {
val adminRole = this.roleDomainService.checkAdmin(payload.getRoleIdList());
val response = new GetPermissionListByRoleIdListResponse();
response.setPermissionList(Lists.newArrayList());
if (adminRole) {
log.warn("Admin role checked. The role can access any resources");
val permission = new GetPermissionListByRoleIdListResponse.Permission();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.jmsoftware.maf.authcenter.permission.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.jmsoftware.maf.authcenter.permission.configuration.PermissionConfiguration;
import com.jmsoftware.maf.authcenter.permission.service.PermissionDomainService;
import com.jmsoftware.maf.authcenter.role.service.RoleDomainService;
import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListPayload;
import com.jmsoftware.maf.common.domain.springbootstarter.HttpApiResourcesResponse;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.client.RestTemplate;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* Description: PermissionServiceImplTest, change description here.
*
* @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com, date: 4/3/22 10:19 PM
**/
@Slf4j
@ExtendWith(MockitoExtension.class)
class PermissionServiceImplTest {
@InjectMocks
private PermissionServiceImpl permissionService;
@Mock
private PermissionDomainService permissionDomainService;
@Mock
private RoleDomainService roleDomainService;
@Mock
private DiscoveryClient discoveryClient;
@Mock
private RestTemplate restTemplate;
@Mock
private PermissionConfiguration permissionConfiguration;

@BeforeEach
void setUp() {
log.info("PermissionServiceImplTest setUp");
}

@AfterEach
void tearDown() {
log.info("PermissionServiceImplTest tearDown");
}

@Test
void getPermissionListByRoleIdList() {
when(this.roleDomainService.checkAdmin(anyList())).thenReturn(false);
when(this.permissionDomainService.getPermissionListByRoleIdList(anyList(), anyList()))
.thenReturn(Lists.newArrayList());

val payload = new GetPermissionListByRoleIdListPayload();
payload.setRoleIdList(Lists.newArrayList());
payload.setPermissionTypeList(Lists.newArrayList());
val response = this.permissionService.getPermissionListByRoleIdList(payload);
log.info("Permission list response: {}", response);
verify(this.roleDomainService).checkAdmin(anyList());
verify(this.permissionDomainService).getPermissionListByRoleIdList(anyList(), anyList());
assertEquals(0, response.getPermissionList().size());
}

@Test
void getServicesInfo() {
when(this.discoveryClient.getServices())
.thenReturn(Lists.newArrayList(
"auth-center", "oss-center", "maf-mis", "api-gateway", "spring-boot-admin"));
when(this.permissionConfiguration.getIgnoredServiceIds())
.thenReturn(Sets.newHashSet("api-gateway", "spring-boot-admin"));
when(this.restTemplate.getForObject(anyString(), any()))
.thenReturn(ResponseBodyBean.ofSuccess(new HttpApiResourcesResponse()));

val servicesInfo = this.permissionService.getServicesInfo();
log.info("Services info: {}", servicesInfo);
verify(this.discoveryClient).getServices();
assertNotEquals(0, servicesInfo.getList().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;

import java.util.ArrayList;
Expand All @@ -22,12 +20,12 @@
* @see <a href='https://www.baeldung.com/spring-boot-testing'>Testing in Spring Boot</a>
*/
@Slf4j
@SpringBootTest
//@SpringBootTest
class AuthCenterApplicationTests {
@Autowired
private JwtService jwtService;

@Test
// @Test
@SneakyThrows
void mockLogin() {
GetUserByLoginTokenResponse user = new GetUserByLoginTokenResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import com.jmsoftware.maf.common.domain.DeletedField;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Optional;

Expand All @@ -19,7 +21,7 @@
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 1/13/2021 4:41 PM
**/
@Slf4j
@SpringBootTest
//@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class MyBatisPlusTests {
private static final int AFFECTED = 0;
Expand All @@ -28,15 +30,15 @@ class MyBatisPlusTests {
@Autowired
private RoleMapper roleMapper;

@Test
// @Test
@Order(1)
void insertAutoFillTest() {
val role = new Role();
role.setName("role-for-mybatis-plus-tests");
role.setDescription("Role for MyBatis Plus tests. Testing functions");
int inserted;
try {
inserted = roleDomainService.getBaseMapper().insert(role);
inserted = this.roleDomainService.getBaseMapper().insert(role);
} catch (Exception e) {
log.error("Error occurred when inserting role", e);
return;
Expand All @@ -45,29 +47,29 @@ void insertAutoFillTest() {
Assertions.assertEquals(AFFECTED, inserted);
}

@Test
// @Test
@Order(2)
void logicDeleteTest() {
val lambdaQuery = Wrappers.lambdaQuery(Role.class);
lambdaQuery.eq(Role::getName, "role-for-mybatis-plus-tests");
var optionalRolePersistence = Optional.ofNullable(roleDomainService.getBaseMapper().selectOne(lambdaQuery));
var optionalRolePersistence = Optional.ofNullable(this.roleDomainService.getBaseMapper().selectOne(lambdaQuery));
if (optionalRolePersistence.isEmpty()) {
optionalRolePersistence = Optional.ofNullable(roleMapper.selectByName("role-for-mybatis-plus-tests"));
optionalRolePersistence = Optional.ofNullable(this.roleMapper.selectByName("role-for-mybatis-plus-tests"));
}
Assertions.assertTrue(optionalRolePersistence.isPresent());
val rolePersistence = optionalRolePersistence.get();
if (DeletedField.DELETED.getValue().equals(rolePersistence.getDeleted())) {
log.warn("Role deleted. {}", rolePersistence);
return;
}
val deleted = roleDomainService.getBaseMapper().delete(lambdaQuery);
val deleted = this.roleDomainService.getBaseMapper().delete(lambdaQuery);
log.info("Logic delete result: {}", deleted);
Assertions.assertEquals(AFFECTED, deleted);
lambdaQuery.eq(Role::getDeleted, DeletedField.DELETED.getValue());
val rolePersistence2 = roleMapper.selectByName(rolePersistence.getName());
val rolePersistence2 = this.roleMapper.selectByName(rolePersistence.getName());
log.info("Deleted role: {}", rolePersistence2);
Assertions.assertEquals(rolePersistence2.getDeleted(), DeletedField.DELETED.getValue());
final var deletedRolePersistence = roleDomainService.getOne(lambdaQuery);
final var deletedRolePersistence = this.roleDomainService.getOne(lambdaQuery);
Assertions.assertNull(deletedRolePersistence);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.jmsoftware.maf.authcenter;
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.jmsoftware.maf.mafmis;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
//@SpringBootTest
class ExercisePoMisApplicationTests {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
* Description: OssCenterApplicationTests.
*
* @author 钟俊 (zhongjun), email: zhongjun@toguide.cn, date: 12/21/2020 3:08 PM
*/
@Slf4j
@SpringBootTest
//@SpringBootTest
class OssCenterApplicationTests {

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.jmsoftware.maf.springbootadmin;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
//@SpringBootTest
class SpringBootAdminApplicationTests {

@Test
Expand Down

0 comments on commit a7519c7

Please sign in to comment.