Skip to content

Commit

Permalink
Implement getCveId
Browse files Browse the repository at this point in the history
  • Loading branch information
fwilhe committed May 3, 2024
1 parent 6911d34 commit f0a3aea
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 37 deletions.
9 changes: 1 addition & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ repositories {

ext {
set('snippetsDir', file("build/generated-snippets"))
set('springBootAdminVersion', "3.2.3")
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'de.codecentric:spring-boot-admin-starter-server'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
Expand All @@ -34,12 +33,6 @@ dependencies {
testImplementation 'org.testcontainers:postgresql'
}

dependencyManagement {
imports {
mavenBom "de.codecentric:spring-boot-admin-dependencies:${springBootAdminVersion}"
}
}

tasks.named('test') {
outputs.dir snippetsDir
useJUnitPlatform()
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/io/gardenlinux/glvd/Cve.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.gardenlinux.glvd;

import jakarta.annotation.Nonnull;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import java.util.Objects;

@Entity(name = "all_cve")
public class Cve {
@Id
@Column(name = "cve_id", nullable = false)
private String id;

@Column(name = "last_mod", nullable = false)
@Nonnull
private String lastModified;

@Column(name = "data", nullable = false)
@Nonnull
private String data;

public Cve() {
}

public Cve(String id, @Nonnull String lastModified, @Nonnull String data) {
this.id = id;
this.lastModified = lastModified;
this.data = data;
}

public String getId() {
return id;
}

@Nonnull
public String getLastModified() {
return lastModified;
}

@Nonnull
public String getData() {
return data;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Cve cve = (Cve) o;
return Objects.equals(id, cve.id) && lastModified.equals(cve.lastModified) && data.equals(cve.data);
}

@Override
public int hashCode() {
int result = Objects.hashCode(id);
result = 31 * result + lastModified.hashCode();
result = 31 * result + data.hashCode();
return result;
}
}
7 changes: 7 additions & 0 deletions src/main/java/io/gardenlinux/glvd/CveRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.gardenlinux.glvd;

import org.springframework.data.jpa.repository.JpaRepository;

public interface CveRepository extends JpaRepository<Cve, String> {

}
6 changes: 3 additions & 3 deletions src/main/java/io/gardenlinux/glvd/GlvdApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@SpringBootApplication
public class GlvdApplication {

public static void main(String[] args) {
SpringApplication.run(GlvdApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(GlvdApplication.class, args);
}

}
28 changes: 28 additions & 0 deletions src/main/java/io/gardenlinux/glvd/GlvdController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.gardenlinux.glvd;

import jakarta.annotation.Nonnull;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/v1/cves", produces = MediaType.APPLICATION_JSON_VALUE)
public class GlvdController {

@Nonnull
private final GlvdService glvdService;

public GlvdController(@Nonnull GlvdService glvdService) {
this.glvdService = glvdService;
}

@GetMapping("/{cveId}")
ResponseEntity<?> getCveId(@PathVariable("cveId") final String cveId) {
var cve = glvdService.getCve(cveId);
return ResponseEntity.ok().body(cve);
}

}
20 changes: 20 additions & 0 deletions src/main/java/io/gardenlinux/glvd/GlvdService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.gardenlinux.glvd;

import jakarta.annotation.Nonnull;
import org.springframework.stereotype.Service;

@Service
public class GlvdService {

@Nonnull
private final CveRepository cveRepository;

public GlvdService(@Nonnull CveRepository cveRepository) {
this.cveRepository = cveRepository;
}

public Cve getCve(String cveId) {
var cve = cveRepository.findById(cveId);
return cve.orElseThrow();
}
}
15 changes: 15 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
spring.application.name=glvd
spring.datasource.url=jdbc:postgresql://localhost:5432/glvd
spring.datasource.username=glvd
spring.datasource.password=glvd
spring.sql.init.mode=always

logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE


spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.type=trace


logging.level.org.hibernate.orm.jdbc.bind=trace
6 changes: 3 additions & 3 deletions src/test/java/io/gardenlinux/glvd/GlvdApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@SpringBootTest
class GlvdApplicationTests {

@Test
void contextLoads() {
}
@Test
void contextLoads() {
}

}
75 changes: 75 additions & 0 deletions src/test/java/io/gardenlinux/glvd/GlvdControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.gardenlinux.glvd;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class GlvdControllerTest {

static DockerImageName glvdPostgresImage = DockerImageName.parse("ghcr.io/gardenlinux/glvd-postgres:edgesampledata")
.asCompatibleSubstituteFor("postgres");
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
glvdPostgresImage
).withDatabaseName("glvd").withUsername("glvd").withPassword("glvd");
@Autowired
CveRepository cveRepository;
@LocalServerPort
private Integer port;

@BeforeAll
static void beforeAll() {
postgres.start();
}

@AfterAll
static void afterAll() {
postgres.stop();
}

@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}

@BeforeEach
void setUp() {
RestAssured.baseURI = "http://localhost:" + port;
}

@Test
void shouldGetCveById() {
given()
.contentType(ContentType.JSON)
.when()
.get("/v1/cves/CVE-2024-1549")
.then()
.statusCode(200)
.body("id", containsString("CVE-2024-1549"));
}

@Test
void tryGetNonExistingCveById() {
given()
.contentType(ContentType.JSON)
.when()
.get("/v1/cves/CVE-1989-1234")
.then()
.statusCode(500);
}
}
23 changes: 0 additions & 23 deletions src/test/java/io/gardenlinux/glvd/TestGlvdApplication.java

This file was deleted.

0 comments on commit f0a3aea

Please sign in to comment.