Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [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.codec.impl; |
| 17 | |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 18 | import java.util.HashMap; |
| 19 | import java.util.Map; |
| 20 | |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 21 | import org.onosproject.codec.CodecContext; |
| 22 | import org.onosproject.codec.JsonCodec; |
| 23 | |
| 24 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | |
| 26 | /** |
| 27 | * Mock codec context for use in codec unit tests. |
| 28 | */ |
Ray Milkey | f195b02 | 2015-02-03 15:13:11 -0800 | [diff] [blame] | 29 | public class MockCodecContext implements CodecContext { |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 30 | |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 31 | private final ObjectMapper mapper = new ObjectMapper(); |
| 32 | private final CodecManager manager = new CodecManager(); |
Sho SHIMIZU | 25a0aa4 | 2015-09-11 15:29:28 -0700 | [diff] [blame] | 33 | private final Map<Class<?>, Object> services = new HashMap<>(); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 34 | |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 35 | /** |
| 36 | * Constructs a new mock codec context. |
| 37 | */ |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 38 | public MockCodecContext() { |
| 39 | manager.activate(); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public ObjectMapper mapper() { |
| 44 | return mapper; |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | @SuppressWarnings("unchecked") |
| 49 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 50 | return manager.getCodec(entityClass); |
| 51 | } |
| 52 | |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 53 | @SuppressWarnings("unchecked") |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 54 | @Override |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 55 | public <T> T getService(Class<T> serviceClass) { |
HIGUCHI Yuta | 5bb99a4 | 2015-03-19 16:52:15 -0700 | [diff] [blame] | 56 | return (T) services.get(serviceClass); |
| 57 | } |
| 58 | |
| 59 | // for registering mock services |
| 60 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 61 | services.put(serviceClass, impl); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | } |