class TestIntegrationUserController(testtools.TestCase):

    def setUp(self):
        super(TestIntegrationUserController, self).setUp()
        self.project_id = "484d3a7eeb4f4462b329c1d0463cf324"
        configure_env(self.project_id)
        self.controller = ControllerResource('projects')

    def test_index(self):
        result = self.controller.index()
        self.assertIsNotNone(result)

    # def test_show(self):
    #     result = self.controller.index()
    #     self.assertIsNotNone(result)

    def test_create_delete(self):
        list1 = self.controller.index()
        parameter = [{'name':'user_integration_11', 'email':'*****@*****.**'}]
        result = self.controller.create(parameter)
        self.assertIsNotNone(result)
        self.assertIsNotNone(result[0])
        result_OK = result[0]
        self.assertIsInstance(result_OK, list)
        self.assertEqual(result_OK.__len__(), parameter.__len__())
        list2 = self.controller.index()
        self.assertEqual(list1.__len__() + parameter.__len__(), list2.__len__())
        #delete
        self.controller.delete([{'id':result_OK[0]['id']}])
        list3 = self.controller.index()
        self.assertEqual(list1.__len__(), list3.__len__())

    def test_bunch_create_delete(self):
        list1 = self.controller.index()
        parameter = [{'name':'user_integration_1', 'email':'*****@*****.**'},
                     {'name':'user_integration_2', 'email':'*****@*****.**'}
                     ]
        result = self.controller.create(parameter)
        self.assertIsNotNone(result)
        self.assertIsNotNone(result[0])
        result_OK = result[0]
        self.assertIsInstance(result_OK, list)
        self.assertEqual(result_OK.__len__(), parameter.__len__())
        list2 = self.controller.index()
        self.assertEqual(list1.__len__() + parameter.__len__(), list2.__len__())
        #delete
        param_delete = []
        for item in result_OK:
            param_delete.append({'id':item['id']})
        self.controller.delete(param_delete)
        list3 = self.controller.index()
        self.assertEqual(list1.__len__(), list3.__len__())

    def test_bunch_create_wrong_id(self):
        wrong_id= [{"id":"8903489034890234"}]
        result = self.controller.delete(wrong_id)
        self.assertIsNotNone(result)
class TestCaseAPIController(testtools.TestCase):

    def setUp(self):
        super(TestCaseAPIController, self).setUp()
        os.environ.data['OS_AUTH_URL'] = '127.0.0.23'
        os.environ.data['OS_PORT'] = '5000'
        os.environ.data['OS_VERSION'] = 'v3'
        os.environ.data['OS_TOKEN'] = 'token'
        self.controller = ControllerResource(mock.MagicMock())


    @mock.patch.object(OpenStackDriver, "index")
    def test_index(self, m_index):
        result = self.controller.index(None)
        self.assertIsNotNone(result)

    @mock.patch.object(OpenStackDriver, "create")
    def test_create(self, m_create):
        result = self.controller.create(None)
        self.assertIsNotNone(result)

    @mock.patch.object(OpenStackDriver, "delete")
    def test_create(self, m_create):
        parameters =[{'id':'78934'}]
        result = self.controller.delete(parameters)
        self.assertIsNotNone(result)
class TestIntegrationProjectController(testtools.TestCase):

    def setUp(self):
        super(TestIntegrationProjectController, self).setUp()
        self.project_id = "484d3a7eeb4f4462b329c1d0463cf324"
        configure_env(self.project_id)
        self.controller = ControllerResource('projects')

    def test_index(self):
        result = self.controller.index()
        self.assertIsNotNone(result)

    # def test_show(self):
    #     result = self.controller.index()
    #     self.assertIsNotNone(result)

    def test_create_delete(self):
        list1 = self.controller.index()
        parameter = [{'name':'project_test4', 'description':' integration project test'}]
        result = self.controller.create(parameter)
        self.assertIsNotNone(result)
        self.assertIsNotNone(result[0])
        result_OK = result[0]
        self.assertIsInstance(result_OK, list)
        self.assertEqual(result_OK.__len__(), parameter.__len__())
        list2 = self.controller.index()
        self.assertEqual(list1.__len__() + parameter.__len__(), list2.__len__())
        #delete
        self.controller.delete([{'id':result_OK[0]['id']}])
        list3 = self.controller.index()
        self.assertEqual(list1.__len__(), list3.__len__())

    def test_bunch_create_delete(self):
        list1 = self.controller.index()
        parameter = [{'name':'project_test111', 'description':' integration project test 1'},
                     {'name':'project_test222', 'description':' integration project test 2'}
                     ]
        result = self.controller.create(parameter)
        self.assertIsNotNone(result)
        self.assertIsNotNone(result[0])
        result_OK = result[0]
        self.assertIsInstance(result_OK, list)
        self.assertEqual(result_OK.__len__(), parameter.__len__())
        list2 = self.controller.index()
        self.assertEqual(list1.__len__() + parameter.__len__(), list2.__len__())
        #delete
        param_delete = []
        for item in result_OK:
            param_delete.append({'id':item['id']})
        self.controller.delete(param_delete)
        list3 = self.controller.index()
        self.assertEqual(list1.__len__(), list3.__len__())

    def test_bunch_create_wrong_id(self):
        wrong_id= [{"id":"8903489034890234"}]
        result = self.controller.delete(wrong_id)
        self.assertIsNotNone(result)

    # def test_bunch_create_ok(self):
    #     wrong_id= [{"id":"bdcdf27857b04883970ae31cf0c00031"}]
    #     result = self.controller.delete(wrong_id)
    #     self.assertIsNotNone(result)

