Skip to content

Commit

Permalink
add missing engagement api. upgrade quarkus. fix artifact def
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanoy committed Feb 21, 2022
1 parent bb77e87 commit d00121f
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 105 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<version>1.0.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<lombok.version>1.18.12</lombok.version>
<lombok.version>1.18.22</lombok.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>2.3.1.Final</quarkus.platform.version>
<quarkus.platform.version>2.7.1.Final</quarkus.platform.version>
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
<wiremock.version>2.31.0</wiremock.version>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public class ActivityResource {
List<String> committedFilesToWatch;

@PostConstruct
void trimToken() {
void trimTokenAndWatch() {
webhookToken = webhookToken.trim();
LOGGER.info("Watched files {}", committedFilesToWatch);
}

@GET
Expand Down Expand Up @@ -129,13 +130,14 @@ public Response postWebhook(@HeaderParam(value = "x-gitlab-token") String gitLab
activityService.purge(hook);
return Response.status(Status.NO_CONTENT).build();
}

if(hook.didFileChange(committedFilesToWatch)) {
LOGGER.debug("Activity spotted for {}", hook.getEngagementName());
activityService.addNewCommits(hook);
return Response.accepted().build();
}

LOGGER.debug("hook file change? {}", hook.didFileChange(committedFilesToWatch));
return Response.ok(hook).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
public interface EngagementApiRestClient {

@GET
List<Engagement> getAllEngagements(@QueryParam("includeCommits") boolean includeCommits, @QueryParam("includeStatus") boolean includeStatus, @QueryParam("pagination") boolean pagination);
List<Engagement> getAllEngagements();

@GET
@Path("/{id}")
Engagement getEngagement(@PathParam("id") long projectId);

@GET
@Path("/project/{id}")
Engagement getEngagementByProject(@PathParam("id") long projectId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import com.redhat.labs.lodestar.activity.model.Engagement;
import com.redhat.labs.lodestar.activity.model.Hook;
import com.redhat.labs.lodestar.activity.model.VersionManifest;
import com.redhat.labs.lodestar.activity.rest.client.EngagementApiRestClient;
import com.redhat.labs.lodestar.activity.rest.client.GitApiRestClient;
import com.redhat.labs.lodestar.activity.rest.client.LodeStarStatusRestClient;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.List;
Expand All @@ -20,10 +17,8 @@
public class EngagementService {
private static final Logger LOGGER = LoggerFactory.getLogger(EngagementService.class);

@ConfigProperty(name = "engagement.component")
String component;

boolean useV1 = true;
@ConfigProperty(name = "engagement.version.one")
boolean useV1 = false;

@Inject
@RestClient
Expand All @@ -33,41 +28,20 @@ public class EngagementService {
@RestClient
EngagementApiRestClient engagementApiRestClient;

@Inject
@RestClient
LodeStarStatusRestClient statusRestClient;

@PostConstruct
void getVersion() {
LOGGER.debug("component {}", component);
VersionManifest vm = statusRestClient.getVersionManifest(component);

if(vm.value == null || !vm.value.startsWith("v")) {
useV1 = false; //Use v2 if not in an env with a true version set
} else {
String[] version = vm.value.split("\\.");
int majorVersion = Integer.parseInt(version[0].substring(1));
LOGGER.debug("major version {} full version {}", majorVersion, vm.value);
useV1 = majorVersion < 2;
}

LOGGER.debug("using v1 {}", useV1);
}

public Engagement getEngagement(Hook hook) {
LOGGER.debug("use {}", useV1);
LOGGER.debug("use v1? {}", useV1);
if(useV1) {
return gitApiRestClient.getEngagement(hook.getProject().getPathWithNamespace(), false);
}

return engagementApiRestClient.getEngagement(hook.getProjectId());
return engagementApiRestClient.getEngagementByProject(hook.getProjectId());
}

public List<Engagement> getAllEngagements() {
if(useV1) {
return gitApiRestClient.getAllEngagements();
}

return engagementApiRestClient.getAllEngagements(false, false, false);
return engagementApiRestClient.getAllEngagements();
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
gitlab.api/mp-rest/url=${GITLAB_API_URL:https://acmegit.com}
engagement.api/mp-rest/url=${ENGAGEMENT_API_URL:http://lodestar-engagements:8080}
git-api-v1.api/mp-rest/url=${GIT_API_URL:http://lodestar-git-api:8080}
lodestar.status.api/mp-rest/url=${STATUS_API_URL:http://lodestar-status:8080}

engagement.component=lodestar-engagements
engagement.version.one=${ENGAGEMENT_VERSION_V1:false}

gitlab.personal.access.token=${GITLAB_TOKEN:t}
gitlab.webhook.token=${WEBHOOK_TOKEN:t}
Expand All @@ -27,7 +27,7 @@ commit.page.size=50
commit.msg.filter.list=${COMMIT_FILTERED_MESSAGE_LIST:manual_refresh}
commit.filter.list=${COMMIT_FILTERED_EMAIL_LIST:[email protected]}

commit.watch.files=${COMMIT_WATCH_FILES:engagement/engagement.json,engagement/hosting.json,engagement/participants.json,engagement/artifact.json}
commit.watch.files=${COMMIT_WATCH_FILES:engagement/engagement.json,engagement/hosting.json,engagement/participants.json,engagement/artifacts.json}

# configure your datasource
quarkus.datasource.db-kind = postgresql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Map<String, String> start() {

body = ResourceLoader.load("seed-engagement-v2.json");

stubFor(get(urlEqualTo("/api/v2/engagements?includeCommits=false&includeStatus=false&pagination=false")).willReturn(aResponse()
stubFor(get(urlEqualTo("/api/v2/engagements")).willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody(body)
));
Expand Down Expand Up @@ -64,6 +64,11 @@ public Map<String, String> start() {
.withBody(body)
));

stubFor(get(urlEqualTo("/api/v2/engagements/project/1")).willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody(body)
));

//v1 get engagement
stubFor(get(urlEqualTo("/api/v1/engagements/namespace/main%2Fstore%2FHats%2FCap%2Fiac?includeStatus=false")).willReturn(aResponse()
.withHeader("Content-Type", "application/json")
Expand All @@ -75,7 +80,7 @@ public Map<String, String> start() {
Map<String, String> config = new HashMap<>();
config.put("gitlab.api/mp-rest/url", wireMockServer.baseUrl());
config.put("engagement.api/mp-rest/url", wireMockServer.baseUrl());
config.put("git-api-v1.api/mp-rest/url", wireMockServer.baseUrl());
//config.put("git-api-v1.api/mp-rest/url", wireMockServer.baseUrl());

return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ void init() {
service.reloadEngagement(e);
}

@Test
void testDBPopulated() {
service.checkDBPopulated();
Mockito.verify(repo, Mockito.timeout(1000L).atLeast(1)).count();
}

@Test
void testPurge() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,95 +4,42 @@
import com.redhat.labs.lodestar.activity.model.GitlabProject;
import com.redhat.labs.lodestar.activity.model.Hook;
import com.redhat.labs.lodestar.activity.model.VersionManifest;
import com.redhat.labs.lodestar.activity.rest.client.EngagementApiRestClient;
import com.redhat.labs.lodestar.activity.rest.client.GitApiRestClient;
import com.redhat.labs.lodestar.activity.rest.client.LodeStarStatusRestClient;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.quarkus.test.junit.mockito.InjectSpy;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;


import java.util.Collections;
import javax.inject.Inject;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@QuarkusTest
public class EngagementServiceTest {
class EngagementServiceTest {

@Inject
EngagementService service;
LodeStarStatusRestClient statusRestClient;
GitApiRestClient gitApiRestClient;

@BeforeEach
public void setUp() {
statusRestClient = Mockito.mock(LodeStarStatusRestClient.class);
gitApiRestClient = Mockito.mock(GitApiRestClient.class);
service = new EngagementService();
service.gitApiRestClient = gitApiRestClient;
service.statusRestClient = statusRestClient;
service.component = "lodestar-engagements";
}

@Test
public void testGetEngagement() {
Mockito.when(statusRestClient.getVersionManifest("lodestar-engagements")).thenReturn(
VersionManifest.builder().value("v1.0.0").build());

Mockito.when(gitApiRestClient.getEngagement("iac", false)).thenReturn(Engagement.builder().build());
void testGetEngagement() {

GitlabProject p = GitlabProject.builder().pathWithNamespace("iac").build();
Engagement e = service.getEngagement(Hook.builder().project(p).build());
Engagement e = service.getEngagement(Hook.builder().project(p).projectId(1L).build());

assertNotNull(e);
Mockito.verify(gitApiRestClient).getEngagement("iac", false);
}

@Test
public void testGetAllEngagement() {
Mockito.when(statusRestClient.getVersionManifest("lodestar-engagements")).thenReturn(
VersionManifest.builder().value("v1.0.0").build());

Mockito.when(gitApiRestClient.getAllEngagements()).thenReturn(Collections.singletonList(Engagement.builder().build()));
void testGetAllEngagement() {

List<Engagement> e = service.getAllEngagements();
Mockito.verify(gitApiRestClient).getAllEngagements();
assertEquals(1, e.size());
}

@Test
public void testVersionsV1() {
Mockito.when(statusRestClient.getVersionManifest("lodestar-engagements")).thenReturn(
VersionManifest.builder().value("v1.0.0").build());

service.getVersion();
assertTrue(service.useV1);
}

@Test
public void testVersionsV2() {
Mockito.when(statusRestClient.getVersionManifest("lodestar-engagements")).thenReturn(
VersionManifest.builder().value("v2.0.0").build());

service.getVersion();
assertFalse(service.useV1);
}

@Test
public void testVersionsMain() {
Mockito.when(statusRestClient.getVersionManifest("lodestar-engagements")).thenReturn(
VersionManifest.builder().value("main").build());

service.getVersion();
assertFalse(service.useV1);
}

public void testVersionsNull() {
Mockito.when(statusRestClient.getVersionManifest("lodestar-engagements")).thenReturn(
VersionManifest.builder().value(null).build());

service.getVersion();
assertFalse(service.useV1);
}

}

0 comments on commit d00121f

Please sign in to comment.