Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codeium cascade did this #242

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package finos.traderx.accountservice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HealthCheckController {

@Autowired
private JdbcTemplate jdbcTemplate;

@GetMapping("/health")
public ResponseEntity<Map<String, Object>> healthCheck() {
Map<String, Object> response = new HashMap<>();
response.put("service", "account-service");
response.put("status", "UP");

try {
// Test database connection
jdbcTemplate.queryForObject("SELECT 1", Integer.class);
response.put("database", "UP");
} catch (Exception e) {
response.put("database", "DOWN");
response.put("error", e.getMessage());
return ResponseEntity.status(503).body(response);
}

// Add additional health metrics
response.put("timestamp", System.currentTimeMillis());

return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package finos.traderx.accountservice.controller;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.mockito.Mockito.*;

@WebMvcTest(HealthCheckController.class)
public class HealthCheckControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private JdbcTemplate jdbcTemplate;

@Test
public void whenDatabaseIsUp_thenReturns200() throws Exception {
when(jdbcTemplate.queryForObject(eq("SELECT 1"), eq(Integer.class))).thenReturn(1);

mockMvc.perform(MockMvcRequestBuilders.get("/health"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value("UP"))
.andExpect(jsonPath("$.database").value("UP"))
.andExpect(jsonPath("$.service").value("account-service"))
.andExpect(jsonPath("$.timestamp").exists());
}

@Test
public void whenDatabaseIsDown_thenReturns503() throws Exception {
when(jdbcTemplate.queryForObject(eq("SELECT 1"), eq(Integer.class)))
.thenThrow(new RuntimeException("Database connection failed"));

mockMvc.perform(MockMvcRequestBuilders.get("/health"))
.andExpect(status().isServiceUnavailable())
.andExpect(jsonPath("$.status").value("UP"))
.andExpect(jsonPath("$.database").value("DOWN"))
.andExpect(jsonPath("$.error").exists())
.andExpect(jsonPath("$.service").value("account-service"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace PeopleService.WebApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class HealthController : ControllerBase
{
private readonly ILogger<HealthController> _logger;

public HealthController(ILogger<HealthController> logger)
{
_logger = logger;
}

[HttpGet]
public async Task<IActionResult> Get()
{
try
{
var healthStatus = new
{
Service = "people-service",
Status = "UP",
Timestamp = DateTimeOffset.UtcNow,
Version = GetType().Assembly.GetName().Version?.ToString()
};

return Ok(healthStatus);
}
catch (Exception ex)
{
_logger.LogError(ex, "Health check failed");
return StatusCode(500, new
{
Status = "DOWN",
Error = ex.Message,
Timestamp = DateTimeOffset.UtcNow
});
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package finos.traderx.positionservice.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HealthCheckController {

@Autowired
private JdbcTemplate jdbcTemplate;

@GetMapping("/health")
public ResponseEntity<Map<String, Object>> healthCheck() {
Map<String, Object> response = new HashMap<>();
response.put("service", "position-service");
response.put("status", "UP");

try {
// Test database connection
jdbcTemplate.queryForObject("SELECT 1", Integer.class);
response.put("database", "UP");
} catch (Exception e) {
response.put("database", "DOWN");
response.put("error", e.getMessage());
return ResponseEntity.status(503).body(response);
}

// Add additional health metrics
response.put("timestamp", System.currentTimeMillis());

return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package finos.traderx.positionservice.controller;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.mockito.Mockito.*;

@WebMvcTest(HealthCheckController.class)
public class HealthCheckControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private JdbcTemplate jdbcTemplate;

@Test
public void whenDatabaseIsUp_thenReturns200() throws Exception {
when(jdbcTemplate.queryForObject(eq("SELECT 1"), eq(Integer.class))).thenReturn(1);

mockMvc.perform(MockMvcRequestBuilders.get("/health"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value("UP"))
.andExpect(jsonPath("$.database").value("UP"))
.andExpect(jsonPath("$.service").value("position-service"))
.andExpect(jsonPath("$.timestamp").exists());
}

@Test
public void whenDatabaseIsDown_thenReturns503() throws Exception {
when(jdbcTemplate.queryForObject(eq("SELECT 1"), eq(Integer.class)))
.thenThrow(new RuntimeException("Database connection failed"));

mockMvc.perform(MockMvcRequestBuilders.get("/health"))
.andExpect(status().isServiceUnavailable())
.andExpect(jsonPath("$.status").value("UP"))
.andExpect(jsonPath("$.database").value("DOWN"))
.andExpect(jsonPath("$.error").exists())
.andExpect(jsonPath("$.service").value("position-service"));
}
}
49 changes: 43 additions & 6 deletions reference-data/src/health/health.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { HealthCheckService, TerminusModule } from '@nestjs/terminus';
import { HealthCheckExecutor } from '@nestjs/terminus/dist/health-check/health-check-executor.service';
import { Test, TestingModule } from '@nestjs/testing';
import HealthController from './health.controller';
import { HttpException } from '@nestjs/common';

describe('HealthController', () => {
let controller: HealthController;
let healthService: HealthCheckService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
Expand All @@ -13,19 +15,54 @@ describe('HealthController', () => {
}).compile();

controller = module.get<HealthController>(HealthController);
healthService = module.get<HealthCheckService>(HealthCheckService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

it('Health Check', async () => {
const health = await controller.check();
expect(health).toEqual({
it('should return successful health check', async () => {
const mockResult = {
status: 'ok',
info: {},
info: {
referenceData: {
status: 'up',
details: {
service: 'reference-data',
timestamp: expect.any(String)
}
}
},
error: {},
details: {}
});
details: {
referenceData: {
status: 'up',
details: {
service: 'reference-data',
timestamp: expect.any(String)
}
}
}
};

const health = await controller.check();
expect(health).toMatchObject(mockResult);
});

it('should handle errors appropriately', async () => {
jest.spyOn(healthService, 'check').mockRejectedValue(new Error('Test error'));

try {
await controller.check();
fail('Should have thrown an error');
} catch (error) {
expect(error).toBeInstanceOf(HttpException);
expect(error.getStatus()).toBe(500);
expect(error.getResponse()).toEqual({
status: 'error',
message: 'Test error'
});
}
});
});
34 changes: 30 additions & 4 deletions reference-data/src/health/health.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
import { Controller, Get } from '@nestjs/common';
import { HealthCheckService, HealthCheck } from '@nestjs/terminus';
import { Controller, Get, HttpException, HttpStatus } from '@nestjs/common';
import { HealthCheckService, HealthCheck, HealthCheckResult } from '@nestjs/terminus';

@Controller('health')
class HealthController {
constructor(private health: HealthCheckService) {}

@Get()
@HealthCheck()
check() {
return this.health.check([]);
async check(): Promise<HealthCheckResult> {
try {
const result = await this.health.check([
// Add specific health checks here
async () => ({
referenceData: {
status: 'up',
details: {
service: 'reference-data',
timestamp: new Date().toISOString()
}
}
})
]);

return result;
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
{
status: 'error',
message: error.message,
},
HttpStatus.INTERNAL_SERVER_ERROR
);
}
}
}
export default HealthController;
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package finos.traderx.tradeprocessor.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HealthCheckController {

@Autowired
private JdbcTemplate jdbcTemplate;

@GetMapping("/health")
public ResponseEntity<Map<String, Object>> healthCheck() {
Map<String, Object> response = new HashMap<>();
response.put("service", "trade-processor");
response.put("status", "UP");

try {
// Test database connection
jdbcTemplate.queryForObject("SELECT 1", Integer.class);
response.put("database", "UP");
} catch (Exception e) {
response.put("database", "DOWN");
response.put("error", e.getMessage());
return ResponseEntity.status(503).body(response);
}

// Add additional health metrics
response.put("timestamp", System.currentTimeMillis());

return ResponseEntity.ok(response);
}
}
Loading