Skip to content

Commit

Permalink
Dependency update v2 (#1060)
Browse files Browse the repository at this point in the history
* fix conflicts

* update redis dependency

* update swagger test (remove deprecated apis)

* update opensrp dependencies

* update log4j packages

* fix breaking changes on swagger v3.0.0

* add swaggerConfig test

* replace stringValueOf with name()
  • Loading branch information
bennsimon authored Feb 10, 2022
1 parent 3899a85 commit f630f94
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 61 deletions.
62 changes: 28 additions & 34 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,31 @@

<artifactId>opensrp-server-web</artifactId>
<packaging>war</packaging>
<version>2.8.48-SNAPSHOT</version>
<version>2.9.0-SNAPSHOT</version>
<name>opensrp-server-web</name>
<description>OpenSRP Server Web Application</description>
<url>https://github.com/OpenSRP/opensrp-server-web</url>

<properties>
<jetty.port>9979</jetty.port>
<main.basedir>${project.basedir}</main.basedir>
<hibernate-entitymanager.version>5.4.12.Final</hibernate-entitymanager.version>
<hibernate-entitymanager.version>5.4.24.Final</hibernate-entitymanager.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.2.4.RELEASE</spring.version>
<spring.security.version>5.3.12.RELEASE</spring.security.version>
<spring.security.oauth.version>2.4.0.RELEASE</spring.security.oauth.version>
<spring.fox.swagger.version>2.9.2</spring.fox.swagger.version>
<spring.data.redis>2.2.4.RELEASE</spring.data.redis>
<redis.jedis.version>3.2.0</redis.jedis.version>
<redis.lettuce.version>5.2.2.RELEASE</redis.lettuce.version>
<spring.version>5.2.19.RELEASE</spring.version>
<spring.security.version>5.2.15.RELEASE</spring.security.version>
<spring.security.oauth.version>2.4.2.RELEASE</spring.security.oauth.version>
<spring.fox.swagger.version>3.0.0</spring.fox.swagger.version>
<spring.data.redis>2.2.13.RELEASE</spring.data.redis>
<redis.jedis.version>3.8.0</redis.jedis.version>
<opensrp.updatePolicy>always</opensrp.updatePolicy>
<nexus-staging-maven-plugin.version>1.5.1</nexus-staging-maven-plugin.version>
<opensrp.core.version>2.12.24-SNAPSHOT</opensrp.core.version>
<opensrp.connector.version>2.3.6-SNAPSHOT</opensrp.connector.version>
<opensrp.core.version>2.13.0-SNAPSHOT</opensrp.core.version>
<opensrp.connector.version>2.4.0-SNAPSHOT</opensrp.connector.version>
<opensrp.interface.version>2.0.1-SNAPSHOT</opensrp.interface.version>
<opensrp.common.version>2.0.3-SNAPSHOT</opensrp.common.version>
<powermock.version>2.0.5</powermock.version>
<opensrp-server-openlmis-stock-version>1.0.3-SNAPSHOT</opensrp-server-openlmis-stock-version>
<opensrp-server-openlmis-stock-version>1.1.1-SNAPSHOT</opensrp-server-openlmis-stock-version>
<lombok.version>1.18.12</lombok.version>
<log4j.version>2.17.1</log4j.version>
</properties>

<distributionManagement>
Expand Down Expand Up @@ -160,16 +159,6 @@
<artifactId>opensrp-server-connector</artifactId>
<version>${opensrp.connector.version}</version>
</dependency>
<dependency>
<groupId>org.smartregister</groupId>
<artifactId>opensrp-server-common</artifactId>
<version>${opensrp.common.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
Expand Down Expand Up @@ -250,7 +239,7 @@
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
<version>1.15</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
Expand All @@ -275,7 +264,7 @@
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-security-adapter</artifactId>
<version>9.0.3</version>
<version>16.1.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand All @@ -285,7 +274,7 @@
<dependency>
<groupId>org.owasp.encoder</groupId>
<artifactId>encoder</artifactId>
<version>1.2.2</version>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down Expand Up @@ -313,7 +302,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -355,7 +344,7 @@
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>3.0.3</version>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.jeasy</groupId>
Expand All @@ -370,7 +359,7 @@
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
Expand All @@ -386,14 +375,14 @@
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-log4j2</artifactId>
<version>5.5.2</version>
<version>5.6.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.lettuce/lettuce-core -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.2.2.RELEASE</version>
<version>6.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
Expand All @@ -413,17 +402,22 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.1</version>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.17.1</version>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.8.1</version>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.binder.db.PostgreSQLDatabaseMetrics;
import io.micrometer.core.instrument.binder.jvm.DiskSpaceMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
import io.micrometer.core.instrument.binder.logging.Log4j2Metrics;
import io.micrometer.core.instrument.binder.system.DiskSpaceMetrics;
import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
import io.micrometer.core.instrument.binder.system.UptimeMetrics;
import io.micrometer.prometheus.PrometheusConfig;
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/org/opensrp/web/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.opensrp.web.config;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
Expand All @@ -18,6 +21,7 @@
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.ServletContext;
import java.util.List;

@Configuration
Expand All @@ -30,12 +34,19 @@ public class SwaggerConfig extends WebMvcConfigurerAdapter {
private static final String BEARER = "Bearer";
private static final String HEADER = "header";

@Autowired
private ServletContext servletContext;

@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("(/rest/.*)|(/multimedia/.*)|(/security/.*)|(/user-details)"))
.paths(PathSelectors.regex(
StringUtils.replace("(basePath/rest/.*)|(basePath/multimedia/.*)|(basePath/security/.*)|(basePath/user-details)",
"basePath",
servletContext.getContextPath()
))) // To be revisited on v3.0.1
.build()
.apiInfo(getApiInfo())
.securityContexts(Lists.newArrayList(securityContext()))
Expand All @@ -54,8 +65,14 @@ public ApiInfo getApiInfo() {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/swagger-ui/")
.setViewName("forward:/swagger-ui/index.html");
}

public SecurityContext securityContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ServletInputStream getInputStream() throws IOException {
@Override
public BufferedReader getReader() throws IOException {
if (rawData == null) {
rawData = IOUtils.toByteArray(this.request.getReader(),StandardCharsets.UTF_8);
rawData = IOUtils.toByteArray(this.request.getReader(), StandardCharsets.UTF_8.name());
servletStream.stream = new ByteArrayInputStream(rawData);
}
updateParameters();
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/opensrp/web/rest/TemplateResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensrp.domain.Template;
import org.opensrp.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
69 changes: 49 additions & 20 deletions src/test/java/org/opensrp/web/config/SwaggerConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.reflect.internal.WhiteboxImpl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
Expand All @@ -16,12 +23,16 @@
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import javax.servlet.ServletContext;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
Expand All @@ -33,14 +44,19 @@ public class SwaggerConfigTest {
private static final String BEARER = "Bearer";
private static final String HEADER = "header";

@Mock
private ServletContext servletContext;

@Before
public void setUp() {
swaggerConfig = mock(SwaggerConfig.class);
MockitoAnnotations.initMocks(this);
doReturn("/opensrp").when(servletContext).getContextPath();
swaggerConfig = spy(new SwaggerConfig());
WhiteboxImpl.setInternalState(swaggerConfig, "servletContext", servletContext);
}

@Test
public void testApi() {
assertNull(swaggerConfig.api());
when(swaggerConfig.api()).thenReturn(createTestDocket());
when(swaggerConfig.getApiInfo()).thenReturn(createTestApiInfo());
when(swaggerConfig.securityContext()).thenReturn(createTestSecurityContext());
Expand All @@ -52,27 +68,40 @@ public void testApi() {

@Test
public void testGetApiInfo() {
assertNull(swaggerConfig.getApiInfo());
when(swaggerConfig.getApiInfo()).thenReturn(createTestApiInfo());
ApiInfo apiInfo = swaggerConfig.getApiInfo();
assertNotNull(apiInfo);
assertEquals(apiInfo.getTitle(), "Test title");
assertEquals(apiInfo.getDescription(), "Test description");
assertEquals(apiInfo.getVersion(), "VERSION 1.0");
assertEquals(apiInfo.getLicense(), "Test LICENSE");
assertEquals(apiInfo.getLicenseUrl(), "Test license url");
assertNotNull(swaggerConfig.getApiInfo());
}

@Test
public void testSecurityContext() {
assertNull(swaggerConfig.securityContext());
when(swaggerConfig.securityContext()).thenReturn(createTestSecurityContext());
SecurityContext securityContext= swaggerConfig.securityContext();
assertNotNull(securityContext);
assertEquals(securityContext.getSecurityReferences().get(0).getReference(), BASIC);
assertEquals(securityContext.getSecurityReferences().get(0).getScopes().size(), 1);
assertEquals(securityContext.getSecurityReferences().get(1).getReference(), BEARER);
assertEquals(securityContext.getSecurityReferences().get(1).getScopes().size(), 1);
assertNotNull(swaggerConfig.securityContext());
}

@Test
public void testAddResourceHandlersShouldInvokeRegistryResourceHandler() {
String pathPattern = "/swagger-ui/**";
String resourceLocation = "classpath:/META-INF/resources/webjars/springfox-swagger-ui/";
ResourceHandlerRegistry mockRegistry = mock(ResourceHandlerRegistry.class);
ResourceHandlerRegistration mockHandlerRegistration = mock(ResourceHandlerRegistration.class);
doReturn(mockHandlerRegistration).when(mockRegistry).addResourceHandler(eq(pathPattern));

swaggerConfig.addResourceHandlers(mockRegistry);

verify(mockRegistry).addResourceHandler(eq(pathPattern));
verify(mockHandlerRegistration).addResourceLocations(eq(resourceLocation));
}

@Test
public void testAddViewControllersShouldInvokeViewControllerAddMethod() {
String path = "/swagger-ui/";
String viewName = "forward:/swagger-ui/index.html";
ViewControllerRegistry mockViewControllerRegistry = mock(ViewControllerRegistry.class);
ViewControllerRegistration mockViewControllerRegistration = mock(ViewControllerRegistration.class);
doReturn(mockViewControllerRegistration).when(mockViewControllerRegistry).addViewController(eq(path));

swaggerConfig.addViewControllers(mockViewControllerRegistry);

verify(mockViewControllerRegistry).addViewController(eq(path));
verify(mockViewControllerRegistration).setViewName(eq(viewName));
}

private Docket createTestDocket() {
Expand Down

0 comments on commit f630f94

Please sign in to comment.