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

Multiple file upload audit #51

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -12,4 +12,5 @@ public interface ResourceRepository extends JpaRepository<Resource, Long> {
Collection<Resource> findAllByProjectAndIsDeletedIsFalse(Project project);
Collection<Resource> findAllByProject_IdAndIsDeletedIsFalse(long projectId);
Collection<Resource> findByIsDeletedIsFalse();
Collection<Resource> findAllByIdIn(Collection<Long> ids);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.tanaguru.domain.constant.EAuditType;
import com.tanaguru.domain.constant.EParameterFamily;
import com.tanaguru.domain.entity.audit.Audit;
import com.tanaguru.domain.entity.audit.Resource;
import com.tanaguru.domain.entity.audit.parameter.AuditAuditParameterValue;
import com.tanaguru.domain.entity.audit.parameter.AuditParameter;
import com.tanaguru.domain.entity.audit.parameter.AuditParameterFamily;
Expand All @@ -33,6 +34,7 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.tanaguru.domain.constant.EAuditType.*;
import static com.tanaguru.domain.constant.ParameterValueConstants.*;
Expand Down Expand Up @@ -215,8 +217,13 @@ public boolean checkParameterValueIsValid(EAuditParameter parameter, String valu
break;

case DOM_ID:
long resourceId = Long.parseLong(value);
result = resourceRepository.existsById(resourceId);
String[] resourceIdsStr = value.split(";");
Collection<Resource> resources = resourceRepository.findAllByIdIn(
Arrays.stream(resourceIdsStr)
.map(Long::parseLong)
.collect(Collectors.toList())
);
result = resourceIdsStr.length == resources.size();
break;

case SCENARIO_ID:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.tanaguru.domain.constant.EAuditParameter;
import com.tanaguru.domain.constant.EAuditType;
import com.tanaguru.domain.constant.EParameterFamily;
import com.tanaguru.domain.entity.audit.Resource;
import com.tanaguru.domain.entity.audit.Scenario;
import com.tanaguru.domain.entity.audit.parameter.AuditParameter;
import com.tanaguru.domain.entity.audit.parameter.AuditParameterFamily;
Expand All @@ -18,10 +19,7 @@
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;

import static com.tanaguru.domain.constant.ParameterValueConstants.*;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -148,20 +146,27 @@ public void checkParameterValueIsValid_waitTimeValid() {

@Test
public void checkParameterValueIsValid_DOMIDValid() {
Mockito.when(resourceRepository.existsById(0L))
.thenReturn(true);
Mockito.when(resourceRepository.findAllByIdIn(Mockito.anyList()))
.thenReturn(Arrays.asList(new Resource()));
assertTrue(auditParameterServiceImpl.checkParameterValueIsValid(EAuditParameter.DOM_ID, "0", null));
}

@Test
public void checkParameterValueIsValid_DOMIDValidMultiple() {
Mockito.when(resourceRepository.findAllByIdIn(Mockito.anyList()))
.thenReturn(Arrays.asList(new Resource(), new Resource()));
assertTrue(auditParameterServiceImpl.checkParameterValueIsValid(EAuditParameter.DOM_ID, "0;1", null));
}

@Test
public void checkParameterValueIsValid_DOMIDInvalidFormat() {
assertFalse(auditParameterServiceImpl.checkParameterValueIsValid(EAuditParameter.DOM_ID, "test", null));
}

@Test
public void checkParameterValueIsValid_DOMIDNotExists() {
Mockito.when(resourceRepository.existsById(0L))
.thenReturn(false);
Mockito.when(resourceRepository.findAllByIdIn(Mockito.anyList()))
.thenReturn(Collections.emptyList());
assertFalse(auditParameterServiceImpl.checkParameterValueIsValid(EAuditParameter.DOM_ID, "0", null));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

public class AuditRunnerFile extends AbstractAuditRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(AuditRunnerFile.class);
private String fileContent;
private Collection<String> fileContents;

public AuditRunnerFile(
Collection<TanaguruTest> tanaguruTests,
Audit audit,
String fileContent,
Collection<String> fileContents,
RemoteWebDriver driver,
String coreScript,
long waitTime,
Expand All @@ -41,31 +41,33 @@ public AuditRunnerFile(
basicAuthLogin,
basicAuthPassword,
enableScreenShot);
this.fileContent = fileContent;
this.fileContents = fileContents;
}

@Override
protected void runImpl() {
File tempFile = null;
try {
tempFile = File.createTempFile("webresource-" + getAudit().getId(), "html");
} catch (IOException e) {
LOGGER.error("[Audit {}] Error while creating temporary file for the runner", getAudit().getId());
}

if (tempFile != null && tempFile.exists()) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile))) {
bw.write(fileContent);
for(String fileContent : fileContents) {
File tempFile = null;
try {
tempFile = File.createTempFile("webresource-" + getAudit().getId(), "html");
} catch (IOException e) {
LOGGER.error("[Audit {}] Error while initializing temporary file for the runner", getAudit().getId());
LOGGER.error("[Audit {}] Error while creating temporary file for the runner", getAudit().getId());
}

webDriverGet("file://" + tempFile.getAbsolutePath());
if (tempFile != null && tempFile.exists()) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile))) {
bw.write(fileContent);
} catch (IOException e) {
LOGGER.error("[Audit {}] Error while initializing temporary file for the runner", getAudit().getId());
}

try {
Files.delete(tempFile.toPath());
} catch (IOException e) {
LOGGER.error("[Audit {}] Error while deleting temporary file for the runner", getAudit().getId());
webDriverGet("file://" + tempFile.getAbsolutePath());

try {
Files.delete(tempFile.toPath());
} catch (IOException e) {
LOGGER.error("[Audit {}] Error while deleting temporary file for the runner", getAudit().getId());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ public interface AuditRunnerFactory {
* @param enableScreeShot True to enable webdriver to take screenshot
* @return An @see AuditRunner
*/
Optional<AuditRunner> createFileRunner(Collection<TanaguruTest> references, Audit audit, String content, long waitTime, Collection<Integer> resolutions, String basicAuthUrl, String basicAuthLogin, String basicAuthPassword, boolean enableScreeShot, BrowserName browserName);
Optional<AuditRunner> createFileRunner(Collection<TanaguruTest> references, Audit audit, Collection<String> fileContents, long waitTime, Collection<Integer> resolutions, String basicAuthUrl, String basicAuthLogin, String basicAuthPassword, boolean enableScreeShot, BrowserName browserName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import javax.transaction.Transactional;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Transactional
Expand Down Expand Up @@ -162,13 +163,21 @@ public Optional<AuditRunner> create(Audit audit) {
browserName);
break;
case UPLOAD:
long resourceId = Long.parseLong(parameterStringMap.get(EAuditParameter.DOM_ID).getValue());
Resource resource = resourceRepository.findById(resourceId)
.orElseThrow(() -> new CustomEntityNotFoundException(CustomError.RESOURCE_NOT_FOUND, resourceId ));
String[] resourceIdsStr = parameterStringMap.get(EAuditParameter.DOM_ID).getValue().split(";");
Collection<Resource> resources = resourceRepository.findAllByIdIn(
Arrays.stream(resourceIdsStr)
.map(Long::parseLong)
.collect(Collectors.toList())
);

Collection<String> fileContents = resources.stream()
.map(Resource::getContent)
.collect(Collectors.toList());

result = createFileRunner(
tanaguruTests,
audit,
resource.getContent(),
fileContents,
waitTime,
resolutions,
basicAuthUrl,
Expand Down Expand Up @@ -305,7 +314,7 @@ public Optional<AuditRunner> createSiteRunner(
public Optional<AuditRunner> createFileRunner(
Collection<TanaguruTest> tanaguruTests,
Audit audit,
String content,
Collection<String> fileContents,
long waitTime,
Collection<Integer> resolutions,
String basicAuthUrl,
Expand All @@ -320,7 +329,7 @@ public Optional<AuditRunner> createFileRunner(
result = Optional.of(new AuditRunnerFile(
tanaguruTests,
audit,
content,
fileContents,
tanaguruDriver.get(),
coreScript,
waitTime,
Expand Down