From 8c725522372883ab4c1e3fcd7e07b42e42d18cc1 Mon Sep 17 00:00:00 2001
From: xiangzihao <460888207@qq.com>
Date: Tue, 31 May 2022 14:01:25 +0800
Subject: [PATCH] [Feature-10290] [API] Refactor
org.apache.dolphinscheduler.api.controller.AccessTokenController#createToken
api (#10301)
* add feature_10290
* fix dolphinscheduler-ui/pnpm-lock.yaml error
* fix dolphinscheduler-ui/pnpm-lock.yaml error
* fix incorrect dependency
* fix incorrect dependency and unit test
* fix unit test error
* add dolphinscheduler-api/logs to .gitignore
* add AccessTokenV2ControllerTest.java
* fix AccessTokenV2ControllerTest error
---
.gitignore | 1 +
dolphinscheduler-api/pom.xml | 6 --
.../api/configuration/AppConfiguration.java | 2 +-
.../api/configuration/SwaggerConfig.java | 37 ++++++-
.../api/controller/AccessTokenController.java | 3 +-
.../controller/AccessTokenV2Controller.java | 76 +++++++++++++
.../api/dto/CreateTokenRequest.java | 56 ++++++++++
.../api/dto/CreateTokenResponse.java | 41 +++++++
.../api/service/AccessTokenService.java | 2 +-
.../service/impl/AccessTokenServiceImpl.java | 11 +-
.../src/main/resources/swagger.properties | 18 ++++
.../controller/AccessTokenControllerTest.java | 4 +-
.../AccessTokenV2ControllerTest.java | 101 ++++++++++++++++++
.../api/service/AccessTokenServiceTest.java | 7 +-
14 files changed, 341 insertions(+), 24 deletions(-)
create mode 100644 dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2Controller.java
create mode 100644 dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenRequest.java
create mode 100644 dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenResponse.java
create mode 100644 dolphinscheduler-api/src/main/resources/swagger.properties
create mode 100644 dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2ControllerTest.java
diff --git a/.gitignore b/.gitignore
index 2452b3ecc1..8e55e20304 100644
--- a/.gitignore
+++ b/.gitignore
@@ -48,6 +48,7 @@ dolphinscheduler-ui/node
dolphinscheduler-common/sql
dolphinscheduler-common/test
dolphinscheduler-worker/logs
+dolphinscheduler-api/logs
# ------------------
# pydolphinscheduler
diff --git a/dolphinscheduler-api/pom.xml b/dolphinscheduler-api/pom.xml
index b5dd813843..075f3bb8d5 100644
--- a/dolphinscheduler-api/pom.xml
+++ b/dolphinscheduler-api/pom.xml
@@ -151,12 +151,6 @@
io.swagger
swagger-models
-
-
- swagger-annotations
- io.swagger
-
-
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/AppConfiguration.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/AppConfiguration.java
index 105e3738df..7bca31e292 100644
--- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/AppConfiguration.java
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/AppConfiguration.java
@@ -101,7 +101,7 @@ public class AppConfiguration implements WebMvcConfigurer {
registry.addInterceptor(loginInterceptor())
.addPathPatterns(LOGIN_INTERCEPTOR_PATH_PATTERN)
.excludePathPatterns(LOGIN_PATH_PATTERN, REGISTER_PATH_PATTERN,
- "/swagger-resources/**", "/webjars/**", "/v2/**",
+ "/swagger-resources/**", "/webjars/**", "/api-docs/**",
"/doc.html", "/swagger-ui.html", "*.html", "/ui/**", "/error");
}
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/SwaggerConfig.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/SwaggerConfig.java
index 346f8f1a18..112dcd1a88 100644
--- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/SwaggerConfig.java
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/configuration/SwaggerConfig.java
@@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
@@ -39,19 +40,45 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@EnableSwaggerBootstrapUI
@ConditionalOnWebApplication
+@PropertySource("classpath:swagger.properties")
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
- public Docket createRestApi() {
- return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
- .apis(RequestHandlerSelectors.basePackage("org.apache.dolphinscheduler.api.controller")).paths(PathSelectors.any())
+ public Docket createV1RestApi() {
+ return new Docket(DocumentationType.SWAGGER_2)
+ .groupName("v1(current)")
+ .apiInfo(apiV1Info())
+ .select()
+ .apis(RequestHandlerSelectors.basePackage("org.apache.dolphinscheduler.api.controller"))
+ .paths(PathSelectors.any())
+ .paths(PathSelectors.regex("^(?!/v2).*"))
.build();
}
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder().title("Dolphin Scheduler Api Docs").description("Dolphin Scheduler Api Docs")
+ private ApiInfo apiV1Info() {
+ return new ApiInfoBuilder()
+ .title("Dolphin Scheduler Api Docs")
+ .description("Dolphin Scheduler Api Docs")
+ .version("V1")
.build();
}
+ @Bean
+ public Docket createV2RestApi() {
+ return new Docket(DocumentationType.SWAGGER_2)
+ .groupName("v2")
+ .apiInfo(apiV2Info())
+ .select()
+ .apis(RequestHandlerSelectors.basePackage("org.apache.dolphinscheduler.api.controller"))
+ .paths(PathSelectors.ant("/v2/**"))
+ .build();
+ }
+ private ApiInfo apiV2Info() {
+ return new ApiInfoBuilder()
+ .title("Dolphin Scheduler Api Docs")
+ .description("Dolphin Scheduler Api Docs")
+ .version("V2")
+ .build();
+ }
}
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java
index 971c76f35f..b08f387304 100644
--- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenController.java
@@ -89,8 +89,7 @@ public class AccessTokenController extends BaseController {
@RequestParam(value = "expireTime") String expireTime,
@RequestParam(value = "token", required = false) String token) {
- Map result = accessTokenService.createToken(loginUser, userId, expireTime, token);
- return returnDataList(result);
+ return accessTokenService.createToken(loginUser, userId, expireTime, token);
}
/**
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2Controller.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2Controller.java
new file mode 100644
index 0000000000..77d05121fb
--- /dev/null
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2Controller.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.dolphinscheduler.api.controller;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+
+import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
+import org.apache.dolphinscheduler.api.dto.CreateTokenRequest;
+import org.apache.dolphinscheduler.api.dto.CreateTokenResponse;
+import org.apache.dolphinscheduler.api.exceptions.ApiException;
+import org.apache.dolphinscheduler.api.service.AccessTokenService;
+import org.apache.dolphinscheduler.api.utils.Result;
+import org.apache.dolphinscheduler.common.Constants;
+import org.apache.dolphinscheduler.dao.entity.User;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestAttribute;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestController;
+
+import springfox.documentation.annotations.ApiIgnore;
+
+import static org.apache.dolphinscheduler.api.enums.Status.CREATE_ACCESS_TOKEN_ERROR;
+
+/**
+ * access token controller
+ */
+@Api(tags = "ACCESS_TOKEN_TAG")
+@RestController
+@RequestMapping("/v2/access-tokens")
+public class AccessTokenV2Controller extends BaseController {
+
+ @Autowired
+ private AccessTokenService accessTokenService;
+
+ /**
+ * create token
+ *
+ * @param loginUser login user
+ * @param createTokenRequest createTokenRequest
+ * @return CreateTokenResponse CreateTokenResponse
+ */
+ @ApiOperation(value = "createToken", notes = "CREATE_TOKEN_NOTES")
+ @PostMapping(consumes = {"application/json"})
+ @ResponseStatus(HttpStatus.CREATED)
+ @ApiException(CREATE_ACCESS_TOKEN_ERROR)
+ @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
+ public CreateTokenResponse createToken(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
+ @RequestBody CreateTokenRequest createTokenRequest) {
+ Result result = accessTokenService.createToken(loginUser,
+ createTokenRequest.getUserId(),
+ createTokenRequest.getExpireTime(),
+ createTokenRequest.getToken());
+ return new CreateTokenResponse(result);
+ }
+}
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenRequest.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenRequest.java
new file mode 100644
index 0000000000..03190f4b9f
--- /dev/null
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenRequest.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.dolphinscheduler.api.dto;
+
+
+import io.swagger.annotations.ApiModelProperty;
+
+public class CreateTokenRequest {
+ @ApiModelProperty(example = "1", required = true)
+ Integer userId;
+
+ @ApiModelProperty(example = "2022-12-31 00:00:00", required = true)
+ String expireTime;
+
+ @ApiModelProperty(example = "abc123xyz")
+ String token;
+
+ public Integer getUserId() {
+ return userId;
+ }
+
+ public void setUserId(Integer userId) {
+ this.userId = userId;
+ }
+
+ public String getExpireTime() {
+ return expireTime;
+ }
+
+ public void setExpireTime(String expireTime) {
+ this.expireTime = expireTime;
+ }
+
+ public String getToken() {
+ return token;
+ }
+
+ public void setToken(String token) {
+ this.token = token;
+ }
+}
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenResponse.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenResponse.java
new file mode 100644
index 0000000000..e96e2b2690
--- /dev/null
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/CreateTokenResponse.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.dolphinscheduler.api.dto;
+
+import org.apache.dolphinscheduler.api.utils.Result;
+import org.apache.dolphinscheduler.dao.entity.AccessToken;
+
+public class CreateTokenResponse extends Result {
+ private AccessToken data;
+
+ public CreateTokenResponse(Result result) {
+ super();
+ this.setCode(result.getCode());
+ this.setMsg(result.getMsg());
+ this.setData((AccessToken) result.getData());
+ }
+
+ @Override
+ public AccessToken getData() {
+ return data;
+ }
+
+ public void setData(AccessToken data) {
+ this.data = data;
+ }
+}
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java
index 99ac4c95cc..1668cffe37 100644
--- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AccessTokenService.java
@@ -55,7 +55,7 @@ public interface AccessTokenService {
* @param token token string (if it is absent, it will be automatically generated)
* @return create result code
*/
- Map createToken(User loginUser, int userId, String expireTime, String token);
+ Result createToken(User loginUser, int userId, String expireTime, String token);
/**
diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java
index be4f9d61ee..f2523faaee 100644
--- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java
+++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AccessTokenServiceImpl.java
@@ -121,8 +121,8 @@ public class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok
*/
@SuppressWarnings("checkstyle:WhitespaceAround")
@Override
- public Map createToken(User loginUser, int userId, String expireTime, String token) {
- Map result = new HashMap<>();
+ public Result createToken(User loginUser, int userId, String expireTime, String token) {
+ Result result = new Result();
// 1. check permission
if (!(canOperatorPermissions(loginUser,null, AuthorizationType.ACCESS_TOKEN,ACCESS_TOKEN_CREATE) || loginUser.getId() == userId)) {
@@ -132,7 +132,10 @@ public class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok
// 2. check if user is existed
if (userId <= 0) {
- throw new IllegalArgumentException("User id should not less than or equals to 0.");
+ String errorMsg = "User id should not less than or equals to 0.";
+ logger.error(errorMsg);
+ putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, errorMsg);
+ return result;
}
// 3. generate access token if absent
@@ -151,7 +154,7 @@ public class AccessTokenServiceImpl extends BaseServiceImpl implements AccessTok
int insert = accessTokenMapper.insert(accessToken);
if (insert > 0) {
- result.put(Constants.DATA_LIST, accessToken);
+ result.setData(accessToken);
putMsg(result, Status.SUCCESS);
} else {
putMsg(result, Status.CREATE_ACCESS_TOKEN_ERROR);
diff --git a/dolphinscheduler-api/src/main/resources/swagger.properties b/dolphinscheduler-api/src/main/resources/swagger.properties
new file mode 100644
index 0000000000..e4f51880d4
--- /dev/null
+++ b/dolphinscheduler-api/src/main/resources/swagger.properties
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+springfox.documentation.swagger.v2.path=/api-docs
diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java
index 21c2438b33..24570f588b 100644
--- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java
+++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenControllerTest.java
@@ -89,11 +89,11 @@ public class AccessTokenControllerTest extends AbstractControllerTest {
MvcResult mvcResult = mockMvc.perform(post("/access-tokens")
.header("sessionId", sessionId)
.params(paramsMap))
- .andExpect(status().isOk())
+ .andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
- Assert.assertEquals(Status.CREATE_ACCESS_TOKEN_ERROR.getCode(), result.getCode().intValue());
+ Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode(), result.getCode().intValue());
logger.info(mvcResult.getResponse().getContentAsString());
}
diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2ControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2ControllerTest.java
new file mode 100644
index 0000000000..205a136db4
--- /dev/null
+++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/AccessTokenV2ControllerTest.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.dolphinscheduler.api.controller;
+
+import org.apache.dolphinscheduler.api.enums.Status;
+import org.apache.dolphinscheduler.api.utils.Result;
+import org.apache.dolphinscheduler.common.utils.JSONUtils;
+import org.junit.Assert;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+/**
+ * access token v2 controller test
+ */
+public class AccessTokenV2ControllerTest extends AbstractControllerTest {
+ private static final Logger logger = LoggerFactory.getLogger(AccessTokenV2ControllerTest.class);
+
+ @Test
+ public void testCreateToken() throws Exception {
+ Map paramsMap = new HashMap<>();
+ paramsMap.put("userId", 1);
+ paramsMap.put("expireTime", "2022-12-31 00:00:00");
+ paramsMap.put("token", "607f5aeaaa2093dbdff5d5522ce00510");
+ MvcResult mvcResult = mockMvc.perform(post("/v2/access-tokens")
+ .header("sessionId", sessionId)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(JSONUtils.toJsonString(paramsMap)))
+ .andExpect(status().isCreated())
+ .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+ .andReturn();
+ Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
+ Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
+ logger.info(mvcResult.getResponse().getContentAsString());
+ }
+
+ @Test
+ public void testCreateTokenIfAbsent() throws Exception {
+ Map paramsMap = new HashMap<>();
+ paramsMap.put("userId", 1);
+ paramsMap.put("expireTime", "2022-12-31 00:00:00");
+ paramsMap.put("token", null);
+
+ MvcResult mvcResult = this.mockMvc
+ .perform(post("/v2/access-tokens")
+ .header("sessionId", this.sessionId)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(JSONUtils.toJsonString(paramsMap)))
+ .andExpect(status().isCreated())
+ .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+ .andReturn();
+
+ Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
+ Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
+ logger.info(mvcResult.getResponse().getContentAsString());
+ }
+
+ @Test
+ public void testExceptionHandler() throws Exception {
+ Map paramsMap = new HashMap<>();
+ paramsMap.put("userId", -1);
+ paramsMap.put("expireTime", "2022-12-31 00:00:00");
+ paramsMap.put("token", "507f5aeaaa2093dbdff5d5522ce00510");
+ MvcResult mvcResult = mockMvc.perform(post("/v2/access-tokens")
+ .header("sessionId", sessionId)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(JSONUtils.toJsonString(paramsMap)))
+ .andExpect(status().isCreated())
+ .andExpect(content().contentType(MediaType.APPLICATION_JSON))
+ .andReturn();
+ Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
+ Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode(), result.getCode().intValue());
+ logger.info(mvcResult.getResponse().getContentAsString());
+ }
+}
diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java
index 3d795c5823..ae362eaa9e 100644
--- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java
+++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AccessTokenServiceTest.java
@@ -40,6 +40,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import org.apache.dolphinscheduler.service.permission.ResourcePermissionCheckService;
import org.assertj.core.util.Lists;
@@ -117,14 +118,14 @@ public class AccessTokenServiceTest {
public void testCreateToken() {
// Given Token
when(accessTokenMapper.insert(any(AccessToken.class))).thenReturn(2);
- Map result = accessTokenService.createToken(getLoginUser(), 1, getDate(), "AccessTokenServiceTest");
+ Result result = accessTokenService.createToken(getLoginUser(), 1, getDate(), "AccessTokenServiceTest");
logger.info(result.toString());
- Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
+ Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
// Token is absent
result = this.accessTokenService.createToken(getLoginUser(), 1, getDate(), null);
logger.info(result.toString());
- Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
+ Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
}
@Test