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
15 changes: 11 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
<exclusion>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
Expand Down Expand Up @@ -285,6 +281,17 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${artifactId}-${version}</finalName>
Expand Down
100 changes: 100 additions & 0 deletions src/test/java/com/iemr/inventory/RoleMasterApplicationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.inventory;

import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;

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 com.iemr.inventory.utils.IEMRApplBeans;

@ExtendWith(MockitoExtension.class)
@DisplayName("RoleMasterApplication Test Suite")
class RoleMasterApplicationTest {

@Mock
private RedisConnectionFactory connectionFactory;

private final RoleMasterApplication application = new RoleMasterApplication();

@Test
@DisplayName("configProperties should supply a fresh properties holder on each call")
void configProperties_shouldSupplyFreshHolder() {
assertNotNull(application.configProperties());
assertNotSame(application.configProperties(), application.configProperties());
}

@Test
@DisplayName("instantiateBeans should supply the shared bean configuration")
void instantiateBeans_shouldSupplyBeanConfiguration() {
assertInstanceOf(IEMRApplBeans.class, application.instantiateBeans());
}

@Test
@DisplayName("configure should point the servlet container at this application class")
void configure_shouldPointAtApplicationClass() {
SpringApplicationBuilder builder = new SpringApplicationBuilder();

assertSame(builder, application.configure(builder));
}

@Test
@DisplayName("redisTemplate should bind the connection factory and serialise keys as plain strings")
void redisTemplate_shouldBindFactoryAndConfigureSerializers() {
RedisTemplate<String, Object> template = application.redisTemplate(connectionFactory);

assertSame(connectionFactory, template.getConnectionFactory());
assertInstanceOf(StringRedisSerializer.class, template.getKeySerializer());
assertInstanceOf(Jackson2JsonRedisSerializer.class, template.getValueSerializer());
}

@Test
@DisplayName("ServletInitializer should point the WAR deployment at the same application class")
void servletInitializer_shouldPointAtApplicationClass() {
SpringApplicationBuilder builder = new SpringApplicationBuilder();

assertSame(builder, new ServletInitializer().configure(builder));
}

@Test
@DisplayName("ServletInitializer onStartup should complete without touching the servlet context")
void servletInitializer_onStartupShouldCompleteQuietly() throws Exception {
jakarta.servlet.ServletContext servletContext =
org.mockito.Mockito.mock(jakarta.servlet.ServletContext.class);

new ServletInitializer().onStartup(servletContext);

org.mockito.Mockito.verifyNoInteractions(servletContext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.inventory.config;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import com.iemr.inventory.utils.http.HTTPRequestInterceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@ExtendWith(MockitoExtension.class)
@DisplayName("Web MVC configuration Test Suite")
class HttpInterceptorConfigTest {

@Mock
private HTTPRequestInterceptor httpInterceptor;
@Mock
private InterceptorRegistry registry;
@Mock
private InterceptorRegistration registration;
@Mock
private HttpServletRequest request;
@Mock
private HttpServletResponse response;

@Test
@DisplayName("addInterceptors should register the session-checking interceptor")
void addInterceptors_shouldRegisterSessionInterceptor() {
HttpInterceptorConfig config = new HttpInterceptorConfig();
ReflectionTestUtils.setField(config, "httpInterceptor", httpInterceptor);
when(registry.addInterceptor(httpInterceptor)).thenReturn(registration);

config.addInterceptors(registry);

verify(registry).addInterceptor(httpInterceptor);
}

@Test
@DisplayName("BlockingMethodInterceptor should complete its post-handle and after-completion hooks quietly")
void blockingMethodInterceptor_shouldCompleteHooksQuietly() {
BlockingMethodInterceptor interceptor = new BlockingMethodInterceptor();

assertDoesNotThrow(() -> interceptor.postHandle(request, response, new Object(), null));
assertDoesNotThrow(() -> interceptor.afterCompletion(request, response, new Object(), null));
}
}
88 changes: 88 additions & 0 deletions src/test/java/com/iemr/inventory/config/RedisConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.inventory.config;

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

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.connection.lettuce.LettuceConnectionFactory;
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 com.iemr.inventory.data.user.M_User;

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

@Mock
private RedisConnectionFactory connectionFactory;

private RedisConfig redisConfig;

@BeforeEach
@DisplayName("Create the configuration with Redis coordinates before each test")
void setUp() {
redisConfig = new RedisConfig();
ReflectionTestUtils.setField(redisConfig, "redisHost", "redis.example.org");
ReflectionTestUtils.setField(redisConfig, "redisPort", 6379);
}

@Test
@DisplayName("lettuceConnectionFactory should point Lettuce at the configured host and port")
void lettuceConnectionFactory_shouldUseConfiguredHostAndPort() {
LettuceConnectionFactory factory = redisConfig.lettuceConnectionFactory();

assertNotNull(factory);
assertEquals("redis.example.org", factory.getHostName());
assertEquals(6379, factory.getPort());
}

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

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

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

assertInstanceOf(StringRedisSerializer.class, template.getKeySerializer());
assertInstanceOf(Jackson2JsonRedisSerializer.class, template.getValueSerializer());
}
}
101 changes: 101 additions & 0 deletions src/test/java/com/iemr/inventory/config/SwaggerConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.inventory.config;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.mock.env.MockEnvironment;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityScheme;

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

@DisplayName("SwaggerConfig Test Suite")
class SwaggerConfigTest {

private static final String SECURITY_SCHEME_NAME = "bearerAuth";
private static final String DEFAULT_URL = "http://localhost:9090";

private SwaggerConfig swaggerConfig;
private MockEnvironment environment;

@BeforeEach
@DisplayName("Create the configuration and an empty environment before each test")
void setUp() {
swaggerConfig = new SwaggerConfig();
environment = new MockEnvironment();
}

@Test
@DisplayName("customOpenAPI should describe the inventory API")
void customOpenAPI_shouldDescribeTheApi() {
OpenAPI openAPI = swaggerConfig.customOpenAPI(environment);

assertNotNull(openAPI.getInfo());
assertEquals("Inventory API", openAPI.getInfo().getTitle());
assertEquals("version", openAPI.getInfo().getVersion());
assertTrue(openAPI.getInfo().getDescription().contains("inventory management"));
}

@Test
@DisplayName("customOpenAPI should declare a bearer security scheme and require it")
void customOpenAPI_shouldDeclareBearerSecurityScheme() {
OpenAPI openAPI = swaggerConfig.customOpenAPI(environment);

SecurityScheme scheme = openAPI.getComponents().getSecuritySchemes().get(SECURITY_SCHEME_NAME);
assertNotNull(scheme);
assertEquals(SecurityScheme.Type.HTTP, scheme.getType());
assertEquals("bearer", scheme.getScheme());
assertEquals(1, openAPI.getSecurity().size());
assertTrue(openAPI.getSecurity().get(0).containsKey(SECURITY_SCHEME_NAME));
}

@Test
@DisplayName("customOpenAPI should fall back to localhost for every unset server url")
void customOpenAPI_shouldFallBackToLocalhostForUnsetUrls() {
OpenAPI openAPI = swaggerConfig.customOpenAPI(environment);

assertEquals(3, openAPI.getServers().size());
openAPI.getServers().forEach(server -> assertEquals(DEFAULT_URL, server.getUrl()));
assertEquals("Dev", openAPI.getServers().get(0).getDescription());
assertEquals("UAT", openAPI.getServers().get(1).getDescription());
assertEquals("Demo", openAPI.getServers().get(2).getDescription());
}

@Test
@DisplayName("customOpenAPI should use the configured dev, UAT and demo urls when present")
void customOpenAPI_shouldUseConfiguredUrls() {
environment.setProperty("api.dev.url", "https://dev.amrit.example.org");
environment.setProperty("api.uat.url", "https://uat.amrit.example.org");
environment.setProperty("api.demo.url", "https://demo.amrit.example.org");

OpenAPI openAPI = swaggerConfig.customOpenAPI(environment);

assertEquals("https://dev.amrit.example.org", openAPI.getServers().get(0).getUrl());
assertEquals("https://uat.amrit.example.org", openAPI.getServers().get(1).getUrl());
assertEquals("https://demo.amrit.example.org", openAPI.getServers().get(2).getUrl());
}
}
Loading
Loading