|
1 | 1 | package de.filefighter.rest.cucumber;
|
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.JsonNode; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
3 | 6 | import de.filefighter.rest.RestApplicationIntegrationTest;
|
4 |
| -import de.filefighter.rest.configuration.RestConfiguration; |
5 | 7 | import de.filefighter.rest.domain.filesystem.data.persistance.FileSystemEntity;
|
6 | 8 | import de.filefighter.rest.domain.filesystem.data.persistance.FileSystemRepository;
|
7 | 9 | import de.filefighter.rest.domain.token.data.persistance.AccessTokenEntity;
|
|
12 | 14 | import io.cucumber.java.en.Given;
|
13 | 15 | import io.cucumber.java.en.Then;
|
14 | 16 | import org.springframework.beans.factory.annotation.Autowired;
|
15 |
| -import org.springframework.http.HttpMethod; |
16 | 17 |
|
| 18 | +import java.io.IOException; |
17 | 19 | import java.time.Instant;
|
18 | 20 | import java.util.Arrays;
|
| 21 | +import java.util.UUID; |
19 | 22 |
|
20 | 23 | import static de.filefighter.rest.domain.token.business.AccessTokenBusinessService.ACCESS_TOKEN_DURATION_IN_SECONDS;
|
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
21 | 26 |
|
22 | 27 | public class CommonCucumberSteps extends RestApplicationIntegrationTest {
|
23 | 28 |
|
24 | 29 | private final UserRepository userRepository;
|
25 | 30 | private final AccessTokenRepository accessTokenRepository;
|
26 | 31 | private final FileSystemRepository fileSystemRepository;
|
| 32 | + private final ObjectMapper objectMapper; |
27 | 33 |
|
28 | 34 | @Autowired
|
29 | 35 | public CommonCucumberSteps(UserRepository userRepository, AccessTokenRepository accessTokenRepository, FileSystemRepository fileSystemRepository) {
|
30 | 36 | this.userRepository = userRepository;
|
31 | 37 | this.accessTokenRepository = accessTokenRepository;
|
32 | 38 | this.fileSystemRepository = fileSystemRepository;
|
| 39 | + this.objectMapper = new ObjectMapper(); |
33 | 40 | }
|
34 | 41 |
|
35 | 42 | @Given("database is empty")
|
@@ -68,53 +75,94 @@ public void userWithIdExistsAndHasUsernamePasswordAndRefreshToken(long userId, S
|
68 | 75 | .build());
|
69 | 76 | }
|
70 | 77 |
|
71 |
| - // file / folder |
| 78 | + // This step almost needs a unit test. |
72 | 79 | @Given("{string} exists with id {long} and path {string}")
|
73 |
| - public void existsWithIdAndPath(String fileOrFolder, long fsItemId, String path) { |
74 |
| - if(fileOrFolder.equals("file")){ |
75 |
| - //TODO: split into folders and files. |
76 |
| - String[] names = path.split("/"); |
77 |
| - System.out.println(Arrays.toString(names)); |
78 |
| - |
79 |
| - fileSystemRepository.save(FileSystemEntity |
80 |
| - .builder() |
81 |
| - .isFile(true) |
82 |
| - .id(fsItemId) |
83 |
| - .create()); |
84 |
| - }else if(fileOrFolder.equals("folder")){ |
85 |
| - fileSystemRepository.save(FileSystemEntity |
86 |
| - .builder() |
87 |
| - .isFile(false) |
88 |
| - .id(fsItemId) |
89 |
| - .path(path) |
90 |
| - .create()); |
91 |
| - }else{ |
92 |
| - throw new IllegalArgumentException("Found not valid string for FileOrFolder in Steps file."); |
| 80 | + public void fileOrFolderExistsWithIdAndPath(String fileOrFolder, long fsItemId, String path) { |
| 81 | + String[] names = path.split("/"); |
| 82 | + StringBuilder completeFilePath = new StringBuilder("/"); |
| 83 | + |
| 84 | + System.out.println(Arrays.toString(names)); |
| 85 | + |
| 86 | + // create root dir. |
| 87 | + fileSystemRepository.save(FileSystemEntity |
| 88 | + .builder() |
| 89 | + .isFile(false) |
| 90 | + .path(completeFilePath.toString()) |
| 91 | + .create()); |
| 92 | + |
| 93 | + |
| 94 | + // create all files and folders. |
| 95 | + for (int i = 0; i < names.length; i++) { |
| 96 | + if (!names[i].isEmpty() && !names[i].isBlank()) { |
| 97 | + boolean isLastOne = i == names.length - 1; |
| 98 | + if(!isLastOne){ |
| 99 | + //is obviously a folder. |
| 100 | + completeFilePath.append(names[i]).append("/"); |
| 101 | + fileSystemRepository.save(FileSystemEntity |
| 102 | + .builder() |
| 103 | + .isFile(false) |
| 104 | + .path(completeFilePath.toString()) |
| 105 | + .create()); |
| 106 | + System.out.println("folder: "+completeFilePath.toString()); |
| 107 | + }else{ |
| 108 | + System.out.println("last one: "+names[i]); |
| 109 | + if (fileOrFolder.equals("file")) { |
| 110 | + fileSystemRepository.save(FileSystemEntity |
| 111 | + .builder() |
| 112 | + .isFile(true) |
| 113 | + .id(fsItemId) |
| 114 | + .create()); |
| 115 | + } else if (fileOrFolder.equals("folder")) { |
| 116 | + completeFilePath.append(names[i]).append("/"); |
| 117 | + fileSystemRepository.save(FileSystemEntity |
| 118 | + .builder() |
| 119 | + .isFile(false) |
| 120 | + .id(fsItemId) |
| 121 | + .path(completeFilePath.toString()) |
| 122 | + .create()); |
| 123 | + } else { |
| 124 | + throw new IllegalArgumentException("Found not valid string for FileOrFolder in Steps file."); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
93 | 128 | }
|
94 | 129 | }
|
95 | 130 |
|
96 | 131 | @And("user {long} is owner of file or folder with id {long}")
|
97 | 132 | public void userIsOwnerOfFileOrFolderWithId(long userId, long fsItemId) {
|
98 | 133 | FileSystemEntity fileSystemEntity = fileSystemRepository.findById(fsItemId);
|
99 |
| - if(null == fileSystemEntity){ |
100 |
| - throw new IllegalArgumentException("FileSystemEntity was null."); |
101 |
| - } |
102 | 134 |
|
103 | 135 | fileSystemEntity.setCreatedByUserId(userId);
|
104 | 136 | fileSystemRepository.save(fileSystemEntity);
|
105 | 137 | }
|
106 | 138 |
|
107 | 139 | //key: value for json type response.
|
108 | 140 | @Then("response contains key {string} and value {string}")
|
109 |
| - public void responseContainsKeyAndValue(String key, String value) { |
| 141 | + public void responseContainsKeyAndValue(String key, String value) throws JsonProcessingException { |
| 142 | + JsonNode rootNode = objectMapper.readTree(latestResponse.getBody()); |
| 143 | + String actualValue = rootNode.get(key).asText(); |
| 144 | + |
| 145 | + assertEquals(value, actualValue); |
110 | 146 | }
|
111 | 147 |
|
112 | 148 | @And("response contains the user with id {long}")
|
113 |
| - public void responseContainsTheUserWithId(long userId) { |
| 149 | + public void responseContainsTheUserWithId(long userId) throws JsonProcessingException { |
| 150 | + JsonNode rootNode = objectMapper.readTree(latestResponse.getBody()); |
| 151 | + long actualValue = rootNode.get("userId").asLong(); |
| 152 | + |
| 153 | + assertEquals(userId, actualValue); |
114 | 154 | }
|
115 | 155 |
|
116 | 156 | @Then("response status code is {int}")
|
117 |
| - public void responseStatusCodeIs(int httpStatusCode) { |
| 157 | + public void responseStatusCodeIs(int httpStatusCode) throws IOException { |
| 158 | + assertEquals(httpStatusCode, latestResponse.getTheResponse().getRawStatusCode()); |
118 | 159 | }
|
119 | 160 |
|
| 161 | + @And("response contains key {string} and value of at least {int}") |
| 162 | + public void responseContainsKeyAndValueOfAtLeast(String key, int value) throws JsonProcessingException { |
| 163 | + JsonNode rootNode = objectMapper.readTree(latestResponse.getBody()); |
| 164 | + int actualValue = rootNode.get(key).asInt(); |
| 165 | + |
| 166 | + assertTrue(actualValue >= value); |
| 167 | + } |
120 | 168 | }
|
0 commit comments