<s> import json <EOL> import mock <EOL> from solum . api . controllers . v1 . datamodel import service as servicemodel <EOL> from solum . api . controllers . v1 import service <EOL> from solum . common import exception <EOL> from solum import objects <EOL> from solum . tests import base <EOL> from solum . tests import fakes <EOL> @ mock . patch ( '<STR_LIT>' , new_callable = fakes . FakePecanRequest ) <EOL> @ mock . patch ( '<STR_LIT>' , new_callable = fakes . FakePecanResponse ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class TestServiceController ( base . BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestServiceController , self ) . setUp ( ) <EOL> objects . load ( ) <EOL> def test_service_get ( self , ServiceHandler , resp_mock , request_mock ) : <EOL> hand_get = ServiceHandler . return_value . get <EOL> fake_service = fakes . FakeService ( ) <EOL> hand_get . return_value = fake_service <EOL> cont = service . ServiceController ( '<STR_LIT>' ) <EOL> resp = cont . get ( ) <EOL> self . assertIsNotNone ( resp ) <EOL> self . assertEqual ( fake_service . name , resp [ '<STR_LIT:result>' ] . name ) <EOL> self . assertEqual ( fake_service . description , <EOL> resp [ '<STR_LIT:result>' ] . description ) <EOL> self . assertEqual ( fake_service . project_id , resp [ '<STR_LIT:result>' ] . project_id ) <EOL> self . assertEqual ( fake_service . uuid , resp [ '<STR_LIT:result>' ] . uuid ) <EOL> hand_get . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:200> , resp_mock . status ) <EOL> def test_service_get_not_found ( self , ServiceHandler , <EOL> resp_mock , request_mock ) : <EOL> hand_get = ServiceHandler . return_value . get <EOL> hand_get . side_effect = exception . ResourceNotFound ( <EOL> name = '<STR_LIT>' , service_id = '<STR_LIT>' ) <EOL> cont = service . ServiceController ( '<STR_LIT>' ) <EOL> cont . get ( ) <EOL> hand_get . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> def test_service_put_none ( self , ServiceHandler , resp_mock , request_mock ) : <EOL> request_mock . body = None <EOL> request_mock . content_type = '<STR_LIT:application/json>' <EOL> hand_put = ServiceHandler . return_value . put <EOL> hand_put . return_value = fakes . FakeService ( ) <EOL> service . ServiceController ( '<STR_LIT>' ) . put ( ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> def test_service_put_not_found ( self , ServiceHandler , <EOL> resp_mock , request_mock ) : <EOL> json_update = { '<STR_LIT>' : '<STR_LIT:foo>' , '<STR_LIT:name>' : '<STR_LIT>' } <EOL> request_mock . body = json . dumps ( json_update ) <EOL> request_mock . content_type = '<STR_LIT:application/json>' <EOL> hand_update = ServiceHandler . return_value . update <EOL> hand_update . side_effect = exception . ResourceNotFound ( <EOL> name = '<STR_LIT>' , <EOL> service_id = '<STR_LIT>' ) <EOL> service . ServiceController ( '<STR_LIT>' ) . put ( ) <EOL> hand_update . assert_called_with ( '<STR_LIT>' , json_update ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> def test_service_put_ok ( self , ServiceHandler , resp_mock , request_mock ) : <EOL> json_update = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> request_mock . body = json . dumps ( json_update ) <EOL> request_mock . content_type = '<STR_LIT:application/json>' <EOL> hand_update = ServiceHandler . return_value . update <EOL> hand_update . return_value = fakes . FakeService ( ) <EOL> service . ServiceController ( '<STR_LIT>' ) . put ( ) <EOL> hand_update . assert_called_with ( '<STR_LIT>' , json_update ) <EOL> self . assertEqual ( <NUM_LIT:200> , resp_mock . status ) <EOL> def test_service_delete_not_found ( self , ServiceHandler , <EOL> resp_mock , request_mock ) : <EOL> hand_delete = ServiceHandler . return_value . delete <EOL> hand_delete . side_effect = exception . ResourceNotFound ( <EOL> name = '<STR_LIT>' , <EOL> service_id = '<STR_LIT>' ) <EOL> obj = service . ServiceController ( '<STR_LIT>' ) <EOL> obj . delete ( ) <EOL> hand_delete . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> def test_service_delete_ok ( self , ServiceHandler , resp_mock , request_mock ) : <EOL> hand_delete = ServiceHandler . return_value . delete <EOL> hand_delete . return_value = None <EOL> obj = service . ServiceController ( '<STR_LIT>' ) <EOL> obj . delete ( ) <EOL> hand_delete . assert_called_with ( '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> @ mock . patch ( '<STR_LIT>' , new_callable = fakes . FakePecanRequest ) <EOL> @ mock . patch ( '<STR_LIT>' , new_callable = fakes . FakePecanResponse ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class TestServicesController ( base . BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestServicesController , self ) . setUp ( ) <EOL> objects . load ( ) <EOL> def test_services_get_all ( self , handler_mock , resp_mock , request_mock ) : <EOL> hand_get_all = handler_mock . return_value . get_all <EOL> fake_service = fakes . FakeService ( ) <EOL> hand_get_all . return_value = [ fake_service ] <EOL> obj = service . ServicesController ( ) <EOL> resp = obj . get_all ( ) <EOL> self . assertIsNotNone ( resp ) <EOL> self . assertEqual ( fake_service . name , resp [ '<STR_LIT:result>' ] [ <NUM_LIT:0> ] . name ) <EOL> self . assertEqual ( fake_service . description , <EOL> resp [ '<STR_LIT:result>' ] [ <NUM_LIT:0> ] . description ) <EOL> self . assertEqual ( fake_service . project_id , resp [ '<STR_LIT:result>' ] [ <NUM_LIT:0> ] . project_id ) <EOL> self . assertEqual ( fake_service . uuid , resp [ '<STR_LIT:result>' ] [ <NUM_LIT:0> ] . uuid ) <EOL> hand_get_all . assert_called_with ( ) <EOL> self . assertEqual ( <NUM_LIT:200> , resp_mock . status ) <EOL> def test_services_post ( self , handler_mock , resp_mock , request_mock ) : <EOL> json_create = { '<STR_LIT:name>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> request_mock . body = json . dumps ( json_create ) <EOL> request_mock . content_type = '<STR_LIT:application/json>' <EOL> handler_create = handler_mock . return_value . create <EOL> handler_create . return_value = fakes . FakeService ( ) <EOL> service . ServicesController ( ) . post ( ) <EOL> handler_create . assert_called_with ( json_create ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> handler_create . assert_called_once_with ( json_create ) <EOL> def test_services_post_nodata ( self , handler_mock , resp_mock , request_mock ) : <EOL> request_mock . body = '<STR_LIT>' <EOL> request_mock . content_type = '<STR_LIT:application/json>' <EOL> handler_create = handler_mock . return_value . create <EOL> handler_create . return_value = fakes . FakeService ( ) <EOL> ret_val = service . ServicesController ( ) . post ( ) <EOL> self . assertEqual ( "<STR_LIT>" , <EOL> str ( ret_val [ '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( <NUM_LIT> , resp_mock . status ) <EOL> class TestServiceAsDict ( base . BaseTestCase ) : <EOL> scenarios = [ <EOL> ( '<STR_LIT:none>' , dict ( data = None ) ) , <EOL> ( '<STR_LIT>' , dict ( data = { '<STR_LIT:name>' : '<STR_LIT:foo>' } ) ) , <EOL> ( '<STR_LIT>' , dict ( data = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' } ) ) <EOL> ] <EOL> def test_as_dict ( self ) : <EOL> objects . load ( ) <EOL> if self . data is None : <EOL> s = servicemodel . Service ( ) <EOL> self . data = { } <EOL> else : <EOL> s = servicemodel . Service ( ** self . data ) <EOL> if '<STR_LIT>' in self . data : <EOL> del self . data [ '<STR_LIT>' ] <EOL> if '<STR_LIT:type>' in self . data : <EOL> del self . data [ '<STR_LIT:type>' ] <EOL> self . assertEqual ( self . data , <EOL> s . as_dict ( objects . registry . Service ) ) </s>
