2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6 * file except in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
15 * SPDX-License-Identifier: Apache-2.0
16 * ============LICENSE_END=========================================================
19 package org.onap.appc.design.services.impl;
21 import static org.junit.Assert.assertEquals;
22 import static org.mockito.Matchers.anyString;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.when;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mockito;
30 import org.onap.appc.design.dbervices.DesignDBService;
31 import org.onap.appc.design.xinterface.XInterfaceService;
32 import org.onap.appc.design.xinterface.XResponseProcessor;
33 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.DbserviceInput;
34 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.ValidatorInput;
35 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.XinterfaceserviceInput;
36 import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.design.request.DesignRequest;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
41 @RunWith(PowerMockRunner.class)
42 @PrepareForTest({DesignDBService.class,XInterfaceService.class,XResponseProcessor.class})
43 public class TestDesignServicesImpl {
45 private DesignServicesImpl designServicesImpl;
46 private DbserviceInput dbserviceInput;
47 private XinterfaceserviceInput xinterfaceserviceInput;
48 private ValidatorInput validatorInput;
49 private DesignRequest designRequest;
50 private DesignDBService designDBService;
51 private XInterfaceService xInterfaceService;
52 private XResponseProcessor xResponseProcessor;
56 public void setUp() throws Exception {
57 designServicesImpl = new DesignServicesImpl();
58 dbserviceInput = Mockito.mock(DbserviceInput.class);
59 designRequest = Mockito.mock(DesignRequest.class);
60 designDBService = Mockito.mock(DesignDBService.class);
61 xInterfaceService = Mockito.mock(XInterfaceService.class);
62 PowerMockito.mockStatic(DesignDBService.class);
63 PowerMockito.mockStatic(XResponseProcessor.class);
64 PowerMockito.mockStatic(XInterfaceService.class);
65 xResponseProcessor = PowerMockito.mock(XResponseProcessor.class);
66 xinterfaceserviceInput = Mockito.mock(XinterfaceserviceInput.class);
67 validatorInput = Mockito.mock(ValidatorInput.class);
68 when(dbserviceInput.getDesignRequest()).thenReturn(designRequest);
69 when(xinterfaceserviceInput.getDesignRequest()).thenReturn(designRequest);
70 when(validatorInput.getDesignRequest()).thenReturn(designRequest);
71 when(designRequest.getRequestId()).thenReturn("123");
72 when(designRequest.getAction()).thenReturn("getDesigns");
73 when(designRequest.getPayload()).thenReturn("{\"artifact-contents\":\"\",\"userID\":\"user\"}");
77 public void testDbService() throws Exception {
78 PowerMockito.when(DesignDBService.initialise()).thenReturn(designDBService);
79 when(designDBService.execute(eq("getDesigns"), anyString(), eq("123"))).thenReturn("success");
81 designServicesImpl.dbservice(dbserviceInput).get().getResult().getStatus().getCode());
85 public void testDbServiceWithException() throws Exception {
86 PowerMockito.when(DesignDBService.initialise()).thenReturn(designDBService);
87 when(designDBService.execute(eq("getDesigns"), anyString(), eq("123")))
88 .thenThrow(new Exception());
90 designServicesImpl.dbservice(dbserviceInput).get().getResult().getStatus().getCode());
94 public void testXinterfaceservice() throws Exception {
95 PowerMockito.when(XInterfaceService.getInstance()).thenReturn(xInterfaceService);
96 PowerMockito.when(XResponseProcessor.getInstance()).thenReturn(xResponseProcessor);
97 when(xResponseProcessor.parseResponse(anyString(), eq("getDesigns"))).thenReturn("success");
98 assertEquals("400", designServicesImpl.xinterfaceservice(xinterfaceserviceInput).get()
99 .getResult().getStatus().getCode());
103 public void testXinterfaceserviceWithException() throws Exception {
104 PowerMockito.when(XInterfaceService.getInstance()).thenReturn(xInterfaceService);
105 PowerMockito.when(XResponseProcessor.getInstance()).thenReturn(xResponseProcessor);
106 when(xResponseProcessor.parseResponse(anyString(), eq("getDesigns"))).thenThrow(new Exception());
107 assertEquals("401", designServicesImpl.xinterfaceservice(xinterfaceserviceInput).get()
108 .getResult().getStatus().getCode());
112 public void testValidatorJson() throws Exception {
113 when(designRequest.getDataType()).thenReturn("JSON");
115 designServicesImpl.validator(validatorInput).get().getResult().getStatus().getCode());
119 public void testValidatorXml() throws Exception {
120 when(designRequest.getPayload()).thenReturn("<artifact-contents></artifact-contents>");
121 when(designRequest.getDataType()).thenReturn("XML");
123 designServicesImpl.validator(validatorInput).get().getResult().getStatus().getCode());
127 public void testValidatorYaml() throws Exception {
128 when(designRequest.getPayload()).thenReturn("artifact-contents: 34843");
129 when(designRequest.getDataType()).thenReturn("YAML");
131 designServicesImpl.validator(validatorInput).get().getResult().getStatus().getCode());
135 public void testValidatorVelocity() throws Exception {
136 when(designRequest.getPayload()).thenReturn("artifact-contents: 34843");
137 when(designRequest.getDataType()).thenReturn("VELOCITY");
139 designServicesImpl.validator(validatorInput).get().getResult().getStatus().getCode());
143 public void testValidatorInvalid() throws Exception {
144 when(designRequest.getDataType()).thenReturn("XYZ");
146 designServicesImpl.validator(validatorInput).get().getResult().getStatus().getCode());