[CORD-2080] Adding 'tosca_key' support in xproto

Change-Id: Ib01c1cdcb81a51819c9e08703e6c76a4ffa09f17
diff --git a/test/test_grpc_models_accessor.py b/test/test_grpc_models_accessor.py
index 3851857..729fac3 100644
--- a/test/test_grpc_models_accessor.py
+++ b/test/test_grpc_models_accessor.py
@@ -18,6 +18,7 @@
 from mock import patch, MagicMock
 from grpc_client.models_accessor import GRPCModelsAccessor
 from grpc_client.resources import RESOURCES
+from grpc_client.KEYS import TOSCA_KEYS
 
 class FakeObj:
     new = None
@@ -34,15 +35,26 @@
 
 mock_resources = {
     'username~pass': {
-        'test-model': FakeResource
+        'test-model': FakeResource,
+        'single-key': FakeResource,
+        'double-key': FakeResource
     }
 }
 
+mock_keys = {
+    'i-do-not-exists': ['name'],
+    'test-model': ['name'],
+    'empty-key': [],
+    'single-key': ['fake_key'],
+    'double-key': ['key_1', 'key_2'],
+}
+
 USERNAME = 'username'
 PASSWORD = 'pass'
 
 class GRPCModelsAccessor_Create_or_update_Test(unittest.TestCase):
 
+    @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
     def test_unkown_user(self):
         """
         [GRPCModelsAccessor] get_model_from_classname: If a user does not have orm classes, raise
@@ -55,6 +67,7 @@
         self.assertEqual(e.exception.message, "[XOS-TOSCA] User 'username' does not have ready resources")
 
     @patch.dict(RESOURCES, mock_resources, clear=True)
+    @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
     def test_unkown_module(self):
         """
         [GRPCModelsAccessor] get_model_from_classname: If a model is not know by the grpc api, raise
@@ -64,35 +77,92 @@
         }
         with self.assertRaises(Exception) as e:
             GRPCModelsAccessor.get_model_from_classname('i-do-not-exists', data, USERNAME, PASSWORD)
-        self.assertEqual(e.exception.message, "[XOS-TOSCA] The model you are trying to create (name: test, class: i-do-not-exists) is not know by xos-core")
+        self.assertEqual(e.exception.message, "[XOS-TOSCA] The model you are trying to create (class: i-do-not-exists, properties, {'name': 'test'}) is not know by xos-core")
+
+    def test_unkown_tosca_key(self):
+        """
+        [GRPCModelsAccessor] get_model_from_classname: If a model does not have a tosca_key, raise
+        """
+        data = {
+            "name": "test"
+        }
+        with self.assertRaises(Exception) as e:
+            GRPCModelsAccessor.get_model_from_classname('no-key', data, USERNAME, PASSWORD)
+        self.assertEqual(e.exception.message, "[XOS-TOSCA] Model no-key doesn't have a tosca_key specified")
+
+    @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
+    def test_empty_tosca_key(self):
+        """
+        [GRPCModelsAccessor] get_model_from_classname: If a model does not have a tosca_key, raise
+        """
+        data = {
+            "name": "test"
+        }
+        with self.assertRaises(Exception) as e:
+            GRPCModelsAccessor.get_model_from_classname('empty-key', data, USERNAME, PASSWORD)
+        self.assertEqual(e.exception.message, "[XOS-TOSCA] Model empty-key doesn't have a tosca_key specified")
+
+    @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
+    def test_tosca_key_are_defined(self):
+        """
+        [GRPCModelsAccessor] get_model_from_classname: a model should have a property for it's tosca_key
+        """
+        data = {
+            "name": "test",
+        }
+        with self.assertRaises(Exception) as e:
+            GRPCModelsAccessor.get_model_from_classname('single-key', data, USERNAME, PASSWORD)
+        self.assertEqual(e.exception.message, "[XOS-TOSCA] Model single-key doesn't have a property for the specified tosca_key ('fake_key')")
 
     @patch.object(FakeResource.objects, "filter")
     @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel))
