Skip to content

Commit

Permalink
Tests for 60% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Finevman committed Oct 5, 2023
1 parent 5278cf4 commit dd35d74
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 15 deletions.
6 changes: 1 addition & 5 deletions frontend/generated/vaadin-featureflags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@ window.Vaadin = window.Vaadin || {};
window.Vaadin.featureFlags = window.Vaadin.featureFlags || {};
window.Vaadin.featureFlags.exampleFeatureFlag = false;
window.Vaadin.featureFlags.viteForFrontendBuild = false;
window.Vaadin.featureFlags.mapComponent = false;
window.Vaadin.featureFlags.spreadsheetComponent = false;
window.Vaadin.featureFlags.hillaPush = false;
window.Vaadin.featureFlags.newLicenseChecker = false;
window.Vaadin.featureFlags.collaborationEngineBackend = false;
window.Vaadin.featureFlags.mapComponent = false;
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@
"@vaadin/vaadin-virtual-list": "23.1.0",
"@vaadin/vertical-layout": "23.1.0",
"@vaadin/virtual-list": "23.1.0",
"construct-style-sheets-polyfill": "3.1.0",
"construct-style-sheets-polyfill": "3.0.4",
"date-fns": "2.28.0",
"line-awesome": "1.3.0",
"lit": "2.2.3"
"lit": "2.1.4"
},
"devDependencies": {
"async": "3.2.2",
Expand Down Expand Up @@ -379,10 +379,10 @@
"@vaadin/vaadin-virtual-list": "23.1.0",
"@vaadin/vertical-layout": "23.1.0",
"@vaadin/virtual-list": "23.1.0",
"construct-style-sheets-polyfill": "3.1.0",
"construct-style-sheets-polyfill": "3.0.4",
"date-fns": "2.28.0",
"line-awesome": "1.3.0",
"lit": "2.2.3"
"lit": "2.1.4"
},
"devDependencies": {
"async": "3.2.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,19 @@ public AppsView(DataBaseService dataBaseService, AuthenticatedUser authenticated
initilizeInfrastuctureEvaluation();
initializeSearchText();
initializeGrid();

VerticalLayout appLayout = new VerticalLayout();

try{
VerticalLayout appLayout = new VerticalLayout();
List<IoTApp> apps = getJsonAppsFromUrl();
for (IoTApp app : apps) {
appLayout.add(createApp(app));
}

add(summaryEvaluation, searchText, appLayout, grid);
}
}
catch(Exception e){
add(summaryEvaluation, searchText, grid);
System.out.println("Exception: "+e);
}

add(summaryEvaluation, searchText, appLayout, grid);
}

