blob: 9c998cc6fdb36c5ba68f411a29507b09d5809d41 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task;
import org.glassfish.jersey.internal.util.Producer;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class WriteFileTaskTest extends TaskTestBase {
private final String content = "line1\nline2\n";
@Test
public void testWrite() {
Path parentDirectory = Paths.get("/foo");
Path path = parentDirectory.resolve("bar");
@SuppressWarnings("unchecked")
Producer<String> contentProducer = (Producer<String>) mock(Producer.class);
when(contentProducer.call()).thenReturn(content);
final String permissions = "rwxr-x---";
final String owner = "owner";
final String group = "group";
WriteFileTask task = new WriteFileTask(path, contentProducer)
.withPermissions(permissions)
.withOwner(owner)
.withGroup(group);
when(fileSystemMock.isRegularFile(path)).thenReturn(false);
when(contextMock.executeSubtask(any(MakeDirectoryTask.class))).thenReturn(false);
assertTrue(task.execute(contextMock));
verify(contentProducer, times(1)).call();
verify(fileSystemMock).writeUtf8File(path, content);
verify(fileSystemMock).setPermissions(path, permissions);
verify(fileSystemMock).setOwner(path, owner);
verify(fileSystemMock).setGroup(path, group);
// Writing a file with the expected content
ArgumentCaptor<MakeDirectoryTask> makeDirectoryTaskCaptor =
ArgumentCaptor.forClass(MakeDirectoryTask.class);
verify(contextMock, times(1))
.executeSubtask(makeDirectoryTaskCaptor.capture());
MakeDirectoryTask makeDirectoryTask = makeDirectoryTaskCaptor.getValue();
assertEquals(parentDirectory, makeDirectoryTask.getPath());
assertTrue(makeDirectoryTask.getWithParents());
}
@Test
public void fileAlreadyExists() {
Path path = Paths.get("foo");
final String permissions = "rwxr-x---";
final String owner = "owner";
final String group = "group";
WriteFileTask task = new WriteFileTask(path, () -> content)
.withPermissions(permissions)
.withOwner(owner)
.withGroup(group);
when(fileSystemMock.isRegularFile(path)).thenReturn(true);
assertFalse(task.execute(contextMock));
}
}
|