Return 422 when updating with invalid notification method

Python api returned 500 and java api returned 400 when updating
alarm definition with an invalid notification method. Change both
to return 422 in this case for both updating and patching alarm
definition.

test_patch_alarm_definition_with_invalid_actions is added too.

Change-Id: I9c1f2e38a2c0bcc20c7079080afd67126ee21908
This commit is contained in:
Kaiyan Sheng 2016-05-02 17:07:11 -06:00
parent 8ef748c5cb
commit ad2deb38fc
3 changed files with 26 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
* (C) Copyright 2014,2016 Hewlett Packard Enterprise Development Company LP
*
* 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
@ -411,6 +411,7 @@ public class AlarmDefinitionService {
if (!actions.isEmpty())
for (String action : actions)
if (!notificationMethodRepo.exists(tenantId, action))
throw new InvalidEntityException("No notification method exists for action %s", action);
throw monasca.api.resource.exception.Exceptions.unprocessableEntity(
"No notification method exists for action %s", action);
}
}

View File

@ -1,4 +1,4 @@
# Copyright 2014 Hewlett-Packard
# (C) Copyright 2014,2016 Hewlett Packard Enterprise Development Company LP
# Copyright 2016 FUJITSU LIMITED
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
@ -754,7 +754,7 @@ class AlarmDefinitionsRepository(sql_repository.SQLRepository,
row = conn.execute(self.select_nm_query,
b_id=action.encode('utf8')).fetchone()
if row is None:
raise exceptions.RepositoryException(
raise exceptions.InvalidUpdateException(
"Non-existent notification id {} submitted for {} "
"notification action".format(action.encode('utf8'),
alarm_state.encode('utf8')))

View File

@ -724,6 +724,27 @@ class TestAlarmDefinitions(base.BaseMonascaTest):
None, notification_id)
self._delete_notification(notification_id)
@test.attr(type="gate")
@test.attr(type=['negative'])
def test_patch_alarm_definition_with_invalid_actions(self):
response_body_list = self._create_alarm_definitions(
expression=None, number_of_definitions=1)
# Patch alarm definition
self.assertRaises(exceptions.UnprocessableEntity,
self.monasca_client.patch_alarm_definition,
id=response_body_list[0]['id'],
alarm_actions=['bad_notification_id'])
self.assertRaises(exceptions.UnprocessableEntity,
self.monasca_client.patch_alarm_definition,
id=response_body_list[0]['id'],
ok_actions=['bad_notification_id'])
self.assertRaises(exceptions.UnprocessableEntity,
self.monasca_client.patch_alarm_definition,
id=response_body_list[0]['id'],
undetermined_actions=['bad_notification_id'])
# Delete
@test.attr(type="gate")