private void initilizeInfrastuctureEvaluation(){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package com.privacydashboard.application.views.apps;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.privacydashboard.application.data.GlobalVariables.QuestionnaireVote;
import com.privacydashboard.application.data.GlobalVariables.Role;
import com.privacydashboard.application.data.entity.IoTApp;
import com.privacydashboard.application.data.entity.User;
import com.privacydashboard.application.data.service.CommunicationService;
import com.privacydashboard.application.data.service.DataBaseService;
import com.privacydashboard.application.security.AuthenticatedUser;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;

public class AvailableAppsViewTest {

private CommunicationService communicationService;
private AuthenticatedUser authenticatedUser;
private DataBaseService dataBaseService;

private AvailableAppsView view;

private static User getAuthUser(){
User user = new User();

user.setName("AuthUser");
user.setId(new UUID(0, 0));
user.setRole(Role.SUBJECT);
user.setMail("auth@mail.test");
user.setHashedPassword("AuthPassword");
user.setProfilePictureUrl("https://auth.html");

return user;
}

private static List<User> getUserRole(Role role){
User user = new User();

user.setName("User"+role.toString());
user.setId(new UUID(0, 0));
user.setRole(role);
user.setMail(role.toString()+"@mail.test");
user.setHashedPassword(role.toString()+"Password");
user.setProfilePictureUrl("https://"+role.toString()+".html");

List<User> list = new ArrayList<>();
list.add(user);

return list;
}

private static IoTApp createOrangeApp(){
String[] consenses = {"Consensus 1", "Consensus 2"};

IoTApp app1 = new IoTApp();
app1.setName("App1");
app1.setQuestionnaireVote(QuestionnaireVote.GREEN);
app1.setDescription("Description1");
app1.setConsenses(consenses);

return app1;
}

@BeforeEach
public void setup(){
authenticatedUser = mock(AuthenticatedUser.class);
dataBaseService = mock(DataBaseService.class);
communicationService = mock(CommunicationService.class);

view = new AvailableAppsView(dataBaseService, authenticatedUser, communicationService);
}

@Test
public void availableAppsConstructorTest(){
assertEquals(view.getElement().getTag(), "div");
assertEquals(view.getElement().getAttribute("class"), "grid-view");

assertEquals(view.getElement().getChild(0).getTag(), "vaadin-vertical-layout");
}

private static Method getCreateAppMethod(){
try{
Method method = AvailableAppsView.class.getDeclaredMethod("createApp", IoTApp.class);
method.setAccessible(true);
return method;
}
catch(NoSuchMethodException e){
System.out.println("Exception: "+e);
}
return null;
}

@Test
public void createAppTest(){
Method createApp = getCreateAppMethod();
VerticalLayout app = new VerticalLayout();

when(authenticatedUser.getUser()).thenReturn(getAuthUser());

when(dataBaseService.getSubjectsFromApp(any())).thenReturn(getUserRole(Role.SUBJECT));
when(dataBaseService.getControllersFromApp(any())).thenReturn(getUserRole(Role.CONTROLLER));
when(dataBaseService.getDPOsFromApp(any())).thenReturn(getUserRole(Role.DPO));

try{
app = (VerticalLayout) createApp.invoke(view, (Object) createOrangeApp());
}
catch(Exception e){
System.out.println("Exception: "+e);
}

assertEquals(app.getElement().getTag(), "vaadin-vertical-layout");
assertEquals(app.getElement().getAttribute("class"), "card canOpen");

assertEquals(app.getElement().getChild(0).getTag(), "vaadin-horizontal-layout");

assertEquals(app.getElement().getChild(0).getChild(0).getTag(), "vaadin-avatar");
assertEquals(app.getElement().getChild(0).getChild(1).getTag(), "span");
assertEquals(app.getElement().getChild(0).getChild(1).getAttribute("class"), "name");
assertEquals(app.getElement().getChild(0).getChild(1).getText(), "App1");

assertEquals(app.getElement().getChild(1).getTag(), "vaadin-details");

assertEquals(app.getElement().getChild(1).getChild(0).getTag(), "div");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getTag(), "vaadin-vertical-layout");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(0).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(0).getChild(0).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(0).getChild(0).getAttribute("class"), "bold");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(0).getChild(0).getText(), "Description: ");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(0).getChild(1).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(0).getChild(1).getText(), "Description1");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(1).getTag(), "div");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(1).getChild(0).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(1).getChild(0).getAttribute("class"), "bold");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(1).getChild(0).getText(), "Evaluation: ");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(1).getChild(1).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(1).getChild(1).getAttribute("class"), "greenName");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(1).getChild(1).getText(), "GREEN ");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(1).getChild(2).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(1).getChild(2).getAttribute("class"), "las la-info-circle pointer");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(2).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(2).getText(), "Privacy Notice");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(2).getAttribute("class"), "link");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(3).getTag(), "vaadin-details");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(3).getChild(0).getTag(), "div");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(3).getChild(0).getChild(0).getTag(), "vaadin-vertical-layout");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(3).getChild(1).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(3).getChild(1).getText(), "Data Controllers: ");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(4).getTag(), "vaadin-details");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(4).getChild(0).getTag(), "div");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(4).getChild(0).getChild(0).getTag(), "vaadin-vertical-layout");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(4).getChild(1).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(4).getChild(1).getText(), "Data Protection Officer: ");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getTag(), "vaadin-details");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(0).getTag(), "div");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(0).getChild(0).getTag(), "vaadin-vertical-layout");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(0).getChild(0).getChild(0).getTag(), "vaadin-horizontal-layout");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(0).getChild(0).getChild(0).getChild(0).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(0).getChild(0).getChild(0).getChild(0).getText(), "Consensus 1");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(0).getChild(0).getChild(0).getChild(1).getTag(), "vaadin-button");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(0).getChild(0).getChild(0).getChild(1).getText(), "Accept consent");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(0).getChild(0).getChild(1).getTag(), "vaadin-horizontal-layout");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(0).getChild(0).getChild(1).getChild(0).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(0).getChild(0).getChild(1).getChild(0).getText(), "Consensus 2");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(0).getChild(0).getChild(1).getChild(1).getTag(), "vaadin-button");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(0).getChild(0).getChild(1).getChild(1).getText(), "Accept consent");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(1).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(5).getChild(1).getText(), "Consenses: ");

assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(6).getTag(), "vaadin-button");
assertEquals(app.getElement().getChild(1).getChild(0).getChild(0).getChild(6).getText(), "Remove everything");

assertEquals(app.getElement().getChild(1).getChild(1).getTag(), "span");
assertEquals(app.getElement().getChild(1).getChild(1).getText(), "More");
}


}

0 comments on commit dd35d74

Please sign in to comment.