Implement kubevirt loadbalancer service with unit tests

Change-Id: I1c29e75aa5196b497f8f12f1d182788e3d173244
(cherry picked from commit 870abf8c7a7904c5953e20807ee7c681d8b5cd0b)
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtLoadBalancerCodecTest.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtLoadBalancerCodecTest.java
new file mode 100644
index 0000000..7aed6f8
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtLoadBalancerCodecTest.java
@@ -0,0 +1,177 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.kubevirtnetworking.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableSet;
+import org.hamcrest.MatcherAssert;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnetworking.api.DefaultKubevirtLoadBalancer;
+import org.onosproject.kubevirtnetworking.api.DefaultKubevirtLoadBalancerRule;
+import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer;
+import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerRule;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.kubevirtnetworking.codec.KubevirtLoadBalancerJsonMatcher.matchesKubevirtLoadBalancer;
+import static org.onosproject.net.NetTestTools.APP_ID;
+
+/**
+ * Unit tests for KubevirtLoadBalancer codec.
+ */
+public final class KubevirtLoadBalancerCodecTest {
+
+    MockCodecContext context;
+
+    JsonCodec<KubevirtLoadBalancer> kubevirtLoadBalancerCodec;
+    JsonCodec<KubevirtLoadBalancerRule> kubevirtLoadBalancerRuleCodec;
+
+    private static final KubevirtLoadBalancerRule RULE1 = DefaultKubevirtLoadBalancerRule.builder()
+            .protocol("tcp")
+            .portRangeMax(8000)
+            .portRangeMin(7000)
+            .build();
+    private static final KubevirtLoadBalancerRule RULE2 = DefaultKubevirtLoadBalancerRule.builder()
+            .protocol("udp")
+            .portRangeMax(9000)
+            .portRangeMin(8000)
+            .build();
+
+    final CoreService mockCoreService = createMock(CoreService.class);
+    private static final String REST_APP_ID = "org.onosproject.rest";
+
+    @Before
+    public void setUp() {
+        context = new MockCodecContext();
+        kubevirtLoadBalancerCodec = new KubevirtLoadBalancerCodec();
+        kubevirtLoadBalancerRuleCodec = new KubevirtLoadBalancerRuleCodec();
+
+        assertThat(kubevirtLoadBalancerCodec, notNullValue());
+        assertThat(kubevirtLoadBalancerRuleCodec, notNullValue());
+
+        expect(mockCoreService.registerApplication(REST_APP_ID))
+                .andReturn(APP_ID).anyTimes();
+        replay(mockCoreService);
+        context.registerService(CoreService.class, mockCoreService);
+    }
+
+    /**
+     * Tests the kubevirt load balancer encoding.
+     */
+    @Test
+    public void testKubevirtLoadBalancerEncode() {
+        KubevirtLoadBalancer lb = DefaultKubevirtLoadBalancer.builder()
+                .name("lb-1")
+                .networkId("net-1")
+                .vip(IpAddress.valueOf("10.10.10.10"))
+                .members(ImmutableSet.of(IpAddress.valueOf("10.10.10.11"),
+                        IpAddress.valueOf("10.10.10.12")))
+                .rules(ImmutableSet.of(RULE1, RULE2))
+                .description("network load balancer")
+                .build();
+
+        ObjectNode lbJson = kubevirtLoadBalancerCodec.encode(lb, context);
+        assertThat(lbJson, matchesKubevirtLoadBalancer(lb));
+    }
+
+    /**
+     * Tests the kubevirt load balancer decoding.
+     */
+    @Test
+    public void testKubevirtLoadBalancerDecode() throws IOException {
+        KubevirtLoadBalancer lb = getKubevirtLoadBalancer("KubevirtLoadBalancer.json");
+
+        assertThat(lb.name(), is("lb-1"));
+        assertThat(lb.description(), is("Example Load Balancer"));
+        assertThat(lb.networkId(), is("net-1"));
+        assertThat(lb.vip(), is(IpAddress.valueOf("10.10.10.10")));
+
+        Set<IpAddress> expectedMembers = ImmutableSet.of(IpAddress.valueOf("10.10.10.11"),
+                IpAddress.valueOf("10.10.10.12"));
+        Set<IpAddress> realMembers = lb.members();
+        assertThat(true, is(expectedMembers.containsAll(realMembers)));
+        assertThat(true, is(realMembers.containsAll(expectedMembers)));
+
+        Set<KubevirtLoadBalancerRule> expectedRules = ImmutableSet.of(RULE1, RULE2);
+        Set<KubevirtLoadBalancerRule> realRules = lb.rules();
+        assertThat(true, is(expectedRules.containsAll(realRules)));
+        assertThat(true, is(realRules.containsAll(expectedRules)));
+    }
+
+    private KubevirtLoadBalancer getKubevirtLoadBalancer(String resourceName) throws IOException {
+        InputStream jsonStream = KubevirtLoadBalancerCodecTest.class.getResourceAsStream(resourceName);
+        JsonNode json = context.mapper().readTree(jsonStream);
+        MatcherAssert.assertThat(json, notNullValue());
+        KubevirtLoadBalancer lb = kubevirtLoadBalancerCodec.decode((ObjectNode) json, context);
+        assertThat(lb, notNullValue());
+        return lb;
+    }
+
+    private class MockCodecContext implements CodecContext {
+
+        private final ObjectMapper mapper = new ObjectMapper();
+        private final CodecManager manager = new CodecManager();
+        private final Map<Class<?>, Object> services = new HashMap<>();
+
+        /**
+         * Constructs a new mock codec context.
+         */
+        public MockCodecContext() {
+            manager.activate();
+        }
+
+        @Override
+        public ObjectMapper mapper() {
+            return mapper;
+        }
+
+        @Override
+        public <T> JsonCodec<T> codec(Class<T> entityClass) {
+            if (entityClass == KubevirtLoadBalancerRule.class) {
+                return (JsonCodec<T>) kubevirtLoadBalancerRuleCodec;
+            }
+            return manager.getCodec(entityClass);
+        }
+
+        @Override
+        public <T> T getService(Class<T> serviceClass) {
+            return (T) services.get(serviceClass);
+        }
+
+        // for registering mock services
+        public <T> void registerService(Class<T> serviceClass, T impl) {
+            services.put(serviceClass, impl);
+        }
+    }
+}
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtLoadBalancerJsonMatcher.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtLoadBalancerJsonMatcher.java
new file mode 100644
index 0000000..9122bd0
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtLoadBalancerJsonMatcher.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.kubevirtnetworking.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onlab.packet.IpAddress;
+import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer;
+import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerRule;
+
+/**
+ * Hamcrest matcher for load balancer.
+ */
+public final class KubevirtLoadBalancerJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+    private static final String NAME = "name";
+    private static final String DESCRIPTION = "description";
+    private static final String VIP = "vip";
+    private static final String NETWORK_ID = "networkId";
+    private static final String MEMBERS = "members";
+    private static final String RULES = "rules";
+
+    private final KubevirtLoadBalancer lb;
+
+    private KubevirtLoadBalancerJsonMatcher(KubevirtLoadBalancer lb) {
+        this.lb = lb;
+    }
+
+    @Override
+    protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+        // check name
+        String jsonName = jsonNode.get(NAME).asText();
+        String name = lb.name();
+        if (!jsonName.equals(name)) {
+            description.appendText("Name was " + jsonName);
+            return false;
+        }
+
+        // check description
+        JsonNode jsonDescription = jsonNode.get(DESCRIPTION);
+        if (jsonDescription != null) {
+            String myDescription = lb.description();
+            if (!jsonDescription.asText().equals(myDescription)) {
+                description.appendText("Description was " + jsonDescription);
+                return false;
+            }
+        }
+
+        // check VIP
+        String jsonVip = jsonNode.get(VIP).asText();
+        String vip = lb.vip().toString();
+        if (!jsonVip.equals(vip)) {
+            description.appendText("VIP was " + jsonVip);
+            return false;
+        }
+
+        // check network ID
+        String jsonNetworkId = jsonNode.get(NETWORK_ID).asText();
+        String networkId = lb.networkId();
+        if (!jsonNetworkId.equals(networkId)) {
+            description.appendText("NetworkId was " + jsonNetworkId);
+            return false;
+        }
+
+        // check members
+        ArrayNode jsonMembers = (ArrayNode) jsonNode.get(MEMBERS);
+        if (jsonMembers != null) {
+            // check size of members array
+            if (jsonMembers.size() != lb.members().size()) {
+                description.appendText("Members was " + jsonMembers.size());
+                return false;
+            }
+
+            for (JsonNode jsonMember : jsonMembers) {
+                IpAddress member = IpAddress.valueOf(jsonMember.asText());
+                if (!lb.members().contains(member)) {
+                    description.appendText("Member not found " + jsonMember.toString());
+                    return false;
+                }
+            }
+        }
+
+        ArrayNode jsonRules = (ArrayNode) jsonNode.get(RULES);
+        if (jsonRules != null) {
+            // check size of rules array
+            if (jsonRules.size() != lb.rules().size()) {
+                description.appendText("Rules was " + jsonRules.size());
+                return false;
+            }
+
+            // check rules
+            for (KubevirtLoadBalancerRule rule : lb.rules()) {
+                boolean ruleFound = false;
+                for (int ruleIndex = 0; ruleIndex < jsonRules.size(); ruleIndex++) {
+                    KubevirtLoadBalancerRuleJsonMatcher ruleMatcher =
+                            KubevirtLoadBalancerRuleJsonMatcher
+                                    .matchesKubevirtLoadBalancerRule(rule);
+                    if (ruleMatcher.matches(jsonRules.get(ruleIndex))) {
+                        ruleFound = true;
+                        break;
+                    }
+                }
+
+                if (!ruleFound) {
+                    description.appendText("Rule not found " + rule.toString());
+                    return false;
+                }
+            }
+        }
+
+        return true;
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(lb.toString());
+    }
+
+    /**
+     * Factory to allocate a kubevirt load balancer matcher.
+     *
+     * @param lb kubevirt load balancer object we are looking for
+     * @return matcher
+     */
+    public static KubevirtLoadBalancerJsonMatcher
+        matchesKubevirtLoadBalancer(KubevirtLoadBalancer lb) {
+        return new KubevirtLoadBalancerJsonMatcher(lb);
+    }
+}
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtLoadBalancerRuleJsonMatcher.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtLoadBalancerRuleJsonMatcher.java
new file mode 100644
index 0000000..404d1ee
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtLoadBalancerRuleJsonMatcher.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.kubevirtnetworking.codec;
+
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerRule;
+
+/**
+ * Hamcrest matcher for kubevirt load balancer.
+ */
+public final class KubevirtLoadBalancerRuleJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+    private final KubevirtLoadBalancerRule rule;
+
+    private static final String PROTOCOL = "protocol";
+    private static final String PORT_RANGE_MAX = "portRangeMax";
+    private static final String PORT_RANGE_MIN = "portRangeMin";
+
+    private KubevirtLoadBalancerRuleJsonMatcher(KubevirtLoadBalancerRule rule) {
+        this.rule = rule;
+    }
+
+    @Override
+    protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+        // check protocol
+        JsonNode jsonProtocol = jsonNode.get(PROTOCOL);
+        if (jsonProtocol != null) {
+            String protocol = rule.protocol();
+            if (!jsonProtocol.asText().equals(protocol)) {
+                description.appendText("Protocol was " + jsonProtocol);
+                return false;
+            }
+        }
+
+        // check port range max
+        JsonNode jsonPortRangeMax = jsonNode.get(PORT_RANGE_MAX);
+        if (jsonPortRangeMax != null) {
+            int portRangeMax = rule.portRangeMax();
+            if (portRangeMax != jsonPortRangeMax.asInt()) {
+                description.appendText("PortRangeMax was " + jsonPortRangeMax);
+                return false;
+            }
+        }
+
+        // check port range min
+        JsonNode jsonPortRangeMin = jsonNode.get(PORT_RANGE_MIN);
+        if (jsonPortRangeMin != null) {
+            int portRangeMin = rule.portRangeMin();
+            if (portRangeMin != jsonPortRangeMin.asInt()) {
+                description.appendText("PortRangeMin was " + jsonPortRangeMin);
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(rule.toString());
+    }
+
+    /**
+     * Factory to allocate an kubevirt load balancer rule matcher.
+     *
+     * @param rule kubevirt load balancer rule object we are looking for
+     * @return matcher
+     */
+    public static KubevirtLoadBalancerRuleJsonMatcher
+        matchesKubevirtLoadBalancerRule(KubevirtLoadBalancerRule rule) {
+        return new KubevirtLoadBalancerRuleJsonMatcher(rule);
+    }
+}
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtLoadBalancerManagerTest.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtLoadBalancerManagerTest.java
new file mode 100644
index 0000000..73e1c35
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/impl/KubevirtLoadBalancerManagerTest.java
@@ -0,0 +1,229 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.kubevirtnetworking.impl;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.MoreExecutors;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onlab.packet.IpAddress;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.core.DefaultApplicationId;
+import org.onosproject.event.Event;
+import org.onosproject.kubevirtnetworking.api.DefaultKubevirtLoadBalancer;
+import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer;
+import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerEvent;
+import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerListener;
+import org.onosproject.store.service.TestStorageService;
+
+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.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerEvent.Type.KUBEVIRT_LOAD_BALANCER_CREATED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerEvent.Type.KUBEVIRT_LOAD_BALANCER_REMOVED;
+import static org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerEvent.Type.KUBEVIRT_LOAD_BALANCER_UPDATED;
+
+/**
+ * Unit tests for kubernetes load balancer manager.
+ */
+public class KubevirtLoadBalancerManagerTest {
+
+    private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
+
+    private static final String LB_NAME = "lb-1";
+    private static final String NETWORK_NAME = "vxlan-1";
+    private static final String UPDATED_DESCRIPTION = "lb-updated";
+    private static final String UNKNOWN_ID = "unknown";
+
+    private static final KubevirtLoadBalancer LB = DefaultKubevirtLoadBalancer.builder()
+            .name(LB_NAME)
+            .description(LB_NAME)
+            .networkId(NETWORK_NAME)
+            .vip(IpAddress.valueOf("10.10.10.10"))
+            .members(ImmutableSet.of())
+            .rules(ImmutableSet.of())
+            .build();
+
+    private static final KubevirtLoadBalancer LB_UPDATED = DefaultKubevirtLoadBalancer.builder()
+            .name(LB_NAME)
+            .description(UPDATED_DESCRIPTION)
+            .networkId(NETWORK_NAME)
+            .vip(IpAddress.valueOf("10.10.10.10"))
+            .members(ImmutableSet.of())
+            .rules(ImmutableSet.of())
+            .build();
+
+    private static final KubevirtLoadBalancer LB_WITH_MEMBERS = DefaultKubevirtLoadBalancer.builder()
+            .name(LB_NAME)
+            .description(LB_NAME)
+            .networkId(NETWORK_NAME)
+            .vip(IpAddress.valueOf("10.10.10.10"))
+            .members(ImmutableSet.of(IpAddress.valueOf("10.10.10.11"),
+                    IpAddress.valueOf("10.10.10.12")))
+            .rules(ImmutableSet.of())
+            .build();
+
+    private final TestKubevirtLoadBalancerListener testListener = new TestKubevirtLoadBalancerListener();
+
+    private KubevirtLoadBalancerManager target;
+    private DistributedKubevirtLoadBalancerStore lbStore;
+
+    @Before
+    public void setUp() throws Exception {
+        lbStore = new DistributedKubevirtLoadBalancerStore();
+        TestUtils.setField(lbStore, "coreService", new TestCoreService());
+        TestUtils.setField(lbStore, "storageService", new TestStorageService());
+        TestUtils.setField(lbStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
+        lbStore.activate();
+
+        target = new KubevirtLoadBalancerManager();
+        TestUtils.setField(target, "coreService", new TestCoreService());
+        target.kubevirtLoadBalancerStore = lbStore;
+        target.addListener(testListener);
+        target.activate();
+    }
+
+    @After
+    public void tearDown() {
+        target.removeListener(testListener);
+        lbStore.deactivate();
+        target.deactivate();
+        lbStore = null;
+        target = null;
+    }
+
+    /**
+     * Tests if getting all load balancers returns the correct set of load balancers.
+     */
+    @Test
+    public void testGetLoadBalancers() {
+        createBasicLoadBalancers();
+        assertEquals("Number of load balancers did not match", 1, target.loadBalancers().size());
+    }
+
+    /**
+     * Tests if getting a load balancer with ID returns the correct load balancer.
+     */
+    @Test
+    public void testGetLoadBalancerByName() {
+        createBasicLoadBalancers();
+        assertNotNull("Load balancer did not match", target.loadBalancer(LB_NAME));
+        assertNull("Load balancer did not match", target.loadBalancer(UNKNOWN_ID));
+    }
+
+    /**
+     * Tests creating and removing a load balancer, and checks if it triggers proper events.
+     */
+    @Test
+    public void testCreateAndRemoveLoadBalancer() {
+        target.createLoadBalancer(LB);
+        assertEquals("Number of load balancers did not match", 1, target.loadBalancers().size());
+        assertNotNull("Load balancer was not created", target.loadBalancer(LB_NAME));
+
+        target.removeLoadBalancer(LB_NAME);
+        assertEquals("Number of load balancers did not match", 0, target.loadBalancers().size());
+        assertNull("Load balancer was not removed", target.loadBalancer(LB_NAME));
+
+        validateEvents(KUBEVIRT_LOAD_BALANCER_CREATED, KUBEVIRT_LOAD_BALANCER_REMOVED);
+    }
+
+    /**
+     * Tests updating a load balancer, and checks if it triggers proper events.
+     */
+    @Test
+    public void testCreateAndUpdateLoadBalancer() {
+        target.createLoadBalancer(LB);
+        assertEquals("Number of load balancers did not match", 1, target.loadBalancers().size());
+        assertEquals("Load balancer did not match", LB_NAME, target.loadBalancer(LB_NAME).name());
+
+        target.updateLoadBalancer(LB_UPDATED);
+        assertEquals("Number of load balancers did not match", 1, target.loadBalancers().size());
+        assertEquals("Load balancer did not match", LB_NAME, target.loadBalancer(LB_NAME).name());
+
+        validateEvents(KUBEVIRT_LOAD_BALANCER_CREATED, KUBEVIRT_LOAD_BALANCER_UPDATED);
+    }
+
+    /**
+     * Tests if creating a null load balancer fails with an exception.
+     */
+    @Test(expected = NullPointerException.class)
+    public void testCreateNullLoadBalancer() {
+        target.createLoadBalancer(null);
+    }
+
+    /**
+     * Tests if creating a duplicate load balancer fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateDuplicateLoadBalancer() {
+        target.createLoadBalancer(LB);
+        target.createLoadBalancer(LB);
+    }
+
+    /**
+     * Tests if removing load balancer with null ID fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testRemoveLoadBalancerWithNull() {
+        target.removeLoadBalancer(null);
+    }
+
+    /**
+     * Tests if updating an unregistered load balancer fails with an exception.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateUnregisteredLoadBalancer() {
+        target.updateLoadBalancer(LB);
+    }
+
+    private void createBasicLoadBalancers() {
+        target.createLoadBalancer(LB);
+    }
+
+    private static class TestCoreService extends CoreServiceAdapter {
+
+        @Override
+        public ApplicationId registerApplication(String name) {
+            return TEST_APP_ID;
+        }
+    }
+
+    private static class TestKubevirtLoadBalancerListener implements KubevirtLoadBalancerListener {
+
+        private List<KubevirtLoadBalancerEvent> events = Lists.newArrayList();
+
+        @Override
+        public void event(KubevirtLoadBalancerEvent event) {
+            events.add(event);
+        }
+    }
+
+    private void validateEvents(Enum... types) {
+        int i = 0;
+        assertEquals("Number of events did not match", types.length, testListener.events.size());
+        for (Event event : testListener.events) {
+            assertEquals("Incorrect event received", types[i], event.type());
+            i++;
+        }
+        testListener.events.clear();
+    }
+}