| /** |
| ****************************************************************************** |
| * 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()); |
| } |
| } |