Skip to content

Commit

Permalink
feat(rhsmServices,redux): issues/10 inventory api, state (#312)
Browse files Browse the repository at this point in the history
* build, dotenv host inventory API endpoint
* redux actions, reducers, types for inventory
* rhsmServices, add base host inventory API
* rhsmApiTypes, inventory API response types
  • Loading branch information
cdcabrera committed Jul 30, 2020
1 parent 30cba6e commit da48f9b
Show file tree
Hide file tree
Showing 16 changed files with 406 additions and 30 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ REACT_APP_INCLUDE_CONTENT_BODY=<esi:include src="${UI_DEPLOY_PATH_PREFIX}/apps/c
REACT_APP_SERVICES_RHSM_VERSION=/api/rhsm-subscriptions/v1/version
REACT_APP_SERVICES_RHSM_REPORT=/api/rhsm-subscriptions/v1/tally/products/
REACT_APP_SERVICES_RHSM_CAPACITY=/api/rhsm-subscriptions/v1/capacity/products/
REACT_APP_SERVICES_RHSM_INVENTORY=/api/rhsm-subscriptions/v1/hosts/products/
REACT_APP_SERVICES_RHSM_OPTIN=/api/rhsm-subscriptions/v1/opt-in
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ REACT_APP_CONFIG_SERVICE_LOCALES_PATH=./locales/{{lng}}.json
REACT_APP_SERVICES_RHSM_VERSION=http://localhost:5000/api/rhsm-subscriptions/v1/version
REACT_APP_SERVICES_RHSM_REPORT=http://localhost:5000/api/rhsm-subscriptions/v1/tally/products/
REACT_APP_SERVICES_RHSM_CAPACITY=http://localhost:5000/api/rhsm-subscriptions/v1/capacity/products/
REACT_APP_SERVICES_RHSM_INVENTORY=http://localhost:5000/api/rhsm-subscriptions/v1/hosts/products/
REACT_APP_SERVICES_RHSM_OPTIN=http://localhost:5000/api/rhsm-subscriptions/v1/opt-in
1 change: 1 addition & 0 deletions .env.proxy
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ REACT_APP_CONFIG_SERVICE_LOCALES_PATH=/locales/{{lng}}.json
REACT_APP_SERVICES_RHSM_VERSION=/api/rhsm-subscriptions/v1/version
REACT_APP_SERVICES_RHSM_REPORT=/api/rhsm-subscriptions/v1/tally/products/
REACT_APP_SERVICES_RHSM_CAPACITY=/api/rhsm-subscriptions/v1/capacity/products/
REACT_APP_SERVICES_RHSM_INVENTORY=/api/rhsm-subscriptions/v1/hosts/products/
REACT_APP_SERVICES_RHSM_OPTIN=/api/rhsm-subscriptions/v1/opt-in
16 changes: 14 additions & 2 deletions src/redux/actions/__tests__/rhsmActions.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import promiseMiddleware from 'redux-promise-middleware';
import { applyMiddleware, combineReducers, createStore } from 'redux';
import moxios from 'moxios';
import { graphReducer, viewReducer } from '../../reducers';
import { graphReducer, inventoryReducer, viewReducer } from '../../reducers';
import { rhsmApiTypes } from '../../../types/rhsmApiTypes';
import { rhsmActions } from '../rhsmActions';

Expand All @@ -11,6 +11,7 @@ describe('RhsmActions', () => {
createStore(
combineReducers({
graph: graphReducer,
inventory: inventoryReducer,
view: viewReducer
}),
applyMiddleware(...middleware)
Expand All @@ -19,7 +20,7 @@ describe('RhsmActions', () => {
beforeEach(() => {
moxios.install();

moxios.stubRequest(/\/(tally|capacity|version).*?/, {
moxios.stubRequest(/\/(tally|capacity|hosts|version).*?/, {
status: 200,
responseText: 'success',
timeout: 1,
Expand All @@ -44,4 +45,15 @@ describe('RhsmActions', () => {
done();
});
});

it('Should return response content for getHostsInventory method', done => {
const store = generateStore();
const dispatcher = rhsmActions.getHostsInventory();

dispatcher(store.dispatch).then(() => {
const response = store.getState().inventory;
expect(response.hostsInventory.fulfilled).toBe(true);
done();
});
});
});
15 changes: 13 additions & 2 deletions src/redux/actions/rhsmActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ const getGraphReportsCapacity = (id = null, query = {}) => dispatch =>
}
});

const rhsmActions = { getGraphReportsCapacity };
const getHostsInventory = (id = null, query = {}) => dispatch =>
dispatch({
type: rhsmTypes.GET_HOSTS_INVENTORY_RHSM,
payload: rhsmServices.getHostsInventory(id, query),
meta: {
id,
query,
notifications: {}
}
});

const rhsmActions = { getGraphReportsCapacity, getHostsInventory };

export { rhsmActions as default, rhsmActions, getGraphReportsCapacity };
export { rhsmActions as default, rhsmActions, getGraphReportsCapacity, getHostsInventory };
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`InventoryReducer should handle all defined error types: rejected types GET_HOSTS_INVENTORY_RHSM 1`] = `
Object {
"result": Object {
"hostsInventory": Object {
"error": true,
"errorMessage": "MESSAGE",
"fulfilled": false,
"meta": Object {},
"metaData": undefined,
"metaId": undefined,
"metaQuery": undefined,
"pending": false,
"status": 0,
},
},
"type": "GET_HOSTS_INVENTORY_RHSM_REJECTED",
}
`;

exports[`InventoryReducer should handle all defined fulfilled types: fulfilled types GET_HOSTS_INVENTORY_RHSM 1`] = `
Object {
"result": Object {
"hostsInventory": Object {
"data": Object {
"test": "success",
},
"date": null,
"error": false,
"errorMessage": "",
"fulfilled": true,
"meta": Object {},
"metaData": undefined,
"metaId": undefined,
"metaQuery": undefined,
"pending": false,
"status": 0,
},
},
"type": "GET_HOSTS_INVENTORY_RHSM_FULFILLED",
}
`;

exports[`InventoryReducer should handle all defined pending types: pending types GET_HOSTS_INVENTORY_RHSM 1`] = `
Object {
"result": Object {
"hostsInventory": Object {
"error": false,
"errorMessage": "",
"fulfilled": false,
"meta": Object {},
"metaData": undefined,
"metaId": undefined,
"metaQuery": undefined,
"pending": true,
},
},
"type": "GET_HOSTS_INVENTORY_RHSM_PENDING",
}
`;
73 changes: 73 additions & 0 deletions src/redux/reducers/__tests__/inventoryReducer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import inventoryReducer from '../inventoryReducer';
import { rhsmTypes as types } from '../../types';
import { reduxHelpers } from '../../common/reduxHelpers';

describe('InventoryReducer', () => {
it('should return the initial state', () => {
expect(inventoryReducer.initialState).toBeDefined();
});

it('should handle all defined error types', () => {
const specificTypes = [types.GET_HOSTS_INVENTORY_RHSM];

specificTypes.forEach(value => {
const dispatched = {
type: reduxHelpers.REJECTED_ACTION(value),
error: true,
payload: {
message: 'MESSAGE',
response: {
status: 0,
statusText: 'ERROR TEST',
data: {
detail: 'ERROR'
}
}
}
};

const resultState = inventoryReducer(undefined, dispatched);

expect({ type: reduxHelpers.REJECTED_ACTION(value), result: resultState }).toMatchSnapshot(
`rejected types ${value}`
);
});
});

it('should handle all defined pending types', () => {
const specificTypes = [types.GET_HOSTS_INVENTORY_RHSM];

specificTypes.forEach(value => {
const dispatched = {
type: reduxHelpers.PENDING_ACTION(value)
};

const resultState = inventoryReducer(undefined, dispatched);

expect({ type: reduxHelpers.PENDING_ACTION(value), result: resultState }).toMatchSnapshot(
`pending types ${value}`
);
});
});

it('should handle all defined fulfilled types', () => {
const specificTypes = [types.GET_HOSTS_INVENTORY_RHSM];

specificTypes.forEach(value => {
const dispatched = {
type: reduxHelpers.FULFILLED_ACTION(value),
payload: {
data: {
test: 'success'
}
}
};

const resultState = inventoryReducer(undefined, dispatched);

expect({ type: reduxHelpers.FULFILLED_ACTION(value), result: resultState }).toMatchSnapshot(
`fulfilled types ${value}`
);
});
});
});
2 changes: 1 addition & 1 deletion src/redux/reducers/graphReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { reduxHelpers } from '../common/reduxHelpers';
* Initial state.
*
* @private
* @type {{reportCapacity: object}}
* @type {{reportCapacity: {}, legend: {}}}
*/
const initialState = {
legend: {},
Expand Down
8 changes: 5 additions & 3 deletions src/redux/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { combineReducers } from 'redux';
import { notifications } from '@redhat-cloud-services/frontend-components-notifications';
import graphReducer from './graphReducer';
import inventoryReducer from './inventoryReducer';
import viewReducer from './viewReducer';
import userReducer from './userReducer';

const reducers = {
notifications,
graph: graphReducer,
view: viewReducer,
user: userReducer
inventory: inventoryReducer,
user: userReducer,
view: viewReducer
};

const reduxReducers = combineReducers(reducers);

export { reduxReducers as default, reduxReducers, graphReducer, viewReducer, userReducer };
export { reduxReducers as default, reduxReducers, graphReducer, inventoryReducer, userReducer, viewReducer };
30 changes: 30 additions & 0 deletions src/redux/reducers/inventoryReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { rhsmTypes } from '../types/rhsmTypes';
import { reduxHelpers } from '../common/reduxHelpers';

/**
* Initial state.
*
* @private
* @type {{inventory: {}}}
*/
const initialState = {
hostsInventory: {}
};

/**
* Apply generated inventory observer/reducer for systemInventory to state, against actions.
*
* @param {object} state
* @param {object} action
* @returns {object|{}}
*/
const inventoryReducer = (state = initialState, action) =>
reduxHelpers.generatedPromiseActionReducer(
[{ ref: 'hostsInventory', type: rhsmTypes.GET_HOSTS_INVENTORY_RHSM }],
state,
action
);

inventoryReducer.initialState = initialState;

export { inventoryReducer as default, initialState, inventoryReducer };
4 changes: 4 additions & 0 deletions src/redux/types/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Object {
"GET_GRAPH_CAPACITY_RHSM": "GET_GRAPH_CAPACITY_RHSM",
"GET_GRAPH_REPORT_CAPACITY_RHSM": "GET_GRAPH_REPORT_CAPACITY_RHSM",
"GET_GRAPH_REPORT_RHSM": "GET_GRAPH_REPORT_RHSM",
"GET_HOSTS_INVENTORY_RHSM": "GET_HOSTS_INVENTORY_RHSM",
"SET_GRAPH_GRANULARITY_RHSM": "SET_GRAPH_GRANULARITY_RHSM",
"SET_GRAPH_SLA_RHSM": "SET_GRAPH_SLA_RHSM",
},
Expand Down Expand Up @@ -72,6 +73,7 @@ Object {
"GET_GRAPH_CAPACITY_RHSM": "GET_GRAPH_CAPACITY_RHSM",
"GET_GRAPH_REPORT_CAPACITY_RHSM": "GET_GRAPH_REPORT_CAPACITY_RHSM",
"GET_GRAPH_REPORT_RHSM": "GET_GRAPH_REPORT_RHSM",
"GET_HOSTS_INVENTORY_RHSM": "GET_HOSTS_INVENTORY_RHSM",
"SET_GRAPH_GRANULARITY_RHSM": "SET_GRAPH_GRANULARITY_RHSM",
"SET_GRAPH_SLA_RHSM": "SET_GRAPH_SLA_RHSM",
},
Expand All @@ -88,6 +90,7 @@ Object {
"GET_GRAPH_CAPACITY_RHSM": "GET_GRAPH_CAPACITY_RHSM",
"GET_GRAPH_REPORT_CAPACITY_RHSM": "GET_GRAPH_REPORT_CAPACITY_RHSM",
"GET_GRAPH_REPORT_RHSM": "GET_GRAPH_REPORT_RHSM",
"GET_HOSTS_INVENTORY_RHSM": "GET_HOSTS_INVENTORY_RHSM",
"SET_GRAPH_GRANULARITY_RHSM": "SET_GRAPH_GRANULARITY_RHSM",
"SET_GRAPH_SLA_RHSM": "SET_GRAPH_SLA_RHSM",
},
Expand Down Expand Up @@ -124,6 +127,7 @@ Object {
"GET_GRAPH_CAPACITY_RHSM": "GET_GRAPH_CAPACITY_RHSM",
"GET_GRAPH_REPORT_CAPACITY_RHSM": "GET_GRAPH_REPORT_CAPACITY_RHSM",
"GET_GRAPH_REPORT_RHSM": "GET_GRAPH_REPORT_RHSM",
"GET_HOSTS_INVENTORY_RHSM": "GET_HOSTS_INVENTORY_RHSM",
"SET_GRAPH_GRANULARITY_RHSM": "SET_GRAPH_GRANULARITY_RHSM",
"SET_GRAPH_SLA_RHSM": "SET_GRAPH_SLA_RHSM",
},
Expand Down
5 changes: 4 additions & 1 deletion src/redux/types/rhsmTypes.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
const GET_GRAPH_CAPACITY_RHSM = 'GET_GRAPH_CAPACITY_RHSM';
const GET_GRAPH_REPORT_RHSM = 'GET_GRAPH_REPORT_RHSM';
const GET_GRAPH_REPORT_CAPACITY_RHSM = 'GET_GRAPH_REPORT_CAPACITY_RHSM';
const GET_HOSTS_INVENTORY_RHSM = 'GET_HOSTS_INVENTORY_RHSM';
const SET_GRAPH_GRANULARITY_RHSM = 'SET_GRAPH_GRANULARITY_RHSM';
const SET_GRAPH_SLA_RHSM = 'SET_GRAPH_SLA_RHSM';

/**
* RHSM API action, reducer types.
*
* @type {{GET_GRAPH_REPORT_CAPACITY_RHSM: string, SET_GRAPH_GRANULARITY_RHSM: string,
* GET_GRAPH_CAPACITY_RHSM: string, SET_GRAPH_SLA_RHSM: string,
* GET_GRAPH_CAPACITY_RHSM: string, SET_GRAPH_SLA_RHSM: string, GET_HOSTS_INVENTORY_RHSM: string,
* GET_GRAPH_REPORT_RHSM: string}}
*/
const rhsmTypes = {
GET_GRAPH_CAPACITY_RHSM,
GET_GRAPH_REPORT_RHSM,
GET_GRAPH_REPORT_CAPACITY_RHSM,
GET_HOSTS_INVENTORY_RHSM,
SET_GRAPH_GRANULARITY_RHSM,
SET_GRAPH_SLA_RHSM
};
Expand All @@ -25,6 +27,7 @@ export {
GET_GRAPH_CAPACITY_RHSM,
GET_GRAPH_REPORT_RHSM,
GET_GRAPH_REPORT_CAPACITY_RHSM,
GET_HOSTS_INVENTORY_RHSM,
SET_GRAPH_GRANULARITY_RHSM,
SET_GRAPH_SLA_RHSM
};
5 changes: 3 additions & 2 deletions src/services/__tests__/rhsmServices.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('RhsmServices', () => {
beforeEach(() => {
moxios.install();

moxios.stubRequest(/\/(tally|capacity|version).*?/, {
moxios.stubRequest(/\/(tally|capacity|hosts|version).*?/, {
status: 200,
responseText: 'success',
timeout: 1
Expand All @@ -17,13 +17,14 @@ describe('RhsmServices', () => {
});

it('should export a specific number of methods and classes', () => {
expect(Object.keys(rhsmServices)).toHaveLength(3);
expect(Object.keys(rhsmServices)).toHaveLength(4);
});

it('should have specific methods', () => {
expect(rhsmServices.getApiVersion).toBeDefined();
expect(rhsmServices.getGraphCapacity).toBeDefined();
expect(rhsmServices.getGraphReports).toBeDefined();
expect(rhsmServices.getHostsInventory).toBeDefined();
});

/**
Expand Down
Loading

0 comments on commit da48f9b

Please sign in to comment.