From 3624fcf9d58fe81cd406653f06f7df5a67942246 Mon Sep 17 00:00:00 2001 From: PatrikBuhr Date: Wed, 8 Apr 2020 08:58:03 +0200 Subject: [PATCH] Added check of callback URL in service registration Change-Id: Ifdf3cbc11e7ac8e0f7b35261954a75147f7edc57 Issue-ID: NONRTRIC-164 Signed-off-by: PatrikBuhr --- .../org/oransc/policyagent/controllers/ServiceController.java | 8 +++++++- .../src/test/java/org/oransc/policyagent/ApplicationTest.java | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/policy-agent/src/main/java/org/oransc/policyagent/controllers/ServiceController.java b/policy-agent/src/main/java/org/oransc/policyagent/controllers/ServiceController.java index 4db3ade5..922ba3d6 100644 --- a/policy-agent/src/main/java/org/oransc/policyagent/controllers/ServiceController.java +++ b/policy-agent/src/main/java/org/oransc/policyagent/controllers/ServiceController.java @@ -28,6 +28,8 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; +import java.net.MalformedURLException; +import java.net.URL; import java.time.Duration; import java.util.ArrayList; import java.util.Collection; @@ -92,13 +94,17 @@ public class ServiceController { s.getCallbackUrl()); } - private void validateRegistrationInfo(ServiceRegistrationInfo registrationInfo) throws ServiceException { + private void validateRegistrationInfo(ServiceRegistrationInfo registrationInfo) + throws ServiceException, MalformedURLException { if (registrationInfo.serviceName.isEmpty()) { throw new ServiceException("Missing mandatory parameter 'serviceName'"); } if (registrationInfo.keepAliveIntervalSeconds < 0) { throw new ServiceException("Keepalive interval shoul be greater or equal to 0"); } + if (!registrationInfo.callbackUrl.isEmpty()) { + new URL(registrationInfo.callbackUrl); + } } @ApiOperation(value = "Register a service") diff --git a/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java b/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java index 9b73892b..3bc231f6 100644 --- a/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java +++ b/policy-agent/src/test/java/org/oransc/policyagent/ApplicationTest.java @@ -558,6 +558,8 @@ public class ApplicationTest { testErrorCode(restClient().put("/service", "crap"), HttpStatus.BAD_REQUEST); testErrorCode(restClient().put("/service", "{}"), HttpStatus.BAD_REQUEST); testErrorCode(restClient().put("/service", createServiceJson("name", -123)), HttpStatus.BAD_REQUEST); + testErrorCode(restClient().put("/service", createServiceJson("name", 0, "missing.portandprotocol.com")), + HttpStatus.BAD_REQUEST); // GET non existing servive testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND); @@ -611,7 +613,11 @@ public class ApplicationTest { } private String createServiceJson(String name, long keepAliveIntervalSeconds) { - ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, "callbackUrl"); + return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/"); + } + + private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) { + ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url); String json = gson.toJson(service); return json; -- 2.16.6