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

from karbor import exception
from karbor.services.operationengine import operation_manager
from karbor.tests import base


class OperationManagerTestCase(base.TestCase):
    """Test cases for OperationManager class."""

    def setUp(self):
        super(OperationManagerTestCase, self).setUp()
        self.om = operation_manager.OperationManager()

    def test_singleton_operation_manager(self):
        second = operation_manager.OperationManager()
        self.assertTrue(self.om == second)

    def test_load_all_class(self):
        self.assertIn("protect", self.om._operation_cls_map)

    def test_invalid_operation_type(self):
        bingo = 0
        try:
            self.om.check_operation_definition("123", {})
        except exception.InvalidInput as ex:
            bingo = 1 if ex.msg.find("operation type") >= 0 else 0

        self.assertEqual(1, bingo)

    def test_invalid_operation_definition(self):
        self.assertRaises(exception.InvalidOperationDefinition,
                          self.om.check_operation_definition,
                          "protect", {})

@@ -566,16 +566,29 @@
 cense.%0A%0A
+import mock%0A%0A
 from kar
@@ -686,199 +686,786 @@
 bor.
-tests import base%0A%0A%0Aclass OperationManagerTestCase(base.TestCase):%0A    %22%22%22Test cases for OperationManager class.%22%22%22%0A%0A    def setUp(self):%0A        super(OperationManagerTestCase, self).setUp()
+services.operationengine import operations%0Afrom karbor.tests import base%0A%0A%0Aclass FakeOperation(operations.base.Operation):%0A    OPERATION_TYPE = 'fake'%0A%0A    @classmethod%0A    def check_operation_definition(cls, operation_definition):%0A        pass%0A%0A    @classmethod%0A    def _execute(cls, operation_definition, param):%0A        pass%0A%0A    @classmethod%0A    def _resume(cls, operation_definition, param, log_ref):%0A        pass%0A%0A%0Aclass OperationManagerTestCase(base.TestCase):%0A    %22%22%22Test cases for OperationManager class.%22%22%22%0A%0A    def setUp(self):%0A        super(OperationManagerTestCase, self).setUp()%0A%0A        mock_obj = mock.Mock()%0A        mock_obj.return_value = %5BFakeOperation%5D%0A        operations.all_operations = mock_obj%0A%0A        self._operation_type = FakeOperation.OPERATION_TYPE
 %0A   
@@ -1720,25 +1720,36 @@
 ssertIn(
-%22protect%22
+self._operation_type
 , self.o
@@ -1779,219 +1779,370 @@
     
-def test_invalid_operation_type(self):%0A        bingo = 0%0A        try:%0A            self.om.check_operation_definition(%22123%22, %7B%7D)%0A        except exception.InvalidInput as ex:%0A            bingo = 1 if ex.msg.find(%22
+@mock.patch.object(FakeOperation, 'check_operation_definition')%0A    def test_check_operation_definition(self, check):%0A        self.om.check_operation_definition(self._operation_type, %7B%7D)%0A        check.assert_called_once_with(%7B%7D)%0A%0A    @mock.patch.object(operations.base.Operation, 'run')%0A    def test_run_operation(self, run):%0A        self.om.run_operation(self._
 oper
@@ -2150,28 +2150,18 @@
 tion
- 
+_
 type
-%22) %3E= 0 else 0%0A
+, %7B%7D)
 %0A   
@@ -2169,33 +2169,38 @@
     
-self
+run
 .assert
-Equal(1, bingo
+_called_once_with(%7B%7D
 )%0A%0A 
@@ -2225,34 +2225,28 @@
 d_operation_
-definition
+type
 (self):%0A    
@@ -2266,16 +2266,21 @@
 rtRaises
+Regex
 (excepti
@@ -2293,29 +2293,78 @@
 alid
-OperationDefinition,%0A
+Input,%0A                               'Invalid operation type:',%0A     
     
@@ -2451,16 +2451,227 @@
     
-%22protect
+     %22123%22, %7B%7D)%0A%0A        self.assertRaisesRegex(exception.InvalidInput,%0A                               'Invalid operation type:',%0A                               self.om.run_operation,%0A                               %22123
 %22, %7B

Update test cases for operation manager