From aa9b320ff93511280cf51b03d38fb9254af6b530 Mon Sep 17 00:00:00 2001 From: sa282w Date: Wed, 25 Jul 2018 13:25:43 -0400 Subject: JUnits for coverage Issue-ID: PORTAL-273 JUnits for sonar coverage Change-Id: Icb88d8563164281d29877bbc2de9c8f1f780aa0c Signed-off-by: sa282w --- .../impl/InitializationServiceImplTest.java | 83 ++++++ .../service/impl/StorageServiceImplTest.java | 57 ++++ .../service/impl/WidgetCatalogServiceImplTest.java | 293 +++++++++++++++++++++ .../controller/WidgetsCatalogControllerTest.java | 284 ++++++++++++++++++++ 4 files changed, 717 insertions(+) create mode 100644 ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/InitializationServiceImplTest.java create mode 100644 ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/WidgetCatalogServiceImplTest.java (limited to 'ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap') diff --git a/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/InitializationServiceImplTest.java b/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/InitializationServiceImplTest.java new file mode 100644 index 00000000..160ded42 --- /dev/null +++ b/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/InitializationServiceImplTest.java @@ -0,0 +1,83 @@ +/*- + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software 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. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * 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. + * + * ============LICENSE_END============================================ + * + * + */ +package org.onap.portalapp.widget.service.impl; + +import static org.mockito.Mockito.when; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.widget.service.MicroserviceService; +import org.onap.portalapp.widget.service.StorageService; +import org.onap.portalapp.widget.service.WidgetCatalogService; + +public class InitializationServiceImplTest { + + @InjectMocks + InitializationServiceImpl initializationServiceImpl; + @Mock + WidgetCatalogService widgetCatalogService; + + @Mock + StorageService storageService; + + @Mock + MicroserviceService microserviceService; + + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testInitialize() { + String name="News"; + + String newServiceName = name + " Microservice"; + when(microserviceService.getMicroserviceIdByName(newServiceName)).thenReturn(1l); + newServiceName = "Events" + " Microservice"; + when(microserviceService.getMicroserviceIdByName(newServiceName)).thenReturn(null); + initializationServiceImpl.initialize(); + + } + + + +} diff --git a/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/StorageServiceImplTest.java b/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/StorageServiceImplTest.java index 336f857c..fc535270 100644 --- a/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/StorageServiceImplTest.java +++ b/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/StorageServiceImplTest.java @@ -39,6 +39,8 @@ package org.onap.portalapp.widget.service.impl; import static org.mockito.Mockito.when; +import java.io.File; +import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; @@ -179,5 +181,60 @@ public void testcheckZipFileInvalid() { } + @Test(expected=IllegalArgumentException.class) + public void testSaveMultiPartFile() { + MockMultipartFile mockMultipartFile = new MockMultipartFile( + "fileData", + "test.zip", + "text/plain", + "test".getBytes()); + + WidgetCatalog catalog=new WidgetCatalog(); + catalog.setServiceId(2l); + catalog.setName("test"); + + storageServiceImpl.save(mockMultipartFile, catalog, 2l); + + + } + + @Test(expected=IllegalArgumentException.class) + public void testInitSave()throws Exception { + MockMultipartFile mockMultipartFile = new MockMultipartFile( + "fileData", + "test.zip", + "text/plain", + "test".getBytes()); + File convFile = new File(mockMultipartFile.getOriginalFilename()); + FileOutputStream fos = new FileOutputStream(convFile); + fos.write(mockMultipartFile.getBytes()); + fos.close(); + WidgetCatalog catalog=new WidgetCatalog(); + catalog.setServiceId(2l); + catalog.setName("test"); + storageServiceImpl.initSave(convFile, catalog, 2l); + convFile.delete(); + + + } + + @Test + public void testWidgetFramework()throws Exception { + List widgetFiles=new ArrayList<>(); + WidgetFile file=new WidgetFile(); + file.setCss("test".getBytes()); + file.setController("test function() Test".getBytes()); + file.setMarkup("Test".getBytes()); + file.setFramework("test".getBytes()); + widgetFiles.add(file); + + when(sessionFactory.getCurrentSession()).thenReturn(currentSession); + when(currentSession.beginTransaction()).thenReturn(transaction); + when(currentSession.createCriteria(WidgetFile.class)).thenReturn(criteria); + when(criteria.list()).thenReturn(widgetFiles); + storageServiceImpl.getWidgetFramework(2l); + + } + } diff --git a/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/WidgetCatalogServiceImplTest.java b/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/WidgetCatalogServiceImplTest.java new file mode 100644 index 00000000..933710d2 --- /dev/null +++ b/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/service/impl/WidgetCatalogServiceImplTest.java @@ -0,0 +1,293 @@ +/*- + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software 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. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * 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. + * + * ============LICENSE_END============================================ + * + * + */ +package org.onap.portalapp.widget.service.impl; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.hibernate.Criteria; +import org.hibernate.SQLQuery; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; +import org.hibernate.criterion.Restrictions; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.portalapp.widget.domain.App; +import org.onap.portalapp.widget.domain.RoleApp; +import org.onap.portalapp.widget.domain.WidgetCatalog; + +public class WidgetCatalogServiceImplTest { + + @InjectMocks + WidgetCatalogServiceImpl widgetCatalogServiceImpl; + + @Mock + Session session; + @Mock + SessionFactory sessionFactory; + @Mock + Transaction transaction; + @Mock + Criteria criteria; + + @Mock + SQLQuery query; + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testSaveMicroserivce() { + List list = buildWidgetCatalog(); + when(sessionFactory.getCurrentSession()).thenReturn(session); + // when(session.beginTransaction()).thenReturn(transaction); + when(session.createCriteria(WidgetCatalog.class)).thenReturn(criteria); + when(criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).thenReturn(criteria); + when(criteria.list()).thenReturn(list); + + List catalogList = widgetCatalogServiceImpl.getWidgetCatalog(); + assertEquals("junit", catalogList.get(0).getName()); + + } + + @Test + public void tesGetUserWidgetCatalog() { + when(sessionFactory.getCurrentSession()).thenReturn(session); + StringBuilder sb = widgetUserCatalog("test"); + when(session.createSQLQuery(sb.toString())).thenReturn(query); + when(query.list()).thenReturn(buildWidgetCatalog()); + List catalogList = widgetCatalogServiceImpl.getUserWidgetCatalog("test"); + assertEquals("junit", catalogList.get(0).getName()); + + } + + @Test + public void getWidgetCatalog() { + + when(sessionFactory.getCurrentSession()).thenReturn(session); + when(session.beginTransaction()).thenReturn(transaction); + when(session.get(WidgetCatalog.class, 1l)).thenReturn(buildWidgetCatalog().get(0)); + WidgetCatalog catalog = widgetCatalogServiceImpl.getWidgetCatalog(1l); + assertEquals("junit", catalog.getName()); + } + + @Test + public void deleteWidgetCatalog() { + Long widgetCatalogId = 1l; + when(sessionFactory.getCurrentSession()).thenReturn(session, session); + when(session.beginTransaction()).thenReturn(transaction, transaction); + when(session.get(WidgetCatalog.class, widgetCatalogId)).thenReturn(buildWidgetCatalog().get(0)); + String queryString = "delete from ep_pers_user_widget_sel where widget_id = :widgetId "; + when(session.createSQLQuery(queryString)).thenReturn(query); + + String deleteEpUserWidget = "delete from ep_pers_user_widget_placement where widget_id = :widgetId "; + String deleteEpUserWidgetCatalog = "delete from ep_widget_catalog_files where widget_id = :widgetId "; + String deleteUserWidgetCatalog = "delete from ep_widget_catalog_parameter where widget_id = :widgetId "; + when(session.createSQLQuery(deleteEpUserWidget)).thenReturn(query); + when(session.createSQLQuery(deleteEpUserWidgetCatalog)).thenReturn(query); + when(session.createSQLQuery(deleteUserWidgetCatalog)).thenReturn(query); + when(query.setParameter("widgetId", widgetCatalogId)).thenReturn(query, query, query, query); + widgetCatalogServiceImpl.deleteWidgetCatalog(widgetCatalogId); + + } + + @Test + public void deleteWidgetCatalogEmpty() { + Long widgetCatalogId = 1l; + when(sessionFactory.getCurrentSession()).thenReturn(session, session); + when(session.beginTransaction()).thenReturn(transaction, transaction); + when(session.get(WidgetCatalog.class, widgetCatalogId)).thenReturn(null); + widgetCatalogServiceImpl.deleteWidgetCatalog(widgetCatalogId); + } + + @Test + public void saveWidgetCatalog() { + WidgetCatalog widgetCatalog = buildWidgetCatalog().get(0); + widgetCatalog.setAllowAllUser("1"); + App app = new App(); + app.setAppId(1l); + RoleApp roleApp = new RoleApp(); + roleApp.setRoleId(1l); + roleApp.setApp(app); + Set roles = new HashSet<>(); + roles.add(roleApp); + widgetCatalog.setWidgetRoles(roles); + String sql = "UPDATE ep_widget_catalog_role SET app_id = " + roleApp.getApp().getAppId() + " WHERE widget_id = " + + widgetCatalog.getId() + " AND ROLE_ID = " + roleApp.getRoleId(); + when(sessionFactory.openSession()).thenReturn(session, session); + when(session.beginTransaction()).thenReturn(transaction); + when(session.createSQLQuery(sql)).thenReturn(query); + widgetCatalogServiceImpl.saveWidgetCatalog(widgetCatalog); + + } + + @Test + public void updateWidgetCatalog() { + Long widgetCatalogId = 1l; + WidgetCatalog widgetCatalog = buildWidgetCatalog().get(0); + widgetCatalog.setServiceId(widgetCatalogId); + widgetCatalog.setAllowAllUser("1"); + App app = new App(); + app.setAppId(1l); + RoleApp roleApp = new RoleApp(); + roleApp.setRoleId(1l); + roleApp.setApp(app); + Set roles = new HashSet<>(); + roles.add(roleApp); + widgetCatalog.setWidgetRoles(roles); + String sql = "UPDATE ep_widget_catalog_role SET app_id = " + roleApp.getApp().getAppId() + " WHERE widget_id = " + + widgetCatalog.getId() + " AND ROLE_ID = " + roleApp.getRoleId(); + when(sessionFactory.getCurrentSession()).thenReturn(session); + when(session.beginTransaction()).thenReturn(transaction, transaction); + when(session.get(WidgetCatalog.class, widgetCatalogId)).thenReturn(buildWidgetCatalog().get(0)); + when(sessionFactory.openSession()).thenReturn(session, session); + when(session.createSQLQuery(sql)).thenReturn(query); + + widgetCatalogServiceImpl.updateWidgetCatalog(widgetCatalogId, widgetCatalog); + + } + + @Test + public void getServiceIdByWidget() { + Long widgetCatalogId = 1l; + when(sessionFactory.getCurrentSession()).thenReturn(session, session); + when(session.beginTransaction()).thenReturn(transaction, transaction); + when(session.get(WidgetCatalog.class, widgetCatalogId)).thenReturn(buildWidgetCatalog().get(0)); + Long serviceId = widgetCatalogServiceImpl.getServiceIdByWidget(widgetCatalogId); + assertEquals(widgetCatalogId, serviceId); + + } + + @Test(expected=NullPointerException.class) + public void getWidgetsByServiceId() { + Long serviceId=1l; + List list = buildWidgetCatalog(); + when(sessionFactory.getCurrentSession()).thenReturn(session,session); + // when(session.beginTransaction()).thenReturn(transaction); + when(session.createCriteria(WidgetCatalog.class)).thenReturn(criteria); + when(criteria.add(Restrictions.eq("serviceId", serviceId))).thenReturn(criteria); + when(criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)).thenReturn(criteria); + when(criteria.list()).thenReturn(list); + + List catalogList = widgetCatalogServiceImpl.getWidgetsByServiceId(serviceId); + assertEquals("junit", catalogList.get(0).getName()); + } + + private List buildWidgetCatalog() { + List list = new ArrayList(); + WidgetCatalog widget = new WidgetCatalog(); + widget.setId(1); + widget.setName("junit"); + widget.setServiceId(1l); + list.add(widget); + return list; + } + + private StringBuilder widgetUserCatalog(String loginName) { + StringBuilder sql = new StringBuilder() + + .append(" select userWidgets.widget_id, userWidgets.wdg_name, userWidgets.wdg_desc, b.x, b.status_cd, b.y, b.height, b.width from ") + .append(" ( ") + .append(" select distinct w.widget_id, w.wdg_name, w.wdg_desc from ") + .append(" ep_widget_catalog w, ") + .append(" ep_widget_catalog_role wr, ") + .append(" fn_user_role ur, ") + .append(" fn_app app, ") + .append(" fn_user u ") + .append(" where ") + .append(" w.widget_id = wr.WIDGET_ID and ") + .append(" ur.app_id = app.app_id and ") + .append(" app.enabled = 'Y' and ") + .append(" wr.role_id = ur.role_id and ") + .append(" ur.user_id = u.user_id and ") + .append(" u.login_id = '" + loginName + + "' and (w.all_user_flag = 'N' or w.all_user_flag is null) ") + .append(" ") + .append(" union all ") + .append(" ") + .append(" ") + .append(" select distinct w.widget_id, w.wdg_name, w.wdg_desc from ") + .append(" ep_widget_catalog w ") + .append(" where w.all_user_flag = 'Y' ") + .append(" ") + .append(" ) userWidgets ") + .append(" ") + .append(" left join ") + .append(" ") + .append(" ( ") + .append(" select case when pers.user_id is null then sel.user_id else pers.user_id end as 'user_id', case when sel.widget_id is null then ") + .append(" pers.widget_id else sel.widget_id end as 'widget_id', pers.x, sel.status_cd, pers.y, pers.height, pers.width ") + .append(" from (select * from ep_pers_user_widget_placement where user_id = (select user_id from fn_user where login_id = '" + + loginName + "')) pers ") + .append(" left outer join ") + .append(" (select * from ep_pers_user_widget_sel where user_id = (select user_id from fn_user where login_id = '" + + loginName + "')) sel ") + .append(" on (pers.user_id = sel.user_id and pers.widget_id = sel.widget_id) ") + .append(" ") + .append(" union ") + .append(" ") + .append(" select case when pers.user_id is null then sel.user_id else pers.user_id end as 'user_id', case when sel.widget_id is null ") + .append(" then pers.widget_id else sel.widget_id end as 'widget_id', pers.x, sel.status_cd, pers.y, pers.height, pers.width ") + .append(" from (select * from ep_pers_user_widget_placement where user_id = (select user_id from fn_user where login_id = '" + + loginName + "')) pers ") + .append(" right outer join ") + .append(" (select * from ep_pers_user_widget_sel where user_id = (select user_id from fn_user where login_id = '" + + loginName + "')) sel ") + .append(" on (pers.user_id = sel.user_id and pers.widget_id = sel.widget_id) ") + .append(" ") + .append(" order by user_id, widget_id ") + .append(" )b ") + .append(" on ") + .append(" (userWidgets.widget_id = b.widget_id) order by b.x; "); + + return sql; + + } + +} diff --git a/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/test/controller/WidgetsCatalogControllerTest.java b/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/test/controller/WidgetsCatalogControllerTest.java index cf3d6ce5..d3dc1cbf 100644 --- a/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/test/controller/WidgetsCatalogControllerTest.java +++ b/ecomp-portal-widget-ms/widget-ms/src/test/java/org/onap/portalapp/widget/test/controller/WidgetsCatalogControllerTest.java @@ -1,3 +1,40 @@ +/*- + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software 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. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * 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. + * + * ============LICENSE_END============================================ + * + * + */ package org.onap.portalapp.widget.test.controller; @@ -104,6 +141,52 @@ public class WidgetsCatalogControllerTest { } + @Test + public void getUserWidgetCatalog_ValidAuthorization_NoError() throws Exception { + List list = new ArrayList(); + WidgetCatalog widget = new WidgetCatalog(); + widget.setId(1); + widget.setName("junit"); + list.add(widget); + Mockito.when(widgetService.getUserWidgetCatalog("test")).thenReturn(list); + + String security_user = "user"; + String security_pass = "password"; + + ReflectionTestUtils.setField(controller, "security_user", security_user, String.class); + ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class); + + String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes())); + mockMvc.perform(get("/microservices/widgetCatalog/test").header("Authorization", basic_auth)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[0].id", is(1))) + .andExpect(jsonPath("$[0].name", is("junit"))); + } + + @Test + public void getUserWidgetCatalog_Authorization_Error() throws Exception { + List list = new ArrayList(); + WidgetCatalog widget = new WidgetCatalog(); + widget.setId(1); + widget.setName("junit"); + list.add(widget); + Mockito.when(widgetService.getUserWidgetCatalog("test")).thenReturn(list); + + String security_user = "user"; + String security_pass = "password"; + String wrong_pass = "wrong"; + + ReflectionTestUtils.setField(controller, "security_user", security_user, String.class); + ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class); + + String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes())); + mockMvc.perform(get("/microservices/widgetCatalog/test").header("Authorization", basic_auth)) + .andExpect(status().isUnauthorized()); + + } + + + @Test public void saveWidgetCatalog_ValidAuthorization_NoError() throws Exception { ValidationRespond respond = new ValidationRespond(true, null); @@ -125,6 +208,29 @@ public class WidgetsCatalogControllerTest { Mockito.verify(widgetService, times(1)).saveWidgetCatalog(any(WidgetCatalog.class)); } + @Test + public void saveWidgetCatalog_Authorization_Error() throws Exception { + ValidationRespond respond = new ValidationRespond(true, null); + Mockito.when(storageService.checkZipFile(any(MultipartFile.class))).thenReturn(respond); + + String security_user = "user"; + String security_pass = "password"; + String wrong_pass = "wrong"; + + ReflectionTestUtils.setField(controller, "security_user", security_user, String.class); + ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class); + + String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes())); + mockMvc.perform(MockMvcRequestBuilders.fileUpload("/microservices/widgetCatalog/").file("file", null) + .param("widget", "{}") + .header("Authorization", basic_auth) + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andExpect(status().isUnauthorized()); + + //Mockito.verify(widgetService, times(1)).saveWidgetCatalog(any(WidgetCatalog.class)); + } + + @Test public void updateWidgetCatalog_ValidAuthorization_NoError() throws Exception { @@ -143,6 +249,23 @@ public class WidgetsCatalogControllerTest { assertEquals(widgetServiceArg.getValue(), widgetId); } + @Test + public void updateWidgetCatalog_Authorization_Error() throws Exception { + String security_user = "user"; + String security_pass = "password"; + String wrong_pass = "wrong"; + Long widgetId = new Long(1); + ArgumentCaptor widgetServiceArg = ArgumentCaptor.forClass(Long.class); + + ReflectionTestUtils.setField(controller, "security_user", security_user, String.class); + ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class); + + String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes())); + mockMvc.perform(put("/microservices/widgetCatalog/" + widgetId).contentType(MediaType.APPLICATION_JSON).content("{}").header("Authorization", basic_auth)).andExpect(status().isUnauthorized()); + + + } + @Test public void updateWidgetCatalogwithFiles_ValidAuthorization_NoError() throws Exception { @@ -169,6 +292,28 @@ public class WidgetsCatalogControllerTest { assertEquals(widgetServiceArg.getValue(), widgetId); } + + @Test + public void updateWidgetCatalogwithFiles_Authorization_error()throws Exception { + + String security_user = "user"; + String security_pass = "password"; + String wrong_pass = "wrong"; + Long widgetId = new Long(1); + + ReflectionTestUtils.setField(controller, "security_user", security_user, String.class); + ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class); + + String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes())); + mockMvc.perform(MockMvcRequestBuilders.fileUpload("/microservices/widgetCatalog/" + widgetId).file("file", null) + .param("widget", "{}") + .header("Authorization", basic_auth) + .contentType(MediaType.MULTIPART_FORM_DATA) + ).andExpect(status().isUnauthorized()); +//("/microservices/widgetCatalog/" + widgetId).contentType(MediaType.APPLICATION_JSON).content("{}").header("Authorization", basic_auth)).andExpect(status().isUnauthorized()); + } + + @Test public void deleteOnboardingWidget_ValidAuthorization_NoError() throws Exception { @@ -191,5 +336,144 @@ public class WidgetsCatalogControllerTest { assertEquals(storageServiceArg.getValue(), widgetId); } + @Test + public void deleteOnboardingWidget_Authorization_Error() throws Exception { + + String security_user = "user"; + String security_pass = "password"; + String wrong_pass = "wrong"; + Long widgetId = new Long(1); + + ReflectionTestUtils.setField(controller, "security_user", security_user, String.class); + ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class); + + String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes())); + mockMvc.perform(MockMvcRequestBuilders.delete("/microservices/widgetCatalog/" + widgetId) + .header("Authorization", basic_auth) + ).andExpect(status().isUnauthorized()); + } + + @Test + public void getServiceIdByWidget_Authorization_error()throws Exception { + + String security_user = "user"; + String security_pass = "password"; + String wrong_pass = "wrong"; + Long widgetId = new Long(1); + + ReflectionTestUtils.setField(controller, "security_user", security_user, String.class); + ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class); + + String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes())); + mockMvc.perform(MockMvcRequestBuilders.get("/microservices/widgetCatalog/parameters/" + widgetId) + .header("Authorization", basic_auth) + ).andExpect(status().isUnauthorized()); + + } + + @Test + public void getServiceIdByWidget_ValidAuthorization_NoError()throws Exception { + + String security_user = "user"; + String security_pass = "password"; + Long widgetId = new Long(1); + + ReflectionTestUtils.setField(controller, "security_user", security_user, String.class); + ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class); + + String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes())); + mockMvc.perform(MockMvcRequestBuilders.get("/microservices/widgetCatalog/parameters/" + widgetId) + .header("Authorization", basic_auth)); + ArgumentCaptor widgetServiceArg = ArgumentCaptor.forClass(Long.class); + ArgumentCaptor storageServiceArg = ArgumentCaptor.forClass(Long.class); + + Mockito.verify(widgetService, times(1)).getServiceIdByWidget(widgetServiceArg.capture()); + assertEquals(widgetServiceArg.getValue(), widgetId); + } + + + @Test + public void getWidgetByServiceIdt_Authorization_error()throws Exception { + + String security_user = "user"; + String security_pass = "password"; + String wrong_pass = "wrong"; + Long serviceId = new Long(1); + + ReflectionTestUtils.setField(controller, "security_user", security_user, String.class); + ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class); + + String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes())); + mockMvc.perform(MockMvcRequestBuilders.get("/microservices/widgetCatalog/service/" + serviceId) + .header("Authorization", basic_auth) + ).andExpect(status().isUnauthorized()); + + } + + @Test + public void getWidgetByServiceIdt_ValidAuthorization_Noerror()throws Exception { + + Long serviceId = new Long(1); + + List list = new ArrayList(); + WidgetCatalog widget = new WidgetCatalog(); + widget.setId(1); + widget.setName("junit"); + list.add(widget); + Mockito.when(widgetService.getWidgetsByServiceId(serviceId)).thenReturn(list); + + String security_user = "user"; + String security_pass = "password"; + + ReflectionTestUtils.setField(controller, "security_user", security_user, String.class); + ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class); + + String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes())); + mockMvc.perform(get("/microservices/widgetCatalog/service/"+serviceId).header("Authorization", basic_auth)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[0].id", is(1))) + .andExpect(jsonPath("$[0].name", is("junit"))); + + } + + @Test + public void getWidgetZipFile_Authorization_error()throws Exception { + + String security_user = "user"; + String security_pass = "password"; + String wrong_pass = "wrong"; + Long widgetId = new Long(1); + ReflectionTestUtils.setField(controller, "security_user", security_user, String.class); + ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class); + + String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + wrong_pass).getBytes())); + mockMvc.perform(MockMvcRequestBuilders.get("/microservices/download/" + widgetId) + .header("Authorization", basic_auth) + ).andExpect(status().isUnauthorized()); + + } + + + @Test + public void getWidgetZipFile_ValidAuthorization_Noerror()throws Exception { + + String security_user = "user"; + String security_pass = "password"; + String wrong_pass = "wrong"; + Long widgetId = new Long(1); + byte[] bytes="Test".getBytes(); + Mockito.when(storageService.getWidgetCatalogContent(widgetId)).thenReturn(bytes); + + ReflectionTestUtils.setField(controller, "security_user", security_user, String.class); + ReflectionTestUtils.setField(controller, "security_pass", security_pass, String.class); + + String basic_auth = "Basic " + new String(Base64.encodeBase64((security_user + ":" + security_pass).getBytes())); + mockMvc.perform(MockMvcRequestBuilders.get("/microservices/download/" + widgetId) + .header("Authorization", basic_auth) + ).andExpect(status().isOk()); + + } + + } -- cgit 1.2.3-korg