Best Karate code snippet using ssl.TestService
Source:MicroServiceFullServiceTest.java  
1/*******************************************************************************2 * Copyright 2016-2017 ZTE, Inc. and others.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except5 * in compliance with the License. You may obtain a copy of the License at6 * 7 * http://www.apache.org/licenses/LICENSE-2.08 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express11 * or implied. See the License for the specific language governing permissions and limitations under12 * the License.13 ******************************************************************************/14package org.onap.msb.apiroute.wrapper.service;15import static org.junit.Assert.assertEquals;16import static org.junit.Assert.assertFalse;17import static org.junit.Assert.assertNull;18import static org.junit.Assert.assertTrue;19import static org.powermock.api.mockito.PowerMockito.when;20import java.lang.reflect.InvocationHandler;21import java.lang.reflect.Method;22import java.util.ArrayList;23import java.util.Collections;24import java.util.Comparator;25import java.util.HashSet;26import java.util.List;27import java.util.Set;28import org.junit.Before;29import org.junit.BeforeClass;30import org.junit.Test;31import org.junit.runner.RunWith;32import org.onap.msb.apiroute.api.MicroServiceFullInfo;33import org.onap.msb.apiroute.api.Node;34import org.onap.msb.apiroute.wrapper.dao.RedisAccessWrapper;35import org.onap.msb.apiroute.wrapper.util.JedisUtil;36import org.onap.msb.apiroute.wrapper.util.MicroServiceUtil;37import org.powermock.api.mockito.PowerMockito;38import org.powermock.core.classloader.annotations.PowerMockIgnore;39import org.powermock.core.classloader.annotations.PrepareForTest;40import org.powermock.modules.junit4.PowerMockRunner;41import com.fiftyonred.mock_jedis.MockJedisPool;42import redis.clients.jedis.JedisPool;43import redis.clients.jedis.JedisPoolConfig;44@RunWith(PowerMockRunner.class)45@PrepareForTest({JedisUtil.class, RedisAccessWrapper.class})46@PowerMockIgnore({"javax.management.*", "jdk.internal.reflect.*"})47public class MicroServiceFullServiceTest {48    private static MicroServiceFullService microServiceFullService = null;49    private static Comparator<MicroServiceFullInfo> serviceComparator = null;50    @BeforeClass51    public static void setUp() throws Exception {52        microServiceFullService = MicroServiceFullService.getInstance();53        serviceComparator = new Comparator<MicroServiceFullInfo>() {54            @Override55            public int compare(MicroServiceFullInfo o1, MicroServiceFullInfo o2) {56                if (!o1.getServiceName().equals(o2.getServiceName()))57                    return (o1.getServiceName()).compareTo(o2.getServiceName());58                if (!o1.getVersion().equals(o2.getVersion()))59                    return (o1.getVersion()).compareTo(o2.getVersion());60                return 0;61            }62        };63    }64    @Before65    public void setUpBeforeTest() throws Exception {66        final JedisPool mockJedisPool = new MockJedisPool(new JedisPoolConfig(), "localhost");67        PowerMockito.mockStatic(JedisUtil.class);68        JedisUtil jedisUtil = PowerMockito.mock(JedisUtil.class);69        when(jedisUtil.borrowJedisInstance()).thenReturn(mockJedisPool.getResource());70        PowerMockito.replace(PowerMockito.method(RedisAccessWrapper.class, "filterKeys")).with(new InvocationHandler() {71            @Override72            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {73                return mockJedisPool.getResource().keys((String) args[0]);74            }75        });76    }77    @Test78    public void testExistsMicroServiceInstance_notExist() {79        try {80            assertFalse(microServiceFullService.existsMicroServiceInstance("notExist", "v1"));81        } catch (Exception e) {82            assert false : "throw exception means error occured!" + e.getMessage();83        }84    }85    @Test86    public void testExistsMicroServiceInstance_Exist() {87        MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();88        microServiceFullInfo.setServiceName("testService");89        microServiceFullInfo.setVersion("v1");90        microServiceFullInfo.setStatus("1");91        microServiceFullInfo.setUrl("/testService/v1");92        microServiceFullInfo.setVisualRange("0");93        microServiceFullInfo.setProtocol("http");94        microServiceFullInfo.setEnable_ssl(false);95        Set<Node> nodeSet = new HashSet<>();96        nodeSet.add(new Node("10.74.148.88", "8080"));97        microServiceFullInfo.setNodes(nodeSet);98        try {99            assertFalse(microServiceFullService.existsMicroServiceInstance("testService", "v1"));100            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);101            assertTrue(microServiceFullService.existsMicroServiceInstance("testService", "v1"));102        } catch (Exception e) {103            assert false : "throw exception means error occured!" + e.getMessage();104        }105    }106    @Test107    public void testSaveMicroServiceInfo2Redis() {108        MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();109        microServiceFullInfo.setServiceName("testService");110        microServiceFullInfo.setVersion("v1");111        microServiceFullInfo.setStatus("1");112        microServiceFullInfo.setUrl("/testService/v1");113        microServiceFullInfo.setVisualRange("0");114        microServiceFullInfo.setProtocol("http");115        microServiceFullInfo.setEnable_ssl(false);116        Set<Node> nodeSet = new HashSet<>();117        nodeSet.add(new Node("10.74.148.88", "8080"));118        microServiceFullInfo.setNodes(nodeSet);119        try {120            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);121            MicroServiceFullInfo actual = microServiceFullService.getMicroServiceInstance("testService", "v1");122            assertEquals(microServiceFullInfo, actual);123        } catch (Exception e) {124            e.printStackTrace();125            assert false : "throw exception means error occured!" + e.getMessage();126        }127    }128    @Test129    public void testDeleteMicroService() {130        MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();131        microServiceFullInfo.setServiceName("testService");132        microServiceFullInfo.setVersion("v1");133        microServiceFullInfo.setStatus("1");134        microServiceFullInfo.setUrl("/testService/v1");135        microServiceFullInfo.setVisualRange("0");136        microServiceFullInfo.setProtocol("http");137        microServiceFullInfo.setEnable_ssl(false);138        Set<Node> nodeSet = new HashSet<>();139        nodeSet.add(new Node("10.74.148.88", "8080"));140        microServiceFullInfo.setNodes(nodeSet);141        try {142            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);143            assertTrue(microServiceFullService.existsMicroServiceInstance("testService", "v1"));144            microServiceFullService.deleteMicroService("testService", "v1");145            assertFalse(microServiceFullService.existsMicroServiceInstance("testService", "v1"));146        } catch (Exception e) {147            assert false : "throw exception means error occured!" + e.getMessage();148        }149    }150    @Test151    public void testUpdateMicroServiceStatus() {152        MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();153        microServiceFullInfo.setServiceName("testService");154        microServiceFullInfo.setVersion("v1");155        microServiceFullInfo.setStatus("1");156        microServiceFullInfo.setUrl("/testService/v1");157        microServiceFullInfo.setVisualRange("0");158        microServiceFullInfo.setProtocol("http");159        microServiceFullInfo.setEnable_ssl(false);160        Set<Node> nodeSet = new HashSet<>();161        nodeSet.add(new Node("10.74.148.88", "8080"));162        microServiceFullInfo.setNodes(nodeSet);163        try {164            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);165            assertEquals("1", microServiceFullService.getMicroServiceInstance("testService", "v1").getStatus());166            microServiceFullService.updateMicroServiceStatus("testService", "v1", "0");167            assertEquals("0", microServiceFullService.getMicroServiceInstance("testService", "v1").getStatus());168        } catch (Exception e) {169            assert false : "throw exception means error occured!" + e.getMessage();170        }171    }172    @Test173    public void testGetAllMicroServiceKey() {174        MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();175        microServiceFullInfo.setServiceName("testService");176        microServiceFullInfo.setVersion("v1");177        microServiceFullInfo.setStatus("1");178        microServiceFullInfo.setUrl("/testService/v1");179        microServiceFullInfo.setVisualRange("0");180        microServiceFullInfo.setProtocol("http");181        microServiceFullInfo.setEnable_ssl(false);182        Set<Node> nodeSet = new HashSet<>();183        nodeSet.add(new Node("10.74.148.88", "8080"));184        microServiceFullInfo.setNodes(nodeSet);185        MicroServiceFullInfo microServiceFullInfo2 = new MicroServiceFullInfo();186        microServiceFullInfo2.setServiceName("testService2");187        microServiceFullInfo2.setVersion("");188        microServiceFullInfo2.setStatus("1");189        microServiceFullInfo2.setUrl("/testService2");190        microServiceFullInfo2.setVisualRange("0");191        microServiceFullInfo2.setProtocol("http");192        microServiceFullInfo2.setEnable_ssl(false);193        Set<Node> nodeSet2 = new HashSet<>();194        nodeSet2.add(new Node("10.74.148.88", "8081"));195        microServiceFullInfo2.setNodes(nodeSet2);196        MicroServiceFullInfo microServiceFullInfo3 = new MicroServiceFullInfo();197        microServiceFullInfo3.setServiceName("testService");198        microServiceFullInfo3.setVersion("v2");199        microServiceFullInfo3.setStatus("1");200        microServiceFullInfo3.setUrl("/testService/v2");201        microServiceFullInfo3.setVisualRange("0");202        microServiceFullInfo3.setProtocol("http");203        microServiceFullInfo3.setEnable_ssl(false);204        Set<Node> nodeSet3 = new HashSet<>();205        nodeSet3.add(new Node("10.74.148.89", "8080"));206        microServiceFullInfo3.setNodes(nodeSet3);207        try {208            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);209            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo2);210            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo3);211            Set<String> result = microServiceFullService.getAllMicroServiceKey();212            final Set<String> expected = new HashSet<String>();213            expected.add("testService");214            expected.add("testService2");215            assertEquals(expected, result);216        } catch (Exception e) {217            assert false : "throw exception means error occured!" + e.getMessage();218        }219    }220    @Test221    public void testGetAllVersionsOfTheService() {222        MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();223        microServiceFullInfo.setServiceName("testService");224        microServiceFullInfo.setVersion("v1");225        microServiceFullInfo.setStatus("1");226        microServiceFullInfo.setUrl("/testService/v1");227        microServiceFullInfo.setVisualRange("0");228        microServiceFullInfo.setProtocol("http");229        microServiceFullInfo.setEnable_ssl(false);230        Set<Node> nodeSet = new HashSet<>();231        nodeSet.add(new Node("10.74.148.88", "8080"));232        microServiceFullInfo.setNodes(nodeSet);233        MicroServiceFullInfo microServiceFullInfo2 = new MicroServiceFullInfo();234        microServiceFullInfo2.setServiceName("testService2");235        microServiceFullInfo2.setVersion("");236        microServiceFullInfo2.setStatus("1");237        microServiceFullInfo2.setUrl("/testService2");238        microServiceFullInfo2.setVisualRange("0");239        microServiceFullInfo2.setProtocol("http");240        microServiceFullInfo2.setEnable_ssl(false);241        Set<Node> nodeSet2 = new HashSet<>();242        nodeSet2.add(new Node("10.74.148.88", "8081"));243        microServiceFullInfo2.setNodes(nodeSet2);244        MicroServiceFullInfo microServiceFullInfo3 = new MicroServiceFullInfo();245        microServiceFullInfo3.setServiceName("testService");246        microServiceFullInfo3.setVersion("v2");247        microServiceFullInfo3.setStatus("1");248        microServiceFullInfo3.setUrl("/testService/v2");249        microServiceFullInfo3.setVisualRange("0");250        microServiceFullInfo3.setProtocol("http");251        microServiceFullInfo3.setEnable_ssl(false);252        Set<Node> nodeSet3 = new HashSet<>();253        nodeSet3.add(new Node("10.74.148.89", "8080"));254        microServiceFullInfo3.setNodes(nodeSet3);255        try {256            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);257            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo3);258            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo2);259            List<MicroServiceFullInfo> result = microServiceFullService.getAllVersionsOfTheService("testService");260            List<MicroServiceFullInfo> expected = new ArrayList<>();261            expected.add(microServiceFullInfo);262            expected.add(microServiceFullInfo3);263            Collections.sort(expected, serviceComparator);264            Collections.sort(result, serviceComparator);265            assertEquals(expected, result);266        } catch (Exception e) {267            assert false : "throw exception means error occured!" + e.getMessage();268        }269    }270    @Test271    public void testGetAllMicroServicesInstances() {272        MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();273        microServiceFullInfo.setServiceName("testService");274        microServiceFullInfo.setVersion("v1");275        microServiceFullInfo.setStatus("1");276        microServiceFullInfo.setUrl("/testService/v1");277        microServiceFullInfo.setVisualRange("0");278        microServiceFullInfo.setProtocol("http");279        microServiceFullInfo.setEnable_ssl(false);280        Set<Node> nodeSet = new HashSet<>();281        nodeSet.add(new Node("10.74.148.88", "8080"));282        microServiceFullInfo.setNodes(nodeSet);283        MicroServiceFullInfo microServiceFullInfo2 = new MicroServiceFullInfo();284        microServiceFullInfo2.setServiceName("testService2");285        microServiceFullInfo2.setVersion("");286        microServiceFullInfo2.setStatus("1");287        microServiceFullInfo2.setUrl("/testService/v1");288        microServiceFullInfo2.setVisualRange("0");289        microServiceFullInfo2.setProtocol("http");290        microServiceFullInfo2.setEnable_ssl(true);291        Set<Node> nodeSet2 = new HashSet<>();292        nodeSet2.add(new Node("10.74.148.89", "8080"));293        microServiceFullInfo2.setNodes(nodeSet2);294        try {295            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);296            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo2);297            List<MicroServiceFullInfo> expected = new ArrayList<MicroServiceFullInfo>();298            expected.add(microServiceFullInfo);299            expected.add(microServiceFullInfo2);300            List<MicroServiceFullInfo> result = microServiceFullService.getAllMicroServiceInstances();301            Collections.sort(expected, serviceComparator);302            Collections.sort(result, serviceComparator);303            assertEquals(expected, result);304        } catch (Exception e) {305            e.printStackTrace();306            assert false : "throw exception means error occured!" + e.getMessage();307        }308    }309    @Test310    public void testDeleteMultiMicroService() {311        MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();312        microServiceFullInfo.setServiceName("testService");313        microServiceFullInfo.setVersion("v1");314        microServiceFullInfo.setStatus("1");315        microServiceFullInfo.setUrl("/testService/v1");316        microServiceFullInfo.setVisualRange("0");317        microServiceFullInfo.setProtocol("http");318        microServiceFullInfo.setEnable_ssl(false);319        Set<Node> nodeSet = new HashSet<>();320        nodeSet.add(new Node("10.74.148.88", "8080"));321        microServiceFullInfo.setNodes(nodeSet);322        MicroServiceFullInfo microServiceFullInfo3 = new MicroServiceFullInfo();323        microServiceFullInfo3.setServiceName("testService");324        microServiceFullInfo3.setVersion("v2");325        microServiceFullInfo3.setStatus("1");326        microServiceFullInfo3.setUrl("/testService/v2");327        microServiceFullInfo3.setVisualRange("0");328        microServiceFullInfo3.setProtocol("http");329        microServiceFullInfo3.setEnable_ssl(false);330        Set<Node> nodeSet3 = new HashSet<>();331        nodeSet3.add(new Node("10.74.148.89", "8080"));332        microServiceFullInfo3.setNodes(nodeSet3);333        try {334            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);335            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo3);336            // two versions of testservice exist337            assertEquals(2, microServiceFullService.getAllVersionsOfTheService("testService").size());338            // delete all versions of testservice339            long size = microServiceFullService340                            .deleteMultiMicroService(MicroServiceUtil.getPrefixedKey("testService", "*"));341            // after delete,no version exist342            assertEquals(0, microServiceFullService.getAllVersionsOfTheService("testService").size());343        } catch (Exception e) {344            assert false : "throw exception means error occured!" + e.getMessage();345        }346    }347    @Test348    public void tesGetMicroServiceInstance_notExist() {349        try {350            assertNull(microServiceFullService.getMicroServiceInstance("notExist", "v1"));351        } catch (Exception e) {352            assert false : "throw exception means error occured!" + e.getMessage();353        }354    }355    @Test356    public void tesExistsGetUpdateDeleteMicroServiceStatus_versionNull() {357        MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo();358        microServiceFullInfo.setServiceName("testService");359        microServiceFullInfo.setVersion("");360        microServiceFullInfo.setStatus("1");361        microServiceFullInfo.setUrl("/testService/v1");362        microServiceFullInfo.setVisualRange("0");363        microServiceFullInfo.setProtocol("http");364        microServiceFullInfo.setEnable_ssl(false);365        Set<Node> nodeSet = new HashSet<>();366        nodeSet.add(new Node("10.74.148.88", "8080"));367        microServiceFullInfo.setNodes(nodeSet);368        try {369            // test null370            assertFalse(microServiceFullService.existsMicroServiceInstance("testService", "null"));371            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);372            assertEquals("1", microServiceFullService.getMicroServiceInstance("testService", "null").getStatus());373            microServiceFullService.updateMicroServiceStatus("testService", "null", "0");374            assertEquals("0", microServiceFullService.getMicroServiceInstance("testService", "null").getStatus());375            microServiceFullService.deleteMicroService("testService", "null");376            assertFalse(microServiceFullService.existsMicroServiceInstance("testService", "null"));377            // test String "null"378            assertFalse(microServiceFullService.existsMicroServiceInstance("testService", null));379            microServiceFullService.saveMicroServiceInfo2Redis(microServiceFullInfo);380            assertEquals("1", microServiceFullService.getMicroServiceInstance("testService", null).getStatus());381            microServiceFullService.updateMicroServiceStatus("testService", null, "0");382            assertEquals("0", microServiceFullService.getMicroServiceInstance("testService", null).getStatus());383            microServiceFullService.deleteMicroService("testService", null);384            assertFalse(microServiceFullService.existsMicroServiceInstance("testService", null));385        } catch (Exception e) {386            assert false : "throw exception means error occured!" + e.getMessage();387        }388    }389    @Test(expected = Exception.class)390    public void tesSaveMicroService_null() throws Exception {391        microServiceFullService.saveMicroServiceInfo2Redis(null);392    }393}...Source:SslTest.java  
...15    static ConfigurableApplicationContext context;16    17    @BeforeClass18    public static void beforeClass() {19        context = TestService.start();      20    }21    22    @Test23    public void testParallel() {24        int port = TestService.getPort(context);25        Results results = Runner.path("classpath:ssl")26                .karateEnv("mock") // skip callSingle, note that the karate-config.js copied from demo may be present27                .systemProperty("jersey.ssl.port", port + "")28                .parallel(1);29        assertTrue(results.getErrorMessages(), results.getFailCount() == 0);30    }    31    @AfterClass32    public static void afterClass() {33        TestService.stop(context);34    }35    36}...TestService
Using AI Code Generation
1import ssl.TestService;2public class 4 {3    public static void main(String[] args) {4        TestService obj = new TestService();5        obj.print();6    }7}8package ssl;9public class TestService {10    public void print() {11        System.out.println("Hello World");12    }13}TestService
Using AI Code Generation
1import ssl.TestService;2{3public static void main(String args[])4{5TestService t=new TestService();6t.show();7}8}9package ssl;10{11public void show()12{13System.out.println("Hello");14}15}16import ssl.TestService;TestService
Using AI Code Generation
1import ssl.TestService;2{3public static void main(String[] args)4{5TestService ts=new TestService();6ts.show();7}8}9package ssl;10{11public void show()12{13System.out.println("Hello World");14}15}TestService
Using AI Code Generation
1import ssl.TestService;2{3public static void main(String[] args)4{5TestService ts = new TestService();6ts.display();7}8}9The display() method of TestService classTestService
Using AI Code Generation
1import ssl.TestService;2{3public static void main(String[] args)4{5TestService service = new TestService();6String message = service.getMessage();7System.out.println(message);8}9}TestService
Using AI Code Generation
1import ssl.TestService;2public class TestServiceClient {3    public static void main(String[] args) {4        TestService service = new TestService();5        System.out.println(service.sayHello("World"));6    }7}8javac -classpath .;ssl.jar TestServiceClient.java9java -classpath .;ssl.jar TestServiceClientTestService
Using AI Code Generation
1import ssl.TestService;2class TestClient {3public static void main(String args[]) {4TestService service = new TestService();5System.out.println(service.sayHello());6}7}Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
