diff --git a/src/service/proxy/proxy-core/build.gradle.kts b/src/service/proxy/proxy-core/build.gradle.kts index a8a26cd82a..52a9ec4fd5 100644 --- a/src/service/proxy/proxy-core/build.gradle.kts +++ b/src/service/proxy/proxy-core/build.gradle.kts @@ -63,6 +63,7 @@ dependencies { testFixturesImplementation(project(":common:common-jetty")) testFixturesImplementation(project(":common:common-messagelog")) testFixturesImplementation(project(":service:op-monitor:op-monitor-api")) + testFixturesImplementation(project(":service:monitor:monitor-rpc-client")) testFixturesImplementation(testFixtures(project(":common:common-properties"))) testFixturesImplementation(testFixtures(project(":lib:keyconf-impl"))) testFixturesImplementation(testFixtures(project(":lib:serverconf-impl"))) diff --git a/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/addon/proxymonitor/ProxyMonitor.java b/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/addon/proxymonitor/ProxyMonitor.java deleted file mode 100644 index 90ea9c5583..0000000000 --- a/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/addon/proxymonitor/ProxyMonitor.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * The MIT License - * - * Copyright (c) 2019- Nordic Institute for Interoperability Solutions (NIIS) - * Copyright (c) 2018 Estonian Information System Authority (RIA), - * Nordic Institute for Interoperability Solutions (NIIS), Population Register Centre (VRK) - * Copyright (c) 2015-2017 Estonian Information System Authority (RIA), Population Register Centre (VRK) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.niis.xroad.proxy.core.addon.proxymonitor; - -import lombok.extern.slf4j.Slf4j; -import org.niis.xroad.monitor.rpc.MonitorRpcClient; -import org.niis.xroad.proxy.core.addon.BindableServiceRegistry; -import org.niis.xroad.proxy.core.addon.proxymonitor.util.ProxyMonitorService; - -/** - * ProxyMonitor initialization - */ -@Slf4j -public class ProxyMonitor { - - private static MonitorRpcClient monitorRpcClient; - - public void init(final BindableServiceRegistry bindableServiceRegistry, MonitorRpcClient monitorClient) { - try { - bindableServiceRegistry.register(new ProxyMonitorService()); - - setMonitorClient(monitorClient); - } catch (Exception e) { - log.error("ProxyMonitor addon has failed to start. Monitor data will not be available!", e); - } - } - - public void shutdown() throws Exception { - if (monitorRpcClient != null) { - monitorRpcClient.close(); - } - } - - public static MonitorRpcClient getClient() { - return monitorRpcClient; - } - - static void setMonitorClient(MonitorRpcClient monitorClient) { - ProxyMonitor.monitorRpcClient = monitorClient; - } - -} diff --git a/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/addon/proxymonitor/serverproxy/ProxyMonitorServiceHandlerImpl.java b/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/addon/proxymonitor/serverproxy/ProxyMonitorServiceHandlerImpl.java index 41c9441587..0a4fedace3 100644 --- a/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/addon/proxymonitor/serverproxy/ProxyMonitorServiceHandlerImpl.java +++ b/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/addon/proxymonitor/serverproxy/ProxyMonitorServiceHandlerImpl.java @@ -46,7 +46,6 @@ import org.niis.xroad.globalconf.GlobalConfProvider; import org.niis.xroad.monitor.rpc.MonitorRpcClient; import org.niis.xroad.opmonitor.api.OpMonitoringData; -import org.niis.xroad.proxy.core.addon.proxymonitor.ProxyMonitor; import org.niis.xroad.proxy.core.addon.proxymonitor.util.MetricTypes; import org.niis.xroad.proxy.core.protocol.ProxyMessage; import org.niis.xroad.proxy.core.serverproxy.AbstractServiceHandler; @@ -83,9 +82,12 @@ public class ProxyMonitorServiceHandlerImpl extends AbstractServiceHandler { new ByteArrayOutputStream(); private SoapMessageEncoder responseEncoder; + private final MonitorRpcClient monitorRpcClient; - public ProxyMonitorServiceHandlerImpl(ServerConfProvider serverConfProvider, GlobalConfProvider globalConfProvider) { + public ProxyMonitorServiceHandlerImpl(ServerConfProvider serverConfProvider, GlobalConfProvider globalConfProvider, + MonitorRpcClient monitorClient) { super(serverConfProvider, globalConfProvider); + this.monitorRpcClient = monitorClient; } @Override @@ -131,8 +133,6 @@ public void startHandling(RequestWrapper servletRequest, ProxyMessage proxyReque //mock implementation responseEncoder = new SimpleSoapEncoder(responseOut); - final MonitorRpcClient client = ProxyMonitor.getClient(); - final GetSecurityServerMetricsResponse metricsResponse = new GetSecurityServerMetricsResponse(); final MetricSetType root = new MetricSetType(); root.setName(serverConfProvider.getIdentifier().toString()); @@ -143,9 +143,7 @@ public void startHandling(RequestWrapper servletRequest, ProxyMessage proxyReque version.setValue(Version.XROAD_VERSION); root.getMetrics().add(version); - if (client != null) { - root.getMetrics().add(MetricTypes.of(client.getMetrics(getMetricNames(proxyRequestMessage), isOwner()))); - } + root.getMetrics().add(MetricTypes.of(monitorRpcClient.getMetrics(getMetricNames(proxyRequestMessage), isOwner()))); SoapMessageImpl result = createResponse(requestMessage.getSoap(), metricsResponse); responseEncoder.soap(result, Collections.emptyMap()); diff --git a/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/configuration/ProxyAddonConfig.java b/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/configuration/ProxyAddonConfig.java index 4ba6276389..7f3a1aacee 100644 --- a/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/configuration/ProxyAddonConfig.java +++ b/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/configuration/ProxyAddonConfig.java @@ -26,36 +26,20 @@ package org.niis.xroad.proxy.core.configuration; import jakarta.enterprise.context.ApplicationScoped; -import jakarta.enterprise.inject.Disposes; -import org.niis.xroad.monitor.rpc.MonitorRpcClient; import org.niis.xroad.proxy.core.ProxyProperties; import org.niis.xroad.proxy.core.addon.BindableServiceRegistry; -import org.niis.xroad.proxy.core.addon.proxymonitor.ProxyMonitor; +import org.niis.xroad.proxy.core.addon.proxymonitor.util.ProxyMonitorService; public class ProxyAddonConfig { @ApplicationScoped - static class ProxyMonitorInitializer { - private ProxyMonitor proxyMonitor; + public BindableServiceRegistry bindableServiceRegistry(ProxyProperties.ProxyAddonProperties addonProperties) { + BindableServiceRegistry bindableServiceRegistry = new BindableServiceRegistry(); - @ApplicationScoped - public BindableServiceRegistry bindableServiceRegistry(ProxyProperties.ProxyAddonProperties addonProperties, - MonitorRpcClient monitorRpcClient) { - BindableServiceRegistry bindableServiceRegistry = new BindableServiceRegistry(); - - if (addonProperties.proxyMonitor().enabled()) { - proxyMonitor = new ProxyMonitor(); - proxyMonitor.init(bindableServiceRegistry, monitorRpcClient); - } - - return bindableServiceRegistry; - } - - public void dispose(@Disposes BindableServiceRegistry bindableServiceRegistry) throws Exception { - if (proxyMonitor != null) { - proxyMonitor.shutdown(); - } + if (addonProperties.proxyMonitor().enabled()) { + bindableServiceRegistry.register(new ProxyMonitorService()); } + return bindableServiceRegistry; } } diff --git a/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/serverproxy/ServiceHandlerLoader.java b/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/serverproxy/ServiceHandlerLoader.java index 730f81d20c..228c94594d 100644 --- a/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/serverproxy/ServiceHandlerLoader.java +++ b/src/service/proxy/proxy-core/src/main/java/org/niis/xroad/proxy/core/serverproxy/ServiceHandlerLoader.java @@ -28,6 +28,7 @@ import jakarta.enterprise.context.ApplicationScoped; import lombok.RequiredArgsConstructor; import org.niis.xroad.globalconf.GlobalConfProvider; +import org.niis.xroad.monitor.rpc.MonitorRpcClient; import org.niis.xroad.opmonitor.api.OpMonitorCommonProperties; import org.niis.xroad.proxy.core.ProxyProperties; import org.niis.xroad.proxy.core.addon.metaservice.serverproxy.MetadataServiceHandlerImpl; @@ -44,6 +45,7 @@ public class ServiceHandlerLoader { private final ServerConfProvider serverConfProvider; private final GlobalConfProvider globalConfProvider; + private final MonitorRpcClient monitorRpcClient; private final ProxyProperties.ProxyAddonProperties addonProperties; private final OpMonitorCommonProperties opMonitorCommonProperties; @@ -56,7 +58,7 @@ public Collection loadSoapServiceHandlers() { handlers.add(new OpMonitoringServiceHandlerImpl(serverConfProvider, globalConfProvider, opMonitorCommonProperties)); } if (addonProperties.proxyMonitor().enabled()) { - handlers.add(new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider)); + handlers.add(new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider, monitorRpcClient)); } return handlers; } diff --git a/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/AbstractProxyIntegrationTest.java b/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/AbstractProxyIntegrationTest.java index 2f9d91c82a..4187ddcd4f 100644 --- a/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/AbstractProxyIntegrationTest.java +++ b/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/AbstractProxyIntegrationTest.java @@ -39,6 +39,7 @@ import org.niis.xroad.common.properties.ConfigUtils; import org.niis.xroad.globalconf.impl.cert.CertHelper; import org.niis.xroad.keyconf.KeyConfProvider; +import org.niis.xroad.monitor.rpc.MonitorRpcClient; import org.niis.xroad.opmonitor.api.OpMonitorCommonProperties; import org.niis.xroad.proxy.core.antidos.AntiDosConfiguration; import org.niis.xroad.proxy.core.clientproxy.AuthTrustVerifier; @@ -170,7 +171,7 @@ static void startServices(ProxyProperties proxyProperties) throws Exception { OpMonitorCommonProperties opMonitorCommonProperties = ConfigUtils.defaultConfiguration(OpMonitorCommonProperties.class); ServiceHandlerLoader serviceHandlerLoader = new ServiceHandlerLoader(TEST_SERVER_CONF, TEST_GLOBAL_CONF, - proxyProperties.addOn(), opMonitorCommonProperties); + mock(MonitorRpcClient.class), proxyProperties.addOn(), opMonitorCommonProperties); serverProxy = new ServerProxy(proxyProperties.server(), mock(AntiDosConfiguration.class), commonBeanProxy, serviceHandlerLoader, opMonitorCommonProperties); serverProxy.init(); diff --git a/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/ProxyMonitorMetaserviceTest.java b/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/ProxyMonitorMetaserviceTest.java index 12d7e6ef99..0863faeea2 100644 --- a/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/ProxyMonitorMetaserviceTest.java +++ b/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/ProxyMonitorMetaserviceTest.java @@ -41,7 +41,6 @@ import org.niis.xroad.monitor.rpc.EnvMonitorRpcChannelProperties; import org.niis.xroad.monitor.rpc.MonitorRpcClient; import org.niis.xroad.proxy.core.ProxyProperties; -import org.niis.xroad.proxy.core.addon.BindableServiceRegistry; import org.niis.xroad.proxy.core.test.MessageTestCase; import org.niis.xroad.proxy.core.test.ProxyTestSuiteHelper; import org.niis.xroad.proxy.core.test.TestContext; @@ -68,6 +67,8 @@ public class ProxyMonitorMetaserviceTest { EnvMonitorRpcChannelProperties.class, Map.of(EnvMonitorRpcChannelProperties.PREFIX + ".port", String.valueOf(findRandomPort()))); + private static MonitorRpcClient monitorRpcClient; + @BeforeAll static void beforeAll() throws Exception { TimeUtils.setClock(Clock.fixed(Instant.parse("2020-01-01T00:00:00Z"), ZoneOffset.UTC)); @@ -83,10 +84,9 @@ static void beforeAll() throws Exception { ProxyTestSuiteHelper.proxyProperties = ConfigUtils.initConfiguration(ProxyProperties.class, proxyProps); ProxyTestSuiteHelper.startTestServices(); - MonitorRpcClient monitorRpcClient = new MonitorRpcClient(new RpcChannelFactory(new InsecureRpcCredentialsConfigurer()), + monitorRpcClient = new MonitorRpcClient(new RpcChannelFactory(new InsecureRpcCredentialsConfigurer()), envMonitorRpcChannelProperties); monitorRpcClient.init(); - new ProxyMonitor().init(new BindableServiceRegistry(), monitorRpcClient); } @AfterAll @@ -102,7 +102,7 @@ Stream proxyMonitorTests() throws Exception { assertThat(testCasesToRun.size()).isGreaterThan(0); System.setProperty(SystemProperties.PROXY_SSL_SUPPORT, "false"); - ctx = new TestContext(ProxyTestSuiteHelper.proxyProperties); + ctx = new TestContext(ProxyTestSuiteHelper.proxyProperties, true, monitorRpcClient); return testCasesToRun.stream() .map(testCase -> dynamicTest(testCase.getId(), diff --git a/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/RestoreMonitorClientAfterTest.java b/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/RestoreMonitorClientAfterTest.java deleted file mode 100644 index 0c3ed95973..0000000000 --- a/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/RestoreMonitorClientAfterTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * The MIT License - * - * Copyright (c) 2019- Nordic Institute for Interoperability Solutions (NIIS) - * Copyright (c) 2018 Estonian Information System Authority (RIA), - * Nordic Institute for Interoperability Solutions (NIIS), Population Register Centre (VRK) - * Copyright (c) 2015-2017 Estonian Information System Authority (RIA), Population Register Centre (VRK) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.niis.xroad.proxy.core.addon.proxymonitor; - -import org.junit.rules.ExternalResource; -import org.niis.xroad.monitor.rpc.MonitorRpcClient; - -/** A JUnit rule to restore the {@link ProxyMonitor} MonitorClient after the test has run. */ -public class RestoreMonitorClientAfterTest extends ExternalResource { - - private MonitorRpcClient monitorClient; - - @Override - protected void before() { - monitorClient = ProxyMonitor.getClient(); - } - - @Override - protected void after() { - setMonitorClient(monitorClient); - } - - /** Set the monitor client for test purposes - * @param monitorClient - */ - public static void setMonitorClient(MonitorRpcClient monitorClient) { - ProxyMonitor.setMonitorClient(monitorClient); - } -} diff --git a/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/serverproxy/ProxyMonitorServiceHandlerMetricsTest.java b/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/serverproxy/ProxyMonitorServiceHandlerMetricsTest.java index 776f5d126d..f7d9c7e307 100644 --- a/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/serverproxy/ProxyMonitorServiceHandlerMetricsTest.java +++ b/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/serverproxy/ProxyMonitorServiceHandlerMetricsTest.java @@ -48,7 +48,6 @@ import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.ProvideSystemProperty; -import org.junit.rules.ExpectedException; import org.niis.xroad.globalconf.GlobalConfProvider; import org.niis.xroad.monitor.common.Metrics; import org.niis.xroad.monitor.common.MetricsGroup; @@ -56,7 +55,6 @@ import org.niis.xroad.monitor.common.SystemMetricsResp; import org.niis.xroad.monitor.rpc.MonitorRpcClient; import org.niis.xroad.opmonitor.api.OpMonitoringData; -import org.niis.xroad.proxy.core.addon.proxymonitor.RestoreMonitorClientAfterTest; import org.niis.xroad.proxy.core.protocol.ProxyMessage; import org.niis.xroad.proxy.core.test.TestSuiteGlobalConf; import org.niis.xroad.proxy.core.test.TestSuiteServerConf; @@ -90,7 +88,6 @@ */ public class ProxyMonitorServiceHandlerMetricsTest { - private static final String EXPECTED_XR_INSTANCE = "EE"; private static final ClientId.Conf DEFAULT_OWNER_CLIENT = ClientId.Conf.create(EXPECTED_XR_INSTANCE, "GOV", "1234TEST_CLIENT"); @@ -104,9 +101,6 @@ public class ProxyMonitorServiceHandlerMetricsTest { private static Unmarshaller unmarshaller; private static MessageFactory messageFactory; - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Rule public final ProvideSystemProperty hibernatePropertiesProperty = new ProvideSystemProperty(SystemProperties.DATABASE_PROPERTIES, @@ -117,14 +111,13 @@ public class ProxyMonitorServiceHandlerMetricsTest { = new ProvideSystemProperty(SystemProperties.CONFIGURATION_PATH, "src/test/resources/"); - @Rule - public final RestoreMonitorClientAfterTest monitorClientRestoreRule = new RestoreMonitorClientAfterTest(); - private ServerConfProvider serverConfProvider; private GlobalConfProvider globalConfProvider; private RequestWrapper mockRequest; private ProxyMessage mockProxyMessage; + private final MonitorRpcClient mockMonitorClient = mock(MonitorRpcClient.class); + /** * Init class-wide test instances */ @@ -158,7 +151,8 @@ public SecurityServerId.Conf getIdentifier() { public void startHandingShouldProduceAllMetrics() throws Exception { // setup - ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider); + ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider, + mockMonitorClient); final SoapMessageImpl soapMessage = build(DEFAULT_OWNER_CLIENT, MONITOR_SERVICE_ID, "testUser", randomUUID().toString()); @@ -183,11 +177,8 @@ public void startHandingShouldProduceAllMetrics() throws Exception { .build()).build()) .build()).build(); - MonitorRpcClient mockMonitorClient = mock(MonitorRpcClient.class); when(mockMonitorClient.getMetrics(anyList(), anyBoolean())).thenReturn(resp.getMetrics()); - RestoreMonitorClientAfterTest.setMonitorClient(mockMonitorClient); - // execution handlerToTest.startHandling(mockRequest, mockProxyMessage, mock(HttpClient.class), mock(OpMonitoringData.class)); @@ -234,8 +225,8 @@ public void startHandingShouldProduceAllMetrics() throws Exception { public void startHandingShouldProduceRequestedMetrics() throws Exception { // setup - ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider); - + ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider, + mockMonitorClient); final SoapMessageImpl soapMessage = build( false, @@ -266,12 +257,9 @@ public void startHandingShouldProduceRequestedMetrics() throws Exception { .build()).build()) .build()).build(); - MonitorRpcClient mockMonitorClient = mock(MonitorRpcClient.class); when(mockMonitorClient.getMetrics(anyList(), anyBoolean())).thenReturn(resp.getMetrics()); - RestoreMonitorClientAfterTest.setMonitorClient(mockMonitorClient); - // execution handlerToTest.startHandling(mockRequest, mockProxyMessage, mock(HttpClient.class), mock(OpMonitoringData.class)); diff --git a/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/serverproxy/ProxyMonitorServiceHandlerTest.java b/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/serverproxy/ProxyMonitorServiceHandlerTest.java index e69bef9564..77dccfd069 100644 --- a/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/serverproxy/ProxyMonitorServiceHandlerTest.java +++ b/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/serverproxy/ProxyMonitorServiceHandlerTest.java @@ -32,28 +32,19 @@ import ee.ria.xroad.common.identifier.ClientId; import ee.ria.xroad.common.identifier.SecurityServerId; import ee.ria.xroad.common.identifier.ServiceId; -import ee.ria.xroad.common.message.SoapHeader; import ee.ria.xroad.common.message.SoapMessageImpl; -import ee.ria.xroad.common.metadata.ObjectFactory; import ee.ria.xroad.common.util.MimeTypes; -import jakarta.xml.bind.JAXBContext; -import jakarta.xml.bind.JAXBException; -import jakarta.xml.bind.Unmarshaller; -import jakarta.xml.soap.MessageFactory; -import jakarta.xml.soap.SOAPException; -import org.eclipse.jetty.server.Request; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.contrib.java.lang.system.ProvideSystemProperty; import org.junit.rules.ExpectedException; import org.niis.xroad.globalconf.GlobalConfProvider; +import org.niis.xroad.monitor.rpc.MonitorRpcClient; import org.niis.xroad.proxy.core.protocol.ProxyMessage; import org.niis.xroad.proxy.core.test.TestSuiteGlobalConf; import org.niis.xroad.proxy.core.test.TestSuiteServerConf; -import org.niis.xroad.proxymonitor.message.GetSecurityServerMetricsResponse; import org.niis.xroad.serverconf.ServerConfProvider; import java.io.IOException; @@ -80,9 +71,6 @@ public class ProxyMonitorServiceHandlerTest { private static final ServiceId.Conf MONITOR_SERVICE_ID = ServiceId.Conf.create(DEFAULT_OWNER_CLIENT, ProxyMonitorServiceHandlerImpl.SERVICE_CODE); - private static Unmarshaller unmarshaller; - private static MessageFactory messageFactory; - @Rule public ExpectedException thrown = ExpectedException.none(); @@ -98,19 +86,9 @@ public class ProxyMonitorServiceHandlerTest { private ServerConfProvider serverConfProvider; private GlobalConfProvider globalConfProvider; - private Request mockRequest; private ProxyMessage mockProxyMessage; - /** - * Init class-wide test instances - */ - @BeforeClass - public static void initCommon() throws JAXBException, SOAPException { - unmarshaller = JAXBContext.newInstance(ObjectFactory.class, SoapHeader.class, - GetSecurityServerMetricsResponse.class) - .createUnmarshaller(); - messageFactory = MessageFactory.newInstance(); - } + private final MonitorRpcClient mockMonitorClient = mock(MonitorRpcClient.class); /** * Init data for tests @@ -125,7 +103,6 @@ public SecurityServerId.Conf getIdentifier() { }; globalConfProvider = new TestSuiteGlobalConf(); - mockRequest = mock(Request.class); mockProxyMessage = mock(ProxyMessage.class); when(mockProxyMessage.getSoapContentType()).thenReturn(MimeTypes.TEXT_XML_UTF8); @@ -135,7 +112,8 @@ public SecurityServerId.Conf getIdentifier() { public void shouldNotBeAbleToHandleWithIncorrectServiceCode() { // setup - ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider); + ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider, + mockMonitorClient); final ServiceId.Conf requestedService = ServiceId.Conf.create(DEFAULT_OWNER_CLIENT, "theWrongService"); @@ -148,7 +126,8 @@ public void shouldNotBeAbleToHandleWithIncorrectServiceCode() { public void shouldBeAbleToHandleWithCorrectServiceCode() { // setup - ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider); + ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider, + mockMonitorClient); // execution & verification assertThat("Should be able to handle the right service", @@ -160,7 +139,8 @@ public void shouldNotNeedToVerifyWhenOwnerCalling() { // setup - ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider); + ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider, + mockMonitorClient); final SoapMessageImpl mockSoap = mock(SoapMessageImpl.class); when(mockSoap.getClient()).thenReturn(DEFAULT_OWNER_CLIENT); @@ -178,7 +158,8 @@ public void shouldNotNeedToVerifyWhenAllowedMonitoringClientCalling() { // setup - ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider); + ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider, + mockMonitorClient); // the allowed monitoring client from test resources monitoring metricNames final ClientId.Conf allowedClient = ClientId.Conf.create(EXPECTED_XR_INSTANCE, "BUSINESS", @@ -200,7 +181,8 @@ public void shouldThrowWhenAccessNotAllowed() { // setup - ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider); + ProxyMonitorServiceHandlerImpl handlerToTest = new ProxyMonitorServiceHandlerImpl(serverConfProvider, globalConfProvider, + mockMonitorClient); // the allowed monitoring client from test resources monitoring metricNames final ClientId.Conf nonAllowedClient = ClientId.Conf.create(EXPECTED_XR_INSTANCE, "COM", diff --git a/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/testcases/SecurityServerMetricsMessage.java b/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/testcases/SecurityServerMetricsMessage.java index 4692492f01..f5a151429d 100644 --- a/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/testcases/SecurityServerMetricsMessage.java +++ b/src/service/proxy/proxy-core/src/test/java/org/niis/xroad/proxy/core/addon/proxymonitor/testcases/SecurityServerMetricsMessage.java @@ -115,7 +115,7 @@ protected void validateNormalResponse(Message receivedResponse) assertThat("Wrong root name", rootSet.getName(), is(DEFAULT_OWNER_SERVER.toString())); assertThat("Wrong amount of received metrics", rootSet.getMetrics().size(), is(2)); - final MetricType proxyVersionMetric = rootSet.getMetrics().get(0); + final MetricType proxyVersionMetric = rootSet.getMetrics().getFirst(); assertThat("Missing proxy version from response", proxyVersionMetric.getName(), equalTo("proxyVersion")); diff --git a/src/service/proxy/proxy-core/src/testFixtures/java/org/niis/xroad/proxy/core/test/TestContext.java b/src/service/proxy/proxy-core/src/testFixtures/java/org/niis/xroad/proxy/core/test/TestContext.java index 18ae755870..c6ae533e98 100644 --- a/src/service/proxy/proxy-core/src/testFixtures/java/org/niis/xroad/proxy/core/test/TestContext.java +++ b/src/service/proxy/proxy-core/src/testFixtures/java/org/niis/xroad/proxy/core/test/TestContext.java @@ -31,6 +31,7 @@ import org.niis.xroad.common.properties.ConfigUtils; import org.niis.xroad.globalconf.impl.cert.CertHelper; import org.niis.xroad.keyconf.KeyConfProvider; +import org.niis.xroad.monitor.rpc.MonitorRpcClient; import org.niis.xroad.opmonitor.api.OpMonitorCommonProperties; import org.niis.xroad.proxy.core.ProxyProperties; import org.niis.xroad.proxy.core.addon.metaservice.clientproxy.MetadataHandler; @@ -69,6 +70,10 @@ public TestContext(ProxyProperties proxyProperties) { } public TestContext(ProxyProperties proxyProperties, boolean startServerProxy) { + this(proxyProperties, startServerProxy, mock(MonitorRpcClient.class)); + } + + public TestContext(ProxyProperties proxyProperties, boolean startServerProxy, MonitorRpcClient monitorRpcClient) { try { org.apache.xml.security.Init.init(); SigningCtxProvider signingCtxProvider = new TestSuiteSigningCtxProvider(globalConfProvider, keyConfProvider); @@ -93,7 +98,7 @@ public TestContext(ProxyProperties proxyProperties, boolean startServerProxy) { AntiDosConfiguration antiDosConfiguration = mock(AntiDosConfiguration.class); OpMonitorCommonProperties opMonitorCommonProperties = ConfigUtils.defaultConfiguration(OpMonitorCommonProperties.class); ServiceHandlerLoader serviceHandlerLoader = new ServiceHandlerLoader(serverConfProvider, globalConfProvider, - proxyProperties.addOn(), opMonitorCommonProperties); + monitorRpcClient, proxyProperties.addOn(), opMonitorCommonProperties); serverProxy = new ServerProxy(proxyProperties.server(), antiDosConfiguration, commonBeanProxy, serviceHandlerLoader, opMonitorCommonProperties); serverProxy.init();