Skip to content

Commit

Permalink
CODENVY-2005: Add paging objects
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Orel <oorel@codenvy.com>
  • Loading branch information
Oleksii Orel committed May 2, 2017
1 parent 31fa576 commit ab4bbd2
Show file tree
Hide file tree
Showing 15 changed files with 1,209 additions and 66 deletions.
5 changes: 3 additions & 2 deletions dashboard/src/components/api/che-api-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {CheWebsocket} from './che-websocket.factory';
import {CheProfile} from './che-profile.factory';
import {ChePreferences} from './che-preferences.factory';
import {CheService} from './che-service.factory';
import {CheHttpBackend} from './test/che-http-backend';
import {CheHttpBackendProviderFactory} from './test/che-http-backend-provider.factory'
import {CheFactoryTemplate} from './che-factory-template.factory';
import {CheHttpBackendFactory} from './test/che-http-backend.factory';
Expand All @@ -33,10 +32,11 @@ import {CheAgent} from './che-agent.factory';
import {CheSsh} from './che-ssh.factory';
import {CheNamespaceRegistry} from './namespace/che-namespace-registry.factory';
import {CheUser} from './che-user.factory';
import {ChePageObject} from './paging-resource/page-object.factory';

export class ApiConfig {

constructor(register) {
constructor(register: che.IRegisterService) {
register.factory('cheWorkspace', CheWorkspace);
register.factory('cheProjectTemplate', CheProjectTemplate);
register.factory('cheFactory', CheFactory);
Expand All @@ -59,5 +59,6 @@ export class ApiConfig {
register.factory('cheSsh', CheSsh);
register.factory('cheNamespaceRegistry', CheNamespaceRegistry);
register.factory('cheUser', CheUser);
register.factory('chePageObject', ChePageObject);
}
}
4 changes: 3 additions & 1 deletion dashboard/src/components/api/che-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
'use strict';
import {CheHttpBackend} from './test/che-http-backend';
import {CheAPIBuilder} from './builder/che-api-builder.factory';
import {CheFactory} from './che-factory.factory';

/**
* Test of the Codenvy Factory API
Expand Down Expand Up @@ -45,7 +47,7 @@ describe('CheFactory', () => {
/**
* Inject factory and http backend
*/
beforeEach(inject((cheFactory, cheAPIBuilder, cheHttpBackend: CheHttpBackend) => {
beforeEach(inject((cheFactory: CheFactory, cheAPIBuilder: CheAPIBuilder, cheHttpBackend: CheHttpBackend) => {

factory = cheFactory;
apiBuilder = cheAPIBuilder;
Expand Down
43 changes: 32 additions & 11 deletions dashboard/src/components/api/che-profile.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

interface IProfileResource<T> extends ng.resource.IResourceClass<T> {
getById(data: { userId: string }): ng.resource.IResource<T>;
setAttributes(data: { attributes: che.IProfileAttributes }): ng.resource.IResource<T>;
setAttributes(data: che.IProfileAttributes): ng.resource.IResource<T>;
setAttributesById(params: { userId: string }, data: che.IProfileAttributes): ng.resource.IResource<T>;
}

/**
Expand Down Expand Up @@ -52,7 +53,8 @@ export class CheProfile {
// remote call
this.remoteProfileAPI = <IProfileResource<che.IProfile>>this.$resource('/api/profile', {}, {
getById: {method: 'GET', url: '/api/profile/:userId'},
setAttributes: {method: 'PUT', url: '/api/profile/attributes'}
setAttributes: {method: 'PUT', url: '/api/profile/attributes'},
setAttributesById: {method: 'PUT', url: '/api/profile/:userId/attributes'}
});

this.profileIdMap = new Map();
Expand All @@ -61,6 +63,17 @@ export class CheProfile {
this.fetchProfile();
}

/**
* Gets the full name if it possible
* @returns {string} full name
*/
getFullName(attr: che.IProfileAttributes): string {
if (!angular.isObject(attr)) {
return '';
}
const {firstName, lastName} = attr;
return `${firstName || ''} ${lastName || ''}`.trim();
}

/**
* Gets the profile
Expand All @@ -76,7 +89,7 @@ export class CheProfile {
*/
fetchProfile(): ng.IPromise<che.IProfile> {
if (this.profile && !this.profile.$resolved) {
return this.$q.when(this.profile);
return this.profile;
}
let profile = this.remoteProfileAPI.get();
// if we don't yet have data
Expand Down Expand Up @@ -104,18 +117,26 @@ export class CheProfile {
* @param attributes {che.IProfileAttributes}
* @returns {ng.IPromise<any>} the promise
*/
setAttributes(attributes: che.IProfileAttributes): ng.IPromise<any> {
let promise = this.remoteProfileAPI.setAttributes(attributes).$promise;
setAttributes(attributes: che.IProfileAttributes, userId?: string): ng.IPromise<any> {
if (angular.isUndefined(userId)) {
return this.remoteProfileAPI.setAttributes(attributes).$promise;
}
let promise = this.remoteProfileAPI.setAttributesById({userId: userId}, attributes).$promise;

return promise;
return promise.then((profile: che.IProfile) => {
if (profile && profile.userId) {
this.profileIdMap.set(profile.userId, profile);
}
this.$q.when(profile);
});
}

/**
* Fetch the profile from the given userId
* @param userId the user for which we will call remote api and get promise
* @returns {ng.IPromise<che.IProfile>} the promise
* Fetch the profile by the given userId
* @param userId {string}
* @returns {ng.IPromise<che.IProfile>}
*/
fetchProfileId(userId: string): ng.IPromise<che.IProfile> {
fetchProfileById(userId: string): ng.IPromise<che.IProfile> {
let promise = this.remoteProfileAPI.getById({userId: userId}).$promise;

return promise.then((profile: che.IProfile) => {
Expand All @@ -129,7 +150,7 @@ export class CheProfile {
});
}

getProfileFromId(userId: string): che.IProfile {
getProfileById(userId: string): che.IProfile {
return this.profileIdMap.get(userId);
}
}
93 changes: 48 additions & 45 deletions dashboard/src/components/api/che-profile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@
* Codenvy, S.A. - initial API and implementation
*/
'use strict';
import {CheProfile} from './che-profile.factory';
import {CheAPIBuilder} from './builder/che-api-builder.factory';
import {CheHttpBackend} from './test/che-http-backend';

/**
* Test of the CheProfile
*/
describe('CheProfile', function () {

describe('CheProfile', () => {
/**
* Profile Factory for the test
*/
var factory;

let factory: CheProfile;
/**
* API builder.
*/
var apiBuilder;

let apiBuilder: CheAPIBuilder;
/**
* Backend for handling http operations
*/
var httpBackend;

let httpBackend: ng.IHttpBackendService;
/**
* che backend
*/
var cheBackend;
let cheBackend: CheHttpBackend;

/**
* setup module
Expand All @@ -43,7 +42,7 @@ describe('CheProfile', function () {
/**
* Inject factory and http backend
*/
beforeEach(inject(function (cheProfile, cheAPIBuilder, cheHttpBackend) {
beforeEach(inject((cheProfile: CheProfile, cheAPIBuilder: CheAPIBuilder, cheHttpBackend: CheHttpBackend) => {
factory = cheProfile;
apiBuilder = cheAPIBuilder;
cheBackend = cheHttpBackend;
Expand All @@ -53,75 +52,79 @@ describe('CheProfile', function () {
/**
* Check assertion after the test
*/
afterEach(function () {
afterEach(() => {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});

/**
* Check that we're able to fetch profile
*/
it('Fetch profile', function () {
// setup tests objects
var profileId = 'idDefaultUser';
var email = 'eclipseChe@eclipse.org';
var firstName = 'FirstName';
var lastName = 'LastName';

var defaultProfile = apiBuilder.getProfileBuilder().withId(profileId).withEmail(email).withFirstName(firstName).withLastName(lastName).build();

// providing request
// add defaultProfile on Http backend
cheBackend.addDefaultProfile(defaultProfile);
it('Fetch profile', () => {
const testProfile = apiBuilder.getProfileBuilder()
.withId('idDefaultUser')
.withEmail('eclipseChe@eclipse.org')
.withFirstName('FirstName')
.withLastName('LastName')
.build();

// setup backend
cheBackend.addDefaultProfile(testProfile);
cheBackend.setup();

// fetch profile
factory.fetchProfile();

// expecting GETs
httpBackend.expectGET('/api/profile');
// flush command

httpBackend.flush();

// now, check profile
var profile = factory.getProfile();
const profile = factory.getProfile();

// check id, email, firstName and lastName in profile attributes
expect(profile.id).toEqual(profileId);
expect(profile.email).toEqual(email);
expect(profile.attributes.firstName).toEqual(firstName);
expect(profile.attributes.lastName).toEqual(lastName);
expect(profile).toEqual(profile);
}
);

/**
* Check that we're able to set attributes into profile
*/
it('Set attributes', function () {
// setup tests object
var testAttributes = {lastName: '<none>', email: 'eclipseChe@eclipse.org'};
it('Set current user attributes', () => {
const testProfile = apiBuilder.getProfileBuilder()
.withEmail('test@test.com')
.withFirstName('testName')
.build();

// setup backend
cheBackend.setAttributes(testProfile.atributes);
cheBackend.setup();
cheBackend.setAttributes(testAttributes);

// fetch profile
factory.setAttributes(testAttributes);
factory.setAttributes(testProfile.atributes);

// expecting a PUT
httpBackend.expectPUT('/api/profile/attributes');

// flush command
httpBackend.flush();
}
);

it('Set attributes for the user by Id', () => {
const testProfile = apiBuilder.getProfileBuilder()
.withId('testId')
.withEmail('test@test.com')
.withFirstName('testName')
.build();

// setup backend
cheBackend.setAttributes(testProfile.attributes, testProfile.userId);
cheBackend.setup();

factory.setAttributes(testProfile.attributes, testProfile.userId);

// now, check profile
var profile = factory.getProfile();
httpBackend.expectPUT(`/api/profile/${testProfile.id}/attributes`);

httpBackend.flush();

// check profile new attributes
expect(profile.attributes).toEqual(testAttributes);
const profile = factory.getProfileById(testProfile.userId);

expect(profile.attributes).toEqual(testProfile.attributes);
}
);

Expand Down
Loading

0 comments on commit ab4bbd2

Please sign in to comment.