Commit 0a5bee6c authored by Henrique Pötter's avatar Henrique Pötter
Browse files

Integration fix and test coverage increase #12, #15


Signed-off-by: Henrique Pötter's avatarEnumaElish <henriquepotter.hp@gmail.com>
Signed-off-by: Rogério Cardoso's avatarRogerio Cardoso <prof.rogeriocardoso@gmail.com>
Signed-off-by: dcmspe's avatarDanilo <dcms.pe@gmail.com>
parent dcb4d036
Pipeline #3614039 passed with stage
in 5 minutes and 46 seconds
......@@ -6,6 +6,8 @@ class DiscoveryController < ApplicationController
before_action :validate_url_params
before_action :find_resources
attr_accessor :catalog_url
def initialize
@catalog_url = SERVICES_CONFIG['services']['catalog'] + '/resources/search?'
@collector_url = SERVICES_CONFIG['services']['collector'] + '/resources/data/last'
......@@ -14,7 +16,12 @@ class DiscoveryController < ApplicationController
def resources
if !@found_resources.blank? && validate_collector_url
uuids = ids_from_catalog
collector_uuids = data_from_collector(uuids)
begin
collector_uuids = data_from_collector(uuids)
rescue
render error_payload('The data collector service is unavailable', 503)
return
end
matched_resources(collector_uuids)
end
if !@found_resources.blank?
......@@ -33,21 +40,18 @@ class DiscoveryController < ApplicationController
end
def data_from_collector(uuids)
begin
collector_response = call_to_data_collector(uuids)
rescue
render error_payload('Service Unavailable', 503)
end
collector_response = call_to_data_collector(uuids)
collector_response['resources'].map do |resource|
resource['uuid']
end
end
# This method is not being covered by the rspec because it dependents on the real service and it response is not predictable
def find_resources
begin
@found_resources = call_to_resource_catalog(build_resource_catalog_url)
rescue
render error_payload('Service Unavailable', 503)
rescue Exception => e
render error_payload('The resource catalog service is unavailable', 503)
end
end
......@@ -59,15 +63,15 @@ class DiscoveryController < ApplicationController
def build_resource_catalog_url
query_string_url = @catalog_url + 'capability=' + params['capability']
if params['radius'].blank? && !params['lat'].blank?
query_string_url += '&' + 'lat=' + params['lat'] + '&'
query_string_url += 'lon=' + params['lon']
elsif !params['radius'].blank? && !params['lat'].blank?
query_string_url += '&' + 'lat=' + params['lat'] + '&'
query_string_url += 'lon=' + params['lon'] + '&'
query_string_url + 'radius=' + params['radius']
query_string_url += 'radius=' + params['radius']
end
query_string_url
end
def validate_url_params
......@@ -120,19 +124,20 @@ class DiscoveryController < ApplicationController
JSON.parse(RestClient.get(discovery_query))
end
# This method is not being covered by the rspec because it dependents on the real service and it response is not predictable
def call_to_data_collector(uuids)
filters = {
sensor_value: {
uuids: uuids,
capabilities: [params['capability']],
range: {
params['capability'] => {
max: params['max_cap_value'],
min: params['min_cap_value'],
equal: params['cap_value']
}
sensor_value: {
uuids: uuids,
capabilities: [params['capability']],
range: {
params['capability'] => {
max: params['max_cap_value'],
min: params['min_cap_value'],
equal: params['cap_value']
}
}
}
}
}
JSON.parse(RestClient.post(@collector_url, filters, content_type: 'application/json'))
end
......
......@@ -4,17 +4,17 @@ module ErrorMessage
ERROR_CODE[200] = 'OK'
ERROR_CODE[400] = 'BadRequest'
ERROR_CODE[400] = 'Bad Request'
ERROR_CODE[401] = 'Unauthorized'
ERROR_CODE[403] = 'Forbidden'
ERROR_CODE[404] = 'NotFound'
ERROR_CODE[422] = 'UnprocessableEntry'
ERROR_CODE[429] = 'TooManyRequests'
ERROR_CODE[404] = 'Not Found'
ERROR_CODE[422] = 'Unprocessable Entry'
ERROR_CODE[429] = 'Too Many Requests'
ERROR_CODE[500] = 'InternalError'
ERROR_CODE[501] = 'InternalError'
ERROR_CODE[502] = 'InternalError'
ERROR_CODE[503] = 'InternalError'
ERROR_CODE[500] = 'Internal Server Error'
ERROR_CODE[501] = 'Not Implemented'
ERROR_CODE[502] = 'Bad Gateway'
ERROR_CODE[503] = 'Service Unavailable'
protected
......
......@@ -2,5 +2,5 @@
# do not include a final slash after the URL
services:
collector: "http://localhost:3002"
catalog: "http://localhost:3001"
collector: "http://107.170.158.70:3002"
catalog: "http://107.170.158.70:3000"
......@@ -14,30 +14,30 @@ describe DiscoveryController, type: 'controller' do
end
it 'when capability has no value found, should fail' do
get 'resources', params: { capability: '' }
get 'resources', params: {capability: ''}
expect(response.status).to eq(400)
end
it 'when lat has no value found, should fail' do
get 'resources', params: { capability: 'temp', lon: '212121', lat: '' }
get 'resources', params: {capability: 'temp', lon: '212121', lat: ''}
expect(response.status).to eq(400)
end
it 'when lon has no value found, should fail' do
get 'resources', params: { capability: 'temp', lat: '212121', lon: '' }
get 'resources', params: {capability: 'temp', lat: '212121', lon: ''}
expect(response.status).to eq(400)
end
it 'when inform a capability, should return OK and a set of id or ids given a capability' do
catalog_response = { 'resources' => [{ 'uuid' => '1', 'lat' => '10', 'lon' => '10' },
{ 'uuid' => '2', 'lat' => '20', 'lon' => '20' },
{ 'uuid' => '3', 'lat' => '30', 'lon' => '30' }] }
catalog_response = {'resources' => [{'uuid' => '1', 'lat' => '10', 'lon' => '10'},
{'uuid' => '2', 'lat' => '20', 'lon' => '20'},
{'uuid' => '3', 'lat' => '30', 'lon' => '30'}]}
allow(@controller).to receive(:call_to_resource_catalog).and_return(catalog_response)
get 'resources', params: { capability: 'temp' }
get 'resources', params: {capability: 'temp'}
hash_response_uuids = JSON.parse(response.body)
expect(response.status).to eq(200)
......@@ -45,12 +45,12 @@ describe DiscoveryController, type: 'controller' do
end
it 'when inform a cap/lat/lon, should return OK and data from data collector for specific params' do
catalog_response = { 'resources' => [{ 'uuid' => '2', 'lat' => '20', 'lon' => '20' },
{ 'uuid' => '3', 'lat' => '30', 'lon' => '30' }] }
catalog_response = {'resources' => [{'uuid' => '2', 'lat' => '20', 'lon' => '20'},
{'uuid' => '3', 'lat' => '30', 'lon' => '30'}]}
allow(@controller).to receive(:call_to_resource_catalog).and_return(catalog_response)
get 'resources', params: { capability: 'temp', lat: '12.34', lon: '43.21' }
get 'resources', params: {capability: 'temp', lat: '12.34', lon: '43.21'}
hash_response_uuids = JSON.parse(response.body)
expect(response.status).to eq(200)
......@@ -58,19 +58,19 @@ describe DiscoveryController, type: 'controller' do
end
it 'when inform a cap/rad without coordinates, should fail' do
get 'resources', params: { capability: 'temp', radius: '100' }
get 'resources', params: {capability: 'temp', radius: '100'}
expect(response.status).to eq(400)
end
it 'when inform a cap/lat/lon/rad, should return OK and resources based on lat lon within a radius' do
catalog_response = { 'resources' => [{ 'uuid' => '4', 'lat' => '40', 'lon' => '40' },
{ 'uuid' => '5', 'lat' => '40', 'lon' => '40' },
{ 'uuid' => '7', 'lat' => '40', 'lon' => '40' }] }
catalog_response = {'resources' => [{'uuid' => '4', 'lat' => '40', 'lon' => '40'},
{'uuid' => '5', 'lat' => '40', 'lon' => '40'},
{'uuid' => '7', 'lat' => '40', 'lon' => '40'}]}
allow(@controller).to receive(:call_to_resource_catalog).and_return(catalog_response)
get 'resources', params: { capability: 'temp', lat: '12.34', lon: '43.21', radius: '100' }
get 'resources', params: {capability: 'temp', lat: '12.34', lon: '43.21', radius: '100'}
hash_response_uuids = JSON.parse(response.body)
expect(response.status).to eq(200)
......@@ -78,9 +78,9 @@ describe DiscoveryController, type: 'controller' do
end
it 'when no found data in catalog that match params, should return 404' do
get 'resources', params: { capability: 'temp', lat: '12.34', lon: '43.21', radius: 80 }
get 'resources', params: {capability: 'temp', lat: '12.34', lon: '43.21', radius: 80}
hash_response_uuids = JSON.parse(response.body)
expected_json = { 'code' => 'NotFound', 'message' => 'No resources have been found' }
expected_json = {'code' => 'Not Found', 'message' => 'No resources have been found'}
expect(response.status).to eq(404)
expect(hash_response_uuids).to eq(expected_json)
......@@ -88,18 +88,18 @@ describe DiscoveryController, type: 'controller' do
context 'when inform a cap/min/max range' do
it 'properly returns all resources found by both catalog and collector' do
catalog_response = { 'resources' => [{ 'uuid' => '7', 'lat' => '40', 'lon' => '40' }] }
collector_response = { 'resources' =>
[{ 'uuid' => '7', 'capabilities' =>
{ 'temp' => [{ 'value' => '28.31', 'date' => '2016-06-21T23:27:35.000Z' }] } }] }
catalog_response = {'resources' => [{'uuid' => '7', 'lat' => '40', 'lon' => '40'}]}
collector_response = {'resources' =>
[{'uuid' => '7', 'capabilities' =>
{'temp' => [{'value' => '28.31', 'date' => '2016-06-21T23:27:35.000Z'}]}}]}
allow(@controller).to receive(:call_to_resource_catalog).and_return(catalog_response)
allow(@controller).to receive(:call_to_data_collector).and_return(collector_response)
get 'resources', params: { capability: 'temp',
lat: '12.34', lon: '43.21',
radius: '100',
min_cap_value: 20, max_cap_value: 30 }
get 'resources', params: {capability: 'temp',
lat: '12.34', lon: '43.21',
radius: '100',
min_cap_value: 20, max_cap_value: 30}
hash_response_uuids = JSON.parse(response.body)
expect(response.status).to eq(200)
......@@ -107,35 +107,73 @@ describe DiscoveryController, type: 'controller' do
end
it 'properly returns only resources found by catalog that match with collector results' do
catalog_response = { 'resources' =>
[{ 'uuid' => '4', 'lat' => '40', 'lon' => '40' },
{ 'uuid' => '5', 'lat' => '40', 'lon' => '40' },
{ 'uuid' => '7', 'lat' => '40', 'lon' => '40' }] }
collector_response = { 'resources' =>
[{ 'uuid' => '7',
'capabilities' => { 'temp' => [{ 'value' => '28.31',
'date' => '2016-06-21T23:27:35.000Z' }] } }] }
catalog_response = {'resources' =>
[{'uuid' => '4', 'lat' => '40', 'lon' => '40'},
{'uuid' => '5', 'lat' => '40', 'lon' => '40'},
{'uuid' => '7', 'lat' => '40', 'lon' => '40'}]}
collector_response = {'resources' =>
[{'uuid' => '7',
'capabilities' => {'temp' => [{'value' => '28.31',
'date' => '2016-06-21T23:27:35.000Z'}]}}]}
allow(@controller).to receive(:call_to_resource_catalog).and_return(catalog_response)
allow(@controller).to receive(:call_to_data_collector).and_return(collector_response)
get 'resources', params: { capability: 'temp',
lat: '12.34', lon: '43.21',
radius: '100',
min_cap_value: 20, max_cap_value: 30 }
get 'resources', params: {capability: 'temp',
lat: '12.34', lon: '43.21',
radius: '100',
min_cap_value: 20, max_cap_value: 30}
hash_response_uuids = JSON.parse(response.body)
expect(response.status).to eq(200)
expect(hash_response_uuids).to eq('resources' => [{ 'uuid' => '7', 'lat' => '40', 'lon' => '40' }])
expect(hash_response_uuids).to eq('resources' => [{'uuid' => '7', 'lat' => '40', 'lon' => '40'}])
end
end
it 'when any service is unavailable, should fail' do
it 'when the resource catalog is unavailable, should fail' do
allow(@controller).to receive(:call_to_resource_catalog).and_raise
get 'resources', params: {capability: 'temp'}
expect(response.status).to eq(503)
end
get 'resources', params: { capability: 'temp' }
it 'when the data collector is unavailable, should fail' do
catalog_response = {'resources' =>
[{'uuid' => '4', 'lat' => '40', 'lon' => '40'},
{'uuid' => '5', 'lat' => '40', 'lon' => '40'},
{'uuid' => '7', 'lat' => '40', 'lon' => '40'}]}
allow(@controller).to receive(:call_to_resource_catalog).and_return(catalog_response)
allow(@controller).to receive(:call_to_data_collector).and_raise
get 'resources', params: {capability: 'temp',
lat: '12.34', lon: '43.21',
radius: '100',
min_cap_value: 20, max_cap_value: 30}
expect(response.status).to eq(503)
end
it 'when creating the resource catalog url, should return a valid resource catalog url' do
get 'resources', params: {capability: 'temp',
lat: '12.34', lon: '43.21',
radius: '100'}
@controller.catalog_url = SERVICES_CONFIG['services']['catalog'] + '/resources/search?'
catalog_url = @controller.send(:build_resource_catalog_url)
expected_url =SERVICES_CONFIG['services']['catalog'] + '/resources/search?'+'capability=temp&lat=12.34&lon=43.21&radius=100'
expect(catalog_url).to eq(expected_url)
end
it 'when creating the resource catalog url, should return a valid resource catalog url' do
get 'resources', params: {capability: 'temp',
lat: '12.34', lon: '43.21'}
@controller.catalog_url = SERVICES_CONFIG['services']['catalog'] + '/resources/search?'
catalog_url = @controller.send(:build_resource_catalog_url)
expected_url =SERVICES_CONFIG['services']['catalog'] + '/resources/search?'+'capability=temp&lat=12.34&lon=43.21'
expect(catalog_url).to eq(expected_url)
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment