[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / test / utils / errorResponseHandler.test.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 import deepFreeze from 'deep-freeze';
17
18 import {cloneAndSet} from '../../test-utils/Util.js';
19 import store from 'sdc-app/AppStore.js';
20 import errorResponseHandler from 'nfvo-utils/ErrorResponseHandler.js';
21 import {typeEnum as modalType} from 'nfvo-components/modal/GlobalModalConstants.js';
22
23 describe('Error Response Handler Util', () => {
24
25         beforeEach(function () {
26                 deepFreeze(store.getState());
27         });
28
29         it('validating error in policyException', done => {
30                 let textStatus = '', errorThrown = '';
31                 let xhr = {
32                         responseJSON: {
33                                 requestError: {
34                                         policyException: {
35                                                 messageId: 'SVC4122',
36                                                 text: 'Error: Invalid data.'
37                                         }
38                                 }
39                         }
40                 };
41                 deepFreeze(xhr);
42
43                 const errorNotification = {                     
44                         title: 'Error: SVC4122',
45                         msg: 'Error: Invalid data.',                    
46                         modalClassName: 'notification-modal',
47                         type: modalType.ERROR   
48                 };
49
50
51
52                 const expectedStore = cloneAndSet(store.getState(), 'modal', errorNotification);
53
54                 errorResponseHandler(xhr, textStatus, errorThrown);
55
56                 setTimeout(function () {                        
57                         expect(store.getState()).toEqual(expectedStore);
58                         done();
59                 }, 100);
60         });
61
62         it('validating error in serviceException with variables', done => {
63                 let textStatus = '', errorThrown = '';
64                 let xhr = {
65                         responseJSON: {
66                                 requestError: {
67                                         serviceException: {
68                                                 messageId: 'SVC4122',
69                                                 text: "Error: Invalid artifact type '%1'.",
70                                                 variables: ['newType']
71                                         }
72                                 }
73                         }
74                 };
75                 deepFreeze(xhr);
76         
77                 const errorNotification = {                     
78                         title: 'Error: SVC4122',
79                         msg: 'Error: Invalid artifact type newType.',                   
80                         modalClassName: 'notification-modal',
81                         type: modalType.ERROR   
82                 };
83
84                 const expectedStore = cloneAndSet(store.getState(), 'modal', errorNotification);
85
86                 errorResponseHandler(xhr, textStatus, errorThrown);
87
88                 setTimeout(function () {
89                         expect(store.getState()).toEqual(expectedStore);
90                         done();
91                 }, 100);
92         });
93
94         it('validating error in response', done => {
95                 let textStatus = '', errorThrown = '';
96                 let xhr = {
97                         responseJSON: {
98                                 status: 'AA',
99                                 message: 'Error: Invalid data.'
100                         }
101                 };
102                 deepFreeze(xhr);
103
104                 const errorNotification = {                     
105                         title: 'AA',
106                         msg: 'Error: Invalid data.',                    
107                         modalClassName: 'notification-modal',
108                         type: modalType.ERROR   
109                 };
110
111                 const expectedStore = cloneAndSet(store.getState(), 'modal', errorNotification);
112
113                 errorResponseHandler(xhr, textStatus, errorThrown);
114
115                 setTimeout(function () {
116                         expect(store.getState()).toEqual(expectedStore);
117                         done();
118                 }, 100);
119         });
120
121         it('validating error in request', done => {
122                 let textStatus = '', errorThrown = '';
123                 let xhr = {
124                         statusText: '500',
125                         responseText: 'Internal server error.'
126                 };
127                 deepFreeze(xhr);
128         
129                 const errorNotification = {                     
130                         title: '500',
131                         msg: 'Internal server error.',                  
132                         modalClassName: 'notification-modal',
133                         type: modalType.ERROR   
134                 };
135
136                 const expectedStore = cloneAndSet(store.getState(), 'modal', errorNotification);
137
138                 errorResponseHandler(xhr, textStatus, errorThrown);
139
140                 setTimeout(function () {
141                         expect(store.getState()).toEqual(expectedStore);
142                         done();
143                 }, 100);
144         });
145 });