Skip to content

Commit

Permalink
feat: Validate ServiceId with Endpoint (#2413)
Browse files Browse the repository at this point in the history
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
zzhao459 and achmelo authored Jun 3, 2022
1 parent 2b0b2e4 commit 9f3825f
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 0 deletions.
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();
}


}
7 changes: 7 additions & 0 deletions gateway-service/src/main/resources/gateway-log-messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ messages:
reason: "Typically z/OSMF is either unavailable or offline."
action: "Verify that z/OSMF is available, accessible by the Gateway service, and online."

- key: org.zowe.apiml.gateway.verifier.wrongServiceId
number: ZWEAG717
type: ERROR
text: "The service id provided is invalid: '%s'"
reason: "The provided id is not valid under conformance criteria."
action: "Verify that conformance criteria, provide valid service id."

# Legacy messages

- key: org.zowe.apiml.security.generic
Expand Down
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);
}

}
}

0 comments on commit 9f3825f

Please sign in to comment.