Skip to content

Commit

Permalink
management service tests v1 (#135)
Browse files Browse the repository at this point in the history
* management-service: couple new tests + small reformat

* management-service: more test

* management-service: fixed most of the tests, few more to go

* management-service: all tests works

* management-service: all tests works with watch

* management-service: refactor + jacoco for test coverage

* client: added cash for management-service tests in docker-compose.test.yml

* management-service: new tests for utils

* management-service: more tests

* management-service: more tests

* management-service: refactor
  • Loading branch information
MDybek authored Dec 5, 2024
1 parent b87cb44 commit 5f364c1
Show file tree
Hide file tree
Showing 27 changed files with 2,110 additions and 73 deletions.
10 changes: 10 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ services:
- LETSENCRYPT_HOST=${REPORTS_PRODUCTION_HOST}
- VIRTUAL_PORT=${REPORTS_SERVICE_PORT}

management-service-test:
container_name: magpie-monitor-tests-management-service
build:
context: ./management-service
dockerfile: Dockerfile
target: tests
volumes:
- maven-repo:/root/.m2

pod-agent:
user: "0" # Elevated permission needed for bind mount
container_name: magpie-monitor-pod-agent
Expand Down Expand Up @@ -129,6 +138,7 @@ volumes:
cache:
es-certs:
external: true
maven-repo:

networks:
default:
Expand Down
13 changes: 12 additions & 1 deletion management-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
FROM eclipse-temurin:21-jdk-jammy as builder
WORKDIR /opt/app

COPY mvnw pom.xml ./
COPY .mvn/ .mvn
RUN chmod +x ./mvnw && ./mvnw dependency:go-offline

COPY ./src ./src
RUN ./mvnw clean install -DskipTests

FROM eclipse-temurin:21-jdk-jammy as tests
WORKDIR /opt/app

COPY --from=builder /opt/app /opt/app

ENTRYPOINT ["./mvnw", "test"]

FROM eclipse-temurin:21-jre-jammy
WORKDIR /opt/app
EXPOSE 8080

COPY --from=builder /opt/app/target/*.jar /opt/app/*.jar
ENTRYPOINT ["java", "-jar", "/opt/app/*.jar" ]

ENTRYPOINT ["java", "-jar", "/opt/app/*.jar"]
95 changes: 87 additions & 8 deletions management-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
<version>3.4.0</version>
<relativePath/>
</parent>
<groupId>pl.pwr</groupId>
Expand All @@ -28,12 +28,15 @@
</scm>
<properties>
<java.version>21</java.version>
<spring.boot.version>3.3.5</spring.boot.version>
<projectlombok.version>1.18.34</projectlombok.version>
<spring.boot.version>3.4.0</spring.boot.version>
<projectlombok.version>1.18.36</projectlombok.version>
<springdoc.version>2.6.0</springdoc.version>
<jjwt.version>0.12.6</jjwt.version>
<commonmark.version>0.22.0</commonmark.version>
<slack-api-client.version>1.43.1</slack-api-client.version>
<commonmark.version>0.24.0</commonmark.version>
<slack-api-client.version>1.44.2</slack-api-client.version>
<groovy-all.version>4.0.24</groovy-all.version>
<spock.version>2.4-M4-groovy-4.0</spock.version>
<mockito.version>5.2.0</mockito.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -60,7 +63,7 @@
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>3.2.4</version>
<version>3.3.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -97,13 +100,13 @@
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>1.27.0</version>
<version>1.30.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.http-client/google-http-client-gson -->
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-gson</artifactId>
<version>1.45.0</version>
<version>1.45.1</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -176,6 +179,30 @@
<artifactId>commonmark</artifactId>
<version>${commonmark.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.groovy/groovy-all -->
<dependency>
<groupId>org.apache.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy-all.version}</version>
<type>pom</type>
</dependency>

<!-- https://mvnrepository.com/artifact/org.spockframework/spock-core -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>${spock.version}</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-inline-->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -184,6 +211,58 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>4.0.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<useFile>false</useFile>
<includes>
<include>**/*Test.java</include>
<include>**/*Spec.java</include>
</includes>
</configuration>
<dependencies>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.8.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public ClusterConfigurationDTO getClusterById(String clusterId) {
return isRunning
.map(running -> ClusterConfigurationDTO.ofCluster(clusterConfiguration, running))
.orElse(ClusterConfigurationDTO.ofCluster(clusterConfiguration, false));

}).orElse(ClusterConfigurationDTO.defaultConfiguration());
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service
Expand All @@ -31,12 +32,10 @@ public class MetadataService {
private final MetadataHistoryService metadataHistoryService;

public List<ClusterMetadataDTO> getAllClusters() {
List<ClusterMetadataDTO> activeClusterMetadataDTOS = getActiveClusters();

Set<ClusterMetadataDTO> inactiveClusterMetadataDTOS = filterInactiveClusters(activeClusterMetadataDTOS);
activeClusterMetadataDTOS.addAll(inactiveClusterMetadataDTOS);

return activeClusterMetadataDTOS;
return Stream.concat(
getActiveClusters().stream(),
filterInactiveClusters(getActiveClusters()).stream())
.collect(Collectors.toList());
}

public List<ClusterMetadataDTO> getActiveClusters() {
Expand Down Expand Up @@ -66,12 +65,11 @@ public Optional<ClusterMetadataDTO> getClusterById(String clusterId) {
}

public List<NodeMetadataDTO> getClusterNodes(String clusterId) {
List<NodeMetadataDTO> activeNodeMetadataDTOS = getActiveNodesForClusterId(clusterId);

Set<NodeMetadataDTO> inactiveNodeMetadataDTOS = filterInactiveNodesForClusterId(clusterId, activeNodeMetadataDTOS);
activeNodeMetadataDTOS.addAll(inactiveNodeMetadataDTOS);

return activeNodeMetadataDTOS;
return Stream.concat(
getActiveNodesForClusterId(clusterId).stream(),
filterInactiveNodesForClusterId(clusterId, getActiveNodesForClusterId(clusterId)).stream()
)
.collect(Collectors.toList());
}

private List<NodeMetadataDTO> getActiveNodesForClusterId(String clusterId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import pl.pwr.zpi.notifications.common.ResourceLoaderUtils;

@Service
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ public SlackReceiver getEncodedWebhookUrl(Long id) {
}

public void deleteSlackReceiver(Long receiverId) {
checkIfReceiverExist(receiverId);
slackRepository.deleteById(receiverId);
}

private void checkIfReceiverExist(Long receiverId) {
if (!slackRepository.existsById(receiverId)) {
throw new IllegalArgumentException("Webhook with given Id not found");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ public Optional<ReportDetailedSummaryDTO> getReportDetailedSummaryById(String re
}

public Optional<ReportIncidentsDTO> getReportIncidents(String reportId) {
return reportRepository.findProjectedIncidentsById(reportId).map(incidentProjection -> {
return ReportIncidentsDTO.builder()
.applicationIncidents(extractApplicationIncidents(incidentProjection))
.nodeIncidents(extractNodeIncidents(incidentProjection))
.build();
});
return reportRepository.findProjectedIncidentsById(reportId).map(incidentProjection -> ReportIncidentsDTO.builder()
.applicationIncidents(extractApplicationIncidents(incidentProjection))
.nodeIncidents(extractNodeIncidents(incidentProjection))
.build());
}

public ReportPaginatedIncidentsDTO<ApplicationIncidentDTO> getReportApplicationIncidents(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package pl.pwr.zpi.user.service;

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import pl.pwr.zpi.user.data.User;
import pl.pwr.zpi.user.repository.UserRepository;

import java.time.Instant;
import java.util.Optional;

@RequiredArgsConstructor
Expand All @@ -16,16 +13,6 @@ public class UserService {

private final UserRepository userRepository;

public Optional<User> getCurrentUser() {
var userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
var userEmail = userDetails.getUsername();
return getUserByEmail(userEmail);
}

public Optional<User> getUserByEmail(String email) {
return userRepository.findByEmail(email);
}

public User saveUser(User user) {
return userRepository.save(user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<tr style="vertical-align: middle;">
<td style="text-align: left; padding: 15px;">
<span style="display: inline-block;">
Magpie Monitor - complex anomaly detection system
Magpie Monitor - Reading logs is for the frogs, let's find insights from them
</span>
</td>
<td style="height: 100%; padding: 0 10px 0 0;">
Expand Down
Loading

0 comments on commit 5f364c1

Please sign in to comment.