# class TestIntegrationNetwork(base.TestController):
#
#     def setUp(self):
#         super(TestIntegrationNetwork, self).setUp()
#         self.controller = network.Controller(None, "/v2.0", "127.0.0.1")
#         self.project_id = "86bf9730b23d4817b431f4c34cc9cc8e"
#         self.public_network = "cd58eade-79a1-4633-8fb7-c7d8a030c942"
#         self.new_network_name = "networkOCCINET"
#         self.req = KeySession().create_request_conection("admin", "stack1", self.project_id)
#
#     def test_list(self):
#         list = self.controller.index(self.req, None)
#         self.assertIsInstance(list.resources[0], network_extend.Network)
#         sortedList = sorted(list.resources, key=lambda Network: Network.title, reverse=True)
#         self.assertEqual("public", sortedList[0].title)
#
#     def test_list_by_tenant(self):
#         tenant_id = self.req.environ["HTTP_X_PROJECT_ID"]
#         list = self.controller.index(self.req, {"attributes": {"project": self.project_id}})
#         sortedList = sorted(list.resources, key=lambda Network: Network.title, reverse=True)
#         self.assertIsInstance(sortedList[0], network_extend.Network)
#         self.assertEqual("public", sortedList[0].title)
#
#     def test_list_by_tenant_error(self):
#         list = self.controller.index(self.req, {"attributes": {"project": "noexits"}})
#         self.assertIs(0, list.resources.__len__())
#
#     def test_show_network(self):
#         net = self.controller.show(self.req, self.public_network)
#         self.assertEqual("public", net.title)
#
#     def test_run_up_network(self):
#         body = None
#         out = None
#         try:
#             net = self.controller.run_action(self.req,self.public_network,body)
#         except Exception as e:
#             out = e
#         self.assertIsInstance(out, exception.NotFound)
#
#     def test_create_delete_network(self):
#         list1 = self.controller.index(self.req, None)
#         #Create
#         net = self.controller.create(self.req, {"attributes": {"occi.core.title": self.new_network_name,"project": self.project_id}})
#         self.assertEqual(self.new_network_name, net.title)
#         list2 = self.controller.index(self.req, None)
#         self.assertEqual(list1.resources.__len__() + 1, list2.resources.__len__())
#
#         # Delete
#         response = self.controller.delete(self.req, {"attributes":{"occi.core.id": net.id}})
#         self.assertIsInstance(response, list)
#         list3 = self.controller.index(self.req, None)
#         self.assertEqual(list1.resources.__len__(), list3.resources.__len__())
#
#     def test_create_delete_network_with_subnet(self):
#         list1 = self.controller.index(self.req, None)
#         ip_version = 4
#         cidr = "11.0.0.1/24"
#         gateway = "11.0.0.3"
#         #Create
#         param = {
#                 "attributes":
#                             {"occi.core.title": self.new_network_name,
#                              "occi.network.ip_version": ip_version,
#                              "occi.networkinterface.address": cidr,
#                              "occi.networkinterface.gateway": gateway,
#                              "project": self.project_id
#                              }
#                 }
#         net = self.controller.create(self.req, param)
#         self.assertEqual(self.new_network_name, net.title)
#         list2 = self.controller.index(self.req, None)
#         self.assertEqual(list1.resources.__len__() + 1, list2.resources.__len__())
#
#          # Delete
#         response = self.controller.delete(self.req, {"attributes":{"occi.core.id": net.id}})
#         self.assertIsInstance(response, list)
#         list3 = self.controller.index(self.req, None)
#         self.assertEqual(list1.resources.__len__(), list3.resources.__len__())
#
#
# """
#     def test_delete_network(self):
#         response = self.controller.delete(self.req, self.new_network_id)
#
#         self.assertEqual(204, response.status_code)
# """