Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support data loader in frontend #2480

Merged
merged 13 commits into from
Jun 24, 2022
198 changes: 198 additions & 0 deletions web/cypress/integration/route/data-loader-import.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

context('Data Loader import', () => {
const selector = {
drawer: '.ant-drawer-content',
selectDropdown: '.ant-select-dropdown',
listTbody: '.ant-table-tbody',
listRow: 'tr.ant-table-row-level-0',
refresh: '.anticon-reload',
notification: '.ant-notification-notice-message',
notificationCloseIcon: '.ant-notification-notice-close',
fileSelector: '[type=file]',
notificationDesc: '.ant-notification-notice-description',
task_name: '#task_name',
merge_method: '#merge_method',
};
const data = {
route_name_0: 'route_name_0',
route_name_1: 'route_name_1',
upstream_node0_host_0: '1.1.1.1',
upstream_node0_host_1: '2.2.2.2',
importErrorMsg: 'required file type is .yaml, .yml or .json but got: .txt',
uploadRouteFiles: [
'../../../api/test/testdata/import/default.json',
'../../../api/test/testdata/import/default.yaml',
'import-error.txt',
],
// Note: export file's name will be end of a timestamp
jsonMask: 'cypress/downloads/*.json',
yamlMask: 'cypress/downloads/*.yaml',
port: '80',
weight: 1,
importRouteSuccess: 'Import Successfully',
deleteRouteSuccess: 'Delete Route Successfully',
deleteUpstreamSuccess: 'Delete Upstream Successfully',
};
const cases = {
API101: '../../../api/test/testdata/import/Postman-API101.yaml',
};

beforeEach(() => {
cy.login();

cy.fixture('selector.json').as('domSelector');
cy.fixture('data.json').as('data');
cy.fixture('export-route-dataset.json').as('exportFile');
});

it('should import API101 with merge mode', () => {
cy.visit('/');
cy.contains('Route').click();

cy.get(selector.refresh).click();
cy.contains('Advanced').click();
cy.contains('Import').should('be.visible').click();

// select Data Loader type
cy.contains('OpenAPI 3').should('be.visible').click();
cy.get(selector.selectDropdown).contains('OpenAPI 3').should('be.visible').click();
cy.get(selector.drawer).get(selector.task_name).type('api101_mm');
cy.get(selector.fileSelector).attachFile(cases.API101);
cy.get(selector.drawer).contains('Submit').click();
cy.get(selector.drawer).contains('Import Successfully').should('be.visible');
cy.get(selector.drawer).contains('Total 3 route imported, 0 failed').click();
cy.get(selector.drawer).contains('Close').click();

// check result
cy.get(selector.listTbody).get(selector.listRow).should('have.length', 3);
cy.contains('api101_mm_customer').should('be.visible');
cy.contains('api101_mm_customer/{customer_id}').should('be.visible');
cy.contains('api101_mm_customers').should('be.visible');

// remove route
for (let i = 0; i < 3; i += 1) {
cy.get(selector.listTbody).get(selector.listRow).contains('More').click();
cy.contains('Delete').should('be.visible').click();
cy.contains('OK').should('be.visible').click();
cy.get(selector.notification).should('contain', data.deleteRouteSuccess);
cy.get(selector.notificationCloseIcon).click();
}
});

it('should import API101 with duplicate upstream', () => {
cy.visit('/');
cy.contains('Route').click();

cy.get(selector.refresh).click();
cy.contains('Advanced').click();
cy.contains('Import').should('be.visible').click();

// select Data Loader type
cy.contains('OpenAPI 3').should('be.visible').click();
cy.get(selector.selectDropdown).contains('OpenAPI 3').should('be.visible').click();
cy.get(selector.drawer).get(selector.task_name).type('api101_mm');
cy.get(selector.fileSelector).attachFile(cases.API101);
cy.get(selector.drawer).contains('Submit').click();
cy.get(selector.drawer).contains('Import Failed').should('be.visible');
cy.get(selector.drawer).contains('Total 1 upstream imported, 1 failed').click();
cy.get(selector.drawer).contains('key: api101_mm is conflicted').should('be.visible');
cy.get(selector.drawer).contains('Close').click();

// remove route
for (let i = 0; i < 3; i += 1) {
cy.get(selector.listTbody).get(selector.listRow).contains('More').click();
cy.contains('Delete').should('be.visible').click();
cy.contains('OK').should('be.visible').click();
cy.get(selector.notification).should('contain', data.deleteRouteSuccess);
cy.get(selector.notificationCloseIcon).click();
}
});

it('should import API101 with non-merge mode', () => {
cy.visit('/');
cy.contains('Route').click();

cy.get(selector.refresh).click();
cy.contains('Advanced').click();
cy.contains('Import').should('be.visible').click();

// select Data Loader type
cy.contains('OpenAPI 3').should('be.visible').click();
cy.get(selector.selectDropdown).contains('OpenAPI 3').should('be.visible').click();
cy.get(selector.drawer).get(selector.task_name).type('api101_nmm');
cy.get(selector.drawer).get(selector.merge_method).click();
cy.get(selector.fileSelector).attachFile(cases.API101);
cy.get(selector.drawer).contains('Submit').click();
cy.get(selector.drawer).contains('Import Successfully').should('be.visible');
cy.get(selector.drawer).contains('Total 5 route imported, 0 failed').click();
cy.get(selector.drawer).contains('Close').click();

// check result
cy.get(selector.listTbody).get(selector.listRow).should('have.length', 5);

// remove route
/**/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section seems to be able to be deleted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

});

it('should import API101 with duplicate route', () => {
cy.visit('/');
cy.contains('Route').click();

cy.get(selector.refresh).click();
cy.contains('Advanced').click();
cy.contains('Import').should('be.visible').click();

// select Data Loader type
cy.contains('OpenAPI 3').should('be.visible').click();
cy.get(selector.selectDropdown).contains('OpenAPI 3').should('be.visible').click();
cy.get(selector.drawer).get(selector.task_name).type('api101_nmm');
cy.get(selector.drawer).get(selector.merge_method).click();
cy.get(selector.fileSelector).attachFile(cases.API101);
cy.get(selector.drawer).contains('Submit').click();
cy.get(selector.drawer).contains('Import Failed').should('be.visible');
cy.get(selector.drawer).contains('Total 5 route imported, 1 failed').click();
cy.get(selector.drawer).contains('is duplicated with route api101_nmm_').should('be.visible');
cy.get(selector.drawer).contains('Close').click();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is best to add assertions to confirm that the drawer has disappeared

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

});

it('should remove all routes and upstreams', function () {
cy.visit('/');
cy.contains('Route').click();
cy.get(selector.refresh).click();
// remove route
for (let i = 0; i < 5; i += 1) {
cy.get(selector.listTbody).get(selector.listRow).contains('More').click();
cy.contains('Delete').should('be.visible').click();
cy.contains('OK').should('be.visible').click();
cy.get(selector.notification).should('contain', data.deleteRouteSuccess);
cy.get(selector.notificationCloseIcon).click();
}

cy.visit('/');
cy.contains('Upstream').click();
cy.get(selector.refresh).click();
// remove route
for (let i = 0; i < 2; i += 1) {
cy.get(selector.listTbody).get(selector.listRow).contains('Delete').click();
cy.contains('Confirm').should('be.visible').click();
cy.get(selector.notification).should('contain', data.deleteUpstreamSuccess);
cy.get(selector.notificationCloseIcon).click();
}
});
});
1 change: 1 addition & 0 deletions web/src/locales/en-US/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ export default {
'menu.serverinfo': 'System Info',
'menu.advanced-feature': 'Advanced',
'menu.more': 'More',
'menu.close': 'Close',
};
1 change: 1 addition & 0 deletions web/src/locales/tr-TR/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ export default {
'menu.serverinfo': 'Sistem Bilgisi',
'menu.advanced-feature': 'Gelişmiş Özellikler',
'menu.more': 'Daha Fazla',
'menu.close': 'Kapat',
};
1 change: 1 addition & 0 deletions web/src/locales/zh-CN/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ export default {
'menu.serverinfo': '系统信息',
'menu.advanced-feature': '高级特性',
'menu.more': '更多',
'menu.close': '关闭',
};
80 changes: 12 additions & 68 deletions web/src/pages/Route/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ import {
Select,
Radio,
Form,
Upload,
Modal,
Divider,
Menu,
Dropdown,
Tooltip,
Expand All @@ -46,7 +44,6 @@ import { omit } from 'lodash';

import { DELETE_FIELDS } from '@/constants';
import { timestampToLocaleString } from '@/helpers';
import type { RcFile } from 'antd/lib/upload';

import {
update,
Expand All @@ -56,11 +53,11 @@ import {
fetchLabelList,
updateRouteStatus,
exportRoutes,
importRoutes,
} from './service';
import { DebugDrawView } from './components/DebugViews';
import { RawDataEditor } from '@/components/RawDataEditor';
import { EXPORT_FILE_MIME_TYPE_SUPPORTED } from './constants';
import DataLoaderImport from '@/pages/Route/components/DataLoader/Import';

const { OptGroup, Option } = Select;

Expand All @@ -81,8 +78,7 @@ const Page: React.FC = () => {

const [labelList, setLabelList] = useState<LabelList>({});
const [selectedRowKeys, setSelectedRowKeys] = useState<string[]>([]);
const [uploadFileList, setUploadFileList] = useState<RcFile[]>([]);
const [showImportModal, setShowImportModal] = useState(false);
const [showImportDrawer, setShowImportDrawer] = useState(false);
const [visible, setVisible] = useState(false);
const [rawData, setRawData] = useState<Record<string, any>>({});
const [id, setId] = useState('');
Expand Down Expand Up @@ -154,27 +150,6 @@ const Page: React.FC = () => {
});
};

const handleImport = () => {
const formData = new FormData();
if (!uploadFileList[0]) {
notification.warn({
message: formatMessage({ id: 'page.route.button.selectFile' }),
});
return;
}
formData.append('file', uploadFileList[0]);
formData.append('type', 'openapi3');

importRoutes(formData).then(() => {
handleTableActionSuccessResponse(
`${formatMessage({ id: 'page.route.button.importOpenApi' })} ${formatMessage({
id: 'component.status.success',
})}`,
);
setShowImportModal(false);
});
};

const ListToolbar = () => {
const tools = [
{
Expand All @@ -194,11 +169,10 @@ const Page: React.FC = () => {
},
},
{
name: formatMessage({ id: 'page.route.button.importOpenApi' }),
name: formatMessage({ id: 'page.route.data_loader.import' }),
icon: <ImportOutlined />,
onClick: () => {
setUploadFileList([]);
setShowImportModal(true);
setShowImportDrawer(true);
},
},
];
Expand Down Expand Up @@ -613,45 +587,15 @@ const Page: React.FC = () => {
);
}}
/>
<Modal
title={formatMessage({ id: 'page.route.button.importOpenApi' })}
visible={showImportModal}
okText={formatMessage({ id: 'component.global.confirm' })}
onOk={handleImport}
onCancel={() => {
setShowImportModal(false);
}}
>
<Upload
fileList={uploadFileList as any}
beforeUpload={(file) => {
setUploadFileList([file]);
return false;
}}
onRemove={() => {
setUploadFileList([]);
{showImportDrawer && (
<DataLoaderImport
onClose={() => setShowImportDrawer(false)}
onFinish={() => {
checkPageList(ref);
setShowImportDrawer(false);
}}
>
<Button type="primary" icon={<ImportOutlined />}>
{formatMessage({ id: 'page.route.button.selectFile' })}
</Button>
</Upload>
<Divider />
<div>
<p>{formatMessage({ id: 'page.route.instructions' })}:</p>
<p>
<a
href="https://apisix.apache.org/docs/dashboard/IMPORT_OPENAPI_USER_GUIDE"
target="_blank"
>
1.{' '}
{`${formatMessage({ id: 'page.route.import' })} ${formatMessage({
id: 'page.route.instructions',
})}`}
</a>
</p>
</div>
</Modal>
/>
)}
</PageHeaderWrapper>
);
};
Expand Down
Loading