kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 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.faultmanagement.web; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.concurrent.ConcurrentHashMap; |
| 20 | |
| 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | |
| 24 | |
| 25 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 27 | |
| 28 | /** |
| 29 | * Mock codec context for use in codec unit tests. |
| 30 | */ |
| 31 | public class AlarmCodecContext implements CodecContext { |
| 32 | |
| 33 | private final ObjectMapper mapper = new ObjectMapper(); |
| 34 | private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>(); |
| 35 | |
| 36 | /** |
| 37 | * Constructs a new mock codec context. |
| 38 | */ |
| 39 | public AlarmCodecContext() { |
| 40 | codecs.clear(); |
| 41 | registerCodec(Alarm.class, new AlarmCodec()); |
| 42 | |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public ObjectMapper mapper() { |
| 47 | return mapper; |
| 48 | } |
| 49 | |
| 50 | @SuppressWarnings("unchecked") |
| 51 | @Override |
| 52 | public <T> T getService(Class<T> serviceClass) { |
| 53 | // TODO |
| 54 | return null; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Registers the specified JSON codec for the given entity class. |
| 59 | * |
| 60 | * @param entityClass entity class |
| 61 | * @param codec JSON codec |
| 62 | * @param <T> entity type |
| 63 | */ |
| 64 | public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) { |
| 65 | codecs.putIfAbsent(entityClass, codec); |
| 66 | } |
| 67 | |
| 68 | @SuppressWarnings("unchecked") |
| 69 | @Override |
| 70 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 71 | return codecs.get(entityClass); |
| 72 | } |
| 73 | } |