# 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.

import json
import random
import subprocess
import unittest

import mock
from swift.common.swob import Request

from swifthlm import middleware as swifthlm


class FakeApp(object):
    def __init__(self, headers=None):
        if headers:
            self.headers = headers
        else:
            self.headers = {}

    def __call__(self, env, start_response):
        start_response('200 OK', self.headers)

        path = env.get('PATH_INFO')
        if path == '/v1/AUTH_.auth/account1/.services':
            return json.dumps({'storage': {
                'cluster_name': 'http://localhost/v1/AUTH_123'}})
        return []


class TestSwiftHLM(unittest.TestCase):

    def test_migrate(self):
        app = swifthlm.HlmMiddleware(FakeApp(), {})

        subprocess.call = mock.Mock()
        random.choice = mock.Mock(return_value='0')
        environ = {'REQUEST_METHOD': 'POST'}
        req = Request.blank('/v1/a/c?MIGRATE', environ=environ)
        resp = req.get_response(app)

        subprocess.call.assert_called_with(
            ['/opt/ibm/swift-hlm-backend/migrate', 'a/c', '000000000000'])
        self.assertEquals(resp.status_int, 200)
        self.assertEquals(resp.body, 'Accepted migration request.\n')

    def test_recall(self):
        app = swifthlm.HlmMiddleware(FakeApp(), {})

        subprocess.call = mock.Mock()
        random.choice = mock.Mock(return_value='0')
        environ = {'REQUEST_METHOD': 'POST'}
        req = Request.blank('/v1/a/c?RECALL', environ=environ)
        resp = req.get_response(app)

        subprocess.call.assert_called_with(
            ['/opt/ibm/swift-hlm-backend/recall', 'a/c', '000000000000'])
        self.assertEquals(resp.status_int, 200)
        self.assertEquals(resp.body, 'Accepted recall request.\n')

    def test_get_status(self):
        app = swifthlm.HlmMiddleware(FakeApp(), {})

        subprocess.check_output = mock.Mock(return_value='status output')
        random.choice = mock.Mock(return_value='0')
        req = Request.blank('/v1/a/c?STATUS')
        resp = req.get_response(app)

        subprocess.check_output.assert_called_with(
            ['/opt/ibm/swift-hlm-backend/status', 'a/c', '000000000000'])
        self.assertEquals(resp.status_int, 200)
        self.assertEquals(resp.body, 'status output')


if __name__ == '__main__':
    unittest.main()

@@ -953,211 +953,8 @@
 ers)
-%0A%0A        path = env.get('PATH_INFO')%0A        if path == '/v1/AUTH_.auth/account1/.services':%0A            return json.dumps(%7B'storage': %7B%0A                'cluster_name': 'http://localhost/v1/AUTH_123'%7D%7D)
 %0A   
@@ -1022,20 +1022,13 @@
 def 
-test_migrate
+setUp
 (sel
@@ -1031,32 +1031,37 @@
 (self):%0A        
+self.
 app = swifthlm.H
@@ -1081,32 +1081,60 @@
 FakeApp(), %7B%7D)%0A%0A
+    def test_migrate(self):%0A
         subproce
@@ -1340,32 +1340,37 @@
 eq.get_response(
+self.
 app)%0A%0A        su
@@ -1628,61 +1628,8 @@
 f):%0A
-        app = swifthlm.HlmMiddleware(FakeApp(), %7B%7D)%0A%0A
     
@@ -1846,32 +1846,37 @@
 eq.get_response(
+self.
 app)%0A%0A        su
@@ -2134,61 +2134,8 @@
 f):%0A
-        app = swifthlm.HlmMiddleware(FakeApp(), %7B%7D)%0A%0A
     
@@ -2334,16 +2334,21 @@
 esponse(
+self.
 app)%0A%0A  

Remove unused code, introduce SetUp to remove code duplicates