Skip to content

Commit c624b37

Browse files
authored
Merge pull request #61 from mcanoy/tests
Added Tests / pom
2 parents 61db057 + 51335c5 commit c624b37

File tree

12 files changed

+282
-68
lines changed

12 files changed

+282
-68
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
xmlns="http://maven.apache.org/POM/4.0.0"
55
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
66
<modelVersion>4.0.0</modelVersion>
7-
<groupId>com.rht_labs</groupId>
8-
<artifactId>open-management-portal-git-api</artifactId>
7+
<groupId>com.redhat.labs</groupId>
8+
<artifactId>lodestar-git-api</artifactId>
99
<version>1.0.0-SNAPSHOT</version>
1010
<properties>
1111
<compiler-plugin.version>3.8.1</compiler-plugin.version>

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/EngagementService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
@ApplicationScoped
3232
public class EngagementService {
33-
public static Logger LOGGER = LoggerFactory.getLogger(EngagementService.class);
33+
public static final Logger LOGGER = LoggerFactory.getLogger(EngagementService.class);
3434

3535
private static final String ENGAGEMENT_PROJECT_NAME = "iac";
3636
private static final String DEFAULT_BRANCH = "master";
@@ -94,7 +94,7 @@ public Project createEngagement(Engagement engagement, String author, String aut
9494
authorEmail, project.isFirst());
9595

9696
if (LOGGER.isDebugEnabled()) {
97-
commit.getActions().stream().forEach(file -> LOGGER.debug("Action File path :: " + file.getFilePath()));
97+
commit.getActions().stream().forEach(file -> LOGGER.debug("Action File path :: {}", file.getFilePath()));
9898
}
9999

100100
// send commit to gitlab
@@ -125,7 +125,7 @@ public List<Hook> getHooks(String customer, String engagment) {
125125
return hookService.getProjectHooks(project.get().getId());
126126
}
127127

128-
return new ArrayList<Hook>();
128+
return new ArrayList<>();
129129
}
130130

