Tests coverage up and some minor bug fixes
[portal.git] / portal-BE / src / test / java / org / onap / portal / controller / WidgetsControllerTest.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.controller;
42
43 import static junit.framework.TestCase.assertEquals;
44 import static junit.framework.TestCase.assertNull;
45 import static org.mockito.Mockito.when;
46
47 import java.time.LocalDateTime;
48 import java.util.ArrayList;
49 import java.util.List;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.onap.portal.dao.fn.FnLanguageDao;
55 import org.onap.portal.dao.fn.FnUserDao;
56 import org.onap.portal.domain.db.fn.FnLanguage;
57 import org.onap.portal.domain.db.fn.FnUser;
58 import org.onap.portal.domain.dto.transport.OnboardingWidget;
59 import org.onap.portal.framework.MockitoTestSuite;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.boot.test.context.SpringBootTest;
62 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
63 import org.springframework.security.core.userdetails.UsernameNotFoundException;
64 import org.springframework.test.context.TestPropertySource;
65 import org.springframework.test.context.junit4.SpringRunner;
66
67 @RunWith(SpringRunner.class)
68 @SpringBootTest
69 @TestPropertySource(locations = "classpath:test.properties")
70 public class WidgetsControllerTest {
71
72        private UsernamePasswordAuthenticationToken principal = new UsernamePasswordAuthenticationToken("demo",
73                "demo123");
74
75        MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
76
77        HttpServletRequest request = mockitoTestSuite.getMockedRequest();
78        HttpServletResponse response = mockitoTestSuite.getMockedResponse();
79
80        @Autowired
81        private WidgetsController widgetsController;
82        @Autowired
83        private
84        FnUserDao fnUserDao;
85        @Autowired
86        private
87        FnLanguageDao fnLanguageDao;
88
89        private FnLanguage language = getFnLanguage();
90        private FnUser questUser = getQuestUser();
91        private FnUser notQuestUser = getNotQuestUser();
92
93        @Test(expected = UsernameNotFoundException.class)
94        public void getOnboardingWidgetsNullUserTest() {
95               UsernamePasswordAuthenticationToken nullPrincipal = new UsernamePasswordAuthenticationToken("nulluser",
96                       "demo123");
97               widgetsController.getOnboardingWidgets(nullPrincipal, request, response);
98        }
99
100        @Test
101        public void getOnboardingWidgetsQuestUserTest() {
102               UsernamePasswordAuthenticationToken questPrincipal = new UsernamePasswordAuthenticationToken("questUser",
103                       "demo123");
104               fnUserDao.save(questUser);
105               List<OnboardingWidget> onboardingWidgets = widgetsController
106                       .getOnboardingWidgets(questPrincipal, request, response);
107               assertNull(onboardingWidgets);
108
109               //Clean up
110               fnUserDao.delete(questUser);
111               fnLanguageDao.delete(language);
112        }
113
114        @Test
115        public void getOnboardingWidgetsUserTest() {
116               UsernamePasswordAuthenticationToken notQuestprincipal = new UsernamePasswordAuthenticationToken("notQuestUser",
117                       "demo123");
118               fnUserDao.save(notQuestUser);
119               List<OnboardingWidget> expected = new ArrayList<>();
120               when(request.getHeader("X-Widgets-Type")).thenReturn("managed");
121
122               List<OnboardingWidget> actual = widgetsController
123                       .getOnboardingWidgets(notQuestprincipal, request, response);
124
125               assertEquals(expected, actual);
126               fnUserDao.delete(notQuestUser);
127        }
128
129        @Test
130        public void getOnboardingWidgetsWrongHeaderTest() {
131               UsernamePasswordAuthenticationToken notQuestprincipal = new UsernamePasswordAuthenticationToken("notQuestUser",
132                       "demo123");
133               fnUserDao.save(notQuestUser);
134               when(request.getHeader("X-Widgets-Type")).thenReturn("test");
135               List<OnboardingWidget> actual = widgetsController
136                       .getOnboardingWidgets(notQuestprincipal, request, response);
137
138               assertNull(actual);
139               fnUserDao.delete(notQuestUser);
140        }
141
142        @Test
143        public void putOnboardingWidget() {
144        }
145
146        @Test
147        public void postOnboardingWidget() {
148        }
149
150        @Test
151        public void deleteOnboardingWidget() {
152        }
153
154        @Test
155        public void putWidgetCatalogSelection() {
156        }
157
158        private FnUser getQuestUser(){
159               return FnUser.builder()
160                       .loginId("questUser")
161                       .loginPwd("demo123")
162                       .lastLoginDate(LocalDateTime.now())
163                       .activeYn(true)
164                       .createdDate(LocalDateTime.now())
165                       .modifiedDate(LocalDateTime.now())
166                       .isInternalYn(true)
167                       .languageId(language)
168                       .guest(true)
169                       .build();
170        }
171
172        private FnUser getNotQuestUser(){
173               return FnUser.builder()
174                       .loginId("notQuestUser")
175                       .loginPwd("demo123")
176                       .lastLoginDate(LocalDateTime.now())
177                       .activeYn(true)
178                       .createdDate(LocalDateTime.now())
179                       .modifiedDate(LocalDateTime.now())
180                       .isInternalYn(true)
181                       .languageId(language)
182                       .guest(false)
183                       .build();
184        }
185
186        private FnLanguage getFnLanguage(){
187               return FnLanguage.builder().languageName("Polish").languageAlias("Pl").build();
188        }
189 }