Skip to content

Commit

Permalink
Merge pull request #159 from ligangty/endpoints
Browse files Browse the repository at this point in the history
Add mock All endpoints
  • Loading branch information
ligangty authored Jan 23, 2024
2 parents 527f1eb + b9c2b80 commit 68de46e
Show file tree
Hide file tree
Showing 4 changed files with 1,906 additions and 14 deletions.
27 changes: 19 additions & 8 deletions src/main/webui/src/app/utils/RestClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ const jsonRest ={
// }
// };

const BASE_API_PATH = "/api/admin/stores";
const storeAPIEndpoint = (pkgType, type, name) => `${BASE_API_PATH}/${pkgType}/${type}/${name}`;
const BASE_STORE_API_PATH = "/api/admin/stores";
const storeAPIEndpoint = (pkgType, type, name) => `${BASE_STORE_API_PATH}/${pkgType}/${type}/${name}`;

const handleResponse = async response => {
if (response.ok){
Expand Down Expand Up @@ -74,6 +74,10 @@ const IndyRest = {
getVersion: async () =>{
const response = await jsonRest.get(`/api/stats/version-info`);
return handleResponse(response);
},
getAllEndpoints: async () =>{
const response = await jsonRest.get(`/api/stats/all-endpoints`);
return handleResponse(response);
}
},
storeRes: {
Expand All @@ -96,7 +100,17 @@ const IndyRest = {
return handleResponse(response);
},
getStores: async (pkgType, type) => {
const response = await jsonRest.get(`${BASE_API_PATH}/${pkgType}/${type}`);
const response = await jsonRest.get(`${BASE_STORE_API_PATH}/${pkgType}/${type}`);
return handleResponse(response);
}
},
storeQueryRes: {
getEndpoints: async pkgType => {
const response = await jsonRest.get(`${BASE_STORE_API_PATH}/query/endpoints/${pkgType}`);
return handleResponse(response);
},
getStoreKeys: async pkgType => {
const response = await jsonRest.get(`${BASE_STORE_API_PATH}/query/storekeys/${pkgType}`);
return handleResponse(response);
}
},
Expand Down Expand Up @@ -133,10 +147,7 @@ const IndyRest = {
const response = await jsonRest.get('/api/admin/auth/userinfo');
return handleResponse(response);
}
},
nfcRes: {
// TODO: not implemented.
},
}
};

export {IndyRest, BASE_API_PATH};
export {IndyRest, BASE_STORE_API_PATH};
8 changes: 4 additions & 4 deletions src/main/webui/src/app/utils/RestClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import fetchMock from "fetch-mock";
import {IndyRest, BASE_API_PATH} from "./RestClient.js";
import {IndyRest, BASE_STORE_API_PATH} from "./RestClient.js";

beforeEach(()=>{

Expand All @@ -31,19 +31,19 @@ describe('IndyRest test', () => {
key: "maven:remote:central", disabled: false, "allow_snapshots": true,
"allow_releases": true, url: "https://repo.maven.apache.org/maven2/",
description: "official maven central"};
fetchMock.mock(`${BASE_API_PATH}/maven/remote/central`, {status: 200, body: JSON.stringify(mockRemoteStore)});
fetchMock.mock(`${BASE_STORE_API_PATH}/maven/remote/central`, {status: 200, body: JSON.stringify(mockRemoteStore)});
const res = await IndyRest.storeRes.get("maven", "remote", "central");
expect(res.success).toBe(true);
expect(res.result).toEqual(mockRemoteStore);
});
it('Check get store: not exists', async () => {
fetchMock.mock(`${BASE_API_PATH}/maven/remote/central`, {status: 404});
fetchMock.mock(`${BASE_STORE_API_PATH}/maven/remote/central`, {status: 404});
const result = await IndyRest.storeRes.get("maven", "remote", "central");
expect(result.success).toBe(false);
expect(result.error).toEqual({status: 404, message: "Not Found"});
});
it('Check get store: error with json body', async () => {
fetchMock.mock(`${BASE_API_PATH}/maven/remote/central`, {status: 500, body: JSON.stringify({error: "Mock internal error"})});
fetchMock.mock(`${BASE_STORE_API_PATH}/maven/remote/central`, {status: 500, body: JSON.stringify({error: "Mock internal error"})});
const result = await IndyRest.storeRes.get("maven", "remote", "central");
expect(result.success).toBe(false);
expect(result.error).toEqual({status: 500, message: "Mock internal error"});
Expand Down
45 changes: 43 additions & 2 deletions src/main/webui/src/server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ app.get([Config.APP_ROOT, `${Config.APP_ROOT}/*`, '/'], (req, res) => {
res.sendFile(indexHtml);
});

// stats APIs start
app.get('/api/stats/package-type/keys', (req, res)=>{
res.status(200).json(["generic-http", "maven", "npm"]);
});
Expand All @@ -38,10 +39,50 @@ app.get('/api/stats/version-info', (req, res) => {
});
});

app.get('/api/stats/all-endpoints', (req, res) => {
const statsEndpointsFile = path.resolve(__dirname, `./mock/list/FakeAllEndPoints.json`);
const list = require(statsEndpointsFile);
res.status(200).json(list);
});

// stats APIs end

// endpoints and storekeys
app.get('/api/admin/stores/query/endpoints/:packageType', (req, res) => {
const [pkgType] = [req.params.packageType];
const statsEndpointsFile = path.resolve(__dirname, `./mock/list/FakeAllEndPoints.json`);
const list = require(statsEndpointsFile);
if (pkgType==="all"){
res.status(200).json(list);
}else{
const filtered = {
items: list.items.filter(i=>pkgType===i.packageType)
};
res.status(200).json(filtered);
}
});

app.get('/api/admin/stores/query/storekeys/:packageType', (req, res) => {
const [pkgType] = [req.params.packageType];
const statsEndpointsFile = path.resolve(__dirname, `./mock/list/FakeAllEndPoints.json`);
const list = require(statsEndpointsFile);
if (pkgType==="all"){
const keys = {
items: list.items.map(i=>i.key)
};
res.status(200).json(keys);
}else{
const filteredKeys = {
items: list.items.filter(i=>pkgType===i.packageType).map(i=>i.key)
};
res.status(200).json(filteredKeys);
}
});


const decideMockListFile = (packgeType, type) => {
const pkgToFileMapping = {"maven": "Maven", "generic-http": "Generic", "npm": "NPM"};
const typeToFileMapping = {"remote": "Remote", "hosted": "Hosted", "group": "Group"};
path.resolve(__dirname, './file-location');
return path.resolve(__dirname, `./mock/list/Fake${pkgToFileMapping[packgeType]}${typeToFileMapping[type]}List.json`);
};

Expand Down Expand Up @@ -149,8 +190,8 @@ app.put(`${STORE_API_BASE}/:packageType/:type/:name`, (req, res) => {
}
});

// Mock authentication
app.get('/api/admin/auth/userinfo', (req, res) => {
const testUser = {userName: "indy", roles: ["admin", "power-user", "user"]};
res.status(200).json(testUser);
});

Loading

0 comments on commit 68de46e

Please sign in to comment.