131131
public Response createHook(String customerName, String engagementName, Hook hook) {
@@ -145,7 +145,7 @@ public Response createHook(String customerName, String engagementName, Hook hook
145145

146146
public Status getProjectStatus(String customerName, String engagementName) {
147147
Status status = null;
148-
Optional<File> file = fileService.getFile(this.getPath(customerName, engagementName), "status.json");
148+
Optional<File> file = fileService.getFile(this.getPath(customerName, engagementName), STATUS_FILE);
149149
if(file.isPresent()) {
150150
status = json.fromJson(file.get().getContent(), Status.class);
151151
}

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/main/java/com/redhat/labs/omp/utils/EncodingUtils.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,8 @@
77
import java.util.Base64;
88

99
public class EncodingUtils {
10-
11-
public static boolean isDecodable(String val) {
12-
13-
try {
14-
Base64.getDecoder().decode(val.getBytes());
15-
} catch (IllegalArgumentException iae) {
16-
return false;
17-
}
18-
19-
return true;
20-
21-
}
10+
11+
private EncodingUtils() {}
2212

2313
public static byte[] base64Encode(byte[] src) {
2414
return Base64.getEncoder().encode(src);

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

Lines changed: 15 additions & 18 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"
@@ -227,7 +222,9 @@ public void deleteFile(Integer projectId, String filePath, File file) {
227222

228223
@Override
229224
public Response commitMultipleFiles(Integer projectId, CommitMultiple commit) {
230-
// TODO: need to be able to have negative scenarios
225+
if("[email protected]".equals(commit.getAuthorEmail())) {
226+
return Response.ok().build();
227+
}
231228
return Response.status(201).build();
232229
}
233230

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
import io.restassured.http.ContentType;
1010

1111
@QuarkusTest
12-
public class ConfigResourceTest {
12+
class ConfigResourceTest {
1313

1414
@Test
15-
public void testGetConfigFileSuccess() {
15+
void testGetConfigFileSuccess() {
1616

1717
given().when().contentType(ContentType.JSON).get("/api/v1/config").then().statusCode(200).body(is(
1818
"{\"content\":\"---\\nproviders:\\n- label: AWS\\n value: ec2\\n regions:\\n - label: US East 1 (N. Virginia)\\n value: us-east-1\\n - label: US East 2 (Ohio)\\n value: us-east-2\\nopenshift:\\n versions:\\n - label: v4.1\\n value: 4.1.31\\n - label: v4.2\\n value: 4.2.16\\n - label: v4.3\\n value: 4.3.0\\n persistent-storage:\\n - label: None\\n value: none\\n - label: 50GB\\n value: 50G\\n - label: 100GB\\n value: 100G\\n - label: 250GB\\n value: 250G\\n - label: 500GB\\n value: 500G\\n cluster-size:\\n - label: Small\\n value: small\\nuser-management:\\n rbac:\\n roles:\\n - label: Developer \\n value: developer\\n - label: Observer \\n value: observer\\n - label: Admin \\n value: admin\\n\",\"encoding\":\"base64\",\"file_path\":\"schema/config.yml\"}"));
1919

2020
}
2121

2222
@Test
23-
public void testGetConfigFileSuccessV2() {
23+
void testGetConfigFileSuccessV2() {
2424

2525
given().when().contentType(ContentType.JSON).get("/api/v2/config").then().statusCode(200).body(is(
2626
"{\"providers\":[{\"label\":\"AWS\",\"value\":\"ec2\",\"regions\":[{\"label\":\"US East 1 (N. Virginia)\","
@@ -33,5 +33,19 @@ public void testGetConfigFileSuccessV2() {
3333
+ "{\"label\":\"Admin\",\"value\":\"admin\"}]}}}"));
3434

3535
}
36+
37+
@Test
38+
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
}

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import io.restassured.http.ContentType;
1212

1313
@QuarkusTest
14-
public class EngagementResourceTest {
14+
class EngagementResourceTest {
1515

1616

1717
@Test
18-
public void testGetAllEngagementsSuccess() {
18+
void testGetAllEngagementsSuccess() {
1919

2020
given()
2121
.when()
@@ -33,7 +33,7 @@ public void testGetAllEngagementsSuccess() {
3333
}
3434

3535
@Test
36-
public void testCreateEngagementSuccess() {
36+
void testCreateEngagementSuccess() {
3737
given()
3838
.when()
3939
.contentType(ContentType.JSON)
@@ -46,7 +46,7 @@ public void testCreateEngagementSuccess() {
4646
}
4747

4848
@Test
49-
public void testUpdateEngagementSuccess() {
49+
void testUpdateEngagementSuccess() {
5050
given()
5151
.when()
5252
.contentType(ContentType.JSON)
@@ -59,7 +59,7 @@ public void testUpdateEngagementSuccess() {
5959
}
6060

6161
@Test
62-
public void testGetWebhooksSuccess() {
62+
void testGetWebhooksSuccess() {
6363
given()
6464
.when()
6565
.contentType(ContentType.JSON)
@@ -71,7 +71,7 @@ public void testGetWebhooksSuccess() {
7171
}
7272

7373
@Test
74-
public void testCreateProjectHookSuccess() {
74+
void testCreateProjectHookSuccess() {
7575
given()
7676
.when()
7777
.contentType(ContentType.JSON)
@@ -82,7 +82,29 @@ public void testCreateProjectHookSuccess() {
8282
}
8383

8484
@Test
85-
public void testGetStatusSuccess() {
85+
void testCreateProjectHookFailAlreadyExists() {
86+
given()
87+
.when()
88+
.contentType(ContentType.JSON)
89+
.body("{\"push_events\": true, \"url\": \"http://webhook.edu/hook\"}")
90+
.post("/api/v1/engagements/customer/jello/lemon/hooks")
91+
.then()
92+
.statusCode(400);
93+
}
94+
95+
@Test
96+
void testCreateProjectHookFailNoProject() {
97+
given()
98+
.when()
99+
.contentType(ContentType.JSON)
100+
.body("{\"push_events\": true, \"url\": \"https://lodestar/webhooks/blah\"}")
101+
.post("/api/v1/engagements/customer/nope/tutti-frutti/hooks")
102+
.then()
103+
.statusCode(400);
104+
}
105+
106+
@Test
107+
void testGetStatusSuccess() {
86108
given()
87109
.when()
88110
.contentType(ContentType.JSON)
@@ -94,7 +116,7 @@ public void testGetStatusSuccess() {
94116
}
95117

96118
@Test
97-
public void testGetProjectSuccess() {
119+
void testGetProjectSuccess() {
98120
given()
99121
.when()
100122
.contentType(ContentType.JSON)

0 commit comments

Comments
 (0)