Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame^] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * SDC |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 6 | * ================================================================================ |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | |
| 21 | import expect from 'expect'; |
| 22 | import $ from 'jquery'; |
| 23 | import RestAPIUtil, {makeQueryParams} from 'src/nfvo-utils/RestAPIUtil'; |
| 24 | |
| 25 | const URL = 'http://bla.ble.blu/'; |
| 26 | |
| 27 | describe('RestAPIUtil Util class', () => { |
| 28 | |
| 29 | beforeEach(()=> { |
| 30 | $.ajax = (options) => options; |
| 31 | }); |
| 32 | |
| 33 | it('RestAPIUtil does exist', () => { |
| 34 | expect(RestAPIUtil).toExist(); |
| 35 | }); |
| 36 | |
| 37 | it('RestAPIUtil makeQueryParams does exist', () => { |
| 38 | expect(makeQueryParams).toExist(); |
| 39 | }); |
| 40 | |
| 41 | it('RestAPIUtil makeQueryParams params', () => { |
| 42 | const pageStart = 1, pageSize = 25; |
| 43 | const response = makeQueryParams({pagination: {pageStart, pageSize}}); |
| 44 | expect(response.pageStart).toBe(pageStart); |
| 45 | expect(response.pageSize).toBe(pageSize); |
| 46 | }); |
| 47 | |
| 48 | it('normal basic fetch', () => { |
| 49 | const response = RestAPIUtil.fetch(URL); |
| 50 | expect(response).toExist(); |
| 51 | }); |
| 52 | |
| 53 | it('no url', function () { |
| 54 | expect(function () { |
| 55 | RestAPIUtil.fetch(); |
| 56 | }).toThrow(/url/); |
| 57 | }); |
| 58 | |
| 59 | it('fetch with pagination', () => { |
| 60 | const pageStart = 1, pageSize = 25; |
| 61 | const response = RestAPIUtil.fetch(URL, {pagination: {pageStart, pageSize}}); |
| 62 | expect(response.pagination).toExist(); |
| 63 | expect(response.url).toInclude(`?pageStart=${pageStart}&pageSize=${pageSize}`); |
| 64 | }); |
| 65 | |
| 66 | it('fetch with sorting', () => { |
| 67 | const sortField = 'name', sortDir = 'ASCENDING'; |
| 68 | const response = RestAPIUtil.fetch(URL, {sorting: {sortField, sortDir}}); |
| 69 | expect(response.sorting).toExist(); |
| 70 | expect(response.url).toInclude(`?sortField=${sortField}&sortDir=${sortDir}`); |
| 71 | }); |
| 72 | |
| 73 | it('fetch with filtering', () => { |
| 74 | const baseFilter = [ |
| 75 | { |
| 76 | criterionValue: 'service', |
| 77 | fieldName: 'Brand', |
| 78 | operator: 'EQUALS', |
| 79 | type: 'STRING' |
| 80 | }, |
| 81 | { |
| 82 | criterionValue: 'resource', |
| 83 | fieldName: 'Brand', |
| 84 | operator: 'EQUALS', |
| 85 | type: 'STRING' |
| 86 | } |
| 87 | ]; |
| 88 | const response = RestAPIUtil.fetch(URL, {filtering: {filterCriteria: baseFilter, logicalRelation: 'OR'}}); |
| 89 | expect(response.filtering).toExist(); |
| 90 | expect(response.url).toInclude('?filter='); |
| 91 | }); |
| 92 | |
| 93 | it('fetch with qParams', () => { |
| 94 | const response = RestAPIUtil.fetch(URL, {qParams: {pageStart: 1, pageSize: 10}}); |
| 95 | expect(response.qParams).toExist(); |
| 96 | }); |
| 97 | |
| 98 | it('fetch with url on options', () => { |
| 99 | const response = RestAPIUtil.fetch(URL, {url:'12345', qParams: {pageStart: 1, pageSize: 10}}); |
| 100 | expect(response.qParams).toExist(); |
| 101 | }); |
| 102 | |
| 103 | it('fetch with url path param', () => { |
| 104 | let someData = 'data'; |
| 105 | const response = RestAPIUtil.fetch(`${URL}{someData}/`, {params: {someData}}); |
| 106 | expect(response.url).toInclude(`/${someData}/`); |
| 107 | }); |
| 108 | |
| 109 | it('fetch with url undefined path param', () => { |
| 110 | const response = RestAPIUtil.fetch(`${URL}{someData}/`, {params: {someData: undefined}}); |
| 111 | expect(response.url).toInclude('/undefined/'); |
| 112 | }); |
| 113 | |
| 114 | it('normal basic create', () => { |
| 115 | const response = RestAPIUtil.create(URL); |
| 116 | expect(response).toExist(); |
| 117 | }); |
| 118 | |
| 119 | it('create with FormData', () => { |
| 120 | let formData = new FormData(); |
| 121 | formData.append('username', 'Chris'); |
| 122 | const response = RestAPIUtil.create(URL, formData); |
| 123 | expect(response).toExist(); |
| 124 | }); |
| 125 | |
| 126 | it('create with FormData with md5', () => { |
| 127 | let formData = new FormData(); |
| 128 | formData.append('username', 'Chris'); |
| 129 | const response = RestAPIUtil.create(URL, formData, {md5: true}); |
| 130 | expect(response).toExist(); |
| 131 | }); |
| 132 | |
| 133 | it('create with file', () => { |
| 134 | let progressCallback = () => {}; |
| 135 | const response = RestAPIUtil.create(URL, {}, {progressCallback, fileSize: 123}); |
| 136 | expect(response).toExist(); |
| 137 | }); |
| 138 | |
| 139 | it('normal basic save', () => { |
| 140 | const response = RestAPIUtil.save(URL); |
| 141 | expect(response).toExist(); |
| 142 | }); |
| 143 | |
| 144 | it('normal basic delete', () => { |
| 145 | const response = RestAPIUtil.destroy(URL); |
| 146 | expect(response).toExist(); |
| 147 | }); |
| 148 | |
| 149 | }); |