Skip to content

Commit a15a327

Browse files
author
Anders Breid
authored
Improved error handeling and added some code comments. (#71)
* Fix multiple users may use frontend, request outside web gui. - Funktion to sent backend url as parameter added. - Gui users may now switch backend without affecting each other. - Before saving new instances the code will check if any new instances has been added to not overwrite instances created by other users. - switch-backend delete now recieves the backend to delete and not the backends to not delete. - If file is invalid or non existand it will be recreated. - Default back end properties may be specified in application-properties. - Default parameters may not be removed from gui but must be removed from application-properties first. - Unit tests have been added and updated. * Modified functional tests to work with the new utils changes. * Fixed some functional tests. * fixed code comments * Updated after comments * Fixed minor issues with switching back end * Added unittest for curl commando possibilities. * Extended time out to 11s * fixed some duplicated functions from comments. * updated after comments * updated after comments * Fixed functional tests * Missed to add 2 changes * updated after comments * Improved design for better useability. - Added loading indicator for buttons instead of displaying all then removing few. - Added refresh on change of back end instance. - Added more restfull way of working on front end back end details endpoints. - Better handeling of some error/messages sent to GUI from front end service. - Changed name of "Subscription Handeling" to just "Subscriptions" to be more generic. * Merge * Fix of path variable name * Improved useability and reduced read from disk * Improved useability and reduced read from disk * . * Improved error handeling, added some code comments * Fixed bug with name validation, better validations. * Fixed invalid id on one message * Renamed a class
1 parent d182ecf commit a15a327

File tree

3 files changed

+115
-48
lines changed

3 files changed

+115
-48
lines changed

src/main/java/com/ericsson/ei/frontend/utils/BackEndInstancesUtils.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class BackEndInstancesUtils {
5858
private BackEndInstanceFileUtils backEndInstanceFileUtils;
5959

6060
private List<BackEndInformation> backEndInformationList = new ArrayList<>();
61-
private boolean parsing = false;
61+
private boolean currentlyParsing = false;
6262
private boolean isRunningTests = false;
6363
private long nextTimeToParse = 0;
6464
private boolean savedSinceLastParsing = false;
@@ -227,7 +227,7 @@ private void parseBackEndInstances() {
227227
if (!parsingIsApplicable()) {
228228
return;
229229
}
230-
parsing = true;
230+
currentlyParsing = true;
231231

232232
try {
233233
JsonArray instances = backEndInstanceFileUtils.getInstancesFromFile();
@@ -240,32 +240,46 @@ private void parseBackEndInstances() {
240240
LOG.error("Failure when trying to parse json " + e.getMessage());
241241
}
242242

243-
parsing = false;
243+
currentlyParsing = false;
244244
savedSinceLastParsing = false;
245245
nextTimeToParse = System.currentTimeMillis() + (SECONDS_BETWEEN_PARSING * 1000);
246246
}
247247

248248
private boolean parsingIsApplicable() {
249-
// Parse for tests
249+
/**
250+
* If this is a test and test is dependent on parsing to be executed
251+
* we want to parse.
252+
*/
250253
if (isRunningTests) {
251254
return true;
252255
}
253256

254-
// Check if we currently are parsing.
255-
if (parsing) {
257+
/**
258+
* If parsing is ongoing wait for it to finish, we do not parse again
259+
* since it should already be up to date.
260+
*/
261+
if (currentlyParsing) {
256262
long stopTime = System.currentTimeMillis() + 10000;
257-
while (parsing && stopTime > System.currentTimeMillis()) {
258-
// Do nothing for maximum of 10 seconds.
263+
while (currentlyParsing && stopTime > System.currentTimeMillis()) {
264+
try {
265+
Thread.sleep(50);
266+
} catch (InterruptedException e) {
267+
}
259268
}
260269
return false;
261270
}
262271

263-
// Check if we did not parse for some time.
272+
/**
273+
* If parsing has not been done for a set amount of time,
274+
* then we want to parse.
275+
*/
264276
if (nextTimeToParse <= System.currentTimeMillis()) {
265277
return true;
266278
}
267279

268-
// Check if we did any save after last parsing.
280+
/**
281+
* If an update has happened to the file, then we should parse the file.
282+
*/
269283
if (savedSinceLastParsing ) {
270284
return true;
271285
}

src/main/resources/static/js/subscription.js

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -726,85 +726,114 @@ jQuery(document).ready(function () {
726726

727727
// /Start ## Save Subscription ##########################################
728728
$('div.modal-footer').on('click', 'button.save_record', function (event) {
729+
var error = false;
729730
event.stopPropagation();
730731
event.preventDefault();
731732
var notificationMessageKeyValuesArray = vm.subscription()[0].notificationMessageKeyValues();
732733
if (!vm.formpostkeyvaluepairs()) {
733734
notificationMessageKeyValuesArray[0].formkey = ""; // OBS must be empty when NOT using REST POST Form key/value pairs
734735
}
735736

736-
737+
$('.addSubscriptionErrors').hide();
737738
//START: Make sure all datatables field has a value
738-
if (!(/[a-z]|[A-Z]|[0-9]|[\_]/.test(String(vm.subscription()[0].subscriptionName()).slice(-1)))) {
739-
window.logMessages("Only numbers,letters and underscore is valid to type in subscriptionName field.");
740-
return;
739+
var subscriptionName = String(vm.subscription()[0].subscriptionName());
740+
// Validate SubscriptionName field
741+
if (subscriptionName == "") {
742+
window.logMessages("Error: SubscriptionName field must have a value");
743+
$('#noNameGiven').text("SubscriptionName must not be empty");
744+
$('#noNameGiven').show();
745+
error = true;
741746
}
742747

743-
//Currently a free-form field
744-
/*
745-
if (!(/[a-z]|[A-Z]|[0-9]|[\:\/\.]/.test(String(vm.subscription()[0].notificationMeta()).slice(-1)))) {
746-
window.logMessages("Only numbers and letters is valid to type in notificationMeta field.");
747-
return;
748-
}
749-
*/
750-
751-
if (vm.subscription()[0].subscriptionName() == "") {
752-
window.logMessages("Error: SubscriptionName field must have a value");
753-
return;
748+
// /(\W)/ Is a regex that matches anything that is not [A-Z,a-z,0-8] and _.
749+
var regExpression = /(\W)/g;
750+
if ((regExpression.test(subscriptionName))) {
751+
var invalidLetters = subscriptionName.match(regExpression);
752+
console.log("Invalid characters: [" + invalidLetters + "].")
753+
window.logMessages(
754+
"Only numbers,letters and underscore is valid to type in subscriptionName "
755+
+ " field. \nInvalid letters [" + invalidLetters + "].");
756+
$('#invalidLetters').text(
757+
"Only letters, numbers and underscore allowed! "
758+
+ "\nInvalid characters: [" + invalidLetters + "]");
759+
$('#invalidLetters').show();
760+
error = true;
754761
}
762+
763+
// Validate notificationType field
755764
if (vm.subscription()[0].notificationType() == null) {
756-
window.logMessages("Error: notificationType field must boolean a value");
757-
return;
765+
window.logMessages("Error: notificationType value needs to be set");
766+
$('#notificationTypeNotSet').text("> NotificationType must be set");
767+
$('#notificationTypeNotSet').show();
768+
error = true;
758769
}
770+
// Validate notificationMeta field
759771
if (vm.subscription()[0].notificationMeta() == "") {
760772
window.logMessages("Error: notificationMeta field must have a value");
761-
return;
773+
$('#noNotificationMetaGiven').text("NotificationMeta must not be empty");
774+
$('#noNotificationMetaGiven').show();
775+
error = true;
762776
}
777+
// Validate repeat field
763778
if (vm.subscription()[0].repeat() == null) {
764-
window.logMessages("Error: repeat field must have a boolean value");
765-
return;
779+
window.logMessages("Error: repeat field must be selected true or false");
780+
$('#repeatNotSet').text("> Repeat must be set");
781+
$('#repeatNotSet').show();
782+
error = true;
766783
}
767784
//END OF: Make sure all datatables field has a value
768785

769-
770786
//START: Check of other subscription fields values
771787
for (i = 0; i < notificationMessageKeyValuesArray.length; i++) {
772788
var test_key = ko.toJSON(notificationMessageKeyValuesArray[i].formkey);
773789
var test_value = ko.toJSON(notificationMessageKeyValuesArray[i].formvalue());
774790
if (vm.formpostkeyvaluepairs()) {
775791
if (test_key.replace(/\s/g, "") === '""' || test_value.replace(/\s/g, "") === '""') {
776792
window.logMessages("Error: Value & Key in notificationMessage must have a values!");
777-
return;
793+
$('#noNotificationKeyOrValue').text("NotificationMessage key and or values must be set");
794+
$('#noNotificationKeyOrValue').show();
795+
error = true;
778796
}
779797
}
780798
else {
781799
if (notificationMessageKeyValuesArray.length !== 1) {
782800
window.logMessages("Error: Only one array is allowed for notificationMessage when NOT using key/value pairs!");
783-
return;
801+
$('#notificationMessageKeyValuesArrayToLarge').text("Only one array is allowed for notificationMessage when NOT using key/value pairs");
802+
$('#notificationMessageKeyValuesArrayToLarge').show();
803+
error = true;
784804
}
785805
else if (test_key !== '""') {
786806
window.logMessages("Error: Key in notificationMessage must be empty when NOT using key/value pairs!");
787-
return;
807+
$('#keyInNotificationMessage').text("Key in notificationMessage must be empty when NOT using key/value pairs");
808+
$('#keyInNotificationMessage').show();
809+
error = true;
788810
}
789811
else if (test_value.replace(/\s/g, "") === '""') {
790812
window.logMessages("Error: Value in notificationMessage must have a value when NOT using key/value pairs!");
791-
return;
813+
$('#noNotificationMessage').text("Value in notificationMessage must have a value when NOT using key/value pairs");
814+
$('#noNotificationMessage').show();
815+
error = true;
792816
}
793817
}
794818
}
795819

796-
797820
var requirementsArray = vm.subscription()[0].requirements();
798821
for (i = 0; i < requirementsArray.length; i++) {
799822
var conditionsArray = requirementsArray[i].conditions();
800823
for (k = 0; k < conditionsArray.length; k++) {
801-
var test_me = ko.toJSON(conditionsArray[k].jmespath());
802-
if (test_me === '""') {
803-
window.logMessages("Error: jmepath field must have a value");
804-
return;
824+
var conditionToTest = ko.toJSON(conditionsArray[k].jmespath());
825+
if (conditionToTest === '""') {
826+
window.logMessages("Error: JMESPath field must have a value");
827+
$('.emptyCondition').text("Condition must not be empty");
828+
$('.emptyCondition').show();
829+
error = true;
805830
}
806831
}
807832
}
833+
// If errors return.
834+
if (error) {
835+
return;
836+
}
808837
//END: Check of other subscription fields values
809838

810839
var id = ko.toJSON(vm.subscription()[0].subscriptionName).trim();

src/main/resources/templates/subscription.html

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ <h3 id="subData">Subscription Data</h3>
8181
<h3 class="modal-title text-center" id="formHeader">Subscription Form</h3>
8282
</div>
8383
</div>
84-
<div class="modal-body form" align="left">
84+
<div class="modal-body form">
8585
<form action="#" id="form" class="form-horizontal">
8686
<input type="hidden" value="" name="id" />
8787
<div id="ViewModelDOMObject" class="form-body">
@@ -104,10 +104,12 @@ <h3 class="modal-title text-center" id="formHeader">Subscription Form</h3>
104104
<div class="form-group">
105105
<label class="pl-1 control-label font-weight-bold">SubscriptionName</label>
106106
<div>
107-
<font color="red" size="2">Only numbers,letters and underscore allowed</font>
107+
<!-- text injected from subscription.js -->
108+
<font class="addSubscriptionErrors" id="invalidLetters" color="red" size="2" />
109+
<font class="addSubscriptionErrors" id="noNameGiven" color="red" size="2" />
110+
108111
<input id="subscriptionNameInput" data-toggle="tooltip" title="Specify a SubsciptionName" data-bind="textInput:$data.subscriptionName"
109-
name="subscriptionName" placeholder="subscriptionName" class="form-control display-inline-table" type="text"
110-
/>
112+
name="subscriptionName" placeholder="subscriptionName" class="form-control display-inline-table" type="text"/>
111113
<span class="help-block"></span>
112114
</div>
113115
</div>
@@ -135,6 +137,7 @@ <h3 class="modal-title text-center" id="formHeader">Subscription Form</h3>
135137
optionsValue: 'value',
136138
value: $data.notificationType,
137139
optionsCaption: 'Choose...'"></select>
140+
<font class="addSubscriptionErrors" id="notificationTypeNotSet" color="red" size="2" />
138141
</div>
139142
</div>
140143

@@ -151,7 +154,18 @@ <h3 class="modal-title text-center" id="formHeader">Subscription Form</h3>
151154

152155
<div class="p-1 border border-primary form-group">
153156
<label class="control-label font-weight-bold">NotificationMessage</label>
154-
157+
<div>
158+
<font class="addSubscriptionErrors" id="noNotificationKeyOrValue" color="red" size="2" />
159+
</div>
160+
<div>
161+
<font class="addSubscriptionErrors" id="notificationMessageKeyValuesArrayToLarge" color="red" size="2" />
162+
</div>
163+
<div>
164+
<font class="addSubscriptionErrors" id="keyInNotificationMessage" color="red" size="2" />
165+
</div>
166+
<div>
167+
<font class="addSubscriptionErrors" id="noNotificationMessage" color="red" size="2" />
168+
</div>
155169
<table width="100%">
156170
<thead>
157171
<tr>
@@ -233,12 +247,14 @@ <h3 class="modal-title text-center" id="formHeader">Subscription Form</h3>
233247
optionsText: $data.repeat(),
234248
value: $data.repeat,
235249
optionsCaption: 'Choose...'"></select>
250+
<font class="addSubscriptionErrors" id="repeatNotSet" color="red" size="2" />
236251
<span class="help-block"></span>
237252
</div>
238253
</div>
239254
<div class="form-group">
240255
<label class="pl-1 control-label font-weight-bold">NotificationMeta</label>
241256
<div>
257+
<font class="addSubscriptionErrors" id="noNotificationMetaGiven" color="red" size="2" />
242258
<textarea id="metaData" data-toggle="tooltip" title="Specify notification meta data" data-bind="textInput:$data.notificationMeta"
243259
name="notificationMeta" placeholder="notificationMeta" class="form-control" type="text"></textarea>
244260
<span class="help-block"></span>
@@ -254,17 +270,24 @@ <h5>OR</h5>
254270
<img data-toggle="tooltip" data-placement="top" width="16" alt="Information" src="assets/images/information.png" title="Info: 'AND' is used between Conditions in Requirement groups, and 'OR' is used between Requirement groups"
255271
/>
256272
</div>
257-
<div class="p-1 border border-primary form-group">
273+
<div class="p-1 border border-primary form-group">
258274
<label class="control-label font-weight-bold">Requirement</label>
259275
<div id="requirementID">
260276

261277
<!-- ko foreach: requirements_item.conditions -->
262278
<div id="conditionID">
279+
263280
<!-- ko if: $index() !== 0 -->
264281
<h5 style="display:inline">AND</h5>
265282
<!-- /ko -->
266-
267283
<label style="display:inline" class="control-label font-weight-bold">Condition</label>
284+
285+
<!-- ko ifnot: $data.jmespath() -->
286+
<div>
287+
<font class="addSubscriptionErrors emptyCondition" color="red" size="2" />
288+
</div>
289+
<!-- /ko -->
290+
268291
<textarea data-toggle="tooltip" title="Write a Subscription Rule(JmePath)" data-bind="textInput:$data.jmespath()" name="jmespath"
269292
placeholder="jmespath" class="form-control" type="text"></textarea>
270293
<div class="pt-1 d-flex justify-content-end">
@@ -301,7 +324,8 @@ <h5 style="display:inline">AND</h5>
301324
<button data-toggle="tooltip" title="Save the changes to EI." type="button" id="btnSave" class="btn btn-primary save_record">
302325
Save
303326
</button>
304-
<button data-toggle="tooltip" title="Cancel and abort all changes" type="button" id="btnFormCancel" class="btn btn-danger" data-dismiss="modal">Cancel
327+
<button data-toggle="tooltip" title="Cancel and abort all changes" type="button" id="btnFormCancel" class="btn btn-danger" data-dismiss="modal">
328+
Cancel
305329
</button>
306330
</div>
307331
</div>

0 commit comments

Comments
 (0)