Merge "Add message to update the schema context of the InMemoryDOMDataStore"
[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 java.io.File;
14 import java.io.FileNotFoundException;
15 import java.net.URI;
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;
21 import java.util.Map;
22
23 import javax.ws.rs.core.UriBuilder;
24 import javax.ws.rs.core.UriInfo;
25
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;
34
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.fasterxml.jackson.databind.SerializationFeature;
37 import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule;
38
39 public class DocGenTestHelper {
40
41     private Map<File, Module> modules;
42     private ObjectMapper mapper;
43
44     public Map<File, Module> loadModules(String resourceDirectory) throws FileNotFoundException,
45             URISyntaxException {
46
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());
54         }
55         for (String fileName : fileList) {
56
57             testFiles.add(new File(testDir, fileName));
58         }
59         return parser.parseYangModelsMapped(testFiles);
60     }
61
62     public Map<File, Module> getModules() {
63         return modules;
64     }
65
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);
71     }
72
73     public SchemaService createMockSchemaService() {
74         return createMockSchemaService(null);
75     }
76
77     public SchemaService createMockSchemaService(SchemaContext mockContext) {
78         if (mockContext == null) {
79             mockContext = createMockSchemaContext();
80         }
81
82         SchemaService mockSchemaService = mock(SchemaService.class);
83         when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
84         return mockSchemaService;
85     }
86
87     public SchemaContext createMockSchemaContext() {
88         SchemaContext mockContext = mock(SchemaContext.class);
89         when(mockContext.getModules()).thenReturn(new HashSet<Module>(modules.values()));
90
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>() {
95                     @Override
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)) {
101                                 return m;
102                             }
103                         }
104                         return null;
105                     }
106                 });
107         return mockContext;
108     }
109
110     public UriInfo createMockUriInfo(String urlPrefix) throws URISyntaxException {
111         final URI uri = new URI(urlPrefix);
112
113         UriBuilder mockBuilder = mock(UriBuilder.class);
114
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>() {
118             @Override
119             public URI answer(InvocationOnMock invocation) throws Throwable {
120                 return URI.create(uri + "/" + subStringCapture.getValue());
121             }
122         });
123
124         UriInfo info = mock(UriInfo.class);
125
126         when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
127         when(info.getBaseUri()).thenReturn(uri);
128         return info;
129     }
130
131 }