Skip to content

Commit 13e39ef

Browse files
tobiasakeemichaf
authored andcommitted
Added SubscriptionValidator class and unit tests. Add validation of n… (#31)
* Added SubscriptionValidator class and unit tests. Add validation of new and updates of subscription before subscription is written to Database. * Added javadoc in SubscriptionValidator class. Added Ericsson Copyright header in the 2 new classes. * Added javadoc in SubscriptionValidator class. Added Ericsson Copyright header in the 2 new classes.
1 parent 98333d7 commit 13e39ef

File tree

4 files changed

+477
-1
lines changed

4 files changed

+477
-1
lines changed

src/main/java/com/ericsson/ei/controller/SubscriptionControllerImpl.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
import com.ericsson.ei.controller.model.Subscription;
3333
import com.ericsson.ei.controller.model.SubscriptionResponse;
3434
import com.ericsson.ei.exception.SubscriptionNotFoundException;
35+
import com.ericsson.ei.exception.SubscriptionValidationException;
3536
import com.ericsson.ei.services.ISubscriptionService;
37+
import com.ericsson.ei.subscriptionhandler.SubscriptionValidator;
3638

3739
import io.swagger.annotations.Api;
3840
import io.swagger.annotations.ApiOperation;
@@ -45,6 +47,8 @@ public class SubscriptionControllerImpl implements SubscriptionController {
4547
@Autowired
4648
private ISubscriptionService subscriptionService;
4749

50+
private SubscriptionValidator subscriptionValidator = new SubscriptionValidator();
51+
4852
private static final Logger LOG = LoggerFactory.getLogger(SubscriptionControllerImpl.class);
4953

5054

@@ -53,13 +57,25 @@ public class SubscriptionControllerImpl implements SubscriptionController {
5357
@ApiOperation(value = "Creates the subscription")
5458
public ResponseEntity<SubscriptionResponse> createSubscription(@RequestBody Subscription subscription) {
5559
SubscriptionResponse subscriptionResponse = new SubscriptionResponse();
60+
61+
try {
62+
subscriptionValidator.validateSubscription(subscription);
63+
}
64+
catch (SubscriptionValidationException e) {
65+
String msg = "Validation of Subscription parameters on:" + subscription.getSubscriptionName() +
66+
" failed! Error: " + e.getMessage();
67+
LOG.error(msg);
68+
subscriptionResponse.setMsg(msg); subscriptionResponse.setStatusCode(HttpStatus.PRECONDITION_FAILED.value());
69+
return new ResponseEntity<SubscriptionResponse>(subscriptionResponse, HttpStatus.PRECONDITION_FAILED);
70+
}
71+
5672
if (!subscriptionService.doSubscriptionExist(subscription.getSubscriptionName())) {
5773
subscriptionService.addSubscription(subscription);
5874
LOG.info("Subscription :" + subscription.getSubscriptionName() + " Inserted Successfully");
5975
subscriptionResponse.setMsg("Inserted Successfully"); subscriptionResponse.setStatusCode(HttpStatus.OK.value());
6076
return new ResponseEntity<SubscriptionResponse>(subscriptionResponse, HttpStatus.OK);
6177
} else {
62-
LOG.error("Subscription :" + subscription.getSubscriptionName() + " identified as duplicate subscription");
78+
LOG.error("Subscription :" + subscription.getSubscriptionName() + " already exists");
6379
subscriptionResponse.setMsg("Subscription already exists"); subscriptionResponse.setStatusCode(HttpStatus.BAD_REQUEST.value());
6480
return new ResponseEntity<SubscriptionResponse>(subscriptionResponse, HttpStatus.BAD_REQUEST);
6581
}
@@ -92,6 +108,18 @@ public ResponseEntity<SubscriptionResponse> updateSubscriptions(@RequestBody Sub
92108
String subscriptionName = subscription.getSubscriptionName();
93109
LOG.info("Subscription :" + subscriptionName + " update started");
94110
SubscriptionResponse subscriptionResponse = new SubscriptionResponse();
111+
112+
try {
113+
subscriptionValidator.validateSubscription(subscription);
114+
}
115+
catch (SubscriptionValidationException e) {
116+
String msg = "Validation of Subscription parameters on:" + subscription.getSubscriptionName() +
117+
" failed! Error: " + e.getMessage();
118+
LOG.error(msg);
119+
subscriptionResponse.setMsg(msg); subscriptionResponse.setStatusCode(HttpStatus.PRECONDITION_FAILED.value());
120+
return new ResponseEntity<SubscriptionResponse>(subscriptionResponse, HttpStatus.PRECONDITION_FAILED);
121+
}
122+
95123
if (subscriptionService.doSubscriptionExist(subscriptionName)) {
96124
subscriptionService.modifySubscription(subscription, subscriptionName);
97125
LOG.info("Subscription :" + subscriptionName + " update completed");
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2017 Ericsson AB.
3+
For a full list of individual contributors, please see the commit history.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
package com.ericsson.ei.exception;
18+
19+
public class SubscriptionValidationException extends Exception {
20+
21+
private static final long serialVersionUID = 2L;
22+
23+
public SubscriptionValidationException() {
24+
super();
25+
}
26+
27+
public SubscriptionValidationException(String message) {
28+
super(message);
29+
}
30+
31+
public SubscriptionValidationException(String message, Throwable e) {
32+
super(message, e);
33+
}
34+
35+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
Copyright 2017 Ericsson AB.
3+
For a full list of individual contributors, please see the commit history.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
package com.ericsson.ei.subscriptionhandler;
18+
19+
//import java.util.List;
20+
import java.util.regex.Pattern;
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
import com.ericsson.ei.controller.model.Subscription;
25+
import com.ericsson.ei.exception.SubscriptionValidationException;
26+
//import com.ericsson.ei.controller.model.Requirement;
27+
28+
public class SubscriptionValidator {
29+
30+
private static final Logger LOG = LoggerFactory.getLogger(SubscriptionValidator.class);
31+
32+
/*
33+
* Validation of parameters values in subscriptions objects.
34+
* Throws SubscriptionValidationException if validation of a parameter fails due to wrong
35+
* format of parameter.
36+
*
37+
* @param Subscription
38+
*/
39+
public void validateSubscription(Subscription subscription) throws SubscriptionValidationException {
40+
41+
LOG.info("Validation of subscription " + subscription.getSubscriptionName() + " Started.");
42+
43+
this.validateSubscriptionName(subscription.getSubscriptionName());
44+
this.validateNotificationMessage(subscription.getNotificationMessage());
45+
this.validateNotificationMeta(subscription.getNotificationMeta());
46+
this.validateNotificationType(subscription.getNotificationType());
47+
// List<Requirement> reqList = subscription.getRequirements();
48+
// for (int i=0; i < reqList.size(); i++) {
49+
// this.validateJmespath(reqList.get(i).getConditions().get(0).getJmespath());
50+
// }
51+
LOG.info("Validating of subscription " + subscription.getSubscriptionName() + " finished successfully.");
52+
}
53+
54+
/*
55+
* Validation of subscriptionName parameter
56+
* Throws SubscriptionValidationException if validation of the parameter fails due to wrong
57+
* format of parameter.
58+
*
59+
* @param subscriptionName
60+
*/
61+
public void validateSubscriptionName(String subscriptionName) throws SubscriptionValidationException {
62+
63+
String regex = "^[A-Za-z0-9_]+$";
64+
65+
if (!Pattern.matches(regex, subscriptionName)) {
66+
throw new SubscriptionValidationException("Wrong format of SubscriptionName: " + subscriptionName);
67+
}
68+
}
69+
70+
/*
71+
* Validation of notificationMessage parameter
72+
* Throws SubscriptionValidationException if validation of the parameter fails due to wrong
73+
* format of parameter.
74+
*
75+
* @param notificationMessage
76+
*/
77+
public void validateNotificationMessage(String notificationMessage) throws SubscriptionValidationException {
78+
79+
String regex = "^[A-Za-z0-9@.]+$";
80+
81+
if (!Pattern.matches(regex, notificationMessage)) {
82+
throw new SubscriptionValidationException("Wrong format of NotificationMessage: " + notificationMessage);
83+
}
84+
}
85+
86+
/*
87+
* Validation of notificationMeta parameter
88+
* Throws SubscriptionValidationException if validation of the parameter fails due to wrong
89+
* format of parameter.
90+
*
91+
* @param notificationMeta
92+
*/
93+
public void validateNotificationMeta(String notificationMeta) throws SubscriptionValidationException {
94+
String regex = ".*[\\s].*";
95+
96+
if (Pattern.matches(regex, notificationMeta)) {
97+
throw new SubscriptionValidationException("Wrong format of NotificationMeta: " + notificationMeta);
98+
}
99+
}
100+
101+
/*
102+
* Validation of notificationType parameter
103+
* Throws SubscriptionValidationException if validation of the parameter fails due to wrong
104+
* format of parameter.
105+
*
106+
* @param notificationType
107+
*/
108+
public void validateNotificationType(String notificationType) throws SubscriptionValidationException {
109+
String regexMail = "[\\s]*MAIL[\\\\s]*";
110+
String regexRestPost = "[\\s]*REST_POST[\\\\s]*";
111+
112+
if (!(Pattern.matches(regexMail, notificationType) || Pattern.matches(regexRestPost, notificationType))) {
113+
throw new SubscriptionValidationException("Wrong format of NotificationType: " + notificationType);
114+
}
115+
}
116+
117+
// public void validateJmespath(String jmespath) {
118+
//// TODO: Validator for Jmepath syntax need to be implemented here.
119+
// throw new SubscriptionValidationException("Wrong format/syntax of Jmepath: " + jmespath);
120+
// }
121+
}

0 commit comments

Comments
 (0)