/*
* ============LICENSE_START========================================================================
* ONAP : tr-069-adapter
* =================================================================================================
* Copyright (C) 2020 CommScope Inc Intellectual Property.
* =================================================================================================
* This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
* ===============LICENSE_END=======================================================================
*/
package org.commscope.tr069adapter.acs.cpe.test.inform;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.Cookie;
import org.apache.activemq.broker.BrokerService;
import org.apache.commons.httpclient.HttpStatus;
import org.commscope.tr069adapter.acs.booter.ACSServiceBooter;
import org.commscope.tr069adapter.acs.common.DeviceInform;
import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
import org.commscope.tr069adapter.acs.common.dto.TR069OperationCode;
import org.commscope.tr069adapter.acs.common.utils.AcsConstants;
import org.commscope.tr069adapter.acs.cpe.builder.DeviceRPCBuilder;
import org.commscope.tr069adapter.acs.cpe.handler.DeviceValidator;
import org.commscope.tr069adapter.acs.cpe.rpc.Inform;
import org.commscope.tr069adapter.acs.requestprocessor.dao.DeviceRPCRequestRepository;
import org.commscope.tr069adapter.acs.requestprocessor.entity.TR069DeviceRPCRequestEntity;
import org.commscope.tr069adapter.acs.requestprocessor.impl.TR069EventNotificationService;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@SpringBootTest(classes = {ACSServiceBooter.class})
@RunWith(SpringJUnit4ClassRunner.class)
@AutoConfigureMockMvc
@ContextConfiguration
public class InformAbortPendingReq {
@Autowired
private MockMvc mockMvc;
@MockBean
private DeviceValidator deviceValidator;
@MockBean
protected DeviceRPCRequestRepository deviceRPCRequestRepository;
@MockBean
private TR069EventNotificationService tr069EventNotificationService;
@MockBean
DeviceRPCBuilder deviceRPCBuilder;
@Autowired
BrokerService broker;
@Test
public void processInformPnPByAbortingPendingRequestTest() throws Exception {
String exampleInform =
"1Airvana0005B9LTE_Enterprise_C-RANSC_Cntrl0005B95190900 BOOTSTRAP12018-04-27T07:09:160Device.ManagementServer.ParameterKeyNoneDevice.ManagementServer.ConnectionRequestURLhttp://10.210.37.1/acscall"
+ "Device.DeviceInfo.ManufacturerAirvanaDevice.DeviceInfo.ManufacturerOUI0005B9Device.DeviceInfo.ProductClassLTE_Enterprise_C-RANSC_CntrlDevice.DeviceInfo.SerialNumber0005B9519090Device.DeviceInfo.HardwareVersion750742.00.04Device.DeviceInfo.SoftwareVersion3.0.00.073Device.DeviceInfo.ProvisioningCodeDevice.FAP.X_0005B9_RUWhiteListDevice.IP.Interface.1.IPv4Enable1Device.IP.Interface.1.IPv4Address.1.IPAddress10.210.37.1"
+ "";
try {
Mockito.doNothing().when(tr069EventNotificationService)
.sendOperationResultToNBI(Mockito.any(DeviceRPCResponse.class));
Mockito.doNothing().when(tr069EventNotificationService)
.sendDeviceInformToNBI(Mockito.any(DeviceInform.class));
Mockito
.when(deviceValidator.isDeviceAuthorized(Mockito.any(Inform.class), Mockito.anyString()))
.thenReturn(new Boolean(true));
Mockito.when(deviceValidator.validateDevice(Mockito.anyString(), Mockito.anyString(),
Mockito.anyString())).thenReturn(new Boolean(true));
List deviceRPCRequestEntities =
new ArrayList();
TR069DeviceRPCRequestEntity entity = new TR069DeviceRPCRequestEntity();
entity.setDeviceId("0005B9519090");
entity.setOperationId(10000L);
entity.setOpCode(TR069OperationCode.ADD_OBJECT.getOperationCode());
entity.setRequestTimeOut(1000L);
deviceRPCRequestEntities.add(entity);
Mockito.when(deviceRPCRequestRepository.findByDeviceIdAndIsProcessed(Mockito.anyString(),
Mockito.anyInt())).thenReturn(deviceRPCRequestEntities);
Mockito.when(deviceRPCRequestRepository.findByDeviceIdAndOperationId(Mockito.anyString(),
Mockito.anyLong())).thenReturn(deviceRPCRequestEntities);
Mockito.when(deviceRPCBuilder.constructDeviceRPC(Mockito.any(DeviceRPCRequest.class)))
.thenReturn(null);
RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/CPEMgmt/acs")
.accept(MediaType.TEXT_PLAIN).content(exampleInform).header("Authorization", "basic")
.contentType(MediaType.TEXT_PLAIN);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = result.getResponse();
assertEquals(HttpStatus.SC_OK, response.getStatus());
Cookie cookie = response.getCookie(AcsConstants.ACS_SESSIONID);
requestBuilder =
MockMvcRequestBuilders.post("/CPEMgmt/acs").accept(MediaType.TEXT_PLAIN).content("")
.header("Authorization", "basic").cookie(cookie).contentType(MediaType.TEXT_PLAIN);
result = mockMvc.perform(requestBuilder).andReturn();
response = result.getResponse();
assertEquals(HttpStatus.SC_NO_CONTENT, response.getStatus());
} catch (Exception e) {
fail(e.getMessage());
}
}
@After
public void stopBroker() throws Exception {
try {
System.out.println("Tearing down the broker");
broker.stop();
broker.waitUntilStopped();
broker = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}