Skip to content
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
3 changes: 3 additions & 0 deletions src/main/environment/bengen_docker.properties
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ bengen.pool-watcher-initial-delay-ms=60000
### Health API warns (status DEGRADED) when available Ben IDs fall below this
health.min-available-beneficiary-ids=5000

### Health API warns (status DEGRADED) when available Ben IDs fall below this
health.min-available-beneficiary-ids=5000

### Redis IP
spring.redis.host=${REDIS_HOST}

Expand Down
6 changes: 6 additions & 0 deletions src/main/environment/bengen_example.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ start-bengen-scheduler=true
# To Run scheduler Every Day
cron-scheduler-bengen=0 1 0 * * ? *

# To Run scheduler Every Day
# cron-scheduler-bengen=0 1 0 * * ? *

# To Run scheduler Every Minute
#cron-scheduler-bengen=0 0/1 * * * ? *

Expand All @@ -37,6 +40,9 @@ bengen.pool-watcher-initial-delay-ms=60000
### Health API warns (status DEGRADED) when available Ben IDs fall below this
health.min-available-beneficiary-ids=5000

### Health API warns (status DEGRADED) when available Ben IDs fall below this
health.min-available-beneficiary-ids=5000

### Redis IP
spring.redis.host=localhost
jwt.secret=my-32-character-ultra-secure-and-ultra-long-secret
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* AMRIT - Accessible Medical Records via Integrated Technologies
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package com.iemr.common.bengen;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(MockitoExtension.class)
@DisplayName("BeneficiaryGenApplication Test Suite")
class BeneficiaryGenApplicationTest {

@Mock
private RedisConnectionFactory connectionFactory;

private BeneficiaryGenApplication application;

@BeforeEach
@DisplayName("Create the application configuration before each test")
void setUp() {
application = new BeneficiaryGenApplication();
}

@Test
@DisplayName("restTemplate should supply a RestTemplate bean for outbound calls")
void restTemplate_shouldSupplyRestTemplateBean() {
RestTemplate restTemplate = application.restTemplate();

assertNotNull(restTemplate);
}

@Test
@DisplayName("redisTemplate should bind the supplied connection factory")
void redisTemplate_shouldBindSuppliedConnectionFactory() {
RedisTemplate<String, Object> template = application.redisTemplate(connectionFactory);

assertNotNull(template);
assertSame(connectionFactory, template.getConnectionFactory());
}

@Test
@DisplayName("redisTemplate should serialise keys as plain strings and values as User JSON")
void redisTemplate_shouldSerialiseKeysAsStringsAndValuesAsJson() {
RedisTemplate<String, Object> template = application.redisTemplate(connectionFactory);

assertTrue(template.getKeySerializer() instanceof StringRedisSerializer);
assertTrue(template.getValueSerializer() instanceof Jackson2JsonRedisSerializer);
}

@Test
@DisplayName("configure should register the application class as the WAR deployment source")
void configure_shouldRegisterApplicationClassAsSource() {
SpringApplicationBuilder builder = new SpringApplicationBuilder();

SpringApplicationBuilder configured = application.configure(builder);

assertSame(builder, configured, "configure should keep building on the supplied builder");
// SpringApplicationBuilder stages sources until build(), so read them back directly.
Set<Class<?>> sources = (Set<Class<?>>) ReflectionTestUtils.getField(configured, "sources");
assertTrue(sources.contains(BeneficiaryGenApplication.class));
}
}
112 changes: 112 additions & 0 deletions src/test/java/com/iemr/common/bengen/config/CorsConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* AMRIT - Accessible Medical Records via Integrated Technologies
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package com.iemr.common.bengen.config;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(MockitoExtension.class)
@DisplayName("CorsConfig Test Suite")
class CorsConfigTest {

private static final String ALLOWED_ORIGINS = "https://amrit.example.org, http://localhost:*";

private CorsConfig corsConfig;

@BeforeEach
@DisplayName("Configure the allow-list before each test")
void setUp() {
corsConfig = new CorsConfig();
ReflectionTestUtils.setField(corsConfig, "allowedOrigins", ALLOWED_ORIGINS);
}

// CorsRegistry.getCorsConfigurations() is protected, so read the registrations
// back reflectively — the same map Spring MVC itself consumes.
@SuppressWarnings("unchecked")
private Map<String, CorsConfiguration> corsConfigurations(CorsRegistry registry) {
return (Map<String, CorsConfiguration>)
ReflectionTestUtils.invokeMethod(registry, "getCorsConfigurations");
}

@Test
@DisplayName("addCorsMappings should register a mapping for every path")
void addCorsMappings_shouldRegisterMappingForEveryPath() {
CorsRegistry registry = new CorsRegistry();

corsConfig.addCorsMappings(registry);

Map<String, CorsConfiguration> configurations = corsConfigurations(registry);
assertEquals(1, configurations.size());
assertNotNull(configurations.get("/**"));
}

@Test
@DisplayName("addCorsMappings should trim and register each configured origin pattern")
void addCorsMappings_shouldTrimAndRegisterOriginPatterns() {
CorsRegistry registry = new CorsRegistry();

corsConfig.addCorsMappings(registry);

CorsConfiguration configuration = corsConfigurations(registry).get("/**");
assertEquals(java.util.List.of("https://amrit.example.org", "http://localhost:*"),
configuration.getAllowedOriginPatterns());
}

@Test
@DisplayName("addCorsMappings should permit the documented methods and headers with credentials")
void addCorsMappings_shouldPermitDocumentedMethodsAndHeaders() {
CorsRegistry registry = new CorsRegistry();

corsConfig.addCorsMappings(registry);

CorsConfiguration configuration = corsConfigurations(registry).get("/**");
assertEquals(java.util.List.of("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"),
configuration.getAllowedMethods());
assertTrue(configuration.getAllowedHeaders().contains("Jwttoken"));
assertTrue(configuration.getExposedHeaders().contains("Authorization"));
assertTrue(configuration.getExposedHeaders().contains("Jwttoken"));
assertEquals(Boolean.TRUE, configuration.getAllowCredentials());
assertEquals(3600L, configuration.getMaxAge());
}

@Test
@DisplayName("addCorsMappings should fail fast when no origin allow-list is configured")
void addCorsMappings_shouldFailFastWithoutAllowList() {
ReflectionTestUtils.setField(corsConfig, "allowedOrigins", null);
CorsRegistry registry = new CorsRegistry();

assertThrows(NullPointerException.class, () -> corsConfig.addCorsMappings(registry));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* AMRIT - Accessible Medical Records via Integrated Technologies
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package com.iemr.common.bengen.config;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import com.iemr.common.bengen.utils.http.HTTPRequestInterceptor;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;

@ExtendWith(MockitoExtension.class)
@DisplayName("InterceptorConfig Test Suite")
class InterceptorConfigTest {

@Mock
private HTTPRequestInterceptor requestInterceptor;

private InterceptorConfig interceptorConfig;

@BeforeEach
@DisplayName("Wire the configuration with a mocked interceptor before each test")
void setUp() {
interceptorConfig = new InterceptorConfig();
ReflectionTestUtils.setField(interceptorConfig, "requestInterceptor", requestInterceptor);
}

@Test
@DisplayName("addInterceptors should register the HTTP request interceptor exactly once")
void addInterceptors_shouldRegisterRequestInterceptorOnce() {
InterceptorRegistry registry = new InterceptorRegistry();

interceptorConfig.addInterceptors(registry);

List<Object> interceptors = (List<Object>) ReflectionTestUtils.invokeMethod(registry, "getInterceptors");
assertEquals(1, interceptors.size());
assertSame(requestInterceptor, interceptors.get(0));
}
}
80 changes: 80 additions & 0 deletions src/test/java/com/iemr/common/bengen/config/RedisConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* AMRIT - Accessible Medical Records via Integrated Technologies
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package com.iemr.common.bengen.config;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.config.ConfigureRedisAction;

import com.iemr.common.bengen.data.user.User;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(MockitoExtension.class)
@DisplayName("RedisConfig Test Suite")
class RedisConfigTest {

@Mock
private RedisConnectionFactory connectionFactory;

private RedisConfig redisConfig;

@BeforeEach
@DisplayName("Create the configuration before each test")
void setUp() {
redisConfig = new RedisConfig();
}

@Test
@DisplayName("configureRedisAction should disable Spring Session's CONFIG command probing")
void configureRedisAction_shouldDisableConfigCommandProbing() {
assertSame(ConfigureRedisAction.NO_OP, redisConfig.configureRedisAction());
}

@Test
@DisplayName("redisTemplate should bind the supplied connection factory")
void redisTemplate_shouldBindSuppliedConnectionFactory() {
RedisTemplate<String, User> template = redisConfig.redisTemplate(connectionFactory);

assertNotNull(template);
assertSame(connectionFactory, template.getConnectionFactory());
}

@Test
@DisplayName("redisTemplate should serialise keys as plain strings and values as User JSON")
void redisTemplate_shouldSerialiseKeysAsStringsAndValuesAsJson() {
RedisTemplate<String, User> template = redisConfig.redisTemplate(connectionFactory);

assertTrue(template.getKeySerializer() instanceof StringRedisSerializer);
assertTrue(template.getValueSerializer() instanceof Jackson2JsonRedisSerializer);
}
}
Loading
Loading