Browse Source

PROD-1221: More XML serialization/parser test cases for poll

Blake Schneider 5 years ago
parent
commit
22be62851b

+ 9 - 9
__tests__/unit/processes/event.spec.js

@@ -11,7 +11,7 @@ const {
   createdEventInvalidEventId1,
 } = require('../xml/event/js-requests');
 
-const { oadrPoll1 } = require('../xml/poll/js-requests');
+const { poll } = require('../xml/poll/js-requests');
 
 const { generatedFromNantumEvent1 } = require('../xml/event/js-responses');
 const { sampleEvent1 } = require('../modules/nantum-responses');
@@ -67,18 +67,18 @@ describe('Event', function() {
 
   describe('poll and updateOptType', function() {
     it('should return the same event on subsequent polls if it has not been opted', async () => {
-      const venId = oadrPoll1.venId;
+      const venId = poll.venId;
       const commonName = v4()
         .replace(/-/g, '')
         .substring(0, 12);
       const pollResponse1 = await rewired.pollForEvents(
-        oadrPoll1,
+        poll,
         commonName,
         venId,
       );
       expect(pollResponse1.events.length).to.eql(1);
       const pollResponse2 = await rewired.pollForEvents(
-        oadrPoll1,
+        poll,
         commonName,
         venId,
       );
@@ -86,12 +86,12 @@ describe('Event', function() {
     });
 
     it('should not return an opted event in subsequent poll response', async () => {
-      const venId = oadrPoll1.venId;
+      const venId = poll.venId;
       const commonName = v4()
         .replace(/-/g, '')
         .substring(0, 12);
       const pollResponse1 = await rewired.pollForEvents(
-        oadrPoll1,
+        poll,
         commonName,
         venId,
       );
@@ -99,7 +99,7 @@ describe('Event', function() {
 
       await rewired.updateOptType(createdEventMax, commonName, venId);
       const pollResponse2 = await rewired.pollForEvents(
-        oadrPoll1,
+        poll,
         commonName,
         venId,
       );
@@ -107,7 +107,7 @@ describe('Event', function() {
     });
 
     it('should fail when an invalid eventId is optedIn', async () => {
-      const venId = oadrPoll1.venId;
+      const venId = poll.venId;
       const commonName = v4()
         .replace(/-/g, '')
         .substring(0, 12);
@@ -124,7 +124,7 @@ describe('Event', function() {
     });
 
     it('should fail when an old modificationNumber is specified', async () => {
-      const venId = oadrPoll1.venId;
+      const venId = poll.venId;
       const commonName = v4()
         .replace(/-/g, '')
         .substring(0, 12);

+ 2 - 8
__tests__/unit/xml/poll/js-requests.js

@@ -1,16 +1,10 @@
 'use strict';
 
-const oadrPoll1 = {
+const poll = {
   _type: 'oadrPoll',
   venId: 'D8:1D:4B:20:5A:65:4C:50:32:FA',
 };
 
-const oadrResponse1 = {
-  _type: 'oadrResponse',
-  venId: 'D8:1D:4B:20:5A:65:4C:50:32:FA',
-};
-
 module.exports = {
-  oadrPoll1,
-  oadrResponse1,
+  poll,
 };

+ 11 - 4
__tests__/unit/xml/poll/js-responses.js

@@ -1,13 +1,20 @@
 'use strict';
 
-const oadrResponse1 = {
+const responseMin = {
+  _type: 'oadrResponse',
+  responseCode: '200',
+  responseRequestId: '9383fc5946cb0e14ef5a',
+};
+
+const responseMax = {
   _type: 'oadrResponse',
   responseCode: '200',
   responseDescription: 'OK',
-  responseRequestId: '',
-  venId: 'aa',
+  responseRequestId: '9383fc5946cb0e14ef5a',
+  venId: 'D8:1D:4B:20:5A:65:4C:50:32:FA',
 };
 
 module.exports = {
-  oadrResponse1,
+  responseMin,
+  responseMax,
 };

+ 19 - 19
__tests__/unit/xml/poll/oadr-poll.spec.js

@@ -3,36 +3,36 @@
 const { expect } = require('chai');
 
 const { parse, serialize } = require('../../../../xml/poll/oadr-poll');
-const { oadrPoll1Xml } = require('./xml-requests');
-const { oadrPoll1 } = require('./js-requests');
+const { pollXml, pollMissingRequiredXml } = require('./xml-requests');
+const { poll } = require('./js-requests');
 
 describe('Poll', function() {
   describe('parse', function() {
-    let parsedResponse;
-
-    before(async () => {
-      parsedResponse = await parse(oadrPoll1Xml);
-    });
-
-    it('successfully parses valid message', function() {
-      expect(parsedResponse.venId).to.eql('D8:1D:4B:20:5A:65:4C:50:32:FA');
+    it('successfully parses xml', async function() {
+      const parsedResponse = await parse(pollXml);
+      expect(parsedResponse).to.eql(poll);
     });
 
     it('successfully parses serialized value', async function() {
-      const serialized = serialize(oadrPoll1);
+      const serialized = serialize(poll);
       const parsedResponse = await parse(serialized);
-      expect(parsedResponse).to.eql(oadrPoll1);
+      expect(parsedResponse).to.eql(poll);
     });
-  });
 
-  describe('serialize', function() {
-    let serializedResponse;
-
-    before(async () => {
-      serializedResponse = await serialize(oadrPoll1);
+    it('throws Error on missing required field', async function() {
+      let lastError;
+      try {
+        await parse(pollMissingRequiredXml);
+      } catch (e) {
+        lastError = e;
+      }
+      expect(lastError).to.be.an('error');
     });
+  });
 
-    it('successfully serializes valid message', function() {
+  describe('serialize', function() {
+    it('successfully serializes valid object', async function() {
+      const serializedResponse = await serialize(poll);
       expect(serializedResponse).to.eql(
         '<oadr2b:oadrPayload xmlns:oadr2b="http://openadr.org/oadr-2.0b/2012/07"><oadr2b:oadrSignedObject><oadr2b:oadrPoll xmlns:ei="http://docs.oasis-open.org/ns/energyinterop/201110" ei:schemaVersion="2.0b"><ei:venID xmlns:ei="http://docs.oasis-open.org/ns/energyinterop/201110">D8:1D:4B:20:5A:65:4C:50:32:FA</ei:venID></oadr2b:oadrPoll></oadr2b:oadrSignedObject></oadr2b:oadrPayload>',
       );

File diff suppressed because it is too large
+ 29 - 20
__tests__/unit/xml/poll/oadr-response.spec.js


File diff suppressed because it is too large
+ 10 - 2
__tests__/unit/xml/poll/xml-requests.js


File diff suppressed because it is too large
+ 28 - 3
__tests__/unit/xml/poll/xml-responses.js


+ 2 - 1
xml/poll/oadr-response.js

@@ -37,10 +37,11 @@ async function parse(input) {
   const result = {
     _type: 'oadrResponse',
     responseCode: code,
-    responseDescription: description,
     responseRequestId: requestId,
   };
 
+  if (description != null) result.responseDescription = description;
+
   if (code < 200 || code >= 300) {
     return result;
   }