Delete netconf
[controller.git] / opendaylight / md-sal / 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 com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
16 import java.io.File;
17 import java.io.FileNotFoundException;
18 import java.net.URI;
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;
24 import java.util.Map;
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;
35
36 public class DocGenTestHelper {
37
38     private Map<File, Module> modules;
39     private ObjectMapper mapper;
40
41     public Map<File, Module> loadModules(String resourceDirectory) throws FileNotFoundException,
42             URISyntaxException {
43
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());
51         }
52         for (String fileName : fileList) {
53
54             testFiles.add(new File(testDir, fileName));
55         }
56         return parser.parseYangModelsMapped(testFiles);
57     }
58
59     public Map<File, Module> getModules() {
60         return modules;
61     }
62
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);
68     }
69
70     public SchemaService createMockSchemaService() {
71         return createMockSchemaService(null);
72     }
73
74     public SchemaService createMockSchemaService(SchemaContext mockContext) {
75         if (mockContext == null) {
76             mockContext = createMockSchemaContext();
77         }
78
79         SchemaService mockSchemaService = mock(SchemaService.class);
80         when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
81         return mockSchemaService;
82     }
83
84     public SchemaContext createMockSchemaContext() {
85         SchemaContext mockContext = mock(SchemaContext.class);
86         when(mockContext.getModules()).thenReturn(new HashSet<Module>(modules.values()));
87
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>() {
93                     @Override
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)) {
99                                 return m;
100                             }
101                         }
102                         return null;
103                     }
104                 });
105         when(mockContext.findModuleByNamespaceAndRevision(namespaceCapture.capture(), dateCapture.capture())).then(
106                 new Answer<Module>() {
107                     @Override
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)) {
113                                 return m;
114                             }
115                         }
116                         return null;
117                     }
118                 });
119         return mockContext;
120     }
121
122     public UriInfo createMockUriInfo(String urlPrefix) throws URISyntaxException {
123         final URI uri = new URI(urlPrefix);
124
125         UriBuilder mockBuilder = mock(UriBuilder.class);
126
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>() {
130             @Override
131             public URI answer(InvocationOnMock invocation) throws Throwable {
132                 return URI.create(uri + "/" + subStringCapture.getValue());
133             }
134         });
135
136         UriInfo info = mock(UriInfo.class);
137
138         when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
139         when(info.getBaseUri()).thenReturn(uri);
140         return info;
141     }
142
143 }