Add collaboration feature
[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', () => {
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                 expect(store.getState()).toEqual(expectedStore);
57         });
58
59         it('validating error in serviceException with variables', () => {
60                 let textStatus = '', errorThrown = '';
61                 let xhr = {
62                         responseJSON: {
63                                 requestError: {
64                                         serviceException: {
65                                                 messageId: 'SVC4122',
66                                                 text: "Error: Invalid artifact type '%1'.",
67                                                 variables: ['newType']
68                                         }
69                                 }
70                         }
71                 };
72                 deepFreeze(xhr);
73         
74                 const errorNotification = {                     
75                         title: 'Error: SVC4122',
76                         msg: 'Error: Invalid artifact type newType.',                   
77                         modalClassName: 'notification-modal',
78                         type: modalType.ERROR   
79                 };
80
81                 const expectedStore = cloneAndSet(store.getState(), 'modal', errorNotification);
82
83                 errorResponseHandler(xhr, textStatus, errorThrown);
84
85                 expect(store.getState()).toEqual(expectedStore);
86         });
87
88         it('validating error in response', () => {
89                 let textStatus = '', errorThrown = '';
90                 let xhr = {
91                         responseJSON: {
92                                 status: 'AA',
93                                 message: 'Error: Invalid data.'
94                         }
95                 };
96                 deepFreeze(xhr);
97
98                 const errorNotification = {                     
99                         title: 'AA',
100                         msg: 'Error: Invalid data.',                    
101                         modalClassName: 'notification-modal',
102                         type: modalType.ERROR   
103                 };
104
105                 const expectedStore = cloneAndSet(store.getState(), 'modal', errorNotification);
106
107                 errorResponseHandler(xhr, textStatus, errorThrown);
108
109                 expect(store.getState()).toEqual(expectedStore);
110         });
111
112         it('validating error in request', () => {
113                 let textStatus = '', errorThrown = '';
114                 let xhr = {
115                         statusText: '500',
116                         responseText: 'Internal server error.'
117                 };
118                 deepFreeze(xhr);
119         
120                 const errorNotification = {                     
121                         title: '500',
122                         msg: 'Internal server error.',                  
123                         modalClassName: 'notification-modal',
124                         type: modalType.ERROR   
125                 };
126
127                 const expectedStore = cloneAndSet(store.getState(), 'modal', errorNotification);
128
129                 errorResponseHandler(xhr, textStatus, errorThrown);
130
131                 expect(store.getState()).toEqual(expectedStore);
132         });
133 });