Files
threshold-ui/addons/api/tests/unit/serializers/host-set-test.js
T

218 lines
6.1 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
2020-08-19 18:38:18 -04:00
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
2020-09-16 14:29:02 -04:00
module('Unit | Serializer | host set', function (hooks) {
2020-08-19 18:38:18 -04:00
setupTest(hooks);
test('it serializes host sets normally, without host_ids', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const record = store.createRecord('host-set', {
name: 'Host Set 1',
compositeType: 'static',
2020-08-19 18:38:18 -04:00
description: 'Description',
2020-09-10 17:59:31 -04:00
host_catalog_id: '123',
2020-09-16 14:29:02 -04:00
version: 1,
2020-08-19 18:38:18 -04:00
});
assert.deepEqual(record.serialize(), {
2020-08-19 18:38:18 -04:00
name: 'Host Set 1',
description: 'Description',
2020-09-10 17:59:31 -04:00
host_catalog_id: '123',
2020-09-16 14:29:02 -04:00
version: 1,
2020-08-19 18:38:18 -04:00
});
});
test('it serializes only host_ids when `adapterOptions.hostIDs` is true', function (assert) {
2020-08-19 18:38:18 -04:00
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('host-set');
const record = store.createRecord('host-set', {
name: 'Host Set 1',
description: 'Description',
2020-09-16 14:29:02 -04:00
host_ids: [{ value: '1' }, { value: '2' }, { value: '3' }],
version: 1,
2020-08-19 18:38:18 -04:00
});
const snapshot = record._createSnapshot();
snapshot.adapterOptions = {
2020-09-16 14:29:02 -04:00
hostIDs: ['4', '5'],
2020-08-19 18:38:18 -04:00
};
const serializedRecord = serializer.serialize(snapshot);
assert.deepEqual(serializedRecord, {
host_ids: ['4', '5'],
2020-09-16 14:29:02 -04:00
version: 1,
2020-08-19 18:38:18 -04:00
});
});
test('it serializes host sets as expected for Aws plugin', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const record = store.createRecord('host-set', {
name: 'Host Set 1',
compositeType: 'aws',
description: 'Description',
host_catalog_id: '123',
version: 1,
preferred_endpoints: [{ value: 'option 1' }, { value: 'option 2' }],
sync_interval_seconds: 1,
filters: [{ value: 'filter 1' }, { value: 'filter 2' }],
});
assert.deepEqual(record.serialize(), {
name: 'Host Set 1',
description: 'Description',
host_catalog_id: '123',
version: 1,
preferred_endpoints: ['option 1', 'option 2'],
sync_interval_seconds: 1,
attributes: {
filters: ['filter 1', 'filter 2'],
},
});
});
test('it serializes host sets as expected for Azure plugin', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const record = store.createRecord('host-set', {
name: 'Host Set 1',
compositeType: 'azure',
description: 'Description',
host_catalog_id: '123',
version: 1,
preferred_endpoints: [{ value: 'option 1' }, { value: 'option 2' }],
sync_interval_seconds: 1,
2022-12-02 13:44:11 -05:00
filter_string: 'filter 1 && filter 2',
});
assert.deepEqual(record.serialize(), {
name: 'Host Set 1',
description: 'Description',
host_catalog_id: '123',
version: 1,
preferred_endpoints: ['option 1', 'option 2'],
sync_interval_seconds: 1,
attributes: {
filter: 'filter 1 && filter 2',
},
});
});
2022-02-03 11:26:34 -08:00
test('it normalizes records with array fields', function (assert) {
2020-08-19 18:38:18 -04:00
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('host-set');
const hostSetModelClass = store.createRecord('host-set').constructor;
const payload = {
id: '1',
name: 'Host Set 1',
type: 'static',
2020-09-16 14:29:02 -04:00
host_ids: ['1', '2', '3'],
2022-02-03 11:26:34 -08:00
preferred_endpoints: ['endpoint 1', 'endpoint 2'],
filters: ['filter 1', 'filter 2'],
2020-08-19 18:38:18 -04:00
};
const normalized = serializer.normalizeSingleResponse(
store,
hostSetModelClass,
payload
);
2020-08-19 18:38:18 -04:00
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'host-set',
attributes: {
type: 'static',
2021-08-04 17:58:08 -04:00
authorized_actions: [],
2020-08-19 18:38:18 -04:00
name: 'Host Set 1',
2022-11-21 19:07:46 -05:00
host_ids: ['1', '2', '3'],
2022-02-03 11:26:34 -08:00
preferred_endpoints: [
{ value: 'endpoint 1' },
{ value: 'endpoint 2' },
],
filters: [{ value: 'filter 1' }, { value: 'filter 2' }],
2020-08-19 18:38:18 -04:00
},
relationships: {},
},
});
});
2020-09-12 11:20:38 -04:00
2022-02-03 11:26:34 -08:00
test('it normalizes missing array fields to empty array', function (assert) {
2022-02-01 11:14:43 -08:00
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('host-set');
const hostSet = store.createRecord('host-set').constructor;
const payload = {
id: '1',
name: 'Host Set 1',
scope: { id: 'o_123' },
};
const normalized = serializer.normalizeSingleResponse(
store,
hostSet,
payload
);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'host-set',
attributes: {
authorized_actions: [],
name: 'Host Set 1',
2022-07-18 11:30:53 -07:00
scope: { id: 'o_123', scope_id: 'o_123' },
2022-02-01 11:14:43 -08:00
host_ids: [],
preferred_endpoints: [],
2022-02-03 11:26:34 -08:00
filters: [],
2020-09-12 11:20:38 -04:00
},
relationships: {},
},
});
});
2022-12-02 13:44:11 -05:00
test('it normalizes filter correctly', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('host-set');
const hostSetModelClass = store.createRecord('host-set').constructor;
const payload = {
id: '1',
name: 'Host Set 1',
type: 'plugin',
host_ids: ['1', '2', '3'],
preferred_endpoints: ['endpoint 1', 'endpoint 2'],
attributes: {
filter: 'filter',
},
};
const normalized = serializer.normalizeSingleResponse(
store,
hostSetModelClass,
payload
);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'host-set',
attributes: {
type: 'plugin',
authorized_actions: [],
name: 'Host Set 1',
host_ids: ['1', '2', '3'],
preferred_endpoints: [
{ value: 'endpoint 1' },
{ value: 'endpoint 2' },
],
filters: [],
filter_string: 'filter',
},
relationships: {},
},
});
});
2020-08-19 18:38:18 -04:00
});