Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored dashboard ExportSearchResultsPage #2611

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import static java.util.Optional.ofNullable;

import java.util.Collection;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


/**
* Base repository (data access) class for JPA entities.
* @param <ID> type of entity identifier.
Expand Down Expand Up @@ -70,6 +72,10 @@ public T getById(ID id) {
public T save(T entity) {
return save(entity, false, false);
}

public void saveAll(final Iterable<T> entities) {
entities.forEach(this::save);
}

public T saveAndFlush(T entity) {
return save(entity, true, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public String getLocaleTitle() {
public int getFilesPerPage() {
return filesPerPage;
}

public boolean canEditDashboard() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be moved to the LOGIC section below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return !this.systemConfig.isReadonlyMode() && getUser().isSuperuser();
}

// -------------------- LOGIC --------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,123 +1,62 @@
package edu.harvard.iq.dataverse.dashboard;

import static java.lang.Boolean.FALSE;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang.StringUtils.EMPTY;

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.omnifaces.cdi.ViewScoped;

import edu.harvard.iq.dataverse.DataverseDao;
import edu.harvard.iq.dataverse.DataverseSession;
import edu.harvard.iq.dataverse.PermissionsWrapper;
import edu.harvard.iq.dataverse.NavigationWrapper;
import edu.harvard.iq.dataverse.persistence.dataset.DatasetFieldType;
import edu.harvard.iq.dataverse.persistence.dataset.DatasetFieldTypeRepository;
import edu.harvard.iq.dataverse.util.SystemConfig;

@ViewScoped
@SuppressWarnings("serial")
@RequestScoped
@Named("ExportSearchResultsPage")
public class DashboardExportSearchResultsPage implements Serializable {

private final DataverseSession session;
private final PermissionsWrapper permissionsWrapper;
private final NavigationWrapper navigation;
private final DataverseDao dataverseDao;
private final SystemConfig systemConfig;
private final DatasetFieldTypeRepository datasetFiledTypeRepo;

private List<Metadata> metadataTypes;
private List<DatasetFieldType> fieldTypes;

@Inject
public DashboardExportSearchResultsPage(final DataverseSession session,
final PermissionsWrapper permissionsWrapper,
final DataverseDao dataverseDao, final SystemConfig systemConfig,
final NavigationWrapper navigation,
final DataverseDao dataverseDao,
final DatasetFieldTypeRepository datasetFiledTypeRepo) {
this.session = session;
this.permissionsWrapper = permissionsWrapper;
this.navigation = navigation;
this.dataverseDao = dataverseDao;
this.systemConfig = systemConfig;
this.datasetFiledTypeRepo = datasetFiledTypeRepo;

this.fieldTypes = this.datasetFiledTypeRepo.findAll();
this.fieldTypes.sort(comparing(DatasetFieldType::getTitle));
}

public List<Metadata> getMetadataTypes() {
return this.metadataTypes;
}

public String init() {
if (canEdit()) {
initMetadataTypes();
return EMPTY;
} else {
return this.permissionsWrapper.notAuthorized();
}
}

private void initMetadataTypes() {
this.metadataTypes = this.datasetFiledTypeRepo.findAll().stream()
.map(Metadata::new).sorted(comparing(Metadata::getTitle))
.collect(toList());
public List<DatasetFieldType> getFieldTypes() {
return this.fieldTypes;
}

private boolean canEdit() {
return !this.systemConfig.isReadonlyMode()
&& this.session.getUser().isSuperuser();
public String verifyAccess() {
return this.session.canEditDashboard() ? EMPTY : this.navigation.notAuthorized();
}

public String save() {
for (final DatasetFieldType fieldType : this.datasetFiledTypeRepo.findAll()) {
fieldType.setExportToFile(isExportedToFile(fieldType.getId()));
this.datasetFiledTypeRepo.save(fieldType);
}
this.datasetFiledTypeRepo.saveAll(this.fieldTypes);
return EMPTY;
}

private boolean isExportedToFile(final Long id) {
return this.metadataTypes.stream().filter(mt -> mt.getId().equals(id))
.findFirst().map(Metadata::isExportable).orElse(FALSE);
}

public String cancel() {
return "/dashboard.xhtml?faces-redirect=true&dataverseId="
+ this.dataverseDao.findRootDataverse().getId();
}

public static class Metadata {

private final Long id;
private final String title;
private final String description;
private boolean exportable;

private Metadata(final DatasetFieldType fieldType) {
this.id = fieldType.getId();
this.title = fieldType.getTitle();
this.description = fieldType.getDescription();
this.exportable = fieldType.isExportToFile();
}

public boolean isExportable() {
return this.exportable;
}

public void setExportable(final boolean exportable) {
this.exportable = exportable;
}

public Long getId() {
return this.id;
}

public String getTitle() {
return this.title;
}

public String getDescription() {
return this.description;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<f:viewParam name="dataverseId"
value="#{dataverseDao.findRootDataverse().id}" />
<f:viewAction action="#{dataverseSession.updateLocaleInViewRoot}" />
<f:viewAction action="#{ExportSearchResultsPage.init()}" />
<f:viewAction action="#{ExportSearchResultsPage.verifyAccess()}" />
<f:viewAction
action="#{dataverseHeaderFragment.initBreadcrumbs(dataverseDao.findRootDataverse())}" />
<f:viewAction
Expand All @@ -40,7 +40,7 @@
<div class="collapse in">
<div class="panel-body">
<div class="form-group">
<p:dataTable value="#{ExportSearchResultsPage.metadataTypes}" var="mt" >
<p:dataTable value="#{ExportSearchResultsPage.fieldTypes}" var="mt" >
<p:column width="20%"
headerText="#{bundle['dashboard.card.exportsearchresults.name']}">
<h:outputText value="#{mt.title}"/>
Expand All @@ -51,7 +51,7 @@
</p:column>
<p:column width="10%"
headerText="#{bundle['dashboard.card.exportsearchresults.exported']}">
<p:selectBooleanCheckbox value="#{mt.exportable}"/>
<p:selectBooleanCheckbox value="#{mt.exportToFile}"/>
</p:column>
</p:dataTable>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
import static org.mockito.Mockito.when;
import static org.mockito.quality.Strictness.LENIENT;

import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;

import edu.harvard.iq.dataverse.DataverseDao;
import edu.harvard.iq.dataverse.DataverseSession;
import edu.harvard.iq.dataverse.PermissionsWrapper;
import edu.harvard.iq.dataverse.persistence.dataset.DatasetFieldType;
import edu.harvard.iq.dataverse.persistence.dataset.DatasetFieldTypeRepository;
import edu.harvard.iq.dataverse.persistence.user.AuthenticatedUser;
Expand All @@ -25,24 +25,20 @@
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = LENIENT)
public class DashboardExportSearchResultsPageTest {

@InjectMocks
private DashboardExportSearchResultsPage page;

@Mock
private DataverseSession session;
@Mock
private SystemConfig systemConfig;
@Mock
private DatasetFieldTypeRepository datasetFiledTypeRepo;
@Mock
private PermissionsWrapper permissionsWrapper;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need a NavigationWrapper mock.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont need it since I don't test DashboardExportSearchResultsPage.verifyAccess() method, and I don't intend to for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you're purposefully breaking the constructor interface by providing null and you're still creating a SystemConfig mock even though it doesn't seem necessary. I mean, it's not very consistent. Tests are in a way much like production code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@Mock
private DataverseDao dataverseDao;


private DashboardExportSearchResultsPage page;

private DatasetFieldType type1 = new DatasetFieldType();
private DatasetFieldType type2 = new DatasetFieldType();
private List<DatasetFieldType> types = asList(this.type1, this.type2);

@BeforeEach
public void setUp() {
Expand All @@ -55,28 +51,29 @@ public void setUp() {
this.type2.setId(2L);
this.type2.setTitle("abc");

when(this.datasetFiledTypeRepo.findAll())
.thenReturn(asList(this.type1, this.type2));
when(this.datasetFiledTypeRepo.findAll()).thenReturn(types);

this.page = new DashboardExportSearchResultsPage(this.session, null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove @InjectMocks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the constructor of DashboardExportSearchResultsPage invokes datasetFiledTypeRepo.findAll().

i need to call when(this.datasetFiledTypeRepo.findAll()).thenReturn(types); BEFORE instantiating DashboardExportSearchResultsPage
otherwise datasetFiledTypeRepo.findAll() returns empty list

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the problem stems from using logic inside the constructor, which isn't always the best choice (specifically in a dependency injection context). For instance, you could have extracted the logic to a @PostCostruct annotated method and call that method here to make the intend (logic usage) more obvious.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

this.dataverseDao, this.datasetFiledTypeRepo);
}

@Test
public void selectionAndSavingWorks() throws Exception {
this.page.init();

assertThat(this.page.getMetadataTypes().size()).isEqualTo(2);
assertThat(this.page.getFieldTypes().size()).isEqualTo(2);

assertThat(this.page.getMetadataTypes().get(0).getId()).isEqualTo(2L);
assertThat(this.page.getMetadataTypes().get(0).getTitle()).isEqualTo("abc");
assertThat(this.page.getMetadataTypes().get(0).isExportable()).isFalse();
assertThat(this.page.getFieldTypes().get(0).getId()).isEqualTo(2L);
assertThat(this.page.getFieldTypes().get(0).getTitle()).isEqualTo("abc");
assertThat(this.page.getFieldTypes().get(0).isExportToFile()).isFalse();

assertThat(this.page.getMetadataTypes().get(1).getId()).isEqualTo(1L);
assertThat(this.page.getMetadataTypes().get(1).getTitle()).isEqualTo("def");
assertThat(this.page.getMetadataTypes().get(1).isExportable()).isFalse();
assertThat(this.page.getFieldTypes().get(1).getId()).isEqualTo(1L);
assertThat(this.page.getFieldTypes().get(1).getTitle()).isEqualTo("def");
assertThat(this.page.getFieldTypes().get(1).isExportToFile()).isFalse();

this.page.getMetadataTypes().get(0).setExportable(true);
this.page.getFieldTypes().get(0).setExportToFile(true);
this.page.save();

verify(this.datasetFiledTypeRepo).save(this.type2);
verify(this.datasetFiledTypeRepo).saveAll(this.types);
assertThat(this.type1.isExportToFile()).isFalse();
assertThat(this.type2.isExportToFile()).isTrue();
}
Expand Down
Loading