2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.rest.doc.impl;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
14 import java.io.FileNotFoundException;
16 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.Date;
19 import java.util.HashSet;
20 import java.util.List;
23 import javax.ws.rs.core.UriBuilder;
24 import javax.ws.rs.core.UriInfo;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.invocation.InvocationOnMock;
28 import org.mockito.stubbing.Answer;
29 import org.opendaylight.controller.sal.core.api.model.SchemaService;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
33 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.fasterxml.jackson.databind.SerializationFeature;
37 import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
39 public class DocGenTestHelper {
41 private Map<File, Module> modules;
42 private ObjectMapper mapper;
44 public Map<File, Module> loadModules(String resourceDirectory) throws FileNotFoundException,
47 URI resourceDirUri = getClass().getResource(resourceDirectory).toURI();
48 final YangModelParser parser = new YangParserImpl();
49 final File testDir = new File(resourceDirUri);
50 final String[] fileList = testDir.list();
51 final List<File> testFiles = new ArrayList<>();
52 if (fileList == null) {
53 throw new FileNotFoundException(resourceDirectory.toString());
55 for (String fileName : fileList) {
57 testFiles.add(new File(testDir, fileName));
59 return parser.parseYangModelsMapped(testFiles);
62 public Map<File, Module> getModules() {
66 public void setUp() throws Exception {
67 modules = loadModules("/yang");
68 mapper = new ObjectMapper();
69 mapper.registerModule(new JsonOrgModule());
70 mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
73 public SchemaService createMockSchemaService() {
74 return createMockSchemaService(null);
77 public SchemaService createMockSchemaService(SchemaContext mockContext) {
78 if (mockContext == null) {
79 mockContext = createMockSchemaContext();
82 SchemaService mockSchemaService = mock(SchemaService.class);
83 when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
84 return mockSchemaService;
87 public SchemaContext createMockSchemaContext() {
88 SchemaContext mockContext = mock(SchemaContext.class);
89 when(mockContext.getModules()).thenReturn(new HashSet<Module>(modules.values()));
91 final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
92 final ArgumentCaptor<Date> dateCapture = ArgumentCaptor.forClass(Date.class);
93 when(mockContext.findModuleByName(moduleCapture.capture(), dateCapture.capture())).then(
94 new Answer<Module>() {
96 public Module answer(InvocationOnMock invocation) throws Throwable {
97 String module = moduleCapture.getValue();
98 Date date = dateCapture.getValue();
99 for (Module m : modules.values()) {
100 if (m.getName().equals(module) && m.getRevision().equals(date)) {
110 public UriInfo createMockUriInfo(String urlPrefix) throws URISyntaxException {
111 final URI uri = new URI(urlPrefix);
113 UriBuilder mockBuilder = mock(UriBuilder.class);
115 final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
116 when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
117 when(mockBuilder.build()).then(new Answer<URI>() {
119 public URI answer(InvocationOnMock invocation) throws Throwable {
120 return URI.create(uri + "/" + subStringCapture.getValue());
124 UriInfo info = mock(UriInfo.class);
126 when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
127 when(info.getBaseUri()).thenReturn(uri);