5265ae2669386b94239657a5070dd334c1875bcf
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / controller / sal / rest / doc / impl / DocGenTestHelper.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.rest.doc.impl;
9
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
12 import com.fasterxml.jackson.databind.ObjectMapper;
13 import com.fasterxml.jackson.databind.SerializationFeature;
14 import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.Set;
23 import javax.ws.rs.core.UriBuilder;
24 import javax.ws.rs.core.UriInfo;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.invocation.InvocationOnMock;
27 import org.mockito.stubbing.Answer;
28 import org.opendaylight.controller.sal.core.api.model.SchemaService;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
35 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
36
37 public class DocGenTestHelper {
38
39     private Set<Module> modules;
40     private ObjectMapper mapper;
41     private SchemaContext schemaContext;
42
43     public Set<Module> loadModules(final String resourceDirectory)
44             throws FileNotFoundException,
45             URISyntaxException, ReactorException {
46
47         final URI resourceDirUri = getClass().getResource(resourceDirectory).toURI();
48         final File testDir = new File(resourceDirUri);
49         final String[] fileList = testDir.list();
50         if (fileList == null) {
51             throw new FileNotFoundException(resourceDirectory.toString());
52         }
53         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
54         for (final String fileName : fileList) {
55             final File file = new File(testDir, fileName);
56             reactor.addSource(new YangStatementSourceImpl(new NamedFileInputStream(file, file.getPath())));
57         }
58
59         this.schemaContext = reactor.buildEffective();
60         return this.schemaContext.getModules();
61     }
62
63     public Collection<Module> getModules() {
64         return this.modules;
65     }
66
67     public void setUp() throws Exception {
68         this.modules = loadModules("/yang");
69         this.mapper = new ObjectMapper();
70         this.mapper.registerModule(new JsonOrgModule());
71         this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
72     }
73
74     public SchemaContext getSchemaContext() {
75         return this.schemaContext;
76     }
77
78     public SchemaService createMockSchemaService() {
79         return createMockSchemaService(null);
80     }
81
82     public SchemaService createMockSchemaService(SchemaContext mockContext) {
83         if (mockContext == null) {
84             mockContext = createMockSchemaContext();
85         }
86
87         final SchemaService mockSchemaService = mock(SchemaService.class);
88         when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
89         return mockSchemaService;
90     }
91
92     public SchemaContext createMockSchemaContext() {
93         final SchemaContext mockContext = mock(SchemaContext.class);
94         when(mockContext.getModules()).thenReturn(this.modules);
95
96         final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
97         final ArgumentCaptor<Date> dateCapture = ArgumentCaptor.forClass(Date.class);
98         final ArgumentCaptor<URI> namespaceCapture = ArgumentCaptor.forClass(URI.class);
99         when(mockContext.findModuleByName(moduleCapture.capture(), dateCapture.capture())).then(
100                 new Answer<Module>() {
101                     @Override
102                     public Module answer(final InvocationOnMock invocation) throws Throwable {
103                         final String module = moduleCapture.getValue();
104                         final Date date = dateCapture.getValue();
105                         for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
106                             if (m.getName().equals(module) && m.getRevision().equals(date)) {
107                                 return m;
108                             }
109                         }
110                         return null;
111                     }
112                 });
113         when(mockContext.findModuleByNamespaceAndRevision(namespaceCapture.capture(), dateCapture.capture())).then(
114                 new Answer<Module>() {
115                     @Override
116                     public Module answer(final InvocationOnMock invocation) throws Throwable {
117                         final URI namespace = namespaceCapture.getValue();
118                         final Date date = dateCapture.getValue();
119                         for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
120                             if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) {
121                                 return m;
122                             }
123                         }
124                         return null;
125                     }
126                 });
127         return mockContext;
128     }
129
130     public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
131         final URI uri = new URI(urlPrefix);
132
133         final UriBuilder mockBuilder = mock(UriBuilder.class);
134
135         final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
136         when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
137         when(mockBuilder.build()).then(new Answer<URI>() {
138             @Override
139             public URI answer(final InvocationOnMock invocation) throws Throwable {
140                 return URI.create(uri + "/" + subStringCapture.getValue());
141             }
142         });
143
144         final UriInfo info = mock(UriInfo.class);
145
146         when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
147         when(info.getBaseUri()).thenReturn(uri);
148         return info;
149     }
150
151 }