GN-87 Sonarwork
diff --git a/src/main/java/org/eclipse/openk/core/controller/BackendController.java b/src/main/java/org/eclipse/openk/core/controller/CentralController.java similarity index 92% rename from src/main/java/org/eclipse/openk/core/controller/BackendController.java rename to src/main/java/org/eclipse/openk/core/controller/CentralController.java index 5b55040..7a48009 100644 --- a/src/main/java/org/eclipse/openk/core/controller/BackendController.java +++ b/src/main/java/org/eclipse/openk/core/controller/CentralController.java
@@ -25,8 +25,8 @@ import java.util.Optional; import java.util.stream.Collectors; -public class BackendController { - private static final Logger logger = Logger.getLogger(BackendController.class); +public class CentralController { + private static final Logger logger = Logger.getLogger(CentralController.class); public ServiceDistributionCluster readServerDistribution(String cluster) throws HttpStatusException { ServiceDistributionCluster[] dcs = ServicesConfigCache.getInstance().getCache(); @@ -46,10 +46,15 @@ return vi; } + + protected RestServiceWrapper createRestServiceWrapper( boolean isHttps ) { + return new RestServiceWrapper(isHttps); + } + public String getServiceHealthState(String clustername, String servicename) throws HttpStatusException { ServiceDistributionCluster.ServiceDistribution dist = findDistribution(clustername, servicename); - RestServiceWrapper w = new RestServiceWrapper(false); + RestServiceWrapper w = createRestServiceWrapper(false ); StringBuilder url = new StringBuilder(); url.append(dist.getProtocol() + "://" + dist.getHost() + ":" + dist.getPortHealth());
diff --git a/src/main/java/org/eclipse/openk/resources/MicsCentralResource.java b/src/main/java/org/eclipse/openk/resources/MicsCentralResource.java index b1134b6..61e62a1 100644 --- a/src/main/java/org/eclipse/openk/resources/MicsCentralResource.java +++ b/src/main/java/org/eclipse/openk/resources/MicsCentralResource.java
@@ -24,7 +24,7 @@ import org.eclipse.openk.core.controller.DispatchController; import org.hibernate.validator.constraints.NotEmpty; import org.eclipse.openk.api.ServiceRequestEnvelope; -import org.eclipse.openk.core.controller.BackendController; +import org.eclipse.openk.core.controller.CentralController; import org.eclipse.openk.core.controller.BaseWebService; import org.eclipse.openk.core.exceptions.HttpStatusException; @@ -55,7 +55,7 @@ @Path("/serviceDistribution/{clustername}") public Response getServiceDistribution(@ApiParam(name = "clustername", value = "The name of the cluster") @PathParam("clustername") @NotEmpty String clustername) { - return invokeRunnable(() -> new BackendController().readServerDistribution(clustername)); + return invokeRunnable(() -> new CentralController().readServerDistribution(clustername)); } @ApiOperation(value = "Health state check", notes = "This service checks if the components of a service are healthy") @@ -66,7 +66,7 @@ @PathParam("clustername") @NotEmpty String clustername, @ApiParam(name = "servicename", value = "The name of the service") @PathParam("servicename") @NotEmpty String servicename) { - return invokeRunnable(() -> new BackendController().getServiceHealthState(clustername, servicename)); + return invokeRunnable(() -> new CentralController().getServiceHealthState(clustername, servicename)); } @ApiOperation(value = "Dispatcher", notes = "") @@ -91,6 +91,6 @@ @GET @Path("/versionInfo") public Response getVersionInfo() { - return invokeRunnable(() -> new BackendController().getVersionInfo(getVersionString())); + return invokeRunnable(() -> new CentralController().getVersionInfo(getVersionString())); } }
diff --git a/src/test/java/org/eclipse/openk/core/controller/BackendControllerTest.java b/src/test/java/org/eclipse/openk/core/controller/BackendControllerTest.java deleted file mode 100644 index e76e762..0000000 --- a/src/test/java/org/eclipse/openk/core/controller/BackendControllerTest.java +++ /dev/null
@@ -1,81 +0,0 @@ -/** -****************************************************************************** -* Copyright © 2018 PTA GmbH. -* All rights reserved. This program and the accompanying materials -* are made available under the terms of the Eclipse Public License v1.0 -* which accompanies this distribution, and is available at -* -* http://www.eclipse.org/legal/epl-v10.html -* -****************************************************************************** -*/ - -package org.eclipse.openk.core.controller; - -import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.openk.core.common.util.ResourceLoaderBase; -import org.junit.Test; -import org.powermock.reflect.Whitebox; -import org.eclipse.openk.api.ServiceDistributionCluster; -import org.eclipse.openk.core.exceptions.HttpStatusException; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - - -public class BackendControllerTest { - - private ServiceDistributionCluster baseTestReadServerDistribution(String clustername, String jsonFile) throws Exception { - ResourceLoaderBase resourceLoaderBase = new ResourceLoaderBase(); - String json = resourceLoaderBase.loadStringFromResource(jsonFile); - - ServicesConfigCache scc = ServicesConfigCache.getInstance(); - ServiceDistributionCluster[] sdc = (ServiceDistributionCluster[]) - Whitebox.invokeMethod(scc,"readServerDistributionFromText", json); - - Whitebox.setInternalState(scc, "cache", sdc); - - BackendController be = new BackendController(); - - ServiceDistributionCluster ret = be.readServerDistribution(clustername); - - return ret; - } - - @Test - public void testReadServerDistribution() throws Exception { - - ServiceDistributionCluster sdc = baseTestReadServerDistribution("elogbook.openK", "testServiceDistributions.json"); - - assertEquals("elogbook.openK", sdc.getClustername()); - assertEquals(1, sdc.getDistributions().length); - assertEquals("172.18.22.160", sdc.getDistributions()[0].getHost()); - } - - @Test - public void testFailReadServerDistribution() throws Exception { - try { - baseTestReadServerDistribution("doesntMatter", "testServiceDist_False.json"); - } catch( HttpStatusException e ) { - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, e.getHttpStatus()); - } - } - - @Test - public void testReadServerDistribution_notfound() throws Exception { - try { - ServiceDistributionCluster sdc = baseTestReadServerDistribution("invalidCluster", "testServiceDistributions.json"); - } catch( HttpStatusException e ) { - assertEquals(HttpStatus.NOT_FOUND_404, e.getHttpStatus()); - } - } - - @Test - public void testReadServerDistribution_activeInactive() throws Exception { - ServiceDistributionCluster sdc = baseTestReadServerDistribution( "elogbook.openk", "testServiceDistributionsTwo_OneIsInactive.json"); - - assertEquals( 1, sdc.getDistributions().length); - assertTrue( sdc.getDistributions()[0].isActive()); - assertEquals( "172.18.22.160", sdc.getDistributions()[0].getHost()); - } -}
diff --git a/src/test/java/org/eclipse/openk/core/controller/CentralControllerTest.java b/src/test/java/org/eclipse/openk/core/controller/CentralControllerTest.java new file mode 100644 index 0000000..7a8f2d1 --- /dev/null +++ b/src/test/java/org/eclipse/openk/core/controller/CentralControllerTest.java
@@ -0,0 +1,150 @@ +/** +****************************************************************************** +* Copyright © 2018 PTA GmbH. +* All rights reserved. This program and the accompanying materials +* are made available under the terms of the Eclipse Public License v1.0 +* which accompanies this distribution, and is available at +* +* http://www.eclipse.org/legal/epl-v10.html +* +****************************************************************************** +*/ + +package org.eclipse.openk.core.controller; + +import org.easymock.EasyMock; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.openk.api.VersionInfo; +import org.eclipse.openk.core.common.util.ResourceLoaderBase; +import org.eclipse.openk.core.communication.RestServiceWrapper; +import org.junit.Test; +import org.powermock.reflect.Whitebox; +import org.eclipse.openk.api.ServiceDistributionCluster; +import org.eclipse.openk.core.exceptions.HttpStatusException; + +import static org.easymock.EasyMock.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + + +public class CentralControllerTest { + + private ServiceDistributionCluster baseTestReadServerDistribution(String clustername, String jsonFile) throws Exception { + ResourceLoaderBase resourceLoaderBase = new ResourceLoaderBase(); + String json = resourceLoaderBase.loadStringFromResource(jsonFile); + + ServicesConfigCache scc = ServicesConfigCache.getInstance(); + ServiceDistributionCluster[] sdc = (ServiceDistributionCluster[]) + Whitebox.invokeMethod(scc,"readServerDistributionFromText", json); + + Whitebox.setInternalState(scc, "cache", sdc); + + CentralController be = new CentralController(); + + ServiceDistributionCluster ret = be.readServerDistribution(clustername); + + return ret; + } + + @Test + public void testReadServerDistribution() throws Exception { + + ServiceDistributionCluster sdc = baseTestReadServerDistribution("elogbook.openK", "testServiceDistributions.json"); + + assertEquals("elogbook.openK", sdc.getClustername()); + assertEquals(2, sdc.getDistributions().length); + assertEquals("172.18.22.160", sdc.getDistributions()[0].getHost()); + } + + @Test + public void testFailReadServerDistribution() throws Exception { + try { + baseTestReadServerDistribution("doesntMatter", "testServiceDist_False.json"); + } catch( HttpStatusException e ) { + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, e.getHttpStatus()); + } + } + + @Test + public void testReadServerDistribution_notfound() throws Exception { + try { + ServiceDistributionCluster sdc = baseTestReadServerDistribution("invalidCluster", "testServiceDistributions.json"); + } catch( HttpStatusException e ) { + assertEquals(HttpStatus.NOT_FOUND_404, e.getHttpStatus()); + } + } + + @Test + public void testReadServerDistribution_activeInactive() throws Exception { + ServiceDistributionCluster sdc = baseTestReadServerDistribution( "elogbook.openk", "testServiceDistributionsTwo_OneIsInactive.json"); + + assertEquals( 1, sdc.getDistributions().length); + assertTrue( sdc.getDistributions()[0].isActive()); + assertEquals( "172.18.22.160", sdc.getDistributions()[0].getHost()); + } + + @Test (expected = HttpStatusException.class) + public void testFindDistribution_notFound() throws Exception { + ServiceDistributionCluster sdc = baseTestReadServerDistribution("elogbook.openK", "testServiceDistributions.json"); + CentralController cc = new CentralController(); + try { + Whitebox.invokeMethod(cc, "findDistribution", "elogbook.openK", "testService"); + } + catch( HttpStatusException hse ) { + assertEquals(HttpStatus.NOT_FOUND_404, hse.getHttpStatus()); + throw hse; + } + } + + @Test + public void testFindDistribution_ok() throws Exception { + ServiceDistributionCluster sdc = baseTestReadServerDistribution("elogbook.openK", "testServiceDistributions.json"); + CentralController cc = new CentralController(); + + Whitebox.invokeMethod(cc, "findDistribution", "elogbook.openK", "auth-n-auth.mics"); + + } + + + @Test (expected = HttpStatusException.class) + public void testFindDistribution_clusterNotFound() throws Exception { + ServiceDistributionCluster sdc = baseTestReadServerDistribution("elogbook.openK", "testServiceDistributions.json"); + CentralController cc = new CentralController(); + try { + Whitebox.invokeMethod(cc, "findDistribution", "invalidCluster", "auth-n-auth.mics"); + } + catch( HttpStatusException hse ) { + assertEquals(HttpStatus.NOT_FOUND_404, hse.getHttpStatus()); + throw hse; + } + } + + @Test + public void testGetVersionInfo() throws Exception { + CentralController cc = new CentralController(); + assertEquals("Bruno2.0", cc.getVersionInfo("Bruno2.0").getBackendVersion()); + assertTrue(Whitebox.invokeMethod(cc, "createRestServiceWrapper", true) instanceof RestServiceWrapper ); + } + + + @Test + public void testHealthCheck() throws Exception { + ServiceDistributionCluster sdc = baseTestReadServerDistribution("elogbook.openK", "testServiceDistributions.json"); + CentralController cc = new CentralController() { + @Override + protected RestServiceWrapper createRestServiceWrapper( boolean isHttps ) throws HttpStatusException { + RestServiceWrapper wrapper = EasyMock.createMock( RestServiceWrapper.class ); + expect( wrapper.performGetRequest( anyObject()) ).andReturn("Ist OK!").anyTimes(); + replay( wrapper ); + verify( wrapper ); + return wrapper; + } + + }; + + + assertEquals("Ist OK!", cc.getServiceHealthState("elogbook.openK", "auth-n-auth.mics")); + assertEquals("Ist OK!", cc.getServiceHealthState("elogbook.openK", "homie")); + + } +}
diff --git a/src/test/java/org/eclipse/openk/core/controller/ServicesConfigCacheTest.java b/src/test/java/org/eclipse/openk/core/controller/ServicesConfigCacheTest.java index d1b5e25..e699fca 100644 --- a/src/test/java/org/eclipse/openk/core/controller/ServicesConfigCacheTest.java +++ b/src/test/java/org/eclipse/openk/core/controller/ServicesConfigCacheTest.java
@@ -38,7 +38,7 @@ ServiceDistributionCluster[] sdc = baseTestReadServerDistribution("testServiceDistributions.json"); assertEquals("elogbook.openK", sdc[0].getClustername()); - assertEquals(1, sdc[0].getDistributions().length); + assertEquals(2, sdc[0].getDistributions().length); assertEquals("172.18.22.160", sdc[0].getDistributions()[0].getHost()); }
diff --git a/src/test/resources/testServiceDistributions.json b/src/test/resources/testServiceDistributions.json index 8049751..0563fc1 100644 --- a/src/test/resources/testServiceDistributions.json +++ b/src/test/resources/testServiceDistributions.json
@@ -14,7 +14,18 @@ "portApp": "9002", "portHealth": "9003", "description": "Authentication Service" - } + }, + { + "active": "true", + "name": "homie", + "protocol": "http", + "host": "172.18.22.160", + "urlPath": "/homie", + "healthUrlPath": "/portal/rest/beservice", + "portApp": "8080", + "portHealth": "8080", + "description": "Authentication Service" + } ] } ] \ No newline at end of file