Merge "Remove import-package declaration"
[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
13 import com.fasterxml.jackson.databind.ObjectMapper;
14 import com.fasterxml.jackson.databind.SerializationFeature;
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.Set;
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.parser.spi.meta.ReactorException;
34 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
35
36 public class DocGenTestHelper {
37
38     private Set<Module> modules;
39     private ObjectMapper mapper;
40     private SchemaContext schemaContext;
41
42     public Set<Module> loadModules(final String resourceDirectory)
43             throws URISyntaxException, FileNotFoundException, ReactorException {
44
45         final URI resourceDirUri = getClass().getResource(resourceDirectory).toURI();
46         final File testDir = new File(resourceDirUri);
47         final String[] fileList = testDir.list();
48         if (fileList == null) {
49             throw new FileNotFoundException(resourceDirectory.toString());
50         }
51         final List<File> files = new ArrayList<>();
52         for (final String fileName : fileList) {
53             files.add(new File(testDir, fileName));
54         }
55
56         this.schemaContext = YangParserTestUtils.parseYangSources(files);
57         return this.schemaContext.getModules();
58     }
59
60     public Collection<Module> getModules() {
61         return this.modules;
62     }
63
64     public void setUp() throws Exception {
65         this.modules = loadModules("/yang");
66         this.mapper = new ObjectMapper();
67         this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
68     }
69
70     public SchemaContext getSchemaContext() {
71         return this.schemaContext;
72     }
73
74     public SchemaService createMockSchemaService() {
75         return createMockSchemaService(null);
76     }
77
78     public SchemaService createMockSchemaService(SchemaContext mockContext) {
79         if (mockContext == null) {
80             mockContext = createMockSchemaContext();
81         }
82
83         final SchemaService mockSchemaService = mock(SchemaService.class);
84         when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
85         return mockSchemaService;
86     }
87
88     public SchemaContext createMockSchemaContext() {
89         final SchemaContext mockContext = mock(SchemaContext.class);
90         when(mockContext.getModules()).thenReturn(this.modules);
91
92         final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
93         final ArgumentCaptor<Date> dateCapture = ArgumentCaptor.forClass(Date.class);
94         final ArgumentCaptor<URI> namespaceCapture = ArgumentCaptor.forClass(URI.class);
95         when(mockContext.findModuleByName(moduleCapture.capture(), dateCapture.capture())).then(
96                 new Answer<Module>() {
97                     @Override
98                     public Module answer(final InvocationOnMock invocation) throws Throwable {
99                         final String module = moduleCapture.getValue();
100                         final Date date = dateCapture.getValue();
101                         for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
102                             if (m.getName().equals(module) && m.getRevision().equals(date)) {
103                                 return m;
104                             }
105                         }
106                         return null;
107                     }
108                 });
109         when(mockContext.findModuleByNamespaceAndRevision(namespaceCapture.capture(), dateCapture.capture())).then(
110                 new Answer<Module>() {
111                     @Override
112                     public Module answer(final InvocationOnMock invocation) throws Throwable {
113                         final URI namespace = namespaceCapture.getValue();
114                         final Date date = dateCapture.getValue();
115                         for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
116                             if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) {
117                                 return m;
118                             }
119                         }
120                         return null;
121                     }
122                 });
123         return mockContext;
124     }
125
126     public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
127         final URI uri = new URI(urlPrefix);
128
129         final UriBuilder mockBuilder = mock(UriBuilder.class);
130
131         final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
132         when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
133         when(mockBuilder.build()).then(new Answer<URI>() {
134             @Override
135             public URI answer(final InvocationOnMock invocation) throws Throwable {
136                 return URI.create(uri + "/" + subStringCapture.getValue());
137             }
138         });
139
140         final UriInfo info = mock(UriInfo.class);
141
142         when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
143         when(info.getBaseUri()).thenReturn(uri);
144         return info;
145     }
146
147 }