+    @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
+    def test_composite_key(self, mock_filter):
+        """
+        [GRPCModelsAccessor] get_model_from_classname: should use a composite key to lookup a model
+        """
+        data = {
+            "name": "test",
+            "key_1": "key1",
+            "key_2": "key2"
+        }
+        with patch.dict(RESOURCES, mock_resources, clear=True):
+            model = GRPCModelsAccessor.get_model_from_classname('double-key', data, USERNAME, PASSWORD)
+            mock_filter.assert_called_with(key_1="key1", key_2="key2")
+            self.assertEqual(model, FakeModel)
+
+    @patch.object(FakeResource.objects, "filter")
+    @patch.object(FakeResource.objects, "new", MagicMock(return_value=FakeModel))
+    @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
     def test_new_model(self, mock_filter):
         """
         [GRPCModelsAccessor] get_model_from_classname: should create a new model
         """
         data = {
-            "name": "test"
+            "name": "test",
+            "fake_key": "key"
         }
         with patch.dict(RESOURCES, mock_resources, clear=True):
-            model = GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
-            mock_filter.assert_called_with(name="test")
+            model = GRPCModelsAccessor.get_model_from_classname('single-key', data, USERNAME, PASSWORD)
+            mock_filter.assert_called_with(fake_key="key")
             self.assertEqual(model, FakeModel)
 
     @patch.object(FakeResource.objects, "filter", MagicMock(return_value=[FakeExistingModel]))
+    @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
     def test_existing_model(self):
         """
         [GRPCModelsAccessor] get_model_from_classname: should update an existing model
         """
         data = {
-            "name": "test"
+            "name": "test",
+            "fake_key": "key"
         }
         with patch.dict(RESOURCES, mock_resources, clear=True):
-            model = GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
+            model = GRPCModelsAccessor.get_model_from_classname('single-key', data, USERNAME, PASSWORD)
             self.assertEqual(model, FakeExistingModel)
 
     @patch.object(FakeResource.objects, "filter", MagicMock(return_value=['a', 'b']))
+    @patch.dict(TOSCA_KEYS, mock_keys, clear=True)
     def test_multiple_models(self):
         """
         [GRPCModelsAccessor] get_model_from_classname: should raise an exception if multiple instances are found
@@ -103,33 +173,7 @@
         with patch.dict(RESOURCES, mock_resources, clear=True):
             with self.assertRaises(Exception) as e:
                 GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
-            self.assertEqual(e.exception.message, "[XOS-Tosca] Model test has multiple instances, I can't handle it")
-
-    @patch.dict(RESOURCES, mock_resources, clear=True)
-    @patch.object(FakeResource.objects, "filter")
-    @patch.object(FakeResource.objects, "new")
-    def test_find_model_without_name_property(self, mock_new, mock_filter):
-        """
-        [GRPCModelsAccessor] get_model_from_classname: should lookup a model by the first property
-        """
-        data = {
-            'foo': 'bar',
-            'something': 'else'
-        }
-        GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
-        mock_filter.assert_called_with(foo="bar")
-        mock_new.assert_called()
-
-    @patch.dict(RESOURCES, mock_resources, clear=True)
-    @patch.object(FakeResource.objects, "new")
-    def test_model_without_properties(self, mock_new):
-        """
-        [GRPCModelsAccessor] get_model_from_classname: should create a new model if not properties are specified
-        """
-        data = {
-        }
-        GRPCModelsAccessor.get_model_from_classname('test-model', data, USERNAME, PASSWORD)
-        mock_new.assert_called()
+            self.assertEqual(e.exception.message, "[XOS-Tosca] Model of class test-model and properties {'name': 'test'} has multiple instances, I can't handle it")
 
 if __name__ == '__main__':
     unittest.main()
\ No newline at end of file