/*
 * Copyright (c) 2017, WSO2 Inc. (http://wso2.com) All Rights Reserved.
 *
 * 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.wso2.carbon.multitenancy.deployment.service.tests;

import org.wso2.carbon.multitenancy.deployment.service.interfaces.DeploymentProvider;
import org.wso2.carbon.multitenancy.deployment.service.models.Deployment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Mock deployment provider simulates deployment provider functionality for implementing tests.
 */
public class MockDeploymentProvider implements DeploymentProvider {

    private Map<String, Map<String, Deployment>> deploymentMap = new HashMap<>();

    @Override
    public List<Deployment> listDeployments(String namespace) {
        if (deploymentMap.get(namespace) == null) {
            return new ArrayList<>();
        }
        return new ArrayList<>(deploymentMap.get(namespace).values());
    }

    @Override
    public Deployment getDeployment(String namespace, String id) {
        if (deploymentMap.get(namespace) == null) {
            return null;
        } else if (id == null || id.isEmpty()) {
            throw new IllegalArgumentException();
        }

        return deploymentMap.get(namespace).get(id);
    }

    @Override
    public void deploy(String namespace, Deployment deployment) {
        if (namespace == null || namespace.isEmpty() || deployment == null) {
            throw new IllegalArgumentException();
        }

        Map<String, Deployment> deployments = deploymentMap.get(namespace);
        if (deployments == null) {
            deployments = new HashMap<>();
            deploymentMap.put(namespace, deployments);
        }
        deployments.put(deployment.getId(), deployment);
    }

    @Override
    public void undeploy(String namespace, Deployment deployment) {
        if (namespace == null || namespace.isEmpty() || deployment == null) {
            throw new IllegalArgumentException();
        }

        Map<String, Deployment> deployments = deploymentMap.get(namespace);
        if (deployments == null) {
            throw new RuntimeException("Deployment [" + deployment + "] not found in namespace " + namespace);
        }

        if (deploymentMap.get(deployment.getId()) != null) {
            deploymentMap.remove(deployment.getId());
        }
    }
}

@@ -1174,17 +1174,26 @@
 yment%3E%3E 
-d
+namespaceD
 eploymen
@@ -1193,16 +1193,17 @@
 ployment
+s
 Map = ne
@@ -1300,33 +1300,42 @@
 ) %7B%0A        if (
-d
+namespaceD
 eploymentMap.get
@@ -1319,32 +1319,33 @@
 espaceDeployment
+s
 Map.get(namespac
@@ -1437,17 +1437,26 @@
 yList%3C%3E(
-d
+namespaceD
 eploymen
@@ -1448,32 +1448,33 @@
 espaceDeployment
+s
 Map.get(namespac
@@ -1579,33 +1579,42 @@
 ) %7B%0A        if (
-d
+namespaceD
 eploymentMap.get
@@ -1598,32 +1598,33 @@
 espaceDeployment
+s
 Map.get(namespac
@@ -1787,17 +1787,26 @@
  return 
-d
+namespaceD
 eploymen
@@ -1798,32 +1798,33 @@
 espaceDeployment
+s
 Map.get(namespac
@@ -2096,36 +2096,48 @@
 ent%3E deployments
- = d
+Map = namespaceD
 eploymentMap.get
@@ -2121,32 +2121,33 @@
 espaceDeployment
+s
 Map.get(namespac
@@ -2165,32 +2165,35 @@
  if (deployments
+Map
  == null) %7B%0A    
@@ -2211,16 +2211,19 @@
 loyments
+Map
  = new H
@@ -2238,33 +2238,42 @@
 ();%0A            
-d
+namespaceD
 eploymentMap.put
@@ -2261,24 +2261,25 @@
 ceDeployment
+s
 Map.put(name
@@ -2296,16 +2296,19 @@
 loyments
+Map
 );%0A     
@@ -2331,16 +2331,19 @@
 loyments
+Map
 .put(dep
@@ -2647,12 +2647,24 @@
 ents
- = d
+Map = namespaceD
 eplo
@@ -2660,32 +2660,33 @@
 espaceDeployment
+s
 Map.get(namespac
@@ -2712,16 +2712,19 @@
 loyments
+Map
  == null
@@ -2867,24 +2867,25 @@
  (deployment
+s
 Map.get(depl
@@ -2933,16 +2933,17 @@
 ployment
+s
 Map.remo

Fix undeployment logic in mock deployment provider