Permalink
Browse files

Merge pull request #7 from andrerod/servicebus

Servicebus
  • Loading branch information...
2 parents ad28988 + fc16eff commit c6246b2b049f6bcdd02570da11a8d658b95034ed André Rodrigues committed Oct 13, 2012
Showing with 190 additions and 0 deletions.
  1. +1 −0 lib/azure.js
  2. +102 −0 lib/services/core/servicebussettings.js
  3. +86 −0 test/services/core/servicebussettings-tests.js
  4. +1 −0 test/testlist.txt
View
@@ -152,6 +152,7 @@ exports.Logger = require('./diagnostics/logger');
exports.ConnectionStringParser = require('./services/core/connectionstringparser');
exports.ServiceSettings = require('./services/core/servicesettings');
exports.StorageServiceSettings = require('./services/core/storageservicesettings');
+exports.ServiceBusSettings = require('./services/core/servicebussettings');
exports.Validate = require('./util/validate');
/*
@@ -0,0 +1,102 @@
+/**
+* Copyright (c) Microsoft. All rights reserved.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+var url = require('url');
+
+var util = require('../../util/util');
+
+var ConnectionStringParser = require('./connectionstringparser');
+var ServiceSettings = require('./servicesettings');
+var Constants = require('../../util/constants');
+var ConnectionStringKeys = Constants.ConnectionStringKeys;
+var Validate = require('../../util/validate');
+
+exports = module.exports = ServiceBusSettings;
+
+var _serviceBusEndpointSetting = ServiceSettings.settingWithFunc(ConnectionStringKeys.SERVICE_BUS_ENDPOINT_NAME, Validate.getIsValidUri());
+var _wrapNameSetting = ServiceSettings.setting(ConnectionStringKeys.SHARED_SECRET_ISSUER_NAME);
+var _wrapPasswordSetting = ServiceSettings.setting(ConnectionStringKeys.SHARED_SECRET_VALUE_NAME);
+
+var validKeys = [
+ ConnectionStringKeys.SERVICE_BUS_ENDPOINT_NAME,
+ ConnectionStringKeys.SHARED_SECRET_ISSUER_NAME,
+ ConnectionStringKeys.SHARED_SECRET_VALUE_NAME
+];
+
+/**
+* Creates new service bus settings instance.
+*
+* @param {string} serviceBusEndpoint The Service Bus endpoint uri.
+* @param {string} namespace The service namespace.
+* @param {string} wrapName The wrap name.
+* @param {string} wrapPassword The wrap password.
+*/
+function ServiceBusSettings(serviceBusEndpointUri, namespace, wrapName, wrapPassword) {
+ this._namespace = namespace;
+ this._serviceBusEndpointUri = serviceBusEndpointUri;
+ this._wrapEndpointUri = 'https://' + namespace + '-sb.accesscontrol.windows.net/WRAPv0.9';
+ this._wrapName = wrapName;
+ this._wrapPassword = wrapPassword;
+}
+
+/**
+* Creates a ServiceBusSettings object from the given connection string.
+*
+* @param {string} connectionString The storage settings connection string.
+*
+* @return {ServiceBusSettings}
+*/
+ServiceBusSettings.createFromConnectionString = function (connectionString) {
+ var tokenizedSettings = ServiceSettings.parseAndValidateKeys(connectionString, validKeys);
+
+ var matchedSpecs = ServiceSettings.matchedSpecification(
+ tokenizedSettings,
+ ServiceSettings.allRequired(
+ _serviceBusEndpointSetting,
+ _wrapNameSetting,
+ _wrapPasswordSetting
+ )
+ );
+
+ if (matchedSpecs) {
+ var endpoint = util.tryGetValueInsensitive(
+ ConnectionStringKeys.SERVICE_BUS_ENDPOINT_NAME,
+ tokenizedSettings
+ );
+
+ // Parse the namespace part from the URI
+ var parsedUrl = url.parse(endpoint);
+ var namespace = parsedUrl.host.split('.')[0];
+
+ var issuerName = util.tryGetValueInsensitive(
+ ConnectionStringKeys.SHARED_SECRET_ISSUER_NAME,
+ tokenizedSettings
+ );
+
+ var issuerValue = util.tryGetValueInsensitive(
+ ConnectionStringKeys.SHARED_SECRET_VALUE_NAME,
+ tokenizedSettings
+ );
+
+ return new ServiceBusSettings(
+ endpoint,
+ namespace,
+ issuerName,
+ issuerValue
+ );
+ }
+
+ ServiceSettings.noMatch(connectionString);
+};
@@ -0,0 +1,86 @@
+/**
+* Copyright (c) Microsoft. All rights reserved.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+var should = require('should');
+var url = require('url');
+
+var testutil = require('../../util/util');
+var azure = testutil.libRequire('azure');
+var Constants = azure.Constants;
+var ConnectionStringKeys = Constants.ConnectionStringKeys;
+var ServiceBusSettings = azure.ServiceBusSettings;
+
+suite('storageservicesettings-tests', function () {
+ test('testCreateFromConnectionStringWithServiceBusAutomaticCase', function () {
+ // Setup
+ var expectedNamespace = 'mynamespace';
+ var expectedServiceBusEndpoint = 'https://' + expectedNamespace + '.servicebus.windows.net';
+ var expectedWrapName = 'myname';
+ var expectedWrapPassword = 'mypassword';
+ var expectedWrapEndpointUri = 'https://' + expectedNamespace + '-sb.accesscontrol.windows.net/WRAPv0.9';
+ var connectionString = 'Endpoint=' + expectedServiceBusEndpoint + ';SharedSecretIssuer=' + expectedWrapName + ';SharedSecretValue=' + expectedWrapPassword;
+
+ // Test
+ var actual = ServiceBusSettings.createFromConnectionString(connectionString);
+
+ // Assert
+ actual._namespace.should.equal(expectedNamespace);
+ actual._serviceBusEndpointUri.should.equal(expectedServiceBusEndpoint);
+ actual._wrapName.should.equal(expectedWrapName);
+ actual._wrapPassword.should.equal(expectedWrapPassword);
+ actual._wrapEndpointUri.should.equal(expectedWrapEndpointUri);
+ });
+
+ test('testCreateFromConnectionStringWithMissingServiceBusEndpointFail', function () {
+ // Setup
+ var connectionString = 'SharedSecretIssuer=name;SharedSecretValue=password';
+
+ // Test
+ (function() {
+ ServiceBusSettings.createFromConnectionString(connectionString);
+ }).should.throw('The provided connection string ' + connectionString + ' does not have complete configuration settings.');
+ });
+
+ test('testCreateFromConnectionStringWithInvalidServiceBusKeyFail', function () {
+ // Setup
+ var invalidKey = 'InvalidKey';
+ var connectionString = invalidKey + '=value;SharedSecretIssuer=name;SharedSecretValue=password';
+
+ // Test
+ (function() {
+ ServiceBusSettings.createFromConnectionString(connectionString);
+ }).should.throw('Invalid connection string setting key ' + invalidKey.toLowerCase());
+ });
+
+ test('testCreateFromConnectionStringWithCaseInvesitive', function () {
+ // Setup
+ var expectedNamespace = 'mynamespace';
+ var expectedServiceBusEndpoint = 'https://' + expectedNamespace + '.servicebus.windows.net';
+ var expectedWrapName = 'myname';
+ var expectedWrapPassword = 'mypassword';
+ var expectedWrapEndpointUri = 'https://' + expectedNamespace + '-sb.accesscontrol.windows.net/WRAPv0.9';
+ var connectionString = 'eNdPoinT=' + expectedServiceBusEndpoint + ';sHarEdsecRetiSsuer=' + expectedWrapName + ';shArEdsecrEtvAluE=' + expectedWrapPassword;
+
+ // Test
+ var actual = ServiceBusSettings.createFromConnectionString(connectionString);
+
+ // Assert
+ actual._namespace.should.equal(expectedNamespace);
+ actual._serviceBusEndpointUri.should.equal(expectedServiceBusEndpoint);
+ actual._wrapName.should.equal(expectedWrapName);
+ actual._wrapPassword.should.equal(expectedWrapPassword);
+ actual._wrapEndpointUri.should.equal(expectedWrapEndpointUri);
+ });
+});
View
@@ -12,6 +12,7 @@ services/core/serviceclient-tests.js
services/core/exponentialretrypolicyfilter-tests.js
services/core/linearretrypolicyfilter-tests.js
services/core/servicesettings-tests.js
+services/core/servicebussettings-tests.js
services/core/storageservicesettings-tests.js
services/queue/queueservice-tests.js
services/table/batchserviceclient-tests.js

0 comments on commit c6246b2

Please sign in to comment.