Skip to content

Commit

Permalink
fixed two tests, updated one persistence method to match its document…
Browse files Browse the repository at this point in the history
…ed/original return format
  • Loading branch information
jvigliotta committed Jan 3, 2025
1 parent 6404335 commit 23524cc
Show file tree
Hide file tree
Showing 3 changed files with 409 additions and 310 deletions.
3 changes: 1 addition & 2 deletions src/persistence/MCWSPersistenceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export default class MCWSPersistenceProvider extends BaseMCWSPersistenceProvider

try {
await persistenceNamespace.opaqueFile(key).create(model);

return true;
} catch (error) {
console.warn('MCWSPersistneceProvider:create', error);
Expand Down Expand Up @@ -78,7 +77,7 @@ export default class MCWSPersistenceProvider extends BaseMCWSPersistenceProvider
} catch (error) {
console.warn('MCWSPersistneceProvider:update', error);

return;
return false;
}
}

Expand Down
340 changes: 224 additions & 116 deletions src/persistence/test/MCWSNamespaceModelProviderSpec.js
Original file line number Diff line number Diff line change
@@ -1,138 +1,246 @@
/*global define,describe,beforeEach,jasmine,Promise,it,expect*/
define([
// '../src/MCWSNamespaceModelProvider'
], function (
// MCWSNamespaceModelProvider
) {
'use strict';

xdescribe('MCWSNamespaceModelProvider', function () {
var namespaceService,

import MCWSUserContainerProvider from '../MCWSUserContainerProvider';
import MCWSPersistenceProvider from '../MCWSPersistenceProvider';
import mcws from '../../services/mcws/mcws';

describe('MCWS Providers', () => {
let openmct;
let someNamespace;
let anotherNamespace;
let personalContainerNamespace;
let personalNamespace;
let namespaces;
let userContainerProvider;
let persistenceProvider;

beforeEach(() => {
openmct = {
user: {
getCurrentUser: () => Promise.resolve({ id: 'myUser' })
}
};

someNamespace = {
id: 'some-namespace:root',
key: 'some-namespace',
name: 'Some Namespace',
url: '/some/namespace/url'
};
anotherNamespace = {
id: 'another-namespace:root',
key: 'another-namespace',
name: 'Another Namespace',
url: '/another/namespace/url',
location: 'some-namespace:root'
};
personalContainerNamespace = {
id: 'personal',
key: 'personal',
name: 'personal',
url: '/some/personal/namespace',
containsNamespaces: true,
childTemplate: {
id: 'personal-{username}:root',
key: 'personal-{username}',
name: '{username}',
url: '/some/personal/namespace/{username}'
}
};
personalNamespace = {
id: 'personal-myUser:root',
key: 'personal-myUser',
name: 'myUser',
url: '/some/personal/namespace/myUser',
location: 'personal'
};

namespaces = [
someNamespace,
anotherNamespace,
personalContainerNamespace,
personalNamespace,
namespaces,
provider;

beforeEach(function () {
namespaceService = jasmine.createSpyObj(
'namespaceService',
[
'getPersistenceNamespaces',
'getContainedNamespaces'
]
personalNamespace
];

const roots = [personalContainerNamespace];
userContainerProvider = new MCWSUserContainerProvider(openmct, roots);
persistenceProvider = new MCWSPersistenceProvider(openmct, roots);

// Mock mcws service calls
spyOn(userContainerProvider, 'getPersistenceNamespaces')
.and.returnValue(Promise.resolve(namespaces));
spyOn(userContainerProvider, 'getContainedNamespaces')
.and.callFake((namespace) => {
if (namespace.id === 'personal') {
return Promise.resolve([personalNamespace]);
}
return Promise.resolve([]);
});
});

describe('MCWSUserContainerProvider', () => {
it('gets container model with contained namespaces', async () => {
const identifier = {
namespace: 'personal',
key: 'container'
};
const model = await userContainerProvider.get(identifier);

expect(model.type).toBe('folder');
expect(model.composition).toEqual([{ key: 'root', namespace: 'personal-myUser' }]);
expect(model.location).toBe('ROOT');
});
});

describe('MCWSPersistenceProvider', () => {
let mcwsNamespace;

beforeEach(() => {
// Mock mcws namespace operations
const fileOps = {
read: () => Promise.resolve({
json: () => Promise.resolve({
type: 'folder',
name: 'Test Object'
})
}),
create: jasmine.createSpy('create').and.returnValue(Promise.resolve(true)),
replace: jasmine.createSpy('replace').and.returnValue(Promise.resolve(true))
};
mcwsNamespace = {
opaqueFile: () => fileOps
};

spyOn(persistenceProvider, 'getPersistenceNamespaces')
.and.returnValue(Promise.resolve(namespaces));
// Mock the private getNamespace method through the mcws import
spyOn(mcws, 'namespace').and.returnValue(mcwsNamespace);
});

it('gets persisted objects', async () => {
const identifier = {
namespace: 'personal-myUser',
key: 'some-object'
};
const result = await persistenceProvider.get(identifier);

expect(result).toBeDefined();
expect(result.type).toBe('folder');
expect(result.name).toBe('Test Object');
expect(result.identifier).toEqual(identifier);
});

it('handles abort signal when getting objects', async () => {
const identifier = {
namespace: 'personal-myUser',
key: 'some-object'
};
const abortSignal = new AbortController().signal;

await persistenceProvider.get(identifier, abortSignal);

expect(mcws.namespace).toHaveBeenCalledWith(
jasmine.any(String),
{ signal: abortSignal }
);
});

someNamespace = {
id: 'some-namespace:root',
key: 'some-namespace',
name: 'Some Namespace',
url: '/some/namespace/url'
it('creates new objects', async () => {
const domainObject = {
identifier: {
namespace: 'some-namespace',
key: 'new-object'
},
type: 'folder',
name: 'New Folder'
};
anotherNamespace = {
id: 'another-namespace:root',
key: 'another-namespace',
name: 'Another Namespace',
url: '/another/namespace/url',
location: 'some-namespace:root'

const success = await persistenceProvider.create(domainObject);
const expectedModel = {
type: 'folder',
name: 'New Folder'
};
personalContainerNamespace = {
id: 'personal',
key: 'personal',
name: 'personal',
url: '/some/personal/namespace'

expect(success).toBe(true);
expect(mcwsNamespace.opaqueFile('new-object').create)
.toHaveBeenCalledWith(expectedModel);
});

it('updates existing objects', async () => {
const domainObject = {
identifier: {
namespace: 'some-namespace',
key: 'existing-object'
},
type: 'folder',
name: 'Updated Folder'
};
personalNamespace = {
id: 'personal-myUser:root',
key: 'personal-myUser}',
name: 'myUser',
url: '/some/personal/namespace/myUser',
location: 'personal'

const success = await persistenceProvider.update(domainObject);
const expectedModel = {
type: 'folder',
name: 'Updated Folder'
};

namespaces = [
someNamespace,
anotherNamespace,
personalContainerNamespace,
personalNamespace
];
expect(success).toBe(true);
expect(mcwsNamespace.opaqueFile('existing-object').replace)
.toHaveBeenCalledWith(expectedModel);
});

namespaceService
.getPersistenceNamespaces
.and.returnValue(Promise.resolve(namespaces));
it('handles errors during get operation', async () => {
const errorNamespace = {
opaqueFile: () => ({
read: () => Promise.reject(new Error('Network Error'))
})
};
mcws.namespace.and.returnValue(errorNamespace);

namespaceService
.getContainedNamespaces
.and.callFake(function (namespace) {
if (namespace.id === 'personal') {
return Promise.resolve([personalNamespace]);
}
return Promise.resolve([]);
});
const identifier = {
namespace: 'personal-myUser',
key: 'error-object'
};
const result = await persistenceProvider.get(identifier);

provider = new MCWSNamespaceModelProvider(namespaceService);
expect(result).toBeUndefined();
});

describe('getModels', function () {
var someNamespaceModel,
anotherNamespaceModel,
personalContainerNamespaceModel,
personalNamespaceModel,
allNamespaceModels;

beforeEach(function (done) {
provider
.getModels([
'some-namespace:root',
'another-namespace:root',
'personal',
'personal-myUser:root'
])
.then(function (models) {
someNamespaceModel = models['some-namespace:root'];
anotherNamespaceModel =
models['another-namespace:root'];
personalContainerNamespaceModel = models.personal;
personalNamespaceModel = models['personal-myUser:root'];
allNamespaceModels = [
someNamespaceModel,
anotherNamespaceModel,
personalContainerNamespaceModel,
personalNamespaceModel
];
})
.then(done);
});
it('handles errors during create operation', async () => {
const errorNamespace = {
opaqueFile: () => ({
create: () => Promise.reject(new Error('Creation Error'))
})
};
mcws.namespace.and.returnValue(errorNamespace);

it('sets type to folder', function () {
allNamespaceModels.forEach(function (namespaceModel) {
expect(namespaceModel.type).toBe('folder');
});
});
const domainObject = {
identifier: {
namespace: 'personal-myUser',
key: 'error-object'
},
type: 'folder'
};
const success = await persistenceProvider.create(domainObject);

it('uses location specified in namespace definition', function () {
expect(anotherNamespaceModel.location)
.toBe('some-namespace:root');
expect(personalNamespaceModel.location)
.toBe('personal');
});
expect(success).toBe(false);
});

it('sets default location if not specified', function () {
expect(someNamespaceModel.location).toBe('ROOT');
expect(personalContainerNamespaceModel.location).toBe('ROOT');
});
it('handles errors during update operation', async () => {
const errorNamespace = {
opaqueFile: () => ({
replace: () => Promise.reject(new Error('Update Error'))
})
};
mcws.namespace.and.returnValue(errorNamespace);

it('sets composition', function () {
expect(someNamespaceModel.composition)
.toEqual(jasmine.any(Array));
expect(anotherNamespaceModel.composition)
.toEqual(jasmine.any(Array));
expect(personalContainerNamespaceModel.composition)
.toEqual(['personal-myUser:root']);
expect(personalNamespaceModel.composition)
.toEqual(jasmine.any(Array));
});
const domainObject = {
identifier: {
namespace: 'personal-myUser',
key: 'error-object'
},
type: 'folder'
};
const success = await persistenceProvider.update(domainObject);

expect(success).toBe(false);
});
});
});
Loading

0 comments on commit 23524cc

Please sign in to comment.