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;
13 import com.fasterxml.jackson.databind.ObjectMapper;
14 import com.fasterxml.jackson.databind.SerializationFeature;
15 import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
17 import java.io.FileNotFoundException;
19 import java.net.URISyntaxException;
20 import java.util.ArrayList;
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.List;
25 import javax.ws.rs.core.UriBuilder;
26 import javax.ws.rs.core.UriInfo;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.invocation.InvocationOnMock;
29 import org.mockito.stubbing.Answer;
30 import org.opendaylight.controller.sal.core.api.model.SchemaService;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
34 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
36 public class DocGenTestHelper {
38 private Map<File, Module> modules;
39 private ObjectMapper mapper;
41 public Map<File, Module> loadModules(String resourceDirectory) throws FileNotFoundException,
44 URI resourceDirUri = getClass().getResource(resourceDirectory).toURI();
45 final YangContextParser parser = new YangParserImpl();
46 final File testDir = new File(resourceDirUri);
47 final String[] fileList = testDir.list();
48 final List<File> testFiles = new ArrayList<>();
49 if (fileList == null) {
50 throw new FileNotFoundException(resourceDirectory.toString());
52 for (String fileName : fileList) {
54 testFiles.add(new File(testDir, fileName));
56 return parser.parseYangModelsMapped(testFiles);
59 public Map<File, Module> getModules() {
63 public void setUp() throws Exception {
64 modules = loadModules("/yang");
65 mapper = new ObjectMapper();
66 mapper.registerModule(new JsonOrgModule());
67 mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
70 public SchemaService createMockSchemaService() {
71 return createMockSchemaService(null);
74 public SchemaService createMockSchemaService(SchemaContext mockContext) {
75 if (mockContext == null) {
76 mockContext = createMockSchemaContext();
79 SchemaService mockSchemaService = mock(SchemaService.class);
80 when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
81 return mockSchemaService;
84 public SchemaContext createMockSchemaContext() {
85 SchemaContext mockContext = mock(SchemaContext.class);
86 when(mockContext.getModules()).thenReturn(new HashSet<Module>(modules.values()));
88 final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
89 final ArgumentCaptor<Date> dateCapture = ArgumentCaptor.forClass(Date.class);
90 final ArgumentCaptor<URI> namespaceCapture = ArgumentCaptor.forClass(URI.class);
91 when(mockContext.findModuleByName(moduleCapture.capture(), dateCapture.capture())).then(
92 new Answer<Module>() {
94 public Module answer(InvocationOnMock invocation) throws Throwable {
95 String module = moduleCapture.getValue();
96 Date date = dateCapture.getValue();
97 for (Module m : modules.values()) {
98 if (m.getName().equals(module) && m.getRevision().equals(date)) {
105 when(mockContext.findModuleByNamespaceAndRevision(namespaceCapture.capture(), dateCapture.capture())).then(
106 new Answer<Module>() {
108 public Module answer(InvocationOnMock invocation) throws Throwable {
109 URI namespace = namespaceCapture.getValue();
110 Date date = dateCapture.getValue();
111 for (Module m : modules.values()) {
112 if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) {
122 public UriInfo createMockUriInfo(String urlPrefix) throws URISyntaxException {
123 final URI uri = new URI(urlPrefix);
125 UriBuilder mockBuilder = mock(UriBuilder.class);
127 final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
128 when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
129 when(mockBuilder.build()).then(new Answer<URI>() {
131 public URI answer(InvocationOnMock invocation) throws Throwable {
132 return URI.create(uri + "/" + subStringCapture.getValue());
136 UriInfo info = mock(UriInfo.class);
138 when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
139 when(info.getBaseUri()).thenReturn(uri);