From 9c7778c63328a68f4b57140b1b73c3dd183e70d7 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Tue, 13 Dec 2016 18:19:41 +0100
Subject: [PATCH] ReactionOverlay must be created with valid data and Promises
 rejection contains stacktrace

---
 frontend-js/package.json                      |  2 +-
 frontend-js/src/main/js/ServerConnector.js    |  2 +
 .../src/main/js/map/AbstractCustomMap.js      | 10 +--
 frontend-js/src/main/js/map/CustomMap.js      |  3 +-
 .../src/main/js/map/data/LayoutData.js        |  2 +
 frontend-js/src/main/js/map/data/MapModel.js  |  2 +
 .../main/js/map/overlay/ReactionOverlay.js    | 76 +++++++++----------
 frontend-js/src/test/js/helper.js             | 33 +++++++-
 .../js/map/overlay/ReactionOverlay-test.js    | 71 +++++------------
 frontend-js/src/test/js/mocha-config.js       |  5 ++
 10 files changed, 103 insertions(+), 103 deletions(-)

diff --git a/frontend-js/package.json b/frontend-js/package.json
index cd96359e23..699ab57282 100644
--- a/frontend-js/package.json
+++ b/frontend-js/package.json
@@ -11,6 +11,7 @@
   "author": "Piotr Gawron",
   "devDependencies": {
     "assert": "1.4.1",
+    "bluebird": "^3.4.6",
     "browserify": "^13.1.1",
     "chai": "^3.5.0",
     "exorcist": "^0.4.0",
@@ -25,7 +26,6 @@
   "dependencies": {
     "log4js": "0.6.38",
     "pileup": "^0.6.8",
-    "promise": "^7.1.1",
     "request": "^2.79.0",
     "xmlhttprequest": "^1.8.0"
   }
diff --git a/frontend-js/src/main/js/ServerConnector.js b/frontend-js/src/main/js/ServerConnector.js
index 895faa5123..0b66432ce2 100644
--- a/frontend-js/src/main/js/ServerConnector.js
+++ b/frontend-js/src/main/js/ServerConnector.js
@@ -1,5 +1,7 @@
 "use strict";
 
+var Promise = require("bluebird");
+
 var logger = require('./logger');
 
 var request = require('request');
diff --git a/frontend-js/src/main/js/map/AbstractCustomMap.js b/frontend-js/src/main/js/map/AbstractCustomMap.js
index 23085cfbdc..ef4260b3c2 100644
--- a/frontend-js/src/main/js/map/AbstractCustomMap.js
+++ b/frontend-js/src/main/js/map/AbstractCustomMap.js
@@ -1,5 +1,7 @@
 "use strict";
 
+var Promise = require("bluebird");
+
 var logger = require('../logger');
 var functions = require('../Functions');
 
@@ -605,12 +607,8 @@ AbstractCustomMap.prototype._showSelectedLayout = function(layoutId, index, leng
 
       var layoutReaction = layoutReactions[j];
       self.getModel().getReactionById(layoutReaction.getId()).then(function(reactionData) {
-        if (reactionData === null || reactionData === undefined) {
-          throw new Error("Cannot find data for reaction: " + layoutReaction.getId());
-        } else {
-          var reactionOverlay = new ReactionOverlay(layoutReaction, reactionData, self, length === 1);
-          self.selectedLayoutOverlays[layoutId].push(reactionOverlay);
-        }
+        var reactionOverlay = new ReactionOverlay(layoutReaction, reactionData, self, length === 1);
+        self.selectedLayoutOverlays[layoutId].push(reactionOverlay);
         elementsProcessed++;
         elementProcessedFun();
       });
diff --git a/frontend-js/src/main/js/map/CustomMap.js b/frontend-js/src/main/js/map/CustomMap.js
index 1d76780c12..1b373bcf73 100644
--- a/frontend-js/src/main/js/map/CustomMap.js
+++ b/frontend-js/src/main/js/map/CustomMap.js
@@ -1,5 +1,7 @@
 "use strict";
 
+var Promise = require("bluebird");
+
 var logger = require('../logger');
 var functions = require('../Functions');
 
@@ -1059,7 +1061,6 @@ CustomMap.prototype.addSelectedLayout = function(identifier) {
     } else {
       self.selectedLayouts[identifier] = true;
 
-      logger.debug("!");
       self.getModel().initLayoutDataByLayoutId(identifier).then(function() {
         self.refreshSelectedLayouts();
         ServerConnector.setVisibleLayouts(JSON.stringify(self.getSelectedLayouts()));
diff --git a/frontend-js/src/main/js/map/data/LayoutData.js b/frontend-js/src/main/js/map/data/LayoutData.js
index f4eb14e31d..132d1867c6 100644
--- a/frontend-js/src/main/js/map/data/LayoutData.js
+++ b/frontend-js/src/main/js/map/data/LayoutData.js
@@ -1,5 +1,7 @@
 "use strict";
 
+var Promise = require("bluebird");
+
 var logger = require('../../logger');
 
 var LayoutAlias = require('./LayoutAlias');
diff --git a/frontend-js/src/main/js/map/data/MapModel.js b/frontend-js/src/main/js/map/data/MapModel.js
index 5354fa3cd3..f8f85679b6 100644
--- a/frontend-js/src/main/js/map/data/MapModel.js
+++ b/frontend-js/src/main/js/map/data/MapModel.js
@@ -1,5 +1,7 @@
 "use strict";
 
+var Promise = require("bluebird");
+
 var logger = require('../../logger');
 
 var Alias = require('./Alias');
diff --git a/frontend-js/src/main/js/map/overlay/ReactionOverlay.js b/frontend-js/src/main/js/map/overlay/ReactionOverlay.js
index 32b256507b..149fccc074 100644
--- a/frontend-js/src/main/js/map/overlay/ReactionOverlay.js
+++ b/frontend-js/src/main/js/map/overlay/ReactionOverlay.js
@@ -10,16 +10,16 @@ var AbstractOverlayElement = require('./AbstractOverlayElement');
  * layout.
  * 
  * @param layoutReaction
- *            {@link LayoutReaction} for which overlay is created
+ *          {@link LayoutReaction} for which overlay is created
  * @param reactionData
- *            {@link Reaction data} of the reaction
+ *          {@link Reaction data} of the reaction
  * @param map
- *            {@link AbstractCustomMap} where overlay should be placed
+ *          {@link AbstractCustomMap} where overlay should be placed
  * @param customized
- *            if true then coloring from layoutReaction is taken, if false then
- *            default is used. Second option is used when more than one layout
- *            is visualized (there is no simple way to visualize few layouts at
- *            the same time)
+ *          if true then coloring from layoutReaction is taken, if false then
+ *          default is used. Second option is used when more than one layout is
+ *          visualized (there is no simple way to visualize few layouts at the
+ *          same time)
  */
 function ReactionOverlay(layoutReaction, reactionData, map, customized, id) {
   // call super constructor
@@ -36,21 +36,17 @@ function ReactionOverlay(layoutReaction, reactionData, map, customized, id) {
 
   this.customized = customized;
 
-  if (layoutReaction !== undefined && layoutReaction !== null) {
-    if (layoutReaction.width) {
-      this.width = layoutReaction.width;
-    }
-    this.color = functions.overlayToColor(layoutReaction);
+  if (layoutReaction === undefined || layoutReaction === null || reactionData === undefined || reactionData === null) {
+    throw new Error("Reaction must be defined");
   }
 
-  if (reactionData !== null && reactionData !== undefined) {
-    this.init();
-    this._id = reactionData.getId();
-  } else if (layoutReaction !== undefined && layoutReaction !== null) {
-    this._id = layoutReaction.getId();
-  } else {
-    this._id = id;
+  if (layoutReaction.width) {
+    this.width = layoutReaction.width;
   }
+  this.color = functions.overlayToColor(layoutReaction);
+
+  this.init();
+  this._id = reactionData.getId();
 }
 
 ReactionOverlay.prototype = Object.create(AbstractOverlayElement.prototype);
@@ -79,7 +75,7 @@ ReactionOverlay.prototype.getBounds = function() {
  * Sets Google map to this {@link ReactionOverlay}.
  * 
  * @param map
- *            map to set
+ *          map to set
  */
 ReactionOverlay.prototype.setMap = function(map) {
   for (var i = 0; i < this.gmapObjects.length; i++) {
@@ -101,8 +97,7 @@ ReactionOverlay.prototype.isShown = function() {
   if (this.gmapObjects.length === 0) {
     return false;
   }
-  if (this.gmapObjects[0].getMap() === null
-      || this.gmapObjects[0].getMap() === undefined) {
+  if (this.gmapObjects[0].getMap() === null || this.gmapObjects[0].getMap() === undefined) {
     return false;
   }
 
@@ -129,7 +124,7 @@ ReactionOverlay.prototype.hide = function() {
  * in customized mode.
  * 
  * @param color
- *            new color value
+ *          new color value
  */
 ReactionOverlay.prototype.setColor = function(color) {
   this.color = color;
@@ -140,7 +135,7 @@ ReactionOverlay.prototype.setColor = function(color) {
  * visualized in customized mode.
  * 
  * @param width
- *            new width value
+ *          new width value
  */
 ReactionOverlay.prototype.setWidth = function(width) {
   if (width !== undefined || width !== null) {
@@ -179,15 +174,15 @@ ReactionOverlay.prototype.getWidth = function() {
  * he should investigate the reaction manually.
  * 
  * @param startX
- *            see {AliasOverlay} class for detailes, in this implementation the
- *            only important information is that when there are more than one
- *            layout visualized at least one of the two parameters (startX,endX)
- *            will differ from {0.0, 1.0} values
+ *          see {AliasOverlay} class for detailes, in this implementation the
+ *          only important information is that when there are more than one
+ *          layout visualized at least one of the two parameters (startX,endX)
+ *          will differ from {0.0, 1.0} values
  * @param endX
- *            see {AliasOverlay} class for detailes, in this implementation the
- *            only important information is that when there are more than one
- *            layout visualized at least one of the two parameters (startX,endX)
- *            will differ from {0.0, 1.0} values
+ *          see {AliasOverlay} class for detailes, in this implementation the
+ *          only important information is that when there are more than one
+ *          layout visualized at least one of the two parameters (startX,endX)
+ *          will differ from {0.0, 1.0} values
  */
 ReactionOverlay.prototype.setBoundsForAlias = function(startX, endX) {
   if (this.isCustomized() && (startX > 0 || endX < 1)) {
@@ -229,13 +224,13 @@ ReactionOverlay.prototype.changedToCustimzed = function() {
  * Creates {google.maps.Polyline} from input data.
  * 
  * @param line
- *            raw data about line taken from server
+ *          raw data about line taken from server
  * @param color
- *            color that should be used for visualization of the line
+ *          color that should be used for visualization of the line
  * @param width
- *            width that should be used for visualization of the line
+ *          width that should be used for visualization of the line
  * @param map
- *            {google.maps.Map} where line shoul dbe visualized
+ *          {google.maps.Map} where line shoul dbe visualized
  * @returns {google.maps.Polyline} from input data
  */
 ReactionOverlay.createLine = function(line, color, width, map) {
@@ -291,20 +286,17 @@ ReactionOverlay.prototype.init = function() {
   var googleLine;
   for (i = 0; i < this.reactionData.startLines.length; i++) {
     line = this.reactionData.startLines[i];
-    googleLine = ReactionOverlay.createLine(line, this.color, this.width, this
-        .getCustomMap());
+    googleLine = ReactionOverlay.createLine(line, this.color, this.width, this.getCustomMap());
     this.gmapObjects.push(googleLine);
   }
   for (i = 0; i < this.reactionData.endLines.length; i++) {
     line = this.reactionData.endLines[i];
-    googleLine = ReactionOverlay.createLine(line, this.color, this.width, this
-        .getCustomMap());
+    googleLine = ReactionOverlay.createLine(line, this.color, this.width, this.getCustomMap());
     this.gmapObjects.push(googleLine);
   }
   for (i = 0; i < this.reactionData.midLines.length; i++) {
     line = this.reactionData.midLines[i];
-    googleLine = ReactionOverlay.createLine(line, this.color, this.width, this
-        .getCustomMap());
+    googleLine = ReactionOverlay.createLine(line, this.color, this.width, this.getCustomMap());
     this.gmapObjects.push(googleLine);
   }
   if (!this.isCustomized()) {
diff --git a/frontend-js/src/test/js/helper.js b/frontend-js/src/test/js/helper.js
index 305e20740a..774533eb22 100644
--- a/frontend-js/src/test/js/helper.js
+++ b/frontend-js/src/test/js/helper.js
@@ -57,9 +57,16 @@ Helper.prototype.createLayoutAlias = function() {
   return result;
 };
 
-Helper.prototype.createLayoutReaction = function() {
+Helper.prototype.createLayoutReaction = function(reaction) {
+  var reactionId;
+  if (reaction instanceof Reaction) {
+    reactionId = reaction.getId();
+  } else {
+    reactionId = this.idCounter++;
+  }
+
   var result = new LayoutReaction({
-    idObject : this.idCounter++,
+    idObject : reactionId,
     width : 3.4,
     color : {
       a : 24,
@@ -78,7 +85,27 @@ Helper.prototype.createReaction = function(map) {
   }
   var result = new Reaction({
     idObject : this.idCounter++,
-    lines : [],
+    lines : [ {
+      start : {
+        x : 434.8904109589041,
+        y : 85.0
+      },
+      end : {
+        x : 410.8341500923087,
+        y : 104.95576185524392
+      },
+      type : "START"
+    }, {
+      start : {
+        x : 404.6769250286157,
+        y : 110.06345991944379
+      },
+      end : {
+        x : 380.62066416202026,
+        y : 130.0192217746877
+      },
+      type : "END"
+    } ],
     centerPoint : new google.maps.Point(0, 0),
     modelId : mapId,
   });
diff --git a/frontend-js/src/test/js/map/overlay/ReactionOverlay-test.js b/frontend-js/src/test/js/map/overlay/ReactionOverlay-test.js
index fd34ca57cf..023ca47871 100644
--- a/frontend-js/src/test/js/map/overlay/ReactionOverlay-test.js
+++ b/frontend-js/src/test/js/map/overlay/ReactionOverlay-test.js
@@ -18,69 +18,40 @@ describe('ReactionOverlay', function() {
 
   it("Constructor", function() {
     var map = helper.createAbstractCustomMap();
+    var reaction = helper.createReaction(map);
+    var layoutReaction = helper.createLayoutReaction(reaction);
 
-    var reaction = new ReactionOverlay(null, null, map, false);
+    var reactionOverlay = new ReactionOverlay(layoutReaction, reaction, map, false);
 
-    assert.ok(reaction.gmapObjects);
-    assert.ok(reaction.getColor());
-    assert.ok(reaction.getWidth());
-    assert.ok(reaction.getBounds());
+    assert.ok(reactionOverlay.gmapObjects);
+    assert.ok(reactionOverlay.getColor());
+    assert.ok(reactionOverlay.getWidth());
+    assert.ok(reactionOverlay.getBounds());
+    assert.ok(reactionOverlay.getReactionData());
+    assert.ok(reactionOverlay.getCustomMap());
+    assert.ok(reactionOverlay.getId());
 
   });
 
   it("show", function() {
     var map = helper.createAbstractCustomMap();
+    var reaction = helper.createReaction(map);
+    var layoutReaction = helper.createLayoutReaction(reaction);
 
-    var javaObject = {
-      lines : [ {
-        start : Object,
-        end : Object,
-        type : "START"
-      }, {
-        start : Object,
-        end : Object,
-        type : "END"
-      }, {
-        start : Object,
-        end : Object,
-        type : "MIDDLE"
-      } ],
-      modelId : 319,
-      idObject : "13178",
-      centerPoint : new google.maps.Point(1, 2)
-    };
-
-    var react = new Reaction(javaObject);
-
-    var reaction = new ReactionOverlay(null, react, map, false);
-
-    reaction.hide();
-    assert.equal(reaction.isShown(), false);
-
-    reaction.show();
-    assert.ok(reaction.isShown());
+    var reactionOverlay = new ReactionOverlay(layoutReaction, reaction, map, false);
 
-    assert.equal(logger.getWarnings().length, 0);
-
-    reaction.show();
-    assert.ok(reaction.isShown());
-    assert.equal(logger.getWarnings().length, 1);
+    reactionOverlay.hide();
+    assert.equal(reactionOverlay.isShown(), false);
 
-  });
+    reactionOverlay.show();
+    assert.ok(reactionOverlay.isShown());
 
-  it("Constructor 2", function() {
-    var map = helper.createAbstractCustomMap();
+    assert.equal(logger.getWarnings().length, 0);
 
-    var react = helper.createReaction();
-    var reaction = new ReactionOverlay(null, react, map, false);
+    reactionOverlay.show();
+    assert.ok(reactionOverlay.isShown());
+    assert.equal(logger.getWarnings().length, 1);
 
-    assert.ok(reaction.gmapObjects);
-    assert.ok(reaction.getColor());
-    assert.ok(reaction.getWidth());
-    assert.ok(reaction.getBounds());
-    assert.ok(reaction.getReactionData());
-    assert.ok(reaction.getCustomMap());
-    assert.ok(reaction.getId());
   });
 
   it("createline", function() {
diff --git a/frontend-js/src/test/js/mocha-config.js b/frontend-js/src/test/js/mocha-config.js
index dfb4a4bae3..f8a84ea24b 100644
--- a/frontend-js/src/test/js/mocha-config.js
+++ b/frontend-js/src/test/js/mocha-config.js
@@ -1,5 +1,8 @@
 "use strict";
 
+var Promise = require("bluebird");
+
+
 // GLOBAL configuration
 var jsdom = require('jsdom');
 global.document = jsdom.jsdom(undefined);
@@ -22,6 +25,8 @@ global.navigator = {
 var logger = require('./logger');
 
 beforeEach(function() {
+  Promise.longStackTraces();
+
   logger.flushBuffer();
   
   global.testDiv = document.createElement("div");
-- 
GitLab