-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Validate ServiceId with Endpoint (#2413)
The REST API will accept the serviceId as a parameter and then check if it is valid under conformance criteria, the information on whether the serviceId is valid will be returned. Signed-off-by: Ziye Zhao <[email protected]> Co-authored-by: achmelo <[email protected]>
- Loading branch information
Showing
3 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
gateway-service/src/main/java/org/zowe/apiml/gateway/conformance/ValidateAPIController.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,80 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Copyright Contributors to the Zowe Project. | ||
*/ | ||
|
||
package org.zowe.apiml.gateway.conformance; | ||
|
||
import java.util.regex.Pattern; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Matcher; | ||
|
||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.zowe.apiml.message.core.MessageService; | ||
|
||
import io.swagger.v3.oas.annotations.parameters.RequestBody; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
|
||
/** | ||
* Controller offered methods for validating serviceID under conformance criteria, it offer methods to | ||
* check the validation of the given serviceID | ||
*/ | ||
@Controller | ||
@RequiredArgsConstructor | ||
public class ValidateAPIController { | ||
|
||
private static final Pattern symbolPattern = Pattern.compile("[^a-z0-9]"); | ||
private static final Predicate<String> isTooLong = serviceId -> (serviceId).length() <= 64; | ||
private final MessageService messageService; | ||
|
||
/** | ||
* Accept serviceID and return the JSON file with appropirate message to show if it is valid | ||
* | ||
* @param serviceID accepted serviceID to check validation | ||
* @return return the JSON file message of whether the serviceID is valid | ||
*/ | ||
@PostMapping( | ||
value = "/validate", | ||
produces = MediaType.APPLICATION_JSON_VALUE | ||
) | ||
public ResponseEntity<?> checkValidate(@RequestBody String serviceID) { | ||
|
||
if (!isTooLong.test(serviceID)) { | ||
String invalidLength = "The serviceid is longer than 64 characters"; | ||
return new ResponseEntity<>(messageService.createMessage("org.zowe.apiml.gateway.verifier.wrongServiceId", invalidLength).mapToApiMessage(), HttpStatus.BAD_REQUEST); | ||
} | ||
else if (checkValidPatternAPI(serviceID)) { | ||
String invalidPattern = "The serviceid contains symbols or upper case letters"; | ||
return new ResponseEntity<>(messageService.createMessage("org.zowe.apiml.gateway.verifier.wrongServiceId", invalidPattern).mapToApiMessage(), HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
return new ResponseEntity<>(HttpStatus.OK); | ||
|
||
} | ||
|
||
/** | ||
* Accept serviceID and check if it contains only lower case characters without symbols, return true if it meets the criteria | ||
* otherwise return false | ||
* | ||
* @param serviceID accept serviceID to check | ||
* @return return boolean variable False if it only contains lower case characters without symbols | ||
*/ | ||
private boolean checkValidPatternAPI(String serviceID) { | ||
|
||
Matcher findSymbol = symbolPattern.matcher(serviceID); | ||
|
||
return findSymbol.find(); | ||
} | ||
|
||
|
||
} |
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
104 changes: 104 additions & 0 deletions
104
...y-service/src/test/java/org/zowe/apiml/gateway/conformance/ValidateAPIControllerTest.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,104 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Copyright Contributors to the Zowe Project. | ||
*/ | ||
|
||
package org.zowe.apiml.gateway.conformance; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.zowe.apiml.acceptance.common.AcceptanceTest; | ||
import org.zowe.apiml.acceptance.common.AcceptanceTestWithTwoServices; | ||
import org.zowe.apiml.message.core.MessageService; | ||
import org.zowe.apiml.message.yaml.YamlMessageService; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static io.restassured.module.mockmvc.RestAssuredMockMvc.standaloneSetup; | ||
|
||
import java.io.IOException; | ||
|
||
@AcceptanceTest | ||
public class ValidateAPIControllerTest extends AcceptanceTestWithTwoServices { | ||
|
||
private String validatePath; | ||
private MessageService messageService; | ||
private ValidateAPIController validateAPIController; | ||
|
||
@BeforeEach | ||
void setup() throws IOException { | ||
validatePath = "/validate"; | ||
} | ||
|
||
@Nested | ||
class GivenControllerServiceID { | ||
|
||
@BeforeEach | ||
void setup() throws IOException { | ||
messageService = new YamlMessageService("/gateway-log-messages.yml"); | ||
validateAPIController = new ValidateAPIController(messageService); | ||
standaloneSetup(validateAPIController); | ||
} | ||
|
||
@Test | ||
void whenServiceId_validate() throws Exception { | ||
given() | ||
.param("serviceID","validserviceid") | ||
.when() | ||
.post(basePath + validatePath) | ||
.then() | ||
.assertThat() | ||
.statusCode(HttpStatus.SC_OK); | ||
} | ||
|
||
@Test | ||
void whenServiceId_InvalidateUpper() throws Exception { | ||
|
||
given() | ||
.param("serviceID", "Invalidserviceidcontainupperletter") | ||
.when() | ||
.post(basePath + validatePath) | ||
.then() | ||
.assertThat() | ||
.body("messageNumber", equalTo("ZWEAG717E"), | ||
"messageContent", containsString("The serviceid contains symbols or upper case letters")) | ||
.statusCode(HttpStatus.SC_BAD_REQUEST); | ||
} | ||
|
||
@Test | ||
void whenServiceId_InvalidateSymbol() throws Exception { | ||
given() | ||
.param("serviceID", "invalid@serviceid_containsymbols") | ||
.when() | ||
.post(basePath + validatePath) | ||
.then() | ||
.assertThat() | ||
.body("messageNumber", equalTo("ZWEAG717E"), | ||
"messageContent", containsString("The serviceid contains symbols or upper case letters")) | ||
.statusCode(HttpStatus.SC_BAD_REQUEST); | ||
} | ||
|
||
|
||
@Test | ||
void whenServiceId_InvalidateLength() throws Exception { | ||
given() | ||
.param("serviceID", "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklwsezxcvbnmqwertyuiop") | ||
.when() | ||
.post(basePath + validatePath) | ||
.then() | ||
.assertThat() | ||
.body("messageNumber", equalTo("ZWEAG717E"), | ||
"messageContent", containsString("The serviceid is longer than 64 characters")) | ||
.statusCode(HttpStatus.SC_BAD_REQUEST); | ||
} | ||
|
||
} | ||
} |