Jian Li | 8bcef8b | 2016-01-06 11:35:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.hamcrest.Description; |
| 20 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 21 | import org.onosproject.net.flowobjective.NextObjective; |
| 22 | |
| 23 | /** |
| 24 | * Hamcrest matcher for nextObjective. |
| 25 | */ |
| 26 | public final class NextObjectiveJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 27 | |
| 28 | final NextObjective nextObjective; |
| 29 | |
| 30 | private NextObjectiveJsonMatcher(NextObjective nextObjective) { |
| 31 | this.nextObjective = nextObjective; |
| 32 | } |
| 33 | |
| 34 | @Override |
| 35 | protected boolean matchesSafely(JsonNode jsonNextObj, Description description) { |
| 36 | ObjectiveJsonMatcher.matchesObjective(nextObjective).matchesSafely(jsonNextObj); |
| 37 | |
| 38 | // check id |
| 39 | int jsonId = jsonNextObj.get("id").asInt(); |
| 40 | int id = nextObjective.id(); |
| 41 | if (jsonId != id) { |
| 42 | description.appendText("id was " + jsonId); |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | // check type |
| 47 | String jsonType = jsonNextObj.get("type").asText(); |
| 48 | String type = nextObjective.type().toString(); |
| 49 | if (!jsonType.equals(type)) { |
| 50 | description.appendText("type was " + jsonType); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | // check size of treatment array |
| 55 | JsonNode jsonTreatments = jsonNextObj.get("treatments"); |
| 56 | if (jsonTreatments.size() != nextObjective.next().size()) { |
| 57 | description.appendText("treatments size was " + jsonTreatments.size()); |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // TODO: need to check the content of treatment collection |
| 62 | |
| 63 | // TODO: need to check the content of selector instance |
| 64 | |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void describeTo(Description description) { |
| 70 | description.appendText(nextObjective.toString()); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Factory to allocate a nextObjective matcher. |
| 75 | * |
| 76 | * @param nextObjective nextObjective object we are looking for |
| 77 | * @return matcher |
| 78 | */ |
| 79 | public static NextObjectiveJsonMatcher matchesNextObjective(NextObjective nextObjective) { |
| 80 | return new NextObjectiveJsonMatcher(nextObjective); |
| 81 | } |
| 82 | } |