blob: 4c869c169364dd245289df4b3f9f13bf71d282b3 [file] [log] [blame]
Ray Milkeyd03eda02015-01-09 14:58:48 -08001/*
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 */
16package org.onosproject.codec.impl;
17
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070018import java.util.HashMap;
19import java.util.Map;
20
Ray Milkeyd03eda02015-01-09 14:58:48 -080021import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23
24import com.fasterxml.jackson.databind.ObjectMapper;
25
26/**
27 * Mock codec context for use in codec unit tests.
28 */
Ray Milkeyf195b022015-02-03 15:13:11 -080029public class MockCodecContext implements CodecContext {
Ray Milkeyd03eda02015-01-09 14:58:48 -080030
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070031 private final ObjectMapper mapper = new ObjectMapper();
32 private final CodecManager manager = new CodecManager();
33 private final Map<Class<? extends Object>, Object> services = new HashMap<>();
Ray Milkeyd03eda02015-01-09 14:58:48 -080034
Ray Milkeydb358082015-01-13 16:34:38 -080035 /**
36 * Constructs a new mock codec context.
37 */
Ray Milkeyd03eda02015-01-09 14:58:48 -080038 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 Yuta5bb99a42015-03-19 16:52:15 -070053 @SuppressWarnings("unchecked")
Ray Milkeyd03eda02015-01-09 14:58:48 -080054 @Override
55 public <T> T get(Class<T> serviceClass) {
HIGUCHI Yuta5bb99a42015-03-19 16:52:15 -070056 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 Milkeyd03eda02015-01-09 14:58:48 -080062 }
63
64}