Skip to content

Commit

Permalink
Changes to mock data objects
Browse files Browse the repository at this point in the history
  • Loading branch information
William-Edwards-STFC committed Dec 17, 2024
1 parent 28d073b commit a210a80
Show file tree
Hide file tree
Showing 66 changed files with 702 additions and 296 deletions.
117 changes: 51 additions & 66 deletions src/mockserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,80 +35,68 @@ async function mockserver() {
}

const respondToPostRequest = function (request) {
const { method, path, body } = request;
if (request.method !== 'POST') {
return;
}

let responsePath;
let requestBody;

if (method !== 'POST') {
try {
// Parse JSON body
console.log("request body before parse = ", request.body);
requestBody = JSON.parse(request.body);
console.log("Request body after parse = ", requestBody)
} catch (error) {
logger.logError('Invalid JSON in request body', { error });
return {
statusCode: 405,
body: JSON.stringify({ error: 'Method not allowed' }),
statusCode: 400,
body: JSON.stringify({ error: 'Invalid JSON in request body' }),
};
}

try {
const requestBody = JSON.parse(body);
let responsePath;

switch (path) {
case '/users-service/getbasicpersondetails':
if (requestBody.userNumber) {
responsePath = `src/responses/user/getbasicpersondetails/${requestBody.userNumber}.json`;
}
break;

case '/users-service/getsearchablebasicpersondetails':
if (requestBody.userNumbers) {
responsePath = `src/responses/user/getsearchablebasicpersondetails/${requestBody.userNumbers}.json`;
}
break;

case '/users-service/getrolesforuser':
if (requestBody.userNumber) {
responsePath = `src/responses/user/getrolesforuser/${requestBody.userNumber}.json`;
}
break;

case '/users-service/getpersondetailsfromsessionid':
if (requestBody.sessionId) {
responsePath = `src/responses/user/getpersondetailsfromsessionid/${requestBody.sessionId}.json`;
}
break;

default:
return {
statusCode: 404,
body: JSON.stringify({ error: 'Endpoint not found' }),
};
// Handle different REST API methods
if (requestBody.method === 'getBasicPersonDetails') {
const { userNumber } = requestBody;
if (userNumber) {
responsePath = `src/responses/user/getbasicpersondetails/${userNumber}.json`;
}

if (!responsePath || !fs.existsSync(responsePath)) {
logger.logError('Response file does not exist', { responsePath });
return {
statusCode: 404,
body: JSON.stringify({ error: 'Resource not found' }),
};
} else if (requestBody.method === 'getSearchableBasicPeopleDetails') {
const { userNumbers } = requestBody;
if (userNumbers && Array.isArray(userNumbers)) {
responsePath = `src/responses/user/getsearchablebasicpersondetails/${userNumbers.join('-')}.json`;
}
} else if (requestBody.method === 'getPersonDetailsFromSessionId') {
const { sessionId } = requestBody;
if (sessionId) {
responsePath = `src/responses/user/getpersondetailsfromsessionid/${sessionId}.json`;
}
} else if (requestBody.method === 'getRolesForUser') {
const { userNumber } = requestBody;
if (userNumber) {
responsePath = `src/responses/user/getrolesforuser/${userNumber}.json`;
}
}

const file = fs.readFileSync(responsePath, 'utf8');
logger.logInfo('Returning response file', { responsePath });
if (!responsePath || !fs.existsSync(responsePath)) {
logger.logError('Response file does not exist', { responsePath });
return {
statusCode: 200,
body: file,
};
} catch (error) {
logger.logError('Error handling request', { error });
return {
statusCode: 400,
body: JSON.stringify({ error: 'Invalid request body' }),
statusCode: 404,
body: JSON.stringify({ error: 'Resource not found' }),
};
}

const file = fs.readFileSync(responsePath, 'utf8');
logger.logInfo('Returning response file', { responsePath });

return {
statusCode: 200,
body: file,
};
};

const endpoints = [
'/users-service/getbasicpersondetails',
'/users-service/getsearchablebasicpersondetails',
'/users-service/getrolesforuser',
'/users-service/getpersondetailsfromsessionid',
];
// Define the REST endpoints
const endpoints = ['/ws/UserOfficeWebService'];

endpoints.forEach((endpoint) => {
mockServerClient('mockServer', 1080)
Expand All @@ -121,11 +109,8 @@ async function mockserver() {
{ unlimited: true }
)
.then(
() => logger.logInfo(`Created callback for POST ${endpoint}`, {}),
(error) =>
logger.logError(`Error while creating callback for ${endpoint}`, {
error,
})
() => logger.logInfo('Created callback for POST requests', {}),
(error) => logger.logError('Error while creating callback for POST requests', { error })
);
});
}
Expand Down
18 changes: 18 additions & 0 deletions src/responses/user/getBasicPeopleDetailsFromSurname.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"personDetails": {
"country": "UNITED KINGDOM",
"deptAcronym": "SCD",
"deptName": "Scientific Computing",
"displayName": "Carl Carlsson",
"email": "[email protected]",
"establishmentId": 5,
"familyName": "Carlsson",
"firstNameKnownAs": "Carl",
"fullName": "Mr Carl Carlsson",
"givenName": "Carl",
"initials": "C",
"orgName": "STFC",
"title": "Mr",
"userNumber": 1
}
}
1 change: 0 additions & 1 deletion src/responses/user/getBasicPeopleDetailsFromSurname.xml

This file was deleted.

35 changes: 19 additions & 16 deletions src/responses/user/getBasicPersonDetails/0.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"country": "UNITED KINGDOM",
"deptAcronym": "SCD",
"deptName": "Scientific Computing",
"displayName": "Carl Carlsson",
"email": "[email protected]",
"establishmentId": "5",
"familyName": "Carlsson",
"firstNameKnownAs": "Carl",
"fullName": "Mr Carl Carlsson",
"givenName": "Carl",
"initials": "C",
"orgName": "STFC",
"title": "Miss",
"userNumber": "0"
}
[
{
"userNumber": "12345",
"country": "fake",
"deptName": "fake",
"displayName": "fake",
"email": "valid",
"establishmentId": "fake",
"familyName": "fake",
"firstNameKnownAs": "fake",
"fullName": "fake",
"givenName": "fake",
"initials": "fake",
"orgName": "fake",
"orgId": "fake",
"title": "fake",
"workPhone": "fake"
}
]
34 changes: 18 additions & 16 deletions src/responses/user/getBasicPersonDetails/1.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"country": "UNITED KINGDOM",
"deptAcronym": "SCD",
"deptName": "Scientific Computing",
"displayName": "Carl Carlsson",
"email": "[email protected]",
"establishmentId": "5",
"familyName": "Carlsson",
"firstNameKnownAs": "Carl",
"fullName": "Mr Carl Carlsson",
"givenName": "Carl",
"initials": "C",
"orgName": "STFC",
"title": "Mr",
"userNumber": "1"
}
[
{
"country": "UNITED KINGDOM",
"deptAcronym": "SCD",
"deptName": "Scientific Computing",
"displayName": "Carl Carlsson",
"email": "[email protected]",
"establishmentId": "5",
"familyName": "Carlsson",
"firstNameKnownAs": "Carl",
"fullName": "Mr Carl Carlsson",
"givenName": "Carl",
"initials": "C",
"orgName": "STFC",
"title": "Mr",
"userNumber": "1"
}
]
45 changes: 17 additions & 28 deletions src/responses/user/getBasicPersonDetails/2.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
{
"Envelope": {
"Body": {
"getBasicPeopleDetailsFromUserNumbersResponse": {
"return": {
"country": "UNITED KINGDOM",
"deptAcronym": "SCD",
"deptName": "Scientific Computing",
"displayName": "Anders Andersson",
"email": "[email protected]",
"establishmentId": "5",
"familyName": "Carlsson",
"firstNameKnownAs": "Carl",
"fullName": "Mr Carl Carlsson",
"givenName": "Carl",
"initials": "C",
"orgName": "STFC",
"title": "Mr",
"userNumber": "2"
},
"_xmlns:ns2": "http://webservice.UserOffice.stfc.com/",
"__prefix": "ns2"
},
"__prefix": "soap"
},
"_xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/",
"__prefix": "soap"
[
{
"country": "UNITED KINGDOM",
"deptAcronym": "SCD",
"deptName": "Scientific Computing",
"displayName": "Anders Andersson",
"email": "[email protected]",
"establishmentId": "5",
"familyName": "Carlsson",
"firstNameKnownAs": "Carl",
"fullName": "Mr Carl Carlsson",
"givenName": "Carl",
"initials": "C",
"orgName": "STFC",
"title": "Mr",
"userNumber": "2"
}
}
]
34 changes: 18 additions & 16 deletions src/responses/user/getBasicPersonDetails/3.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"country": "UNITED KINGDOM",
"deptAcronym": "SCD",
"deptName": "Scientific Computing",
"displayName": "Nils",
"email": "[email protected]",
"establishmentId": "5",
"familyName": "Nilsson",
"firstNameKnownAs": "Nils",
"fullName": "Mr Nils Nilsson",
"givenName": "Nils",
"initials": "N",
"orgName": "STFC",
"title": "Mr",
"userNumber": "3"
}
[
{
"country": "UNITED KINGDOM",
"deptAcronym": "SCD",
"deptName": "Scientific Computing",
"displayName": "Nils",
"email": "[email protected]",
"establishmentId": "5",
"familyName": "Nilsson",
"firstNameKnownAs": "Nils",
"fullName": "Mr Nils Nilsson",
"givenName": "Nils",
"initials": "N",
"orgName": "STFC",
"title": "Mr",
"userNumber": "3"
}
]
34 changes: 18 additions & 16 deletions src/responses/user/getBasicPersonDetails/4.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{
"country": "UNITED KINGDOM",
"deptAcronym": "SCD",
"deptName": "Scientific Computing",
"displayName": "Benjamin Beckley",
"email": "[email protected]",
"establishmentId": "7",
"familyName": "Beckley",
"firstNameKnownAs": "Ben",
"fullName": "Mr Benjamin Beckley",
"givenName": "Benjamin",
"initials": "B",
"orgName": "STFC",
"title": "Mr",
"userNumber": "4"
}
[
{
"country": "UNITED KINGDOM",
"deptAcronym": "SCD",
"deptName": "Scientific Computing",
"displayName": "Benjamin Beckley",
"email": "[email protected]",
"establishmentId": "7",
"familyName": "Beckley",
"firstNameKnownAs": "Ben",
"fullName": "Mr Benjamin Beckley",
"givenName": "Benjamin",
"initials": "B",
"orgName": "STFC",
"title": "Mr",
"userNumber": "4"
}
]
Loading

0 comments on commit a210a80

Please sign in to comment.