Skip to content

Commit d0adb4a

Browse files
committed
fix up tests
1 parent 61db057 commit d0adb4a

File tree

7 files changed

+186
-37
lines changed

7 files changed

+186
-37
lines changed

src/main/java/com/redhat/labs/omp/service/ConfigService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
@ApplicationScoped
2323
public class ConfigService {
24-
public static Logger LOGGER = LoggerFactory.getLogger(ConfigService.class);
24+
public static final Logger LOGGER = LoggerFactory.getLogger(ConfigService.class);
2525

2626
@ConfigProperty(name = "config.file")
2727
String configFile;
@@ -70,7 +70,7 @@ public List<HookConfig> getHookConfig() {
7070

7171
if (!optional.isPresent()) {
7272
LOGGER.error("No webhook file could be found. This is abnormal but not a deal breaker");
73-
return new ArrayList<HookConfig>();
73+
return new ArrayList<>();
7474
}
7575

7676
File file = optional.get();

src/main/java/com/redhat/labs/omp/service/GroupService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
@ApplicationScoped
1818
public class GroupService {
19-
public static Logger LOGGER = LoggerFactory.getLogger(ProjectService.class);
19+
public static final Logger LOGGER = LoggerFactory.getLogger(GroupService.class);
2020

2121
@Inject
2222
@RestClient

src/main/java/com/redhat/labs/omp/service/ProjectService.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
import org.slf4j.Logger;
1212
import org.slf4j.LoggerFactory;
1313

14-
import com.redhat.labs.exception.UnexpectedGitLabResponseException;
1514
import com.redhat.labs.omp.models.gitlab.Project;
1615
import com.redhat.labs.omp.models.gitlab.ProjectSearchResults;
1716
import com.redhat.labs.omp.rest.client.GitLabService;
1817

1918
@ApplicationScoped
2019
public class ProjectService {
21-
public static Logger LOGGER = LoggerFactory.getLogger(ProjectService.class);
20+
public static final Logger LOGGER = LoggerFactory.getLogger(ProjectService.class);
2221

2322
@Inject
2423
@RestClient
@@ -28,8 +27,7 @@ public class ProjectService {
2827
boolean doNotDelete;
2928

3029
// get a project
31-
public Optional<Project> getProjectByName(Integer namespaceId, String name)
32-
throws UnexpectedGitLabResponseException {
30+
public Optional<Project> getProjectByName(Integer namespaceId, String name) {
3331

3432
Optional<Project> optional = Optional.empty();
3533

@@ -50,7 +48,8 @@ public Optional<Project> getProjectByName(Integer namespaceId, String name)
5048

5149
}
5250

53-
public List<ProjectSearchResults> getAllProjectsByNane(String name) {
51+
//Needed anymore?
52+
public List<ProjectSearchResults> getAllProjectsByName(String name) {
5453
return gitLabService.getProjectByName(name);
5554
}
5655

@@ -83,7 +82,7 @@ public Optional<Project> createProject(Project project) {
8382
project.preserve();
8483
}
8584

86-
LOGGER.debug("create project " + project);
85+
LOGGER.debug("create project {}", project);
8786

8887
// try to create project
8988
Project createdProject = gitLabService.createProject(project);

src/test/java/com/redhat/labs/omp/mocks/MockGitLabService.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,11 @@ public List<ProjectSearchResults> getProjectByName(String name) {
8383
List<ProjectSearchResults> results = new ArrayList<>();
8484

8585
if("iac".contentEquals(name)) {
86-
ProjectSearchResults project = ProjectSearchResults.builder().id(45).name("iac").description("bla").path("path").namespace(new Namespace()).build();
86+
ProjectSearchResults project = ProjectSearchResults.builder().id(45).name("iac").description("bla").path("iac")
87+
.namespace(Namespace.builder().id(45).build()).build();
8788
results.add(project);
8889
}
8990

90-
91-
92-
// results.add(
93-
// ProjectSearchResults.builder()
94-
// .id(3)
95-
// .name("iac")
96-
// .path("iac")
97-
// .description("iac for project1")
98-
// .namespace(Namespace
99-
// .builder()
100-
// .parentId(2)
101-
// .build())
102-
// .build());
103-
10491
return results;
10592

10693
}
@@ -136,7 +123,9 @@ public Project createProject(Project project) {
136123

137124
@Override
138125
public Project updateProject(Integer projectId, Project project) {
139-
// TODO Auto-generated method stub
126+
if(projectId == 45) {
127+
return new Project();
128+
}
140129
return null;
141130
}
142131

@@ -150,12 +139,18 @@ public void deleteProjectById(Integer projectId) {
150139
public File getFile(String projectId, String filePath, String ref) {
151140

152141
if ("schema/meta.dat".equalsIgnoreCase(filePath)) {
153-
154142
String content = "./residency.yml";
155143
content = new String(EncodingUtils.base64Encode(content.getBytes()), StandardCharsets.UTF_8);
156144
return File.builder().filePath(filePath).content(content).build();
157145

158146
}
147+
148+
if ("schema/webhooks.yaml".equalsIgnoreCase(filePath)) {
149+
String content = ResourceLoader.load("webhooks.yaml");
150+
content = new String(EncodingUtils.base64Encode(content.getBytes()), StandardCharsets.UTF_8);
151+
return File.builder().filePath(filePath).content(content).build();
152+
153+
}
159154

160155
if ("schema/residency.yml".equalsIgnoreCase(filePath)) {
161156
String content = "---\n" + "\n" + "residency:\n" + " id: \"{engagement.id}\"\n"

src/test/java/com/redhat/labs/omp/resource/ConfigResourceTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,19 @@ public void testGetConfigFileSuccessV2() {
3333
+ "{\"label\":\"Admin\",\"value\":\"admin\"}]}}}"));
3434

3535
}
36+
37+
@Test
38+
public void testGetHookFileSuccess() {
39+
40+
given()
41+
.when()
42+
.contentType(ContentType.JSON)
43+
.get("/api/v2/config/webhooks")
44+
.then()
45+
.statusCode(200)
46+
.body(is("[{\"baseUrl\":\"https://labs.com/webhooks/\",\"name\":\"labs\",\"pushEvent\":true,\"pushEventsBranchFilter\":\"master\",\"token\":\"abc\"},"
47+
+ "{\"baseUrl\":\"https://rht.com/hooks/\",\"name\":\"rht\",\"pushEvent\":true,\"pushEventsBranchFilter\":\"master\",\"token\":\"def\"}]"));
48+
49+
}
3650

3751
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.redhat.labs.omp.service;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import java.util.List;
8+
9+
import org.junit.jupiter.api.Test;
10+
11+
import com.redhat.labs.exception.FileNotFoundException;
12+
import com.redhat.labs.omp.config.JsonMarshaller;
13+
import com.redhat.labs.omp.mocks.MockGitLabService;
14+
import com.redhat.labs.omp.models.gitlab.HookConfig;
15+
16+
import io.quarkus.runtime.StartupEvent;
17+
18+
class ConfigServiceTest {
19+
20+
@Test void testGetConfigFileNotFound() {
21+
ConfigService service = new ConfigService();
22+
service.configFile = "schema/notfound.yaml";
23+
24+
FileService fileService = new FileService();
25+
fileService.gitLabService = new MockGitLabService();
26+
service.fileService = fileService;
27+
28+
Exception exception = assertThrows(FileNotFoundException.class, () -> {
29+
service.getConfigFile();
30+
});
31+
32+
assertEquals("the configured file was not found in the gitlab repository.", exception.getMessage());
33+
}
34+
35+
@Test void testGetHookConfigPreLoaded() {
36+
ConfigService service = new ConfigService();
37+
service.webHooksFile = "src/test/resources/webhooks.yaml";
38+
service.marshaller = new JsonMarshaller();
39+
40+
service.onStart(new StartupEvent());
41+
List<HookConfig> hookConfigList = service.getHookConfig();
42+
43+
assertNotNull(hookConfigList);
44+
assertEquals(2, hookConfigList.size());
45+
assertEquals("abc", hookConfigList.iterator().next().getToken());
46+
47+
}
48+
49+
@Test void testGetHookConfigLabNotEmpty() {
50+
ConfigService service = new ConfigService();
51+
service.webHooksFile = "/schema/webhooks.yaml";
52+
service.marshaller = new JsonMarshaller();
53+
54+
FileService fileService = new FileService();
55+
fileService.gitLabService = new MockGitLabService();
56+
service.fileService = fileService;
57+
58+
service.onStart(new StartupEvent());
59+
List<HookConfig> hookConfigList = service.getHookConfig();
60+
61+
assertNotNull(hookConfigList);
62+
assertEquals(2, hookConfigList.size());
63+
assertEquals("abc", hookConfigList.iterator().next().getToken());
64+
}
65+
66+
@Test void testGetHookConfigLabsEmpty() {
67+
ConfigService service = new ConfigService();
68+
service.webHooksFile = "schema/notfound.yaml";
69+
service.marshaller = new JsonMarshaller();
70+
71+
FileService fileService = new FileService();
72+
fileService.gitLabService = new MockGitLabService();
73+
service.fileService = fileService;
74+
75+
service.onStart(new StartupEvent());
76+
List<HookConfig> hookConfigList = service.getHookConfig();
77+
78+
assertNotNull(hookConfigList);
79+
assertEquals(0, hookConfigList.size());
80+
}
81+
}
Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package com.redhat.labs.omp.service;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
38
import java.util.List;
49
import java.util.Optional;
510

611
import javax.inject.Inject;
7-
8-
import org.junit.jupiter.api.Assertions;
912
import org.junit.jupiter.api.Test;
1013

14+
import com.redhat.labs.omp.mocks.MockGitLabService;
1115
import com.redhat.labs.omp.models.gitlab.Project;
16+
import com.redhat.labs.omp.models.gitlab.ProjectSearchResults;
1217

1318
import io.quarkus.test.junit.QuarkusTest;
1419

@@ -18,28 +23,83 @@ public class ProjectServiceTest {
1823
@Inject
1924
ProjectService projectService;
2025

21-
@Test
22-
public void createValidProject() {
26+
@Test void createValidProject() {
2327

2428
Project project = Project.builder().name("valid").build();
2529
Optional<Project> created = projectService.createProject(project);
2630

27-
Assertions.assertTrue(created.isPresent());
31+
assertTrue(created.isPresent());
2832
}
2933

30-
@Test
31-
public void createInValidProject() {
34+
@Test void createInValidProject() {
3235

3336
Project project = Project.builder().name("invalid").build();
3437
Optional<Project> created = projectService.createProject(project);
3538

36-
Assertions.assertFalse(created.isPresent());
39+
assertFalse(created.isPresent());
3740
}
3841

39-
@Test
40-
public void getProjectsByGroup() {
42+
@Test void createAndPreserve() {
43+
ProjectService noquarkus = new ProjectService();
44+
noquarkus.doNotDelete = true;
45+
noquarkus.gitLabService = new MockGitLabService();
46+
47+
Optional<Project> project = noquarkus.createProject(Project.builder().build());
48+
49+
assertTrue(project.isPresent());
50+
Project p = project.get();
51+
assertNotNull(p.getTagList());
52+
assertEquals(1, p.getTagList().size());
53+
assertEquals("DO_NOT_DELETE", p.getTagList().get(0));
54+
}
55+
56+
@Test void getProjectsByGroup() {
4157
List<Project> projects = projectService.getProjectsByGroup(10, true);
4258

43-
Assertions.assertEquals(1, projects.size());
59+
assertEquals(1, projects.size());
60+
}
61+
62+
@Test void getProjectFound() {
63+
Optional<Project> found = projectService.getProjectByName(45, "iac");
64+
65+
assertTrue(found.isPresent());
66+
}
67+
68+
@Test void getProjectFoundButWrongNamespace() {
69+
Optional<Project> found = projectService.getProjectByName(46, "iac");
70+
71+
assertFalse(found.isPresent());
72+
}
73+
74+
@Test void getProjectNotFound() {
75+
Optional<Project> notFound = projectService.getProjectByName(77, "Banana");
76+
77+
assertEquals(Optional.empty(), notFound);
78+
}
79+
80+
@Test void getProjectAllByName() {
81+
List<ProjectSearchResults> found = projectService.getAllProjectsByName("iac");
82+
assertNotNull(found);
83+
assertEquals(1, found.size());
84+
85+
}
86+
87+
@Test void getProjectByPath() {
88+
Optional<Project> found = projectService.getProjectById(66);
89+
assertFalse(found.isPresent());
90+
}
91+
92+
@Test void updateProjectValid() {
93+
Optional<Project> updated = projectService.updateProject(45, new Project());
94+
assertTrue(updated.isPresent());
95+
}
96+
97+
@Test void updateProjectInvalid() {
98+
Optional<Project> updated = projectService.updateProject(46, new Project());
99+
assertFalse(updated.isPresent());
100+
}
101+
102+
@Test void deleteProjectWorks() {
103+
projectService.deleteProject(45);
44104
}
45105
}

0 commit comments

Comments
